1+ from flask import Flask , request , jsonify
2+ import gzip
3+ import zlib
4+ from datetime import datetime
5+
6+ app = Flask (__name__ )
7+
8+ def log_request_details ():
9+ """Log all request details in a formatted way"""
10+ timestamp = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
11+
12+ print ("=" * 80 )
13+ print (f"[{ timestamp } ] NEW REQUEST" )
14+ print ("=" * 80 )
15+
16+ # Method and URL
17+ print (f"Method: { request .method } " )
18+ print (f"URL: { request .url } " )
19+ print (f"Path: { request .path } " )
20+ print (f"Query String: { request .query_string .decode ('utf-8' )} " )
21+
22+ # Headers
23+ print ("\n --- HEADERS ---" )
24+ for header_name , header_value in request .headers :
25+ print (f"{ header_name } : { header_value } " )
26+
27+ # Query Parameters
28+ if request .args :
29+ print ("\n --- QUERY PARAMETERS ---" )
30+ for key , value in request .args .items ():
31+ print (f"{ key } : { value } " )
32+
33+ # Form Data
34+ if request .form :
35+ print ("\n --- FORM DATA ---" )
36+ for key , value in request .form .items ():
37+ print (f"{ key } : { value } " )
38+
39+ # Files
40+ if request .files :
41+ print ("\n --- FILES ---" )
42+ for key , file in request .files .items ():
43+ print (f"{ key } : { file .filename } (Content-Type: { file .content_type } )" )
44+
45+ # Raw Body with decompression support
46+ try :
47+ raw_body = request .get_data ()
48+ content_encoding = request .headers .get ('Content-Encoding' , '' ).lower ()
49+
50+ print ("\n --- REQUEST BODY ---" )
51+
52+ if not raw_body :
53+ print ("(empty)" )
54+ else :
55+ # Handle compressed content
56+ decompressed_body = None
57+
58+ if content_encoding == 'gzip' :
59+ try :
60+ decompressed_body = gzip .decompress (raw_body ).decode ('utf-8' )
61+ print ("(Content was gzip-compressed, showing decompressed version)" )
62+ except Exception as e :
63+ print (f"Failed to decompress gzip content: { e } " )
64+ elif content_encoding == 'deflate' :
65+ try :
66+ decompressed_body = zlib .decompress (raw_body ).decode ('utf-8' )
67+ print ("(Content was deflate-compressed, showing decompressed version)" )
68+ except Exception as e :
69+ print (f"Failed to decompress deflate content: { e } " )
70+ elif content_encoding in ['br' , 'brotli' ]:
71+ print ("(Content is brotli-compressed - brotli decompression not available)" )
72+ print ("Raw compressed data (first 200 bytes):" )
73+ print (repr (raw_body [:200 ]))
74+ elif content_encoding == 'zstd' :
75+ print ("(Content is zstd-compressed - zstd decompression not available)" )
76+ print ("Raw compressed data (first 200 bytes):" )
77+ print (repr (raw_body [:200 ]))
78+ else :
79+ # No compression or unknown compression
80+ try :
81+ decompressed_body = raw_body .decode ('utf-8' )
82+ except UnicodeDecodeError :
83+ print ("(Binary content - showing first 200 bytes as hex)" )
84+ print (raw_body [:200 ].hex ())
85+
86+ # Display the decompressed content
87+ if decompressed_body :
88+ print (decompressed_body )
89+
90+ # Also show raw length info
91+ print (f"\n Raw body length: { len (raw_body )} bytes" )
92+ print (f"Decompressed length: { len (decompressed_body )} bytes" )
93+
94+ except Exception as e :
95+ print (f"\n --- REQUEST BODY ---" )
96+ print (f"Error reading body: { e } " )
97+
98+ print ("=" * 80 )
99+ print ()
100+
101+ # Catch all routes for any HTTP method
102+ @app .route ('/' , defaults = {'path' : '' }, methods = ['GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' , 'HEAD' , 'OPTIONS' ])
103+ @app .route ('/<path:path>' , methods = ['GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' , 'HEAD' , 'OPTIONS' ])
104+ def debug_endpoint (path ):
105+ log_request_details ()
106+
107+ # Return a simple response
108+ response_data = {
109+ "message" : "Request received and logged" ,
110+ "method" : request .method ,
111+ "path" : f"/{ path } " if path else "/" ,
112+ "timestamp" : datetime .now ().isoformat ()
113+ }
114+
115+ return jsonify (response_data ), 200
116+
117+ if __name__ == '__main__' :
118+ print ("Starting HTTP Debug Server..." )
119+ print ("All incoming requests will be logged to console" )
120+ print ("Server listening on port 8080" )
121+ print ("=" * 80 )
122+ app .run (host = '0.0.0.0' , port = 8080 , debug = False )
0 commit comments