diff --git a/warcprox/mitmproxy.py b/warcprox/mitmproxy.py index d6fe96d..74b6ca7 100644 --- a/warcprox/mitmproxy.py +++ b/warcprox/mitmproxy.py @@ -117,10 +117,10 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler): def do_COMMAND(self): if not self.is_connect: if self.command == 'PUTMETA': - self._prepare_custom_record(method=self.command, type_='metadata') + self._special_request(method=self.command, type_='metadata') return # if self.command == 'PUTRES': - # self._handle_custom_record(type_='resource') + # self._special_request(type_='resource') # return try: @@ -137,7 +137,7 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler): self._proxy_request() - def _handle_custom_record(self, method, type_): + def _special_request(self, method, type_): raise Exception('Not supported') def _proxy_request(self): diff --git a/warcprox/warcprox.py b/warcprox/warcprox.py index 7fcebbd..59115fb 100644 --- a/warcprox/warcprox.py +++ b/warcprox/warcprox.py @@ -216,30 +216,35 @@ class WarcProxyHandler(warcprox.mitmproxy.MitmProxyHandler): return recorded_url - def _handle_custom_record(self, method, type_): - self.url = self.path + def _special_request(self, method, type_): + try: + self.url = self.path - if 'Content-Length' in self.headers and 'Content-Type' in self.headers: - request_data = self.rfile.read(int(self.headers['Content-Length'])) + if (method == 'PUTMETA' and 'Content-Length' in self.headers + and 'Content-Type' in self.headers): + request_data = self.rfile.read(int(self.headers['Content-Length'])) - warcprox_meta = self.headers.get('Warcprox-Meta') + warcprox_meta = self.headers.get('Warcprox-Meta') - rec_custom = RecordedUrl(url=self.url, - request_data=request_data, - response_recorder=None, - remote_ip=b'', - warcprox_meta=warcprox_meta, - content_type=self.headers['Content-Type'].encode('latin1'), - custom_type=type_, - method=method, - status=204, size=len(request_data)) + rec_custom = RecordedUrl(url=self.url, + request_data=request_data, + response_recorder=None, + remote_ip=b'', + warcprox_meta=warcprox_meta, + content_type=self.headers['Content-Type'].encode('latin1'), + custom_type=type_, + method=method, + status=204, size=len(request_data)) - self.server.recorded_url_q.put(rec_custom) - self.send_response(204, 'OK') - else: - self.send_error(400, 'Bad request') + self.server.recorded_url_q.put(rec_custom) + self.send_response(204, 'OK') + else: + self.send_error(400, 'Bad request') - self.end_headers() + self.end_headers() + except: + self.logger.error("uncaught except in _special_request", exc_info=True) + raise def log_error(self, fmt, *args): # logging better handled elsewhere?