1
1
import html
2
- from typing import Optional
3
-
2
+ import time
3
+ from typing import Optional , Tuple
4
4
from mitmproxy import connections
5
5
from mitmproxy import flow
6
6
from mitmproxy import version
7
7
from mitmproxy .net import http
8
8
9
-
10
- class HTTPRequest (http .Request ):
11
- """
12
- A mitmproxy HTTP request.
13
- """
14
-
15
- # This is a very thin wrapper on top of :py:class:`mitmproxy.net.http.Request` and
16
- # may be removed in the future.
17
-
18
- def __init__ (
19
- self ,
20
- first_line_format ,
21
- method ,
22
- scheme ,
23
- host ,
24
- port ,
25
- path ,
26
- http_version ,
27
- headers ,
28
- content ,
29
- trailers = None ,
30
- timestamp_start = None ,
31
- timestamp_end = None ,
32
- is_replay = False ,
33
- ):
34
- http .Request .__init__ (
35
- self ,
36
- first_line_format ,
37
- method ,
38
- scheme ,
39
- host ,
40
- port ,
41
- path ,
42
- http_version ,
43
- headers ,
44
- content ,
45
- trailers ,
46
- timestamp_start ,
47
- timestamp_end ,
48
- )
49
- # Is this request replayed?
50
- self .is_replay = is_replay
51
- self .stream = None
52
-
53
- def get_state (self ):
54
- state = super ().get_state ()
55
- state ["is_replay" ] = self .is_replay
56
- return state
57
-
58
- def set_state (self , state ):
59
- state = state .copy ()
60
- self .is_replay = state .pop ("is_replay" )
61
- super ().set_state (state )
62
-
63
- @classmethod
64
- def wrap (self , request ):
65
- """
66
- Wraps an existing :py:class:`mitmproxy.net.http.Request`.
67
- """
68
- req = HTTPRequest (
69
- first_line_format = request .data .first_line_format ,
70
- method = request .data .method ,
71
- scheme = request .data .scheme ,
72
- host = request .data .host ,
73
- port = request .data .port ,
74
- path = request .data .path ,
75
- http_version = request .data .http_version ,
76
- headers = request .data .headers ,
77
- content = request .data .content ,
78
- trailers = request .data .trailers ,
79
- timestamp_start = request .data .timestamp_start ,
80
- timestamp_end = request .data .timestamp_end ,
81
- )
82
- return req
83
-
84
- def __hash__ (self ):
85
- return id (self )
86
-
87
-
88
- class HTTPResponse (http .Response ):
89
- """
90
- A mitmproxy HTTP response.
91
- """
92
-
93
- # This is a very thin wrapper on top of :py:class:`mitmproxy.net.http.Response` and
94
- # may be removed in the future.
95
-
96
- def __init__ (
97
- self ,
98
- http_version ,
99
- status_code ,
100
- reason ,
101
- headers ,
102
- content ,
103
- trailers = None ,
104
- timestamp_start = None ,
105
- timestamp_end = None ,
106
- is_replay = False
107
- ):
108
- http .Response .__init__ (
109
- self ,
110
- http_version ,
111
- status_code ,
112
- reason ,
113
- headers ,
114
- content ,
115
- trailers ,
116
- timestamp_start = timestamp_start ,
117
- timestamp_end = timestamp_end ,
118
- )
119
-
120
- # Is this request replayed?
121
- self .is_replay = is_replay
122
- self .stream = None
123
-
124
- @classmethod
125
- def wrap (self , response ):
126
- """
127
- Wraps an existing :py:class:`mitmproxy.net.http.Response`.
128
- """
129
- resp = HTTPResponse (
130
- http_version = response .data .http_version ,
131
- status_code = response .data .status_code ,
132
- reason = response .data .reason ,
133
- headers = response .data .headers ,
134
- content = response .data .content ,
135
- trailers = response .data .trailers ,
136
- timestamp_start = response .data .timestamp_start ,
137
- timestamp_end = response .data .timestamp_end ,
138
- )
139
- return resp
9
+ HTTPRequest = http .Request
10
+ HTTPResponse = http .Response
140
11
141
12
142
13
class HTTPFlow (flow .Flow ):
@@ -197,8 +68,7 @@ def make_error_response(
197
68
message : str = "" ,
198
69
headers : Optional [http .Headers ] = None ,
199
70
) -> HTTPResponse :
200
- reason = http .status_codes .RESPONSES .get (status_code , "Unknown" )
201
- body = """
71
+ body : bytes = """
202
72
<html>
203
73
<head>
204
74
<title>{status_code} {reason}</title>
@@ -210,7 +80,7 @@ def make_error_response(
210
80
</html>
211
81
""" .strip ().format (
212
82
status_code = status_code ,
213
- reason = reason ,
83
+ reason = http . status_codes . RESPONSES . get ( status_code , "Unknown" ) ,
214
84
message = html .escape (message ),
215
85
).encode ("utf8" , "replace" )
216
86
@@ -222,19 +92,23 @@ def make_error_response(
222
92
Content_Type = "text/html"
223
93
)
224
94
225
- return HTTPResponse (
226
- b"HTTP/1.1" ,
227
- status_code ,
228
- reason ,
229
- headers ,
230
- body ,
231
- )
95
+ return HTTPResponse .make (status_code , body , headers )
232
96
233
97
234
- def make_connect_request (address ) :
98
+ def make_connect_request (address : Tuple [ str , int ]) -> HTTPRequest :
235
99
return HTTPRequest (
236
- "authority" , b"CONNECT" , None , address [0 ], address [1 ], None , b"HTTP/1.1" ,
237
- http .Headers (), b""
100
+ host = address [0 ],
101
+ port = address [1 ],
102
+ method = b"CONNECT" ,
103
+ scheme = b"" ,
104
+ authority = f"{ address [0 ]} :{ address [1 ]} " .encode (),
105
+ path = b"" ,
106
+ http_version = b"HTTP/1.1" ,
107
+ headers = http .Headers (),
108
+ content = b"" ,
109
+ trailers = None ,
110
+ timestamp_start = time .time (),
111
+ timestamp_end = time .time (),
238
112
)
239
113
240
114
@@ -247,9 +121,11 @@ def make_connect_response(http_version):
247
121
b"Connection established" ,
248
122
http .Headers (),
249
123
b"" ,
124
+ None ,
125
+ time .time (),
126
+ time .time (),
250
127
)
251
128
252
129
253
- expect_continue_response = HTTPResponse (
254
- b"HTTP/1.1" , 100 , b"Continue" , http .Headers (), b""
255
- )
130
+ def make_expect_continue_response ():
131
+ return HTTPResponse .make (100 )
0 commit comments