|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Origin that sends a 301 without consuming the request body, then checks |
| 3 | +whether a reused connection carries leftover (corrupted) data. Handles |
| 4 | +multiple connections so that a fixed ATS can open a fresh one for the |
| 5 | +second request.""" |
| 6 | + |
| 7 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 8 | +# or more contributor license agreements. See the NOTICE file |
| 9 | +# distributed with this work for additional information |
| 10 | +# regarding copyright ownership. The ASF licenses this file |
| 11 | +# to you under the Apache License, Version 2.0 (the |
| 12 | +# "License"); you may not use this file except in compliance |
| 13 | +# with the License. You may obtain a copy of the License at |
| 14 | +# |
| 15 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | +# |
| 17 | +# Unless required by applicable law or agreed to in writing, software |
| 18 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | +# See the License for the specific language governing permissions and |
| 21 | +# limitations under the License. |
| 22 | + |
| 23 | +import argparse |
| 24 | +import socket |
| 25 | +import sys |
| 26 | +import threading |
| 27 | +import time |
| 28 | + |
| 29 | +VALID_METHODS = {'GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH'} |
| 30 | + |
| 31 | + |
| 32 | +def read_until_headers_complete(conn: socket.socket) -> bytes: |
| 33 | + data = b'' |
| 34 | + while b'\r\n\r\n' not in data: |
| 35 | + chunk = conn.recv(4096) |
| 36 | + if not chunk: |
| 37 | + return data |
| 38 | + data += chunk |
| 39 | + return data |
| 40 | + |
| 41 | + |
| 42 | +def is_valid_http_request_line(line: str) -> bool: |
| 43 | + parts = line.strip().split(' ') |
| 44 | + if len(parts) < 3: |
| 45 | + return False |
| 46 | + return parts[0] in VALID_METHODS and parts[-1].startswith('HTTP/') |
| 47 | + |
| 48 | + |
| 49 | +def send_200(conn: socket.socket) -> None: |
| 50 | + ok_body = b'OK' |
| 51 | + conn.sendall( |
| 52 | + b'HTTP/1.1 200 OK\r\n' |
| 53 | + b'Content-Length: ' + str(len(ok_body)).encode() + b'\r\n' |
| 54 | + b'\r\n' + ok_body) |
| 55 | + |
| 56 | + |
| 57 | +def handle_connection(conn: socket.socket, args: argparse.Namespace, |
| 58 | + result: dict) -> None: |
| 59 | + try: |
| 60 | + data = read_until_headers_complete(conn) |
| 61 | + if not data: |
| 62 | + # Readiness probe. |
| 63 | + conn.close() |
| 64 | + return |
| 65 | + |
| 66 | + first_line = data.split(b'\r\n')[0].decode('utf-8', errors='replace') |
| 67 | + |
| 68 | + if first_line.startswith('POST'): |
| 69 | + # First request: send 301 without consuming the body. |
| 70 | + time.sleep(args.delay) |
| 71 | + |
| 72 | + body = b'Redirecting' |
| 73 | + response = ( |
| 74 | + b'HTTP/1.1 301 Moved Permanently\r\n' |
| 75 | + b'Location: http://example.com/\r\n' |
| 76 | + b'Connection: keep-alive\r\n' |
| 77 | + b'Content-Length: ' + str(len(body)).encode() + b'\r\n' |
| 78 | + b'\r\n' + body |
| 79 | + ) |
| 80 | + conn.sendall(response) |
| 81 | + |
| 82 | + # Wait for potential reuse on this connection. |
| 83 | + conn.settimeout(args.timeout) |
| 84 | + try: |
| 85 | + second_data = b'' |
| 86 | + while b'\r\n' not in second_data: |
| 87 | + chunk = conn.recv(4096) |
| 88 | + if not chunk: |
| 89 | + break |
| 90 | + second_data += chunk |
| 91 | + |
| 92 | + if second_data: |
| 93 | + second_line = second_data.split(b'\r\n')[0].decode('utf-8', errors='replace') |
| 94 | + if is_valid_http_request_line(second_line): |
| 95 | + send_200(conn) |
| 96 | + else: |
| 97 | + result['corrupted'] = True |
| 98 | + err_body = b'corrupted' |
| 99 | + conn.sendall( |
| 100 | + b'HTTP/1.1 400 Bad Request\r\n' |
| 101 | + b'Content-Length: ' + str(len(err_body)).encode() + b'\r\n' |
| 102 | + b'\r\n' + err_body) |
| 103 | + except socket.timeout: |
| 104 | + pass |
| 105 | + |
| 106 | + elif first_line.startswith('GET'): |
| 107 | + # Second request on a new connection (fix is working). |
| 108 | + result['new_connection'] = True |
| 109 | + send_200(conn) |
| 110 | + |
| 111 | + conn.close() |
| 112 | + except Exception: |
| 113 | + try: |
| 114 | + conn.close() |
| 115 | + except Exception: |
| 116 | + pass |
| 117 | + |
| 118 | + |
| 119 | +def main() -> int: |
| 120 | + parser = argparse.ArgumentParser() |
| 121 | + parser.add_argument('port', type=int) |
| 122 | + parser.add_argument('--delay', type=float, default=1.0) |
| 123 | + parser.add_argument('--timeout', type=float, default=5.0) |
| 124 | + args = parser.parse_args() |
| 125 | + |
| 126 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 127 | + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 128 | + sock.bind(('', args.port)) |
| 129 | + sock.listen(5) |
| 130 | + sock.settimeout(args.timeout + 5) |
| 131 | + |
| 132 | + result = {'corrupted': False, 'new_connection': False} |
| 133 | + threads = [] |
| 134 | + connections_handled = 0 |
| 135 | + |
| 136 | + try: |
| 137 | + while connections_handled < 10: |
| 138 | + try: |
| 139 | + conn, _ = sock.accept() |
| 140 | + t = threading.Thread(target=handle_connection, |
| 141 | + args=(conn, args, result)) |
| 142 | + t.daemon = True |
| 143 | + t.start() |
| 144 | + threads.append(t) |
| 145 | + connections_handled += 1 |
| 146 | + except socket.timeout: |
| 147 | + break |
| 148 | + except Exception: |
| 149 | + pass |
| 150 | + |
| 151 | + for t in threads: |
| 152 | + t.join(timeout=args.timeout + 2) |
| 153 | + |
| 154 | + sock.close() |
| 155 | + return 0 |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == '__main__': |
| 159 | + sys.exit(main()) |
0 commit comments