def test_decode_encode(self): """Encode and Decode should be able to run one on the result of the other and return the original""" original = URL(u'https://w3af.com:443/file.asp?id=1%202') decoded = original.url_decode() encoded = decoded.url_encode() self.assertEqual(original.url_encode(), encoded)
def from_httplib_resp(cls, httplibresp, original_url=None): """ Factory function. Build a HTTPResponse object from a httplib.HTTPResponse instance :param httplibresp: httplib.HTTPResponse instance :param original_url: Optional 'url_object' instance. :return: A HTTPResponse instance """ resp = httplibresp code, msg, hdrs, body = (resp.code, resp.msg, resp.info(), resp.read()) hdrs = Headers(hdrs.items()) if original_url: url_inst = URL(resp.geturl(), original_url.encoding) url_inst = url_inst.url_decode() else: url_inst = original_url = URL(resp.geturl()) httplib_time = DEFAULT_WAIT_TIME if hasattr(httplibresp, 'get_wait_time'): # This is defined in the keep alive http response object httplib_time = httplibresp.get_wait_time() if isinstance(resp, urllib2.HTTPError): # This is possible because in errors.py I do: # err = urllib2.HTTPError(req.get_full_url(), code, msg, hdrs, resp) charset = getattr(resp.fp, 'encoding', None) else: # The encoding attribute is only set on CachedResponse instances charset = getattr(resp, 'encoding', None) return cls(code, body, hdrs, url_inst, original_url, msg, charset=charset, time=httplib_time)