Skip to content

Commit

Permalink
Update for Gleam v0.32
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Nov 5, 2023
1 parent b1b353c commit 23d1477
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 77 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Updated for Gleam v0.32. All references to "bit string" have been changed to
"bit array" to match.
- The `wisp` module gains the `get_query` function.

## v0.6.0 - 2023-10-19
Expand Down
12 changes: 6 additions & 6 deletions examples/utilities/tiny_database/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_erlang", version = "0.20.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "F216A80C8FDFF774447B494D5E08AE4E9A911E971727B9A78FEBF5F300914584" },
{ name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" },
{ name = "gleam_otp", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "E31B158857E3D2AF946FE6E90E0CB21699AF226F4630E93FBEAC5DB4515F8920" },
{ name = "gleam_stdlib", version = "0.30.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "704258528887F95075FFED7AAE1CCF836A9B88E3AADA2F69F9DA15815F94A4F9" },
{ name = "gleam_erlang", version = "0.23.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DA7A8E5540948DE10EB01B530869F8FF2FF6CAD8CFDA87626CE6EF63EBBF87CB" },
{ name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" },
{ name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" },
{ name = "gleam_stdlib", version = "0.32.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "07D64C26D014CF570F8ACADCE602761EA2E74C842D26F2FD49B0D61973D9966F" },
{ name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
{ name = "ids", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang", "gleam_otp"], otp_app = "ids", source = "hex", outer_checksum = "7A378014D40E848326FBEE8AC0C9B35EB9C8094DC4414D89F9A5AA99397A6042" },
{ name = "simplifile", version = "0.1.10", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "263B7C7F4B29263555DEA2D30BA918425A27120CDD1E1352744EAB4D56CE01CE" },
{ name = "ids", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "ids", source = "hex", outer_checksum = "36E0E8005716D0030A598B4F5AFB4C269002C119C9DAB100AD3D715B7159705E" },
{ name = "simplifile", version = "0.1.14", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "10EA0207796F20488A3A166C50A189C9385333F3C9FAC187729DE7B9CE4ADDBC" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

Expand Down
2 changes: 1 addition & 1 deletion examples/utilities/tiny_database/src/tiny_database.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import gleam/dynamic
import ids/nanoid
import gleam/json
import gleam/list
import gleam/map.{Map}
import gleam/map.{type Map}
import simplifile

pub opaque type Connection {
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "wisp"
version = "0.6.0"
gleam = ">= 0.30.0"
gleam = ">= 0.32.0"
description = "A practical web framework for Gleam"
licences = ["Apache-2.0"]

Expand Down
14 changes: 7 additions & 7 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
packages = [
{ name = "exception", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "71F00057D38ADB03BBCCD0E3B07AB2C236BD49DBA7E7611A9DADBD1E26C9F53D" },
{ name = "gleam_bitwise", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "E2A46EE42E5E9110DAD67E0F71E7358CBE54D5EC22C526DD48CBBA3223025792" },
{ name = "gleam_crypto", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_bitwise", "gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "42429CED0F838B40014E1C017B0495C46C311D08035D2C2D43B749B91A4374F6" },
{ name = "gleam_erlang", version = "0.22.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "367D8B41A7A86809928ED1E7E55BFD0D46D7C4CF473440190F324AFA347109B4" },
{ name = "gleam_http", version = "3.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "FAE9AE3EB1CA90C2194615D20FFFD1E28B630E84DACA670B28D959B37BCBB02C" },
{ name = "gleam_crypto", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "42429CED0F838B40014E1C017B0495C46C311D08035D2C2D43B749B91A4374F6" },
{ name = "gleam_erlang", version = "0.23.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DA7A8E5540948DE10EB01B530869F8FF2FF6CAD8CFDA87626CE6EF63EBBF87CB" },
{ name = "gleam_http", version = "3.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0B09AAE8EB547C4F2F2D3F8917A0A4D2EF75DFF0232389332BAFE19DBBFDB92B" },
{ name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" },
{ name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" },
{ name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" },
{ name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" },
{ name = "gleam_stdlib", version = "0.32.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "07D64C26D014CF570F8ACADCE602761EA2E74C842D26F2FD49B0D61973D9966F" },
{ name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
{ name = "glisten", version = "0.9.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "91809C44C52456D96C8317A19246DE1C06ED494C40D282CD9380565E879A52C4" },
{ name = "glisten", version = "0.9.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_otp", "gleam_erlang"], otp_app = "glisten", source = "hex", outer_checksum = "91809C44C52456D96C8317A19246DE1C06ED494C40D282CD9380565E879A52C4" },
{ name = "marceau", version = "1.1.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "1AAD727A30BE0F95562C3403BB9B27C823797AD90037714255EEBF617B1CDA81" },
{ name = "mist", version = "0.14.0", build_tools = ["gleam"], requirements = ["glisten", "gleam_erlang", "gleam_http", "gleam_stdlib", "gleam_otp"], otp_app = "mist", source = "hex", outer_checksum = "7CDD0396D9A556F1069D83E9AF2B24388AAC478B9B4846615C6D4797E1D3C6A3" },
{ name = "mist", version = "0.14.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "glisten", "gleam_stdlib", "gleam_otp", "gleam_http"], otp_app = "mist", source = "hex", outer_checksum = "7CDD0396D9A556F1069D83E9AF2B24388AAC478B9B4846615C6D4797E1D3C6A3" },
{ name = "simplifile", version = "0.1.14", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "10EA0207796F20488A3A166C50A189C9385333F3C9FAC187729DE7B9CE4ADDBC" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]
Expand Down
85 changes: 43 additions & 42 deletions src/wisp.gleam
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import exception
import gleam/base
import gleam/bit_builder
import gleam/bit_string
import gleam/bytes_builder
import gleam/bit_array
import gleam/bool
import gleam/crypto
import gleam/dynamic.{Dynamic}
import gleam/dynamic.{type Dynamic}
import gleam/erlang
import gleam/http.{Method}
import gleam/http.{type Method}
import gleam/http/cookie
import gleam/http/request.{Request as HttpRequest}
import gleam/http/response.{Response as HttpResponse}
import gleam/http/request.{type Request as HttpRequest}
import gleam/http/response.{
type Response as HttpResponse, Response as HttpResponse,
}
import gleam/int
import gleam/json
import gleam/list
import gleam/option.{Option}
import gleam/option.{type Option}
import gleam/result
import gleam/string
import gleam/string_builder.{StringBuilder}
import gleam/string_builder.{type StringBuilder}
import gleam/uri
import marceau
import mist
Expand Down Expand Up @@ -88,8 +89,8 @@ fn wrap_mist_chunk(

fn mist_response(response: Response) -> HttpResponse(mist.ResponseData) {
let body = case response.body {
Empty -> mist.Bytes(bit_builder.new())
Text(text) -> mist.Bytes(bit_builder.from_string_builder(text))
Empty -> mist.Bytes(bytes_builder.new())
Text(text) -> mist.Bytes(bytes_builder.from_string_builder(text))
File(path) -> mist_send_file(path)
}
response
Expand All @@ -102,7 +103,7 @@ fn mist_send_file(path: String) -> mist.ResponseData {
Error(error) -> {
log_error(string.inspect(error))
// TODO: return 500
mist.Bytes(bit_builder.new())
mist.Bytes(bytes_builder.new())
}
}
}
Expand Down Expand Up @@ -538,7 +539,7 @@ fn make_connection(body_reader: Reader, secret_key_base: String) -> Connection {
}

type BufferedReader {
BufferedReader(reader: Reader, buffer: BitString)
BufferedReader(reader: Reader, buffer: BitArray)
}

type Quotas {
Expand Down Expand Up @@ -571,7 +572,7 @@ type Reader =
fn(Int) -> Result(Read, Nil)

type Read {
Chunk(BitString, next: Reader)
Chunk(BitArray, next: Reader)
ReadingFinished
}

Expand Down Expand Up @@ -799,7 +800,7 @@ pub fn require_string_body(
next: fn(String) -> Response,
) -> Response {
case read_body_to_bitstring(request) {
Ok(body) -> or_400(bit_string.to_string(body), next)
Ok(body) -> or_400(bit_array.to_string(body), next)
Error(_) -> entity_too_large()
}
}
Expand Down Expand Up @@ -827,9 +828,9 @@ pub fn require_string_body(
/// }
/// ```
///
pub fn require_bit_string_body(
pub fn require_bit_array_body(
request: Request,
next: fn(BitString) -> Response,
next: fn(BitArray) -> Response,
) -> Response {
case read_body_to_bitstring(request) {
Ok(body) -> next(body)
Expand All @@ -841,7 +842,7 @@ pub fn require_bit_string_body(
// network errors.
/// Read the entire body of the request as a bit string.
///
/// You may instead wish to use the `require_bit_string_body` or the
/// You may instead wish to use the `require_bit_array_body` or the
/// `require_string_body` middleware functions instead.
///
/// This function does not cache the body in any way, so if you call this
Expand All @@ -853,7 +854,7 @@ pub fn require_bit_string_body(
/// If the body is larger than the `max_body_size` limit then an empty response
/// with status code 413: Entity too large will be returned to the client.
///
pub fn read_body_to_bitstring(request: Request) -> Result(BitString, Nil) {
pub fn read_body_to_bitstring(request: Request) -> Result(BitArray, Nil) {
let connection = request.body
read_body_loop(
connection.reader,
Expand All @@ -867,14 +868,14 @@ fn read_body_loop(
reader: Reader,
read_chunk_size: Int,
max_body_size: Int,
accumulator: BitString,
) -> Result(BitString, Nil) {
accumulator: BitArray,
) -> Result(BitArray, Nil) {
use chunk <- result.try(reader(read_chunk_size))
case chunk {
ReadingFinished -> Ok(accumulator)
Chunk(chunk, next) -> {
let accumulator = bit_string.append(accumulator, chunk)
case bit_string.byte_size(accumulator) > max_body_size {
let accumulator = bit_array.append(accumulator, chunk)
case bit_array.byte_size(accumulator) > max_body_size {
True -> Error(Nil)
False ->
read_body_loop(next, read_chunk_size, max_body_size, accumulator)
Expand Down Expand Up @@ -1057,13 +1058,13 @@ fn read_multipart(

// No file name, this is a regular form value that we hold in memory.
option.None -> {
let append = fn(data, chunk) { Ok(bit_string.append(data, chunk)) }
let append = fn(data, chunk) { Ok(bit_array.append(data, chunk)) }
let q = quotas.body
let result =
multipart_body(reader, parse, boundary, read_size, q, append, <<>>)
use #(reader, quota, value) <- result.try(result)
let quotas = Quotas(..quotas, body: quota)
use value <- result.map(bit_string_to_string(value))
use value <- result.map(bit_array_to_string(value))
let data = FormData(..data, values: [#(name, value), ..data.values])
#(data, reader, quotas)
}
Expand All @@ -1078,14 +1079,14 @@ fn read_multipart(
}
}

fn bit_string_to_string(bits: BitString) -> Result(String, Response) {
bit_string.to_string(bits)
fn bit_array_to_string(bits: BitArray) -> Result(String, Response) {
bit_array.to_string(bits)
|> result.replace_error(bad_request())
}

fn multipart_file_append(
path: String,
chunk: BitString,
chunk: BitArray,
) -> Result(String, Response) {
chunk
|> simplifile.append_bits(path)
Expand All @@ -1105,21 +1106,21 @@ fn or_500(result: Result(a, b)) -> Result(a, Response) {

fn multipart_body(
reader: BufferedReader,
parse: fn(BitString) -> Result(http.MultipartBody, Response),
parse: fn(BitArray) -> Result(http.MultipartBody, Response),
boundary: String,
chunk_size: Int,
quota: Int,
append: fn(t, BitString) -> Result(t, Response),
append: fn(t, BitArray) -> Result(t, Response),
data: t,
) -> Result(#(Option(BufferedReader), Int, t), Response) {
use #(chunk, reader) <- result.try(read_chunk(reader, chunk_size))
let size_read = bit_string.byte_size(chunk)
let size_read = bit_array.byte_size(chunk)
use output <- result.try(parse(chunk))

case output {
http.MultipartBody(parsed, done, remaining) -> {
// Decrement the quota by the number of bytes consumed.
let used = size_read - bit_string.byte_size(remaining) - 2
let used = size_read - bit_array.byte_size(remaining) - 2
let used = case done {
// If this is the last chunk, we need to account for the boundary.
True -> used - 4 - string.byte_size(boundary)
Expand Down Expand Up @@ -1171,7 +1172,7 @@ fn multipart_content_disposition(
fn read_chunk(
reader: BufferedReader,
chunk_size: Int,
) -> Result(#(BitString, Reader), Response) {
) -> Result(#(BitArray, Reader), Response) {
buffered_read(reader, chunk_size)
|> result.replace_error(bad_request())
|> result.try(fn(chunk) {
Expand All @@ -1184,7 +1185,7 @@ fn read_chunk(

fn multipart_headers(
reader: BufferedReader,
parse: fn(BitString) -> Result(http.MultipartHeaders, Response),
parse: fn(BitArray) -> Result(http.MultipartHeaders, Response),
chunk_size: Int,
quotas: Quotas,
) -> Result(#(List(http.Header), BufferedReader, Quotas), Response) {
Expand All @@ -1193,7 +1194,7 @@ fn multipart_headers(

case headers {
http.MultipartHeaders(headers, remaining) -> {
let used = bit_string.byte_size(chunk) - bit_string.byte_size(remaining)
let used = bit_array.byte_size(chunk) - bit_array.byte_size(remaining)
use quotas <- result.map(decrement_body_quota(quotas, used))
let reader = BufferedReader(reader, remaining)
#(headers, reader, quotas)
Expand Down Expand Up @@ -1576,7 +1577,7 @@ pub fn log_debug(message: String) -> Nil {
///
pub fn random_string(length: Int) -> String {
crypto.strong_random_bytes(length)
|> base.url_encode64(False)
|> bit_array.base64_url_encode(False)
|> string.slice(0, length)
}

Expand All @@ -1591,7 +1592,7 @@ pub fn random_string(length: Int) -> String {
///
pub fn sign_message(
request: Request,
message: BitString,
message: BitArray,
algorithm: crypto.HashAlgorithm,
) -> String {
crypto.sign_message(message, <<request.body.secret_key_base:utf8>>, algorithm)
Expand All @@ -1608,7 +1609,7 @@ pub fn sign_message(
pub fn verify_signed_message(
request: Request,
message: String,
) -> Result(BitString, Nil) {
) -> Result(BitArray, Nil) {
crypto.verify_signed_message(message, <<request.body.secret_key_base:utf8>>)
}

Expand Down Expand Up @@ -1664,7 +1665,7 @@ pub fn set_cookie(
max_age: option.Some(max_age),
)
let value = case security {
PlainText -> base.encode64(<<value:utf8>>, False)
PlainText -> bit_array.base64_encode(<<value:utf8>>, False)
Signed -> sign_message(request, <<value:utf8>>, crypto.Sha512)
}
response
Expand Down Expand Up @@ -1702,10 +1703,10 @@ pub fn get_cookie(
|> list.key_find(name),
)
use value <- result.try(case security {
PlainText -> base.decode64(value)
PlainText -> bit_array.base64_decode(value)
Signed -> verify_signed_message(request, value)
})
bit_string.to_string(value)
bit_array.to_string(value)
}

//
Expand All @@ -1719,7 +1720,7 @@ pub fn get_cookie(
/// `wisp/testing` module instead.
///
pub fn create_canned_connection(
body: BitString,
body: BitArray,
secret_key_base: String,
) -> Connection {
make_connection(
Expand Down
6 changes: 3 additions & 3 deletions src/wisp/internal/logger.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gleam/map.{Map}
import gleam/erlang/atom.{Atom}
import gleam/dynamic.{Dynamic}
import gleam/map.{type Map}
import gleam/erlang/atom.{type Atom}
import gleam/dynamic.{type Dynamic}

pub type LogLevel {
Emergency
Expand Down
Loading

0 comments on commit 23d1477

Please sign in to comment.