text stringlengths 0 93.6k |
|---|
def print_info(self, req, req_body, res, res_body): |
def parse_qsl(s): |
return '\n'.join("%-20s %s" % (k, v) for k, v in urlparse.parse_qsl(s, keep_blank_values=True)) |
req_header_text = "%s %s %s\n%s" % (req.command, req.path, req.request_version, req.headers) |
res_header_text = "%s %d %s\n%s" % (res.response_version, res.status, res.reason, res.headers) |
print with_color(33, req_header_text) |
u = urlparse.urlsplit(req.path) |
if u.query: |
query_text = parse_qsl(u.query) |
print with_color(32, "==== QUERY PARAMETERS ====\n%s\n" % query_text) |
cookie = req.headers.get('Cookie', '') |
if cookie: |
cookie = parse_qsl(re.sub(r';\s*', '&', cookie)) |
print with_color(32, "==== COOKIE ====\n%s\n" % cookie) |
auth = req.headers.get('Authorization', '') |
if auth.lower().startswith('basic'): |
token = auth.split()[1].decode('base64') |
print with_color(31, "==== BASIC AUTH ====\n%s\n" % token) |
if req_body is not None: |
req_body_text = None |
content_type = req.headers.get('Content-Type', '') |
if content_type.startswith('application/x-www-form-urlencoded'): |
req_body_text = parse_qsl(req_body) |
elif content_type.startswith('application/json'): |
try: |
json_obj = json.loads(req_body) |
json_str = json.dumps(json_obj, indent=2) |
if json_str.count('\n') < 50: |
req_body_text = json_str |
else: |
lines = json_str.splitlines() |
req_body_text = "%s\n(%d lines)" % ('\n'.join(lines[:50]), len(lines)) |
except ValueError: |
req_body_text = req_body |
elif len(req_body) < 1024: |
req_body_text = req_body |
if req_body_text: |
print with_color(32, "==== REQUEST BODY ====\n%s\n" % req_body_text) |
print with_color(36, res_header_text) |
cookies = res.headers.getheaders('Set-Cookie') |
if cookies: |
cookies = '\n'.join(cookies) |
print with_color(31, "==== SET-COOKIE ====\n%s\n" % cookies) |
if res_body is not None: |
res_body_text = None |
content_type = res.headers.get('Content-Type', '') |
if content_type.startswith('application/json'): |
try: |
json_obj = json.loads(res_body) |
json_str = json.dumps(json_obj, indent=2) |
if json_str.count('\n') < 50: |
res_body_text = json_str |
else: |
lines = json_str.splitlines() |
res_body_text = "%s\n(%d lines)" % ('\n'.join(lines[:50]), len(lines)) |
except ValueError: |
res_body_text = res_body |
elif content_type.startswith('text/html'): |
m = re.search(r'<title[^>]*>\s*([^<]+?)\s*</title>', res_body, re.I) |
if m: |
h = HTMLParser() |
print with_color(32, "==== HTML TITLE ====\n%s\n" % h.unescape(m.group(1).decode('utf-8'))) |
elif content_type.startswith('text/') and len(res_body) < 1024: |
res_body_text = res_body |
if res_body_text: |
print with_color(32, "==== RESPONSE BODY ====\n%s\n" % res_body_text) |
def request_handler(self, req, req_body): |
pass |
def response_handler(self, req, req_body, res, res_body): |
pass |
def save_handler(self, req, req_body, res, res_body): |
self.print_info(req, req_body, res, res_body) |
def test(HandlerClass=ProxyRequestHandler, ServerClass=ThreadingHTTPServer, protocol="HTTP/1.1"): |
if sys.argv[1:]: |
port = int(sys.argv[1]) |
else: |
port = 8080 |
server_address = ('::1', port) |
HandlerClass.protocol_version = protocol |
httpd = ServerClass(server_address, HandlerClass) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.