From 2501919978663166228818b7225afa6e8545b958 Mon Sep 17 00:00:00 2001 From: hellerve Date: Thu, 12 May 2016 22:07:17 +0200 Subject: [PATCH] made request ready for zeps --- module.zp | 15 +++++++++++++++ request.zp | 32 +++++++++++++++++++------------- 2 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 module.zp diff --git a/module.zp b/module.zp new file mode 100644 index 0000000..78dd4ce --- /dev/null +++ b/module.zp @@ -0,0 +1,15 @@ +#{:name "request" + :version "1.0.0" + :license "GPLv2" + :tests [] + :type :library + :author #{:name "Veit Heller" + :email "veit@veitheller.de" + :github "@hellerve"} + :location #{:github "hellerve/request" + :zpr "request"} + :zepto #{:version :any + :ez #f + :bz #f} + :dependencies () + :dev-dependencies ()} diff --git a/request.zp b/request.zp index 276acd3..4bda328 100644 --- a/request.zp +++ b/request.zp @@ -4,6 +4,7 @@ (list "set-default-headers!" set-default-headers) (list "set-follow-redirects!" set-follow-redirects) (list "request" request) + (list "head" head) (list "get" get) (list "put" put) (list "post" post) @@ -55,7 +56,7 @@ (let* ((body (req :body)) (split (string:split body "\r\n")) (num (car split)) - (size (string->number (++ "#x" num))) + (size (string->number (++ "#x" (if (truthy? num) num "0")))) (diff (- size (- (length body) (+ (length num) 4))))) (if (eq? size 0) (end-request req sock) @@ -71,13 +72,13 @@ (next (substring next (+ (length num) 2) (length next))) (size (+ (string->number (++ "#x" num)) 2))) (if (eq? size 2) - (end-request req sock) + (end-request (hash:set req :body (++ acc chunk)) sock) (loop (++ acc chunk) next size))) (loop acc (++ cur (byte-vector->string (net:recv sock (make-small (- size (length cur)))))) size))))))) - (request (lambda (scheme path . req-headers) + (request (lambda (scheme path opts) (let* ((path (if (string:starts-with path "http://") (substring path 7 (length path)) path)) (path (if (string:starts-with path "https://") (substring path 8 (length path)) path)) (split (string:split path #\/)) @@ -86,12 +87,14 @@ (split (string:split host+port #\:)) (host (car split)) (port (get-from split 1 "80")) - (req-headers (get-from req-headers 0 DFLT-HEADERS)) + (req-headers (get-from opts :headers DFLT-HEADERS)) + (body (get-from opts :body "")) (headers (hash:kv-reduce (lambda (acc kv) (++ acc (->string (car kv)) ": " (->string (cadr kv)) "\r\n")) "" req-headers)) - (text (++ scheme " /" location " HTTP/1.1\r\nHost: " host+port "\r\n" headers "\r\n\r\n")) + (text (++ scheme " /" location " HTTP/1.1\r\nHost: " host+port "\r\n" headers "\r\n\r\n" + (if (truthy? body) (++ body "\r\n\r\n") body))) (sock (net:socket "stream"))) (begin (net:connect sock (net:get-addr-info host port)) @@ -115,14 +118,17 @@ (parse-request (byte-vector->string (++ bytes recvd)) scheme path req-headers #t sock) (loop (net:recv sock (min BSIZE (- len cur))) (++ bytes recvd) ncur len))))))))) - (get (lambda (path . headers) - (request "GET" path (get-from headers 0 DFLT-HEADERS)))) + (head (lambda (path . opts) + (request "HEAD" path (get-from opts 0 #{})))) - (post (lambda (path . headers) - (request "POST" path) (get-from headers 0 DFLT-HEADERS))) + (get (lambda (path . opts) + (request "GET" path (get-from opts 0 #{})))) - (put (lambda (path . headers) - (request "PUT" path) (get-from headers 0 DFLT-HEADERS))) + (post (lambda (path . opts) + (request "POST" path (get-from opts 0 #{})))) - (delete (lambda (path . headers) - (request "DELETE" path) (get-from headers 0 DFLT-HEADERS)))) + (put (lambda (path . opts) + (request "PUT" path (get-from opts 0 #{})))) + + (delete (lambda (path . opts) + (request "DELETE" path (get-from opts 0 #{})))))