Skip to content

Commit

Permalink
Option to have planck.http follow redirects
Browse files Browse the repository at this point in the history
Fixes #840
  • Loading branch information
mfikes committed Nov 30, 2018
1 parent 10ed5e6 commit 16a7f13
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions planck-c/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions planck-cljs/src/planck/http.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}))
Expand All @@ -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?))
Expand All @@ -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
Expand Down Expand Up @@ -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\"]
Expand All @@ -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\"]
Expand All @@ -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\"]
Expand Down
10 changes: 6 additions & 4 deletions site/src/planck-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ _Vars_
`:content-type`, keyword or string Valid keywords are `:json` or `:xml`<br/>
`:headers`, map, a map containing headers<br/>
`:user-agent`, string, the user agent header to send<br/>
`: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<br/>
`:binary-response`, boolean, encode response body as vector of unsigned bytes

Spec<br/>
_args_: `(cat :url string? :opts (? (keys :opt-un [::timeout ::debug ::accepts ::content-type ::headers ::socket ::binary-response ::insecure ::user-agent])))`<br/>
_args_: `(cat :url string? :opts (? (keys :opt-un [::timeout ::debug ::accepts ::content-type ::headers ::socket ::binary-response ::insecure ::user-agent ::follow-redirects ::max-redirects])))`<br/>
_ret_: `(keys :req-un [::body ::headers ::status])`

### <a name="head"></a>head
Expand Down Expand Up @@ -64,7 +66,7 @@ Spec<br/>
`([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:<br/>
These options include the relevant options for get in addition to:<br/>
`:form-params`, a map, will become the body of the request, urlencoded<br/>
`:multipart-params`, a list of tuples, used for file-upload<br/>
`{:multipart-params [["name" "value"]`<br/>
Expand All @@ -78,7 +80,7 @@ Spec<br/>
`([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:<br/>
These options include the relevant options for get in addition to:<br/>
`:form-params`, a map, will become the body of the request, urlencoded<br/>
`:multipart-params`, a list of tuples, used for file-upload<br/>
`{:multipart-params [["name" "value"]`<br/>
Expand All @@ -92,7 +94,7 @@ Spec<br/>
`([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:<br/>
These options include the relevant options for get in addition to:<br/>
`:form-params`, a map, will become the body of the request, urlencoded<br/>
`:multipart-params`, a list of tuples, used for file-upload<br/>
`{:multipart-params [["name" "value"]`<br/>
Expand Down

0 comments on commit 16a7f13

Please sign in to comment.