Skip to content

Instantly share code, notes, and snippets.

@gchiu
Last active December 18, 2015 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gchiu/5811372 to your computer and use it in GitHub Desktop.
Save gchiu/5811372 to your computer and use it in GitHub Desktop.
mini-http
rebol []
; mini-http is a minimalistic http implementation
mini-http: funct [url [url!] method [word! string!] code [string!] timeout [integer!]
/callback cb
/cookies cookie [string!]
] [
url-obj: http-request: payload: result: port: none
unless cookies [cookie: copy ""]
http-request: {$method $path HTTP/1.0
Host: $host
User-Agent: Mozilla/5.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://$host
Content-Length: $len
Cookie: $cookie
$code}
; Content-Type: text/plain; charset=UTF-8
url-obj: construct/with sys/decode-url url make object! copy [port-id: 80 path: ""]
if empty? url-obj/path [url-obj/path: copy "/"]
payload: reword http-request reduce [
'method method
'path url-obj/path
'host url-obj/host
'len length? code
'cookie cookie
'code code
]
port: make port! rejoin [tcp:// url-obj/host ":" url-obj/port-id]
f-body: compose/deep copy/deep [
switch/default event/type [
lookup [open event/port false]
connect [write event/port (to binary! payload) false]
wrote [read event/port false]
read done [
print result: to-string event/port/data
true
]
] [true]
]
port/awake: func [event /local result] f-body
open port
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment