|
| 1 | +from collections import deque |
| 2 | +from datetime import datetime |
| 3 | +from flask import Flask, request, jsonify |
| 4 | +from os import environ |
| 5 | + |
| 6 | +app = Flask('HTTP Request Catcher') |
| 7 | + |
| 8 | +# Maximum number of requests to store in history (configurable via environment variable) |
| 9 | +MAX_REQUEST_HISTORY = int(environ.get('MAX_REQUEST_HISTORY', 1000)) |
| 10 | + |
| 11 | +last_request = None |
| 12 | +all_requests = deque(maxlen=MAX_REQUEST_HISTORY) |
| 13 | + |
| 14 | + |
| 15 | +@app.route('/__last_request__', methods=['GET']) |
| 16 | +def get_last_request(): |
| 17 | + return jsonify(last_request), 200 |
| 18 | + |
| 19 | + |
| 20 | +@app.route('/__all_requests__', methods=['GET']) |
| 21 | +def get_all_requests(): |
| 22 | + return jsonify(list(all_requests)), 200 |
| 23 | + |
| 24 | + |
| 25 | +@app.route('/__find_request__', methods=['GET']) |
| 26 | +def find_request(): |
| 27 | + """ |
| 28 | + Find requests matching header or body values. |
| 29 | + Query parameters: |
| 30 | + - header_<name>=<value>: Match requests with specific header value |
| 31 | + - body=<value>: Match requests containing this value in body |
| 32 | + - method=<value>: Match requests with specific HTTP method |
| 33 | + - url=<value>: Match requests with URL containing this value |
| 34 | + """ |
| 35 | + matches = [] |
| 36 | + |
| 37 | + for req in all_requests: |
| 38 | + match = True |
| 39 | + |
| 40 | + for key, value in request.args.items(): |
| 41 | + if key.startswith('header_'): |
| 42 | + header_name = key[7:] # Remove 'header_' prefix |
| 43 | + req_headers = {k.lower(): v for k, v in req['headers'].items()} |
| 44 | + if req_headers.get(header_name.lower()) != value: |
| 45 | + match = False |
| 46 | + break |
| 47 | + elif key == 'body': |
| 48 | + if value not in req.get('data', ''): |
| 49 | + match = False |
| 50 | + break |
| 51 | + elif key == 'method': |
| 52 | + if req.get('method', '').upper() != value.upper(): |
| 53 | + match = False |
| 54 | + break |
| 55 | + elif key == 'url': |
| 56 | + if value not in req.get('url', ''): |
| 57 | + match = False |
| 58 | + break |
| 59 | + |
| 60 | + if match: |
| 61 | + matches.append(req) |
| 62 | + |
| 63 | + return jsonify(matches), 200 |
| 64 | + |
| 65 | + |
| 66 | +@app.route('/__clear__', methods=['POST', 'DELETE']) |
| 67 | +def clear_requests(): |
| 68 | + global last_request |
| 69 | + last_request = None |
| 70 | + all_requests.clear() |
| 71 | + return '', 204 |
| 72 | + |
| 73 | + |
| 74 | +@app.route('/', defaults={'path': ''}, methods=['PUT', 'POST', 'GET', 'HEAD', 'DELETE', 'PATCH', 'OPTIONS']) |
| 75 | +@app.route('/<path:path>', methods=['PUT', 'POST', 'GET', 'HEAD', 'DELETE', 'PATCH', 'OPTIONS']) |
| 76 | +def catch(path): |
| 77 | + global last_request |
| 78 | + |
| 79 | + last_request = { |
| 80 | + 'method': request.method, |
| 81 | + 'data': request.data.decode('utf-8'), |
| 82 | + 'headers': dict(request.headers), |
| 83 | + 'url': request.url, |
| 84 | + 'time': datetime.now().isoformat(), |
| 85 | + } |
| 86 | + all_requests.append(last_request) |
| 87 | + |
| 88 | + return '', 200 |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + app.run(host='0.0.0.0', port=5000) |
0 commit comments