Skip to content

Commit a33536d

Browse files
committed
python: Support http/1.1
1 parent 55d3d2a commit a33536d

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

.github/workflows/python.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ jobs:
1818
uses: actions/setup-python@v2
1919
with:
2020
python-version: 3.9
21+
- name: Install requirements
22+
run: pip install -r requirements.txt
2123
- name: Install Black
22-
run: pip install black
24+
run: pip install black pytest
2325
- name: Run black --check .
2426
run: black --check .
27+
- name: Test with pytest
28+
run: pytest -v

python/http.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ def _get_headers_and_body(sock, host, port, path):
4242
sock.connect((host, port))
4343

4444
# 5. Send request
45-
sock.send(f"GET {path} HTTP/1.0\r\n".encode())
45+
sock.send(f"GET {path} HTTP/1.1\r\n".encode())
4646
sock.send(f"Host: {host}\r\n".encode())
47-
sock.send("Accept-Encoding: br,gzip,deflate\r\n".encode())
47+
sock.send(f"Connection: close\r\n".encode())
48+
sock.send(f"User-Agent: homemade-browser\r\n".encode())
49+
sock.send(f"Accept-Encoding: br,gzip,deflate\r\n".encode())
4850
sock.send("\r\n".encode())
4951

5052
# 6. Receive response
@@ -63,7 +65,6 @@ def _get_headers_and_body(sock, host, port, path):
6365
line = response.readline().decode()
6466
if line == "\r\n":
6567
break
66-
6768
header, value = line.split(":", 1)
6869
headers[header.lower()] = value.strip()
6970

python/tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
3+
sys.path.append("../")

python/tests/test_http.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
from http import request
4+
5+
6+
class RequestTest(unittest.TestCase):
7+
def test_http_request(self):
8+
headers, body = request("http://example.com/")
9+
self.assertGreater(len(body), 0)
10+
self.assertIn("content-type", headers)
11+
12+
def test_https_request(self):
13+
headers, body = request("https://www.facebook.com/")
14+
self.assertGreater(len(body), 0)
15+
self.assertIn("content-type", headers)
16+
17+
def test_data_request(self):
18+
headers, body = request("data:text/html,Hello world")
19+
self.assertEqual(body, "Hello world")
20+
self.assertEqual(headers["content-type"], "text/html")
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()

0 commit comments

Comments
 (0)