···1import exception
2import gleam/bit_array
3import gleam/bool
4-import gleam/bytes_builder.{type BytesBuilder}
5import gleam/crypto
6import gleam/dict.{type Dict}
7import gleam/dynamic.{type Dynamic}
···19import gleam/option.{type Option}
20import gleam/result
21import gleam/string
22-import gleam/string_builder.{type StringBuilder}
23import gleam/uri
24import logging
25import marceau
···35pub type Body {
36 /// A body of unicode text.
37 ///
38- /// The body is represented using a `StringBuilder`. If you have a `String`
39- /// you can use the `string_builder.from_string` function to convert it.
40 ///
41- Text(StringBuilder)
42 /// A body of binary data.
43 ///
44- /// The body is represented using a `BytesBuilder`. If you have a `BitArray`
45- /// you can use the `bytes_builder.from_bit_array` function to convert it.
46 ///
47- Bytes(BytesBuilder)
48 /// A body of the contents of a file.
49 ///
50 /// This will be sent efficiently using the `send_file` function of the
···146/// # Examples
147///
148/// ```gleam
0149/// response(200)
150-/// |> file_download_from_memory(named: "myfile.txt", containing: "Hello, Joe!")
151/// // -> Response(
152/// // 200,
153/// // [#("content-disposition", "attachment; filename=\"myfile.txt\"")],
···158pub fn file_download_from_memory(
159 response: Response,
160 named name: String,
161- containing data: BytesBuilder,
162) -> Response {
163 let name = uri.percent_encode(name)
164 response
···177/// # Examples
178///
179/// ```gleam
180-/// let body = string_builder.from_string("<h1>Hello, Joe!</h1>")
181/// html_response(body, 200)
182/// // -> Response(200, [#("content-type", "text/html")], Text(body))
183/// ```
184///
185-pub fn html_response(html: StringBuilder, status: Int) -> Response {
186 HttpResponse(
187 status,
188 [#("content-type", "text/html; charset=utf-8")],
···198/// # Examples
199///
200/// ```gleam
201-/// let body = string_builder.from_string("{\"name\": \"Joe\"}")
202/// json_response(body, 200)
203/// // -> Response(200, [#("content-type", "application/json")], Text(body))
204/// ```
205///
206-pub fn json_response(json: StringBuilder, status: Int) -> Response {
207 HttpResponse(
208 status,
209 [#("content-type", "application/json; charset=utf-8")],
···219/// # Examples
220///
221/// ```gleam
222-/// let body = string_builder.from_string("<h1>Hello, Joe!</h1>")
223/// response(201)
224/// |> html_body(body)
225/// // -> Response(201, [#("content-type", "text/html; charset=utf-8")], Text(body))
226/// ```
227///
228-pub fn html_body(response: Response, html: StringBuilder) -> Response {
229 response
230 |> response.set_body(Text(html))
231 |> response.set_header("content-type", "text/html; charset=utf-8")
···239/// # Examples
240///
241/// ```gleam
242-/// let body = string_builder.from_string("{\"name\": \"Joe\"}")
243/// response(201)
244/// |> json_body(body)
245/// // -> Response(201, [#("content-type", "application/json; charset=utf-8")], Text(body))
246/// ```
247///
248-pub fn json_body(response: Response, json: StringBuilder) -> Response {
249 response
250 |> response.set_body(Text(json))
251 |> response.set_header("content-type", "application/json; charset=utf-8")
252}
253254-/// Set the body of a response to a given string builder.
255///
256/// You likely want to also set the request `content-type` header to an
257/// appropriate value for the format of the content.
···259/// # Examples
260///
261/// ```gleam
262-/// let body = string_builder.from_string("Hello, Joe!")
263/// response(201)
264-/// |> string_builder_body(body)
265/// // -> Response(201, [], Text(body))
266/// ```
267///
268-pub fn string_builder_body(
269- response: Response,
270- content: StringBuilder,
271-) -> Response {
272 response
273 |> response.set_body(Text(content))
274}
275276-/// Set the body of a response to a given string builder.
277///
278/// You likely want to also set the request `content-type` header to an
279/// appropriate value for the format of the content.
···287/// // -> Response(
288/// // 201,
289/// // [],
290-/// // Text(string_builder.from_string("Hello, Joe"))
291/// // )
292/// ```
293///
294pub fn string_body(response: Response, content: String) -> Response {
295 response
296- |> response.set_body(Text(string_builder.from_string(content)))
297}
298299/// Escape a string so that it can be safely included in a HTML document.
···1475 http.Get, True -> {
1476 let path =
1477 path
1478- |> string.drop_left(string.length(prefix))
1479 |> string.replace(each: "..", with: "")
1480 |> internal.join_path(directory, _)
1481
···1import exception
2import gleam/bit_array
3import gleam/bool
4+import gleam/bytes_tree.{type BytesTree}
5import gleam/crypto
6import gleam/dict.{type Dict}
7import gleam/dynamic.{type Dynamic}
···19import gleam/option.{type Option}
20import gleam/result
21import gleam/string
22+import gleam/string_tree.{type StringTree}
23import gleam/uri
24import logging
25import marceau
···35pub type Body {
36 /// A body of unicode text.
37 ///
38+ /// The body is represented using a `StringTree`. If you have a `String`
39+ /// you can use the `string_tree.from_string` function to convert it.
40 ///
41+ Text(StringTree)
42 /// A body of binary data.
43 ///
44+ /// The body is represented using a `BytesTree`. If you have a `BitArray`
45+ /// you can use the `bytes_tree.from_bit_array` function to convert it.
46 ///
47+ Bytes(BytesTree)
48 /// A body of the contents of a file.
49 ///
50 /// This will be sent efficiently using the `send_file` function of the
···146/// # Examples
147///
148/// ```gleam
149+/// let content = bytes_tree.from_string("Hello, Joe!")
150/// response(200)
151+/// |> file_download_from_memory(named: "myfile.txt", containing: content)
152/// // -> Response(
153/// // 200,
154/// // [#("content-disposition", "attachment; filename=\"myfile.txt\"")],
···159pub fn file_download_from_memory(
160 response: Response,
161 named name: String,
162+ containing data: BytesTree,
163) -> Response {
164 let name = uri.percent_encode(name)
165 response
···178/// # Examples
179///
180/// ```gleam
181+/// let body = string_tree.from_string("<h1>Hello, Joe!</h1>")
182/// html_response(body, 200)
183/// // -> Response(200, [#("content-type", "text/html")], Text(body))
184/// ```
185///
186+pub fn html_response(html: StringTree, status: Int) -> Response {
187 HttpResponse(
188 status,
189 [#("content-type", "text/html; charset=utf-8")],
···199/// # Examples
200///
201/// ```gleam
202+/// let body = string_tree.from_string("{\"name\": \"Joe\"}")
203/// json_response(body, 200)
204/// // -> Response(200, [#("content-type", "application/json")], Text(body))
205/// ```
206///
207+pub fn json_response(json: StringTree, status: Int) -> Response {
208 HttpResponse(
209 status,
210 [#("content-type", "application/json; charset=utf-8")],
···220/// # Examples
221///
222/// ```gleam
223+/// let body = string_tree.from_string("<h1>Hello, Joe!</h1>")
224/// response(201)
225/// |> html_body(body)
226/// // -> Response(201, [#("content-type", "text/html; charset=utf-8")], Text(body))
227/// ```
228///
229+pub fn html_body(response: Response, html: StringTree) -> Response {
230 response
231 |> response.set_body(Text(html))
232 |> response.set_header("content-type", "text/html; charset=utf-8")
···240/// # Examples
241///
242/// ```gleam
243+/// let body = string_tree.from_string("{\"name\": \"Joe\"}")
244/// response(201)
245/// |> json_body(body)
246/// // -> Response(201, [#("content-type", "application/json; charset=utf-8")], Text(body))
247/// ```
248///
249+pub fn json_body(response: Response, json: StringTree) -> Response {
250 response
251 |> response.set_body(Text(json))
252 |> response.set_header("content-type", "application/json; charset=utf-8")
253}
254255+/// Set the body of a response to a given string tree.
256///
257/// You likely want to also set the request `content-type` header to an
258/// appropriate value for the format of the content.
···260/// # Examples
261///
262/// ```gleam
263+/// let body = string_tree.from_string("Hello, Joe!")
264/// response(201)
265+/// |> string_tree_body(body)
266/// // -> Response(201, [], Text(body))
267/// ```
268///
269+pub fn string_tree_body(response: Response, content: StringTree) -> Response {
000270 response
271 |> response.set_body(Text(content))
272}
273274+/// Set the body of a response to a given string.
275///
276/// You likely want to also set the request `content-type` header to an
277/// appropriate value for the format of the content.
···285/// // -> Response(
286/// // 201,
287/// // [],
288+/// // Text(string_tree.from_string("Hello, Joe"))
289/// // )
290/// ```
291///
292pub fn string_body(response: Response, content: String) -> Response {
293 response
294+ |> response.set_body(Text(string_tree.from_string(content)))
295}
296297/// Escape a string so that it can be safely included in a HTML document.
···1473 http.Get, True -> {
1474 let path =
1475 path
1476+ |> string.drop_start(string.length(prefix))
1477 |> string.replace(each: "..", with: "")
1478 |> internal.join_path(directory, _)
1479