def on_request(self, req): proxy_auth = _get_proxy_auth() if req.uri.scheme == "https": proxy = os.environ.get('https_proxy') if proxy: if proxy_auth: proxy_auth = 'Proxy-authorization: %s' % proxy_auth proxy_connect = 'CONNECT %s HTTP/1.0\r\n' % (req.uri.netloc) user_agent = "User-Agent: restkit/%s\r\n" % __version__ proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, user_agent) proxy_uri = urlparse.urlparse(proxy) proxy_host, proxy_port = self._host_port(proxy_uri) # Connect to the proxy server, # very simple recv and error checking p_sock = sock.connect((proxy_host, int(proxy_port)) ) sock.send(p_sock, proxy_pieces) # wait header p = Parser.parse_response() headers = [] buf = "" buf = sock.recv(p_sock, util.CHUNK_SIZE) i = self.parser.filter_headers(headers, buf) if i == -1 and buf: while True: data = sock.recv(p_sock, util.CHUNK_SIZE) if not data: break buf += data i = self.parser.filter_headers(headers, buf) if i != -1: break if p.status_int != 200: raise ProxyError('Error status=%s' % p.status) sock._ssl_wrap_socket(p_sock, None, None) # update socket req.socket = p_sock req.host = proxy_host else: proxy = os.environ.get('http_proxy') if proxy: proxy_uri = urlparse.urlparse(proxy) proxy_host, proxy_port = self._host_port(proxy_uri) if proxy_auth: headers['Proxy-Authorization'] = proxy_auth.strip() req.headers.append(('Proxy-Authorization', proxy_auth.strip())) req.host = proxy_host req.port = proxy_port
def run(): src = data_source(self.fname) func(src, Parser.parse_response())
def run(): src = data_source(self.fname) func(src, Parser.parse_request())
def request(self, url, method='GET', body=None, headers=None): """ make effective request :param url: str, url string :param method: str, by default GET. http verbs :param body: the body, could be a string, an iterator or a file-like object :param headers: dict or list of tupple, http headers """ self.parser = Parser.parse_response(should_close=self.should_close) self._sock = None self.url = url self.final_url = url self.parse_url(url) self.method = method.upper() # headers are better as list headers = headers or [] if isinstance(headers, dict): headers = list(headers.items()) ua = USER_AGENT normalized_headers = [] content_len = None accept_encoding = 'identity' chunked = False # default host try: host = self.uri.netloc.encode('ascii') except UnicodeEncodeError: host = self.uri.netloc.encode('idna') # normalize headers for name, value in headers: name = name.title() if name == "User-Agent": ua = value elif name == "Content-Length": content_len = str(value) elif name == "Accept-Encoding": accept_encoding = 'identity' elif name == "Host": host = value elif name == "Transfer-Encoding": if value.lower() == "chunked": chunked = True normalized_headers.append((name, value)) else: if not isinstance(value, types.StringTypes): value = str(value) normalized_headers.append((name, value)) # set content lengh if needed if body and body is not None: if not content_len: if hasattr(body, 'fileno'): try: body.flush() except IOError: pass content_len = str(os.fstat(body.fileno())[6]) elif hasattr(body, 'getvalue'): try: content_len = str(len(body.getvalue())) except AttributeError: pass elif isinstance(body, types.StringTypes): body = util.to_bytestring(body) content_len = len(body) if content_len: normalized_headers.append(("Content-Length", content_len)) elif not chunked: raise RequestError("Can't determine content length and" + "Transfer-Encoding header is not chunked") if self.method in ('POST', 'PUT') and not body: normalized_headers.append(("Content-Length", "0")) self.body = body self.headers = normalized_headers self.ua = ua self.chunked = chunked self.host_hdr = host self.accept_encoding = accept_encoding # Finally do the request return self.do_send()