Copilot commented on code in PR #13820:
URL: https://github.com/apache/trafficserver/pull/13820#discussion_r4248201766


##########
tests/gold_tests/query/query_retry_origin.py:
##########
@@ -0,0 +1,76 @@
+'''
+An origin that closes the first connection after reading the request, without
+responding, and answers every later request with a 200.
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+import argparse
+import socket
+import sys
+
+
+def read_request(conn: socket.socket) -> tuple[str, bytes]:
+    data = b''
+    while b'\r\n\r\n' not in data:
+        chunk = conn.recv(4096)
+        if not chunk:
+            return '', b''
+        data += chunk
+    head, _, body = data.partition(b'\r\n\r\n')
+    lines = head.decode('latin-1').split('\r\n')
+    method = lines[0].split(' ')[0]
+    length = 0
+    for line in lines[1:]:
+        name, _, value = line.partition(':')
+        if name.strip().lower() == 'content-length':
+            length = int(value.strip())
+    while len(body) < length:
+        chunk = conn.recv(4096)
+        if not chunk:
+            break
+        body += chunk
+    return method, body[:length]
+
+
+def main() -> int:
+    parser = argparse.ArgumentParser()
+    parser.add_argument('port', type=int)
+    args = parser.parse_args()
+
+    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener:
+        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        listener.bind(('127.0.0.1', args.port))
+        listener.listen()
+        attempt = 0
+        while True:
+            conn, _ = listener.accept()
+            with conn:
+                method, body = read_request(conn)
+                if not method:
+                    continue
+                attempt += 1
+                print(f'request attempt={attempt} method={method} 
body={body.decode()}', flush=True)

Review Comment:
   `body.decode()` uses the default UTF-8 codec and can raise 
`UnicodeDecodeError` if the request body contains non-UTF8 bytes, which would 
crash the test origin process. Consider decoding with an explicit codec and 
error handling (e.g., `latin-1` or `utf-8` with `errors="replace"`), or 
printing a safe representation (e.g., `repr(body)`), so the helper remains 
robust even if the request payload changes.



##########
tests/gold_tests/query/query_retry_origin.py:
##########
@@ -0,0 +1,76 @@
+'''
+An origin that closes the first connection after reading the request, without
+responding, and answers every later request with a 200.
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+import argparse
+import socket
+import sys
+
+
+def read_request(conn: socket.socket) -> tuple[str, bytes]:
+    data = b''
+    while b'\r\n\r\n' not in data:
+        chunk = conn.recv(4096)
+        if not chunk:
+            return '', b''
+        data += chunk

Review Comment:
   Building `data` via repeated `bytes` concatenation (`data += chunk`) is 
O(n²) due to repeated reallocations/copies as the header grows. Using a 
`bytearray` (or accumulating chunks into a list and `b''.join(...)`) avoids 
quadratic behavior and makes this helper more resilient if headers are large.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to