def extract(data, out): try: methods = re.findall('(GET|POST) (.*) HTTP', data)[0] url = methods[1] method = methods[0] headers = {} if method == 'GET': head = data.split('\r\n')[1:] for h in head: if ': ' in h[2:]: headers[h.split(': ')[0]] = h.split(': ')[1] host = headers['Host'].replace(' ', '') uri = "http://%s%s" % (host, url) if out: print "%s %s" % (method, uri) content_deal(headers, host, method, postdata='', uri=uri, packet=data) elif method == 'POST': body = data.split('\r\n\r\n')[1] head = data.split('\r\n\r\n')[0].split('\r\n')[1:] for h in head: if ': ' in h[2:]: headers[h.split(': ')[0]] = h.split(': ')[1] host = headers['Host'].replace(' ', '') uri = "http://%s%s" % (host, url) if out: print "%s %s" % (method, uri) content_deal(headers, host, method, postdata=body, uri=uri, packet=data) except Exception, e: if 'url' in dir() and "method" in dir(): print "%s %s" % (method, uri) print "Http Error: " + str(e)
def get(self): try: logger.debug('Handle %s request to %s', self.request.method, self.request.uri) def handle_response(response): if (response.error and not isinstance( response.error, tornado.httpclient.HTTPError)): self.set_status(500) self.write('Internal server error:\n' + str(response.error)) else: self.set_status(response.code, response.reason) self._headers = tornado.httputil.HTTPHeaders( ) # clear tornado default header for header, v in response.headers.get_all(): if header not in ('Content-Length', 'Transfer-Encoding', 'Content-Encoding', 'Connection'): self.add_header( header, v ) # some header appear multiple times, eg 'Set-Cookie' if response.body: self.set_header('Content-Length', len(response.body)) self.write(response.body) #print 11 self.finish() body = self.request.body if not body: body = "" try: if 'Proxy-Connection' in self.request.headers: del self.request.headers['Proxy-Connection'] fetch_request(self.request.uri, handle_response, method=self.request.method, body=body, headers=self.request.headers, follow_redirects=False, allow_nonstandard_methods=True) uri = self.request.uri headers = dict(self.request.headers) data = self.extract_request(uri, headers, self.request.method, body) host = urlparse(uri).netloc content_deal(headers, host, self.request.method, postdata=body, uri=uri, packet=data) except tornado.httpclient.HTTPError as e: if hasattr(e, 'response') and e.response: handle_response(e.response) else: self.set_status(500) self.write('Internal server error:\n' + str(e)) self.finish() except: return
def get_res(data, connstream, https): try: headers = {} post = '' if data[0:7] == 'CONNECT': connstream.sendall("HTTP/1.1 200 Connection established\r\n\r\n") https_things(connstream) return if not re.search('(GET|POST) (.*) HTTP', data): return methods = re.findall('(GET|POST) (.*) HTTP', data)[0] url = methods[1] method = methods[0] if method == 'GET': head = data.split('\r\n')[1:] for h in head: if ': ' in h[2:]: headers[h.split(': ')[0]] = h.split(': ')[1] host = headers['Host'].replace(' ', '') if not https: uri = url else: uri = "https://%s%s" % (host, url) print uri content_deal(headers, host, method, postdata='', uri=uri, packet=data) if 'Host' in headers.keys(): del headers['Host'] res = requests.get(uri, headers=headers, verify=False) response = get_str(res) connstream.sendall(response) connstream.close() return elif method == 'POST': body = data.split('\r\n\r\n')[1] head = data.split('\r\n\r\n')[0].split('\r\n')[1:] for h in head: if ': ' in h[2:]: headers[h.split(': ')[0]] = h.split(': ')[1] host = headers['Host'].replace(' ', '') if not https: uri = url else: uri = "https://%s%s" % (host, url) print uri content_deal(headers, host, method, postdata=body, uri=uri, packet=data) if 'Host' in headers.keys(): del headers['Host'] res = requests.post(uri, headers=headers, data=body, verify=False) response = get_str(res) connstream.sendall(response) connstream.close() return except Exception, e: if 'url' in dir(): print url print "Http Error: " + str(e) try: err = "HTTP/1.1 500 Internal Server Error\r\n" err += "Content-Length-Type: text/html;\r\n" err += "Content-Length: 17\r\n\r\n" err += "HTTP Error" connstream.sendall() connstream.close() except Exception as e: pass