Merge pull request #98 from Kriechi/master

backport changes
This commit is contained in:
Thomas Kriechbaumer 2015-09-21 18:51:44 +02:00
commit 21579f0add
2 changed files with 19 additions and 18 deletions

View File

@ -216,7 +216,7 @@ class Message(object):
def body(self, body): def body(self, body):
self._body = body self._body = body
if isinstance(body, bytes): if isinstance(body, bytes):
self.headers[b"Content-Length"] = str(len(body)).encode() self.headers[b"content-length"] = str(len(body)).encode()
content = body content = body
@ -268,8 +268,8 @@ class Request(Message):
response. That is, we remove ETags and If-Modified-Since headers. response. That is, we remove ETags and If-Modified-Since headers.
""" """
delheaders = [ delheaders = [
b"If-Modified-Since", b"if-modified-since",
b"If-None-Match", b"if-none-match",
] ]
for i in delheaders: for i in delheaders:
self.headers.pop(i, None) self.headers.pop(i, None)
@ -279,16 +279,16 @@ class Request(Message):
Modifies this request to remove headers that will compress the Modifies this request to remove headers that will compress the
resource's data. resource's data.
""" """
self.headers["Accept-Encoding"] = "identity" self.headers["accept-encoding"] = b"identity"
def constrain_encoding(self): def constrain_encoding(self):
""" """
Limits the permissible Accept-Encoding values, based on what we can Limits the permissible Accept-Encoding values, based on what we can
decode appropriately. decode appropriately.
""" """
accept_encoding = native(self.headers.get("Accept-Encoding"), "ascii") accept_encoding = native(self.headers.get("accept-encoding"), "ascii")
if accept_encoding: if accept_encoding:
self.headers["Accept-Encoding"] = ( self.headers["accept-encoding"] = (
', '.join( ', '.join(
e e
for e in encoding.ENCODINGS for e in encoding.ENCODINGS
@ -300,7 +300,7 @@ class Request(Message):
""" """
Update the host header to reflect the current target. Update the host header to reflect the current target.
""" """
self.headers["Host"] = self.host self.headers["host"] = self.host
def get_form(self): def get_form(self):
""" """
@ -309,9 +309,9 @@ class Request(Message):
indicates non-form data. indicates non-form data.
""" """
if self.body: if self.body:
if HDR_FORM_URLENCODED in self.headers.get("Content-Type", b"").lower(): if HDR_FORM_URLENCODED in self.headers.get("content-type", b"").lower():
return self.get_form_urlencoded() return self.get_form_urlencoded()
elif HDR_FORM_MULTIPART in self.headers.get("Content-Type", b"").lower(): elif HDR_FORM_MULTIPART in self.headers.get("content-type", b"").lower():
return self.get_form_multipart() return self.get_form_multipart()
return ODict([]) return ODict([])
@ -321,12 +321,12 @@ class Request(Message):
Returns an empty ODict if there is no data or the content-type Returns an empty ODict if there is no data or the content-type
indicates non-form data. indicates non-form data.
""" """
if self.body and HDR_FORM_URLENCODED in self.headers.get("Content-Type", b"").lower(): if self.body and HDR_FORM_URLENCODED in self.headers.get("content-type", b"").lower():
return ODict(utils.urldecode(self.body)) return ODict(utils.urldecode(self.body))
return ODict([]) return ODict([])
def get_form_multipart(self): def get_form_multipart(self):
if self.body and HDR_FORM_MULTIPART in self.headers.get("Content-Type", b"").lower(): if self.body and HDR_FORM_MULTIPART in self.headers.get("content-type", b"").lower():
return ODict( return ODict(
utils.multipartdecode( utils.multipartdecode(
self.headers, self.headers,
@ -341,7 +341,7 @@ class Request(Message):
""" """
# FIXME: If there's an existing content-type header indicating a # FIXME: If there's an existing content-type header indicating a
# url-encoded form, leave it alone. # url-encoded form, leave it alone.
self.headers[b"Content-Type"] = HDR_FORM_URLENCODED self.headers[b"content-type"] = HDR_FORM_URLENCODED
self.body = utils.urlencode(odict.lst) self.body = utils.urlencode(odict.lst)
def get_path_components(self): def get_path_components(self):
@ -398,9 +398,9 @@ class Request(Message):
but not the resolved name. This is disabled by default, as an but not the resolved name. This is disabled by default, as an
attacker may spoof the host header to confuse an analyst. attacker may spoof the host header to confuse an analyst.
""" """
if hostheader and "Host" in self.headers: if hostheader and "host" in self.headers:
try: try:
return self.headers["Host"].decode("idna") return self.headers["host"].decode("idna")
except ValueError: except ValueError:
pass pass
if self.host: if self.host:
@ -429,7 +429,7 @@ class Request(Message):
headers. headers.
""" """
v = cookies.format_cookie_header(odict) v = cookies.format_cookie_header(odict)
self.headers["Cookie"] = v self.headers["cookie"] = v
@property @property
def url(self): def url(self):
@ -485,7 +485,7 @@ class Response(Message):
return "<Response: {status_code} {msg} ({contenttype}, {size})>".format( return "<Response: {status_code} {msg} ({contenttype}, {size})>".format(
status_code=self.status_code, status_code=self.status_code,
msg=self.msg, msg=self.msg,
contenttype=self.headers.get("Content-Type", "unknown content type"), contenttype=self.headers.get("content-type", "unknown content type"),
size=size) size=size)
def get_cookies(self): def get_cookies(self):
@ -498,7 +498,7 @@ class Response(Message):
attributes (e.g. HTTPOnly) are indicated by a Null value. attributes (e.g. HTTPOnly) are indicated by a Null value.
""" """
ret = [] ret = []
for header in self.headers.get_all("Set-Cookie"): for header in self.headers.get_all("set-cookie"):
v = cookies.parse_set_cookie_header(native(header, "ascii")) v = cookies.parse_set_cookie_header(native(header, "ascii"))
if v: if v:
name, value, attrs = v name, value, attrs = v
@ -521,4 +521,4 @@ class Response(Message):
i[1][1] i[1][1]
) )
) )
self.headers.set_all("Set-Cookie", values) self.headers.set_all("set-cookie", values)

View File

@ -279,6 +279,7 @@ class Reader(_FileLike):
if tuple(int(x) for x in OpenSSL.__version__.split(".")[:2]) > (0, 15): if tuple(int(x) for x in OpenSSL.__version__.split(".")[:2]) > (0, 15):
return self.o.recv(length, socket.MSG_PEEK) return self.o.recv(length, socket.MSG_PEEK)
else: else:
# TODO: remove once a new version is released
# Polyfill for pyOpenSSL <= 0.15.1 # Polyfill for pyOpenSSL <= 0.15.1
# Taken from https://github.com/pyca/pyopenssl/commit/1d95dea7fea03c7c0df345a5ea30c12d8a0378d2 # Taken from https://github.com/pyca/pyopenssl/commit/1d95dea7fea03c7c0df345a5ea30c12d8a0378d2
buf = SSL._ffi.new("char[]", length) buf = SSL._ffi.new("char[]", length)