diff options
author | Eduardo Julian | 2022-11-06 20:52:21 -0400 |
---|---|---|
committer | Eduardo Julian | 2022-11-06 20:52:21 -0400 |
commit | ae4c0a4746d59b552ebeba166a43ce756dd265af (patch) | |
tree | 8548fb3e4a77bd986d459a639ee31cf2455fe20e /stdlib/source/library/lux/world | |
parent | fd8ea1e1b9cae781abe42aeadda2e0ef149994d6 (diff) |
More efficient code-generation for text composition.
Diffstat (limited to 'stdlib/source/library/lux/world')
-rw-r--r-- | stdlib/source/library/lux/world/net/http.lux | 4 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/net/http/client.lux | 3 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/net/http/request.lux | 234 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/net/mime.lux | 4 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/net/uri/encoding.lux | 18 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/time/day.lux | 7 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/time/month.lux | 7 |
7 files changed, 130 insertions, 147 deletions
diff --git a/stdlib/source/library/lux/world/net/http.lux b/stdlib/source/library/lux/world/net/http.lux index 05cf85509..d03a8d398 100644 --- a/stdlib/source/library/lux/world/net/http.lux +++ b/stdlib/source/library/lux/world/net/http.lux @@ -2,9 +2,7 @@ [library [lux (.except #version #host) [control - [try (.only Try)] - [concurrency - [frp (.only Channel)]]] + [try (.only Try)]] [data [binary (.only Binary)]]]] [/ diff --git a/stdlib/source/library/lux/world/net/http/client.lux b/stdlib/source/library/lux/world/net/http/client.lux index f3851016a..8ba9a1694 100644 --- a/stdlib/source/library/lux/world/net/http/client.lux +++ b/stdlib/source/library/lux/world/net/http/client.lux @@ -227,7 +227,8 @@ (these))) (def .public (async client) - (-> (Client IO) (Client Async)) + (-> (Client IO) + (Client Async)) (implementation (def (request method url headers data) (|> (at client request method url headers data) diff --git a/stdlib/source/library/lux/world/net/http/request.lux b/stdlib/source/library/lux/world/net/http/request.lux index 477fbf2e3..05b55332a 100644 --- a/stdlib/source/library/lux/world/net/http/request.lux +++ b/stdlib/source/library/lux/world/net/http/request.lux @@ -1,143 +1,127 @@ (.require [library [lux (.except) + [abstract + [monad (.only Monad)]] [control - pipe - ["[0]" monad (.only do)] - ["[0]" maybe] - ["[0]" try (.only Try)] - [concurrency - ["[0]" async (.only Async)] - ["[0]" frp]]] + ["[0]" try]] [data - ["[0]" number - ["n" nat]] - ["[0]" text - ["[0]" encoding]] + ["[0]" binary (.only Binary)] + [text + [encoding + ["[0]" utf8 (.use "[1]#[0]" codec)]]] [format - ["[0]" context (.only Context Property)] - ["[0]" json (.only JSON) - ["<[1]>" \\parser]]] - [collection - ["[0]" list (.use "[1]#[0]" functor mix)] - ["[0]" dictionary]]] - [meta - [macro - ["^" pattern]]] - [world - ["[0]" binary (.only Binary)]]]] - ["[0]" // (.only Body Response Server) - ["[1][0]" response] - ["[1][0]" query] - ["[1][0]" cookie]]) + ["[0]" json (.only JSON) (.use "[1]#[0]" codec)]]]]] + ["[0]" // (.only Body) + ["[0]" version] + ["[0]" header (.only Header)] + ["/[1]" // (.only) + ["[0]" mime] + [uri (.only URI) + ["[0]" scheme] + ["[0]" query (.only Query) (.use "[1]#[0]" codec)]]]]) (type .public (Request !) - [Identification Protocol Resource (Message !)]) + (Record + [#identification //.Identification + #protocol //.Protocol + #resource //.Resource + #message (//.Message !)])) -(type .public (Server !) - (-> (Request !) - (! (Response !)))) +(def (body ! it) + (All (_ !) + (-> (Monad !) Binary + (//.Body !))) + (function (_ _) + (at ! in {try.#Success [(binary.size it) it]}))) -(def .public (static response) - (-> Response Server) - (function (_ request) - (async.resolved response))) +(def .public (utf8 ! it) + (All (_ !) + (-> (Monad !) Text + (Request !))) + [#identification [//.#local [///.#host "" + ///.#port 0] + //.#remote [///.#host "" + ///.#port 0]] + #protocol [//.#version version.v1_1 + //.#scheme scheme.http] + #resource [//.#method {//.#Post} + //.#uri ""] + #message [//.#headers (|> header.empty + (header.has header.content_type mime.utf_8)) + //.#body (body ! (utf8#encoded it))]]) -(def (merge inputs) - (-> (List Binary) Binary) - (let [[_ output] (try.trusted - (monad.mix try.monad - (function (_ input [offset output]) - (let [amount (binary.size input)] - (at try.functor each (|>> [(n.+ amount offset)]) - (binary.copy amount 0 input offset output)))) - [0 (|> inputs - (list#each binary.size) - (list#mix n.+ 0) - binary.empty)] - inputs))] - output)) +(def .public text ..utf8) -(def (read_text_body body) - (-> Body (Async (Try Text))) - (do async.monad - [blobs (frp.list body)] - (in (at encoding.utf8 decoded (merge blobs))))) +(def .public (json ! it) + (All (_ !) + (-> (Monad !) JSON + (Request !))) + [#identification [//.#local [///.#host "" + ///.#port 0] + //.#remote [///.#host "" + ///.#port 0]] + #protocol [//.#version version.v1_1 + //.#scheme scheme.http] + #resource [//.#method {//.#Post} + //.#uri ""] + #message [//.#headers (|> header.empty + (header.has header.content_type mime.json)) + //.#body (body ! (utf8#encoded (json#encoded it)))]]) -(def failure - (//response.bad_request "")) +(def .public (form ! it) + (All (_ !) + (-> (Monad !) Query + (Request !))) + [#identification [//.#local [///.#host "" + ///.#port 0] + //.#remote [///.#host "" + ///.#port 0]] + #protocol [//.#version version.v1_1 + //.#scheme scheme.http] + #resource [//.#method {//.#Post} + //.#uri ""] + #message [//.#headers (|> header.empty + (header.has header.content_type mime.form)) + //.#body (body ! (utf8#encoded (query#encoded it)))]]) -(def .public (json reader server) - (All (_ a) (-> (<json>.Reader a) (-> a Server) Server)) - (function (_ (^.let request [identification protocol resource message])) - (do async.monad - [?raw (read_text_body (the //.#body message))] - (when (do try.monad - [raw ?raw - content (at json.codec decoded raw)] - (json.result content reader)) - {try.#Success input} - (server input request) - - {try.#Failure error} - (async.resolved ..failure))))) +(with_template [<name> <scheme>] + [(def .public <name> + (All (_ !) + (-> (Request !) + (Request !))) + (|>> (has [#protocol //.#scheme] <scheme>)))] -(def .public (text server) - (-> (-> Text Server) Server) - (function (_ (^.let request [identification protocol resource message])) - (do async.monad - [?raw (read_text_body (the //.#body message))] - (when ?raw - {try.#Success content} - (server content request) - - {try.#Failure error} - (async.resolved ..failure))))) + [http scheme.http] + [https scheme.https] + ) -(def .public (query property server) - (All (_ a) (-> (Property a) (-> a Server) Server)) - (function (_ [identification protocol resource message]) - (let [full (the //.#uri resource) - [uri query] (|> full - (text.split_by "?") - (maybe.else [full ""]))] - (when (do try.monad - [query (//query.parameters query) - input (context.result query property)] - (in [[identification protocol (has //.#uri uri resource) message] - input])) - {try.#Success [request input]} - (server input request) - - {try.#Failure error} - (async.resolved ..failure))))) +(with_template [<name> <method>] + [(def .public <name> + (All (_ !) + (-> (Request !) + (Request !))) + (has [#resource //.#method] {<method>}))] -(def .public (form property server) - (All (_ a) (-> (Property a) (-> a Server) Server)) - (function (_ (^.let request [identification protocol resource message])) - (do async.monad - [?body (read_text_body (the //.#body message))] - (when (do try.monad - [body ?body - form (//query.parameters body)] - (context.result form property)) - {try.#Success input} - (server input request) - - {try.#Failure error} - (async.resolved ..failure))))) + [post //.#Post] + [get //.#Get] + [put //.#Put] + [patch //.#Patch] + [delete //.#Delete] + [head //.#Head] + [connect //.#Connect] + [options //.#Options] + [trace //.#Trace] + ) -(def .public (cookies property server) - (All (_ a) (-> (Property a) (-> a Server) Server)) - (function (_ (^.let request [identification protocol resource message])) - (when (do try.monad - [cookies (|> (the //.#headers message) - (dictionary.value "Cookie") - (maybe.else "") - //cookie.get)] - (context.result cookies property)) - {try.#Success input} - (server input request) - - {try.#Failure error} - (async.resolved ..failure)))) +(def .public (uri it) + (All (_ !) + (-> URI (Request !) + (Request !))) + (|>> (has [#resource //.#uri] it))) + +(def .public (with_header it value) + (All (_ ! of) + (-> (Header of) of (Request !) + (Request !))) + (|>> (revised [#message //.#headers] (header.has it value)))) diff --git a/stdlib/source/library/lux/world/net/mime.lux b/stdlib/source/library/lux/world/net/mime.lux index 8e3919836..409e13080 100644 --- a/stdlib/source/library/lux/world/net/mime.lux +++ b/stdlib/source/library/lux/world/net/mime.lux @@ -104,6 +104,10 @@ [audio_3gpp2 "audio/3gpp2"] [video_3gpp2 "video/3gpp2"] [compressed_7z "application/x-7z-compressed"] + + ... https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST + [form "application/x-www-form-urlencoded"] + [multi_part_form "multipart/form-data"] ) (def .public (text encoding) diff --git a/stdlib/source/library/lux/world/net/uri/encoding.lux b/stdlib/source/library/lux/world/net/uri/encoding.lux index 91a5c2020..085d80744 100644 --- a/stdlib/source/library/lux/world/net/uri/encoding.lux +++ b/stdlib/source/library/lux/world/net/uri/encoding.lux @@ -60,10 +60,9 @@ (let [index' (++ index)] (again index' index' - (all .text_composite# - output - (.text_clip# slice_start (nat.- slice_start index) input) - <encoding>)))] + (.text_composite# output + (.text_clip# slice_start (nat.- slice_start index) input) + <encoding>)))] <reserved>)) @@ -71,9 +70,8 @@ (again (++ index) slice_start output))) - (all .text_composite# - output - (.text_clip# slice_start (nat.- slice_start index) input)))))) + (.text_composite# output + (.text_clip# slice_start (nat.- slice_start index) input)))))) ) (def escape (char "%")) @@ -102,9 +100,9 @@ .let [index' (++ encoding_end)]] (again index' index' - (all .text_composite# output - (.text_clip# slice_start (nat.- slice_start index) input) - (text.of_char value)))) + (.text_composite# output + (.text_clip# slice_start (nat.- slice_start index) input) + (text.of_char value)))) (exception.except ..invalid [input]))) _ diff --git a/stdlib/source/library/lux/world/time/day.lux b/stdlib/source/library/lux/world/time/day.lux index 60c30a816..cda78a83f 100644 --- a/stdlib/source/library/lux/world/time/day.lux +++ b/stdlib/source/library/lux/world/time/day.lux @@ -158,10 +158,9 @@ (Exception Nat) (exception.report (list ["Number" (at n.decimal encoded number)] - ["Valid range" (all .text_composite# - (at n.decimal encoded (..number {#Sunday})) - " ~ " - (at n.decimal encoded (..number {#Saturday})))]))) + ["Valid range" (.text_composite# (at n.decimal encoded (..number {#Sunday})) + " ~ " + (at n.decimal encoded (..number {#Saturday})))]))) (def .public (by_number number) (-> Nat (Try Day)) diff --git a/stdlib/source/library/lux/world/time/month.lux b/stdlib/source/library/lux/world/time/month.lux index 93625a2c2..b6fc1f8fa 100644 --- a/stdlib/source/library/lux/world/time/month.lux +++ b/stdlib/source/library/lux/world/time/month.lux @@ -83,10 +83,9 @@ (Exception Nat) (exception.report (list ["Number" (at n.decimal encoded number)] - ["Valid range" (all .text_composite# - (at n.decimal encoded (..number {#January})) - " ~ " - (at n.decimal encoded (..number {#December})))]))) + ["Valid range" (.text_composite# (at n.decimal encoded (..number {#January})) + " ~ " + (at n.decimal encoded (..number {#December})))]))) (def .public (by_number number) (-> Nat (Try Month)) |