Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit e18730a

Browse files
committed
har entry data scope fix
1 parent 136de49 commit e18730a

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

browserup-proxy-core/src/main/resources/mitmproxy/har_dump.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def populate_har_entry_with_default_response(self, flow):
513513

514514
ctx.log.debug('Creating new har entry for request: {}'.format(full_url))
515515

516-
har_entry = flow.server_conn.currentHarEntry
516+
har_entry = flow.metadata['har_entry']
517517

518518
har_entry['pageref'] = self.get_current_page_ref()
519519
har_entry['startedDateTime'] = datetime.fromtimestamp(flow.request.timestamp_start, timezone.utc).isoformat()
@@ -555,7 +555,7 @@ def request(self, flow):
555555
if HarCaptureTypes.RESPONSE_CONTENT in self.har_capture_types:
556556
self.capture_request_content(flow)
557557

558-
har_entry = flow.server_conn.currentHarEntry
558+
har_entry = flow.metadata['har_entry']
559559
har_entry['request']['bodySize'] = \
560560
len(flow.request.raw_content) if flow.request.raw_content else 0
561561

@@ -589,7 +589,7 @@ def capture_request_content(self, flow):
589589
}
590590

591591
def response(self, flow):
592-
har_entry = flow.server_conn.currentHarEntry
592+
har_entry = flow.metadata['har_entry']
593593

594594
ctx.log.debug('Incoming response for request to url: {}'.format(flow.request.url))
595595

browserup-proxy-core/src/main/resources/mitmproxy/http_connect_capture.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ def generate_http_connect_timing(self):
5555

5656
# TCP Callbacks
5757

58-
def tcp_resolving_server_address_finished(self, server_conn):
59-
if not hasattr(server_conn, 'currentHarEntry'):
58+
def tcp_resolving_server_address_finished(self, flow):
59+
if 'har_entry' not in flow.metadata:
6060
return
61-
self.populate_dns_timings(server_conn)
61+
self.populate_dns_timings(flow)
6262
self.dns_resolution_finished_nanos = self.now_time_nanos()
6363

6464
if self.dns_resolution_started_nanos > 0:
@@ -67,7 +67,7 @@ def tcp_resolving_server_address_finished(self, server_conn):
6767
else:
6868
self.get_http_connect_timing()['dnsTimeNanos'] = 0
6969

70-
def tcp_resolving_server_address_started(self, server_conn):
70+
def tcp_resolving_server_address_started(self, flow):
7171
self.dns_resolution_started_nanos = int(round(self.now_time_nanos()))
7272
self.connection_started_nanos = int(round(self.now_time_nanos()))
7373
self.proxy_to_server_resolution_started()
@@ -89,10 +89,10 @@ def http_proxy_to_server_request_started(self, flow):
8989
def http_proxy_to_server_request_finished(self, flow):
9090
self.send_finished_nanos = self.now_time_nanos()
9191
if self.send_started_nanos > 0:
92-
self.get_har_entry(flow.server_conn)['timings'][
92+
self.get_har_entry(flow)['timings'][
9393
'send'] = self.send_finished_nanos - self.send_started_nanos
9494
else:
95-
self.get_har_entry(flow.server_conn)['timings']['send'] = 0
95+
self.get_har_entry(flow)['timings']['send'] = 0
9696

9797
def http_server_to_proxy_response_receiving(self, flow):
9898
self.response_receive_started_nanos = self.now_time_nanos()
@@ -136,23 +136,23 @@ def error(self, flow):
136136

137137
# Populate data
138138

139-
def populate_dns_timings(self, server_conn):
140-
har_entry = self.get_har_entry(server_conn)
139+
def populate_dns_timings(self, flow):
140+
har_entry = self.get_har_entry(flow)
141141
if self.dns_resolution_started_nanos > 0 and har_entry:
142142
time_now = self.now_time_nanos()
143143
dns_nanos = time_now - self.dns_resolution_started_nanos
144144
har_entry['timings']['dnsNanos'] = dns_nanos
145145

146146
def populate_timings_for_failed_connect(self, flow):
147-
har_entry = self.get_har_entry(flow.server_conn)
147+
har_entry = self.get_har_entry(flow)
148148
if self.connection_started_nanos > 0:
149149
connect_nanos = self.now_time_nanos() - self.connection_started_nanos
150150
har_entry['timings']['connectNanos'] = connect_nanos
151-
self.populate_dns_timings(flow.server_conn)
151+
self.populate_dns_timings(flow)
152152

153153
def populate_server_ip_address(self, flow, original_error):
154154
if flow.server_conn is not None and flow.server_conn.ip_address is not None:
155-
self.get_har_entry(flow.server_conn)['serverIPAddress'] = str(
155+
self.get_har_entry(flow)['serverIPAddress'] = str(
156156
flow.server_conn.ip_address[0])
157157

158158
def get_resource(self):
@@ -161,18 +161,18 @@ def get_resource(self):
161161
def proxy_to_server_resolution_failed(self, flow, req_host_port, original_error):
162162
msg = RESOLUTION_FAILED_ERROR_MESSAGE + req_host_port
163163
self.create_har_entry_for_failed_connect(flow, msg)
164-
self.populate_dns_timings(flow.server_conn)
164+
self.populate_dns_timings(flow)
165165
self.populate_server_ip_address(flow, original_error)
166166

167-
self.get_har_entry(flow.server_conn)['time'] = self.calculate_total_elapsed_time(flow)
167+
self.get_har_entry(flow)['time'] = self.calculate_total_elapsed_time(flow)
168168

169169
def proxy_to_server_connection_failed(self, flow, original_error):
170170
msg = CONNECTION_FAILED_ERROR_MESSAGE
171171
self.create_har_entry_for_failed_connect(flow, msg)
172172
self.populate_timings_for_failed_connect(flow)
173173
self.populate_server_ip_address(flow, original_error)
174174

175-
self.get_har_entry(flow.server_conn)['time'] = self.calculate_total_elapsed_time(flow)
175+
self.get_har_entry(flow)['time'] = self.calculate_total_elapsed_time(flow)
176176

177177
def server_to_proxy_response_timed_out(self, flow, req_host_port, original_error):
178178
msg = RESPONSE_TIMED_OUT_ERROR_MESSAGE
@@ -182,7 +182,7 @@ def server_to_proxy_response_timed_out(self, flow, req_host_port, original_error
182182

183183
current_time_nanos = self.now_time_nanos()
184184

185-
har_entry = self.get_har_entry(flow.server_conn)
185+
har_entry = self.get_har_entry(flow)
186186

187187
if self.send_started_nanos > 0 and self.send_finished_nanos == 0:
188188
har_entry['timings']['sendNanos'] = current_time_nanos - self.send_started_nanos
@@ -196,11 +196,11 @@ def server_to_proxy_response_timed_out(self, flow, req_host_port, original_error
196196
har_entry['time'] = self.calculate_total_elapsed_time(flow)
197197

198198
def create_har_entry_for_failed_connect(self, flow, msg):
199-
har_entry = self.get_har_entry(flow.server_conn)
199+
har_entry = self.get_har_entry(flow)
200200
har_entry['response']['_errorMessage'] = msg
201201

202202
def calculate_total_elapsed_time(self, flow):
203-
timings = self.get_har_entry(flow.server_conn)['timings']
203+
timings = self.get_har_entry(flow)['timings']
204204
result = (0 if timings.get('blockedNanos', -1) == -1 else timings['blockedNanos']) + \
205205
(0 if timings.get('dnsNanos', -1) == -1 else timings['dnsNanos']) + \
206206
(0 if timings.get('connectNanos', -1) == -1 else timings['connectNanos']) + \
@@ -209,8 +209,8 @@ def calculate_total_elapsed_time(self, flow):
209209
(0 if timings.get('receiveNanos', -1) == -1 else timings['receiveNanos'])
210210
return self.nano_to_ms(result)
211211

212-
def get_har_entry(self, server_conn):
213-
return server_conn.currentHarEntry
212+
def get_har_entry(self, flow):
213+
return flow.metadata['har_entry']
214214

215215
def get_http_connect_timing(self):
216216
if self.http_connect_timing is None:

browserup-proxy-core/src/main/resources/mitmproxy/init_flow.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ def get_resource(self):
4545
return InitFlowResource(self)
4646

4747
def http_connect(self, flow):
48-
if not hasattr(flow.server_conn, 'currentHarEntry'):
48+
if 'har_entry' not in flow.metadata:
4949
self.init_har_entry(flow)
5050

5151
def request(self, flow):
52-
if not hasattr(flow.server_conn, 'currentHarEntry'):
52+
if 'har_entry' not in flow.metadata:
5353
self.init_har_entry(flow)
5454

5555
def init_har_entry(self, flow):
56-
setattr(flow.server_conn, 'currentHarEntry', self.har_dump_addon.generate_har_entry())
57-
self.har_dump_addon.append_har_entry(flow.server_conn.currentHarEntry)
56+
flow.metadata['har_entry'] = self.har_dump_addon.generate_har_entry()
57+
self.har_dump_addon.append_har_entry(flow.metadata['har_entry'])
5858

5959
addons = [
6060
InitFlowAddOn()

browserup-proxy-core/src/test/groovy/com/browserup/bup/mitmproxy/NewHarTest.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import org.junit.Test
2222
import java.text.SimpleDateFormat
2323
import java.util.concurrent.TimeUnit
2424

25+
import static com.browserup.bup.mitmproxy.MitmProxyProcessManager.MitmProxyLoggingLevel.*
2526
import static com.github.tomakehurst.wiremock.client.WireMock.*
2627
import static org.hamcrest.Matchers.*
2728
import static org.junit.Assert.*
29+
import static org.mockito.ArgumentMatchers.eq
2830
import static org.mockito.Mockito.mock
2931
import static org.mockito.Mockito.when
3032

@@ -118,9 +120,9 @@ class NewHarTest extends MockServerTest {
118120
)
119121

120122
proxy = new MitmProxyServer()
121-
proxy.setHarCaptureTypes([CaptureType.RESPONSE_COOKIES] as Set)
122123
proxy.setTrustAllServers(true)
123124
proxy.start()
125+
proxy.setHarCaptureTypes([CaptureType.RESPONSE_COOKIES] as Set)
124126

125127
proxy.newHar()
126128

@@ -138,7 +140,7 @@ class NewHarTest extends MockServerTest {
138140
Thread.sleep(500)
139141
Har har = proxy.getHar()
140142

141-
assertThat("Expected to find entries in the HAR", har.getLog().getEntries(), not(empty()))
143+
assertThat("Expected one HAR entry", har.getLog().getEntries(), hasSize(1))
142144
assertThat("Expected to find two cookies in the HAR", har.getLog().getEntries().first().response.cookies, hasSize(2))
143145

144146
HarCookie maxAgeCookie = har.getLog().getEntries().first().response.cookies[0]

0 commit comments

Comments
 (0)