Skip to content

Commit

Permalink
curl_httpclient: run streaming and header callbacks on the IOLoop.
Browse files Browse the repository at this point in the history
This ensures a more consistent execution environment and allows
a streaming or header callback to invoke other CurlAsyncHTTPClient
methods.

This change also fills in HTTPResponse.headers even when a header_callback
is used, similar to simple_httpclient's behavior.

Closes tornadoweb#1188.
  • Loading branch information
bdarnell committed Sep 21, 2014
1 parent 05c933d commit 4ad3e1e
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions tornado/curl_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from __future__ import absolute_import, division, print_function, with_statement

import collections
import functools
import logging
import pycurl
import threading
Expand Down Expand Up @@ -287,15 +288,12 @@ def _curl_setup_request(self, curl, request, buffer, headers):
curl.setopt(pycurl.HTTPHEADER,
[native_str("%s: %s" % i) for i in request.headers.items()])

if request.header_callback:
curl.setopt(pycurl.HEADERFUNCTION,
lambda line: request.header_callback(native_str(line)))
else:
curl.setopt(pycurl.HEADERFUNCTION,
lambda line: self._curl_header_callback(
headers, native_str(line)))
curl.setopt(pycurl.HEADERFUNCTION,
functools.partial(self._curl_header_callback,
headers, request.header_callback))
if request.streaming_callback:
write_function = request.streaming_callback
write_function = lambda chunk: self.io_loop.add_callback(
request.streaming_callback, chunk)
else:
write_function = buffer.write
if bytes is str: # py2
Expand Down Expand Up @@ -434,7 +432,10 @@ def ioctl(cmd):
if request.prepare_curl_callback is not None:
request.prepare_curl_callback(curl)

def _curl_header_callback(self, headers, header_line):
def _curl_header_callback(self, headers, header_callback, header_line):
header_line = native_str(header_line)
if header_callback is not None:
self.io_loop.add_callback(header_callback, header_line)
# header_line as returned by curl includes the end-of-line characters.
header_line = header_line.strip()
if header_line.startswith("HTTP/"):
Expand Down

0 comments on commit 4ad3e1e

Please sign in to comment.