def use_GAE(self): if self.command == "OPTIONS": return self.send_method_allows(self.headers, self.payload) if self.command not in self.gae_support_methods: xlog.warn("Method %s not support in GAEProxy for %s", self.command, self.path) return self.wfile.write(('HTTP/1.1 404 Not Found\r\n\r\n').encode()) gae_handler.handler(self.command, self.url, self.headers, self.payload, self.wfile)
def use_GAE(self): if self.command == "OPTIONS": return self.send_method_allows(self.headers, self.payload) if self.command not in self.gae_support_methods: xlog.warn("Method %s not support in GAEProxy for %s", self.command, self.path) return self.wfile.write( ('HTTP/1.1 404 Not Found\r\n\r\n').encode()) gae_handler.handler(self.command, self.url, self.headers, self.payload, self.wfile)
def do_AGENT(self): request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = b'' if 'Content-Length' in request_headers: try: payload = self.rfile.read(int(request_headers.get('Content-Length', 0))) except NetWorkIOError as e: logging.error('handle_method_urlfetch read payload failed:%s', e) return gae_handler.handler(self.command, self.path, request_headers, payload, self.wfile)
def go_AGENT(self): def get_crlf(rfile): crlf = rfile.readline(2) if crlf != "\r\n": xlog.warn("chunk header read fail crlf") request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = b'' if 'Content-Length' in request_headers: try: payload_len = int(request_headers.get('Content-Length', 0)) #xlog.debug("payload_len:%d %s %s", payload_len, self.command, self.path) payload = self.rfile.read(payload_len) except NetWorkIOError as e: xlog.error('handle_method_urlfetch read payload failed:%s', e) return elif 'Transfer-Encoding' in request_headers: # chunked, used by facebook android client payload = "" while True: chunk_size_str = self.rfile.readline(65537) chunk_size_list = chunk_size_str.split(";") chunk_size = int("0x" + chunk_size_list[0], 0) if len(chunk_size_list) > 1 and chunk_size_list[1] != "\r\n": xlog.warn("chunk ext: %s", chunk_size_str) if chunk_size == 0: while True: line = self.rfile.readline(65537) if line == "\r\n": break else: xlog.warn("entity header:%s", line) break payload += self.rfile.read(chunk_size) get_crlf(self.rfile) if self.command == "OPTIONS": return self.send_method_allows(request_headers, payload) if self.command not in self.gae_support_methods: xlog.warn("Method %s not support in GAEProxy for %s", self.command, self.path) return self.wfile.write( ('HTTP/1.1 404 Not Found\r\n\r\n').encode()) xlog.debug("GAE %s %s from:%s", self.command, self.path, self.address_string()) gae_handler.handler(self.command, self.url, request_headers, payload, self.wfile)
def do_AGENT(self): def get_crlf(rfile): crlf = rfile.readline(2) if crlf != "\r\n": xlog.warn("chunk header read fail crlf") request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = b'' if 'Content-Length' in request_headers: try: payload_len = int(request_headers.get('Content-Length', 0)) #xlog.debug("payload_len:%d %s %s", payload_len, self.command, self.path) payload = self.rfile.read(payload_len) except NetWorkIOError as e: xlog.error('handle_method_urlfetch read payload failed:%s', e) return elif 'Transfer-Encoding' in request_headers: # chunked, used by facebook android client payload = "" while True: chunk_size_str = self.rfile.readline(65537) chunk_size_list = chunk_size_str.split(";") chunk_size = int("0x"+chunk_size_list[0], 0) if len(chunk_size_list) > 1 and chunk_size_list[1] != "\r\n": xlog.warn("chunk ext: %s", chunk_size_str) if chunk_size == 0: while True: line = self.rfile.readline(65537) if line == "\r\n": break else: xlog.warn("entity header:%s", line) break payload += self.rfile.read(chunk_size) get_crlf(self.rfile) if self.command == "OPTIONS": return self.send_method_allows(request_headers, payload) if self.command not in self.gae_support_methods: xlog.warn("Method %s not support in GAEProxy for %s", self.command, self.path) return self.wfile.write(('HTTP/1.1 404 Not Found\r\n\r\n').encode()) xlog.debug("GAE %s %s", self.command, self.path) gae_handler.handler(self.command, self.path, request_headers, payload, self.wfile)
def do_AGENT(self): def get_crlf(rfile): crlf = rfile.readline(2) if crlf != "\r\n": xlog.warn("chunk header read fail crlf") request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = b'' if 'Content-Length' in request_headers: try: payload_len = int(request_headers.get('Content-Length', 0)) #xlog.debug("payload_len:%d %s %s", payload_len, self.command, self.path) payload = self.rfile.read(payload_len) except NetWorkIOError as e: xlog.error('handle_method_urlfetch read payload failed:%s', e) return elif 'Transfer-Encoding' in request_headers: # chunked, used by facebook android client payload = "" while True: chunk_size_str = self.rfile.readline(65537) chunk_size_list = chunk_size_str.split(";") chunk_size = int("0x" + chunk_size_list[0], 0) if len(chunk_size_list) > 1 and chunk_size_list[1] != "\r\n": xlog.warn("chunk ext: %s", chunk_size_str) if chunk_size == 0: while True: line = self.rfile.readline(65537) if line == "\r\n": break else: xlog.warn("entity header:%s", line) break payload += self.rfile.read(chunk_size) get_crlf(self.rfile) xlog.debug("GAE %s %s", self.command, self.path) gae_handler.handler(self.command, self.path, request_headers, payload, self.wfile)
def do_AGENT(self): def get_crlf(rfile): crlf = rfile.readline(2) if crlf != "\r\n": logging.warn("chunk header read fail crlf") request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = b'' if 'Content-Length' in request_headers: try: payload_len = int(request_headers.get('Content-Length', 0)) #logging.debug("payload_len:%d %s %s", payload_len, self.command, self.path) payload = self.rfile.read(payload_len) except NetWorkIOError as e: logging.error('handle_method_urlfetch read payload failed:%s', e) return elif 'Transfer-Encoding' in request_headers: # chunked, used by facebook android client payload = "" while True: chunk_size_str = self.rfile.readline(65537) chunk_size_list = chunk_size_str.split(";") chunk_size = int("0x"+chunk_size_list[0], 0) if len(chunk_size_list) > 1 and chunk_size_list[1] != "\r\n": logging.warn("chunk ext: %s", chunk_size_str) if chunk_size == 0: while True: line = self.rfile.readline(65537) if line == "\r\n": break else: logging.warn("entity header:%s", line) break payload += self.rfile.read(chunk_size) get_crlf(self.rfile) gae_handler.handler(self.command, self.path, request_headers, payload, self.wfile)
def go_AGENT(self): request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = self.read_payload() if self.command == "OPTIONS": return self.send_method_allows(request_headers, payload) if self.command not in self.gae_support_methods: xlog.warn("Method %s not support in GAEProxy for %s", self.command, self.path) return self.wfile.write(('HTTP/1.1 404 Not Found\r\n\r\n').encode()) xlog.debug("GAE %s %s from:%s", self.command, self.url, self.address_string()) if gae_handler.handler(self.command, self.host, self.url, request_headers, payload, self.wfile, self.go_DIRECT) != "ok": self.close_connection = 1
def handler(method, host, url, headers, body, wfile): time_request = time.time() if "Connection" in headers and headers["Connection"] == "close": del headers["Connection"] errors = [] response = None while True: if time.time() - time_request > 30: return gae_handler.return_fail_message(wfile) try: response = fetch(method, host, url, headers, body) if response: if response.status > 400: server_type = response.getheader('Server', "") if "G" not in server_type and "g" not in server_type and server_type not in google_server_types: try: path = "https://" + host + url gae_handler.handler(method, path, headers, body, wfile) except: xlog.warn( "IP:%s host:%s not support GAE, server type:%s status:%d", response.ssl_sock.ip, host, server_type, response.status) google_ip.report_connect_fail(response.ssl_sock.ip) response.close() continue break except OpenSSL.SSL.SysCallError as e: errors.append(e) xlog.warn("direct_handler.handler err:%r %s/%s", e, host, url) except Exception as e: errors.append(e) xlog.exception('direct_handler.handler %r %s %s , retry...', e, host, url) try: send_to_browser = True try: response_headers = dict( (k.title(), v) for k, v in response.getheaders()) wfile.write("HTTP/1.1 %d %s\r\n" % (response.status, response.reason)) for key, value in response.getheaders(): send_header(wfile, key, value) wfile.write("\r\n") except Exception as e: send_to_browser = False wait_time = time.time() - time_request xlog.info( "direct_handler.handler send response fail. t:%d e:%r %s%s", wait_time, e, host, url) if method == 'HEAD' or response.status in (204, 304): xlog.info("DIRECT t:%d %d %s %s", (time.time() - time_request) * 1000, response.status, host, url) https_manager.save_ssl_connection_for_reuse( response.ssl_sock, host) response.close() return if 'Transfer-Encoding' in response_headers: length = 0 while True: try: data = response.read(8192) except httplib.IncompleteRead, e: data = e.partial except Exception as e: google_ip.report_connect_closed(response.ssl_sock.ip, "receive fail") xlog.warn( "direct_handler.handler send Transfer-Encoding t:%d e:%r %s/%s", time.time() - time_request, e, host, url) response.close() return if send_to_browser: try: if not data: wfile.write('0\r\n\r\n') break length += len(data) wfile.write('%x\r\n' % len(data)) wfile.write(data) wfile.write('\r\n') except Exception as e: send_to_browser = False xlog.info( "direct_handler.handler send Transfer-Encoding t:%d e:%r %s/%s", time.time() - time_request, e, host, url) else: if not data: break