From 18aa9707719071933de6ab6393de2890c237a14f Mon Sep 17 00:00:00 2001 From: Jess Bowers Date: Fri, 6 Oct 2023 12:46:22 -0400 Subject: [PATCH 1/3] fixes issue with header with no space Some headers fail when field_name:value (no space after colon) --- echo.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/echo.py b/echo.py index ffaad5a..858fd52 100755 --- a/echo.py +++ b/echo.py @@ -3,6 +3,7 @@ import datetime import socket import sys +import re # Block size is set to 8192 because thats usually the max header size BLOCK_SIZE = 8192 @@ -64,9 +65,11 @@ def build_request(first_chunk): lines = first_chunk.decode('utf-8', 'ignore').split('\r\n') h = {'request-line': lines[0]} i = 1 + header_kv = re.compile('(\w.*):(.*)') while i < len(lines[1:]) and lines[i] != '': - k, v = lines[i].split(': ') - h.update({k.lower(): v}) + m = header_kv.match(lines[i]) + k, v = m.group(1), m.group(2) + h.update({k.lower().strip(): v.strip()}) i += 1 r = { "header": h, From 635dee00b661651139cbd3ae42897c1c6c62c87e Mon Sep 17 00:00:00 2001 From: Jess Bowers Date: Fri, 6 Oct 2023 12:55:51 -0400 Subject: [PATCH 2/3] cleanup --- echo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/echo.py b/echo.py index 858fd52..742f968 100755 --- a/echo.py +++ b/echo.py @@ -65,11 +65,11 @@ def build_request(first_chunk): lines = first_chunk.decode('utf-8', 'ignore').split('\r\n') h = {'request-line': lines[0]} i = 1 - header_kv = re.compile('(\w.*):(.*)') + header_kv = re.compile('(\w\S+)\s?:\s?(\w.*)') while i < len(lines[1:]) and lines[i] != '': m = header_kv.match(lines[i]) k, v = m.group(1), m.group(2) - h.update({k.lower().strip(): v.strip()}) + h.update({k.lower(): v}) i += 1 r = { "header": h, From 563c05e414ef81c77c983fdd027ebd80675b224a Mon Sep 17 00:00:00 2001 From: Jess Bowers Date: Fri, 6 Oct 2023 13:01:33 -0400 Subject: [PATCH 3/3] fixes bug with Accept: */* --- echo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/echo.py b/echo.py index 742f968..98ee172 100755 --- a/echo.py +++ b/echo.py @@ -65,7 +65,7 @@ def build_request(first_chunk): lines = first_chunk.decode('utf-8', 'ignore').split('\r\n') h = {'request-line': lines[0]} i = 1 - header_kv = re.compile('(\w\S+)\s?:\s?(\w.*)') + header_kv = re.compile('(\w\S+)\s?:\s?(\S.*)') while i < len(lines[1:]) and lines[i] != '': m = header_kv.match(lines[i]) k, v = m.group(1), m.group(2)