Skip to content

Commit

Permalink
FEAT: enable raw map! and binary! data in HTTP scheme's write a…
Browse files Browse the repository at this point in the history
…ction

Using this:
```
write http://localhost:8081/result/ #{0102}
write http://localhost:8081/result/ #(a: 1)

```
is now same like:
```
write http://localhost:8081/result/ [POST #(Content-Type: "application/octet-stream") #{0102}]
write http://localhost:8081/result/ [POST #(Content-Type: "application/json; charset=utf-8") #(a: 1)]

```
  • Loading branch information
Oldes committed May 29, 2020
1 parent c4c3747 commit 55a8f92
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/mezz/prot-http.r
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,17 @@ sys/make-scheme [
][
sys/log/debug 'HTTP "WRITE"
;?? port
unless any [block? :value binary? :value any-string? :value][value: form :value]
unless block? value [value: reduce [[Content-Type: "application/x-www-form-urlencoded; charset=utf-8"] value]]
case [
map? value [
value: reduce [[Content-Type: "application/json; charset=utf-8"] encode 'JSON value]
]
binary? value [
value: reduce [[Content-Type: "application/octet-stream"] value]
]
not block? value [
value: reduce [[Content-Type: "application/x-www-form-urlencoded; charset=utf-8"] form value]
]
]

either any-function? :port/awake [
unless open? port [cause-error 'Access 'not-open port/spec/ref]
Expand Down
18 changes: 15 additions & 3 deletions src/modules/httpd.r3
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,14 @@ sys/make-scheme [
]
]

On-Post: func[ctx [object!] /local content header length type][
On-Post: func[ctx [object!] /local content header length type temp][
;@@ TODO: handle `Expect` header: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.20
header: ctx/inp/header
length: select header 'Content-Length
unless find header 'Content-Type [
;make sure there is any Content-Type
extend header 'Content-Type "application/octet-stream"
]
either all [
integer? length
length > length? content: ctx/inp/content
Expand All @@ -291,11 +295,19 @@ sys/make-scheme [
][
ctx/state: 'data-received
ctx/out/status: 200
type: header/Content-Type: decode-content-params select header 'Content-Type
type: header/Content-Type: decode-content-params header/Content-Type
;@@ TODO: handle charset if not utf-8!
case [
type/2 = "x-www-form-urlencoded" [
; using full target decoder, although only values are needed
ctx/inp/content: select decode-target content 'values
temp: decode-target content
ctx/inp/content: reduce [
select temp 'values ; parsed [key value ...]
select temp 'original ; raw data
]
]
type/2 = "json" [
ctx/inp/content: decode 'json content
]
type/1 = "multipart" [
ctx/inp/content: decode-multipart-data type content
Expand Down

0 comments on commit 55a8f92

Please sign in to comment.