Fixes #5710. Closes #5711.

This commit is contained in:
Dominik Picheta
2017-04-15 09:03:12 +02:00
parent 55b5401dc6
commit 5cf31417a6

View File

@@ -434,7 +434,7 @@ proc `[]=`*(p: var MultipartData, name: string,
## "<html><head></head><body><p>test</p></body></html>")
p.add(name, file.content, file.name, file.contentType)
proc format(p: MultipartData): tuple[header, body: string] =
proc format(p: MultipartData): tuple[contentType, body: string] =
if p == nil or p.content == nil or p.content.len == 0:
return ("", "")
@@ -449,7 +449,7 @@ proc format(p: MultipartData): tuple[header, body: string] =
if not found:
break
result.header = "Content-Type: multipart/form-data; boundary=" & bound & "\c\L"
result.contentType = "multipart/form-data; boundary=" & bound
result.body = ""
for s in p.content:
result.body.add("--" & bound & "\c\L" & s)
@@ -640,7 +640,7 @@ proc post*(url: string, extraHeaders = "", body = "",
## ``multipart/form-data`` POSTs comfortably.
##
## **Deprecated since version 0.15.0**: use ``HttpClient.post`` instead.
let (mpHeaders, mpBody) = format(multipart)
let (mpContentType, mpBody) = format(multipart)
template withNewLine(x): untyped =
if x.len > 0 and not x.endsWith("\c\L"):
@@ -650,9 +650,12 @@ proc post*(url: string, extraHeaders = "", body = "",
var xb = mpBody.withNewLine() & body
var xh = extraHeaders.withNewLine() & mpHeaders.withNewLine() &
var xh = extraHeaders.withNewLine() &
withNewLine("Content-Length: " & $len(xb))
if not multipart.isNil:
xh.add(withNewLine("Content-Type: " & mpContentType))
result = request(url, httpPOST, xh, xb, sslContext, timeout, userAgent,
proxy)
var lastURL = url
@@ -1188,7 +1191,7 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
##
## This procedure will follow redirects up to a maximum number of redirects
## specified in ``client.maxRedirects``.
let (mpHeader, mpBody) = format(multipart)
let (mpContentType, mpBody) = format(multipart)
# TODO: Support FutureStream for `body` parameter.
template withNewLine(x): untyped =
if x.len > 0 and not x.endsWith("\c\L"):
@@ -1199,7 +1202,7 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
var headers = newHttpHeaders()
if multipart != nil:
headers["Content-Type"] = mpHeader.split(": ")[1]
headers["Content-Type"] = mpContentType
headers["Content-Length"] = $len(xb)
result = await client.requestAux(url, $HttpPOST, xb, headers)