-
Notifications
You must be signed in to change notification settings - Fork 1
/
http2curl.py
69 lines (54 loc) · 2.21 KB
/
http2curl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import unittest
from urllib.parse import quote
def http_to_curl(http_request: str) -> str:
# Split the HTTP request into lines
lines = http_request.strip().split('\n')
# Extract the request method and URL
request_line = lines[0]
request_method, request_url, _ = request_line.split()
# Encode the URL
request_url = quote(request_url, safe="%/:=&?~#+!$,;'@()*[]")
# Initialize the cURL command
curl_command = f"curl -X {request_method}"
# Add the URL to the cURL command
curl_command += f" '{request_url}'"
# Iterate through the remaining lines in the HTTP request
for line in lines[1:]:
# Split the line into key/value pairs
key, value = map(str.strip, line.split(':', 1))
# Add the key/value pair to the cURL command as a header
curl_command += f" -H '{key}: {value}'"
return curl_command
class TestHTTPToCURL(unittest.TestCase):
def test_http_to_curl(self):
# Test a simple HTTP request with no special characters
http_request = """\
POST /api/users HTTP/1.1
Content-Type: application/json
User-Agent: curl/7.68.0
Accept: */*
Content-Length: 26
{"name": "Alice", "age": 30}"""
expected_curl_command = """\
curl -X POST -H 'Content-Type: application/json' -H 'User-Agent: curl/7.68.0' -H 'Accept: */*' -H 'Content-Length: 26' 'http://example.com/api/users'"""
self.assertEqual(http_to_curl(http_request), expected_curl_command)
# Test an HTTP request with special characters in the URL
http_request = """\
POST /api/users?id=' OR 1=1 HTTP/1.1
Content-Type: application/json
User-Agent: curl/7.68.0
Accept: */*
Content-Length: 26
{"name": "Alice", "age": 30}"""
expected_curl_command = """\
curl -X POST -H 'Content-Type: application/json' -H 'User-Agent: curl/7.68.0' -H 'Accept: */*' -H 'Content-Length: 26' 'http://example.com/api/users?id=%27%20OR%201%3D1'"""
self.assertEqual(http_to_curl(http_request), expected_curl_command)
# Test an HTTP request with a ' character in the User-Agent header
http_request = """\
POST /api/users HTTP/1.1
Content-Type: application/json
User-Agent: curl/7.68.0
Accept: */*
Content-Length: 26
{"name": "Alice", "age": 30}"""