From 16a7f13408b5fb1847dff3a9ba2f2657c384bc39 Mon Sep 17 00:00:00 2001 From: Mike Fikes Date: Wed, 21 Nov 2018 21:21:21 -0500 Subject: [PATCH] Option to have planck.http follow redirects Fixes #840 --- CHANGELOG.md | 1 + planck-c/http.c | 13 +++++++++++++ planck-cljs/src/planck/http.cljs | 12 ++++++++---- site/src/planck-http.md | 10 ++++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba8b1014..4e41f900 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This change ## [Unreleased] ### Added +- Option to have `planck.http` follow redirects ([#842](https://github.com/mfikes/planck/issues/842)) - Support setting user agent in `planck.http` ([#838](https://github.com/mfikes/planck/issues/838)) ### Fixed diff --git a/planck-c/http.c b/planck-c/http.c index 9a5ecd84..dec6a765 100644 --- a/planck-c/http.c +++ b/planck-c/http.c @@ -137,6 +137,19 @@ JSValueRef function_http_request(JSContextRef ctx, JSObjectRef function, JSObjec user_agent = value_to_c_string(ctx, user_agent_ref); curl_easy_setopt(handle, CURLOPT_USERAGENT, user_agent); } + + JSValueRef follow_redirects_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("follow-redirects"), NULL); + if (JSValueIsBoolean(ctx, follow_redirects_ref)) { + if (JSValueToBoolean(ctx, follow_redirects_ref)) { + curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1); + + JSValueRef max_redirects_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("max-redirects"), NULL); + if (JSValueIsNumber(ctx, max_redirects_ref)) { + long max_redirects = (long)JSValueToNumber(ctx, max_redirects_ref, NULL); + curl_easy_setopt(handle, CURLOPT_MAXREDIRS, max_redirects); + } + } + } JSObjectRef result = JSObjectMake(ctx, NULL, NULL); JSValueProtect(ctx, result); diff --git a/planck-cljs/src/planck/http.cljs b/planck-cljs/src/planck/http.cljs index 6839e527..1b0401ac 100644 --- a/planck-cljs/src/planck/http.cljs +++ b/planck-cljs/src/planck/http.cljs @@ -184,6 +184,8 @@ :content-type, keyword or string Valid keywords are :json or :xml :headers, map, a map containing headers :user-agent, string, the user agent header to send + :follow-redirects, boolean, follow HTTP location redirects + :max-redirects, number, maximum number of redirects to follow :socket, string, specifying a system path to a socket to use :binary-response, boolean, encode response body as vector of unsigned bytes" ([url] (get url {})) @@ -198,6 +200,8 @@ (and (every? keyword? (keys m)) (every? string? (vals m)))))) (s/def ::user-agent string?) +(s/def ::follow-redirects boolean?) +(s/def ::max-redirects pos-int?) (s/def ::socket string?) (s/def ::binary-response boolean?) (s/def ::body (s/or :string string? :binary vector?)) @@ -206,7 +210,7 @@ (s/fdef get :args (s/cat :url string? :opts (s/? (s/keys :opt-un [::timeout ::debug ::accepts ::content-type ::headers ::socket - ::binary-response ::insecure ::user-agent]))) + ::binary-response ::insecure ::user-agent ::follow-redirects ::max-redirects]))) :ret (s/keys :req-un [::body ::headers ::status])) (defn head @@ -243,7 +247,7 @@ (defn post "Performs a POST request. It takes an URL and an optional map of options - These options include the options for get in addition to: + These options include the relevant options for get in addition to: :form-params, a map, will become the body of the request, urlencoded :multipart-params, a list of tuples, used for file-upload {:multipart-params [[\"name\" \"value\"] @@ -261,7 +265,7 @@ (defn put "Performs a PUT request. It takes an URL and an optional map of options - These options include the options for get in addition to: + These options include the relevant options for get in addition to: :form-params, a map, will become the body of the request, urlencoded :multipart-params, a list of tuples, used for file-upload {:multipart-params [[\"name\" \"value\"] @@ -276,7 +280,7 @@ (defn patch "Performs a PATCH request. It takes an URL and an optional map of options - These options include the options for get in addition to: + These options include the relevant options for get in addition to: :form-params, a map, will become the body of the request, urlencoded :multipart-params, a list of tuples, used for file-upload {:multipart-params [[\"name\" \"value\"] diff --git a/site/src/planck-http.md b/site/src/planck-http.md index e2ee28d2..6cbca98a 100644 --- a/site/src/planck-http.md +++ b/site/src/planck-http.md @@ -24,11 +24,13 @@ _Vars_ `:content-type`, keyword or string Valid keywords are `:json` or `:xml`
`:headers`, map, a map containing headers
`:user-agent`, string, the user agent header to send
+ `:follow-redirects`, boolean, follow HTTP location redirects + `:max-redirects`, number, maximum number of redirects to follow `:socket`, string, specifying a system path to a socket to use
`:binary-response`, boolean, encode response body as vector of unsigned bytes Spec
- _args_: `(cat :url string? :opts (? (keys :opt-un [::timeout ::debug ::accepts ::content-type ::headers ::socket ::binary-response ::insecure ::user-agent])))`
+ _args_: `(cat :url string? :opts (? (keys :opt-un [::timeout ::debug ::accepts ::content-type ::headers ::socket ::binary-response ::insecure ::user-agent ::follow-redirects ::max-redirects])))`
_ret_: `(keys :req-un [::body ::headers ::status])` ### head @@ -64,7 +66,7 @@ Spec
`([url] [url opts])` Performs a POST request. It takes an URL and an optional map of options - These options include the options for get in addition to:
+ These options include the relevant options for get in addition to:
`:form-params`, a map, will become the body of the request, urlencoded
`:multipart-params`, a list of tuples, used for file-upload
`{:multipart-params [["name" "value"]`
@@ -78,7 +80,7 @@ Spec
`([url] [url opts])` Performs a PUT request. It takes an URL and an optional map of options - These options include the options for get in addition to:
+ These options include the relevant options for get in addition to:
`:form-params`, a map, will become the body of the request, urlencoded
`:multipart-params`, a list of tuples, used for file-upload
`{:multipart-params [["name" "value"]`
@@ -92,7 +94,7 @@ Spec
`([url] [url opts])` Performs a PATCH request. It takes an URL and an optional map of options - These options include the options for get in addition to:
+ These options include the relevant options for get in addition to:
`:form-params`, a map, will become the body of the request, urlencoded
`:multipart-params`, a list of tuples, used for file-upload
`{:multipart-params [["name" "value"]`