diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2017-05-04 16:02:50 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-05-04 16:02:50 +0200 |
commit | 764cc0217735454c166cb7dd490db58f5fa6fe26 (patch) | |
tree | 63633b39ae24815e8b677ccbc9bac2cb01c39d3b /lib/pure/httpclient.nim | |
parent | afa80092d378a6dbc116c0aa3ed3964fd8c599d6 (diff) | |
parent | c1aa973758a60d7ef0e698c94861b74132612de5 (diff) | |
download | Nim-764cc0217735454c166cb7dd490db58f5fa6fe26.tar.gz |
Merge branch 'devel' into araq
Diffstat (limited to 'lib/pure/httpclient.nim')
-rw-r--r-- | lib/pure/httpclient.nim | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 62c7e2067..1b8a20b65 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -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 @@ -1030,32 +1033,38 @@ proc newConnection(client: HttpClient | AsyncHttpClient, if client.currentURL.hostname != url.hostname or client.currentURL.scheme != url.scheme or client.currentURL.port != url.port: + let isSsl = url.scheme.toLowerAscii() == "https" + + if isSsl and not defined(ssl): + raise newException(HttpRequestError, + "SSL support is not available. Cannot connect over SSL.") + if client.connected: client.close() - when client is HttpClient: - client.socket = newSocket() - elif client is AsyncHttpClient: - client.socket = newAsyncSocket() - else: {.fatal: "Unsupported client type".} - # TODO: I should be able to write 'net.Port' here... let port = if url.port == "": - if url.scheme.toLower() == "https": + if isSsl: nativesockets.Port(443) else: nativesockets.Port(80) else: nativesockets.Port(url.port.parseInt) - if url.scheme.toLower() == "https": - when defined(ssl): - client.sslContext.wrapSocket(client.socket) - else: - raise newException(HttpRequestError, - "SSL support is not available. Cannot connect over SSL.") + when client is HttpClient: + client.socket = await net.dial(url.hostname, port) + elif client is AsyncHttpClient: + client.socket = await asyncnet.dial(url.hostname, port) + else: {.fatal: "Unsupported client type".} + + when defined(ssl): + if isSsl: + try: + client.sslContext.wrapConnectedSocket(client.socket, handshakeAsClient) + except: + client.socket.close() + raise getCurrentException() - await client.socket.connect(url.hostname, port) client.currentURL = url client.connected = true @@ -1188,7 +1197,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 +1208,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) |