Skip to content

Commit

Permalink
fix Issue 14877 - std.net.curl needs PATCH http method
Browse files Browse the repository at this point in the history
- use infilesize for anything but post requests
  though apparently curl can use both for non-POST
  methods
  • Loading branch information
MartinNowak committed Aug 23, 2015
1 parent 2bc204c commit ceb29a9
Showing 1 changed file with 56 additions and 7 deletions.
63 changes: 56 additions & 7 deletions std/net/curl.d
Expand Up @@ -748,6 +748,49 @@ unittest
}


/** HTTP patch content.
*
* Params:
* url = resource to patch
* patchData = data to send as the body of the request. An array
* of an arbitrary type is accepted and will be cast to ubyte[]
* before sending it.
* conn = HTTP connection to use
*
* The template parameter $(D T) specifies the type to return. Possible values
* are $(D char) and $(D ubyte) to return $(D char[]) or $(D ubyte[]).
*
* Example:
* ----
* auto http = HTTP();
* http.addRequestHeader("Content-Type", "application/json");
* auto content = patch("d-lang.appspot.com/testUrl2", `{"title": "Patched Title"}`, http);
* ----
*
* Returns:
* A T[] range containing the content of the resource pointed to by the URL.
*
* See_Also: $(LREF HTTP.Method)
*/
T[] patch(T = char, PatchUnit)(const(char)[] url, const(PatchUnit)[] patchData,
HTTP conn = HTTP())
if (is(T == char) || is(T == ubyte))
{
conn.method = HTTP.Method.patch;
return _basicHTTP!(T)(url, patchData, conn);
}

unittest
{
// google appspot does not allow patchmethod.
// if (!netAllowed()) return;
//
// auto http = HTTP();
// http.addRequestHeader("Content-Type", "application/json");
// auto content = patch("d-lang.appspot.com/testUrl2", `{"title": "Patched Title"}`, http);
}


/*
* Helper function for the high level interface.
*
Expand All @@ -758,7 +801,8 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien
{
immutable doSend = sendData !is null &&
(client.method == HTTP.Method.post ||
client.method == HTTP.Method.put);
client.method == HTTP.Method.put ||
client.method == HTTP.Method.patch);

scope (exit)
{
Expand Down Expand Up @@ -2317,6 +2361,10 @@ struct HTTP
p.curl.set(CurlOption.customrequest, "CONNECT");
opt = CurlOption.customrequest;
break;
case Method.patch:
p.curl.set(CurlOption.customrequest, "PATCH");
opt = CurlOption.customrequest;
break;
}

scope (exit) p.curl.clear(opt);
Expand Down Expand Up @@ -2819,14 +2867,14 @@ struct HTTP
CurlOption lenOpt;

// Force post if necessary
if (p.method != Method.put && p.method != Method.post)
if (p.method != Method.put && p.method != Method.post &&
p.method != Method.patch)
p.method = Method.post;

if (p.method == Method.put)
lenOpt = CurlOption.infilesize_large;
else
// post
if (p.method == Method.post)
lenOpt = CurlOption.postfieldsize_large;
else
lenOpt = CurlOption.infilesize_large;

if (len == size_t.max)
{
Expand Down Expand Up @@ -2879,7 +2927,8 @@ struct HTTP
del, ///
options, ///
trace, ///
connect ///
connect, ///
patch, ///
}

/**
Expand Down

0 comments on commit ceb29a9

Please sign in to comment.