tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

patch.py (1564B)


      1 # flake8: noqa
      2 # This Source Code Form is subject to the terms of the Mozilla Public
      3 # License, v. 2.0. If a copy of the MPL was not distributed with this
      4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      5 
      6 # patch for https://bugzilla.mozilla.org/show_bug.cgi?id=1655869
      7 # see https://github.com/HDE/arsenic/issues/85
      8 from arsenic.connection import *
      9 
     10 
     11 @ensure_task
     12 async def request(
     13    self, *, url: str, method: str, data=None, timeout=None
     14 ) -> Tuple[int, Any]:
     15    if data is None:
     16        data = {}
     17    if method not in {"POST", "PUT"}:
     18        data = None
     19        headers = {}
     20    else:
     21        headers = {"Content-Type": "application/json"}
     22    body = json.dumps(data) if data is not None else None
     23    full_url = self.prefix + url
     24    log.info(
     25        "request", url=strip_auth(full_url), method=method, body=body, headers=headers
     26    )
     27 
     28    async with self.session.request(
     29        url=full_url, method=method, data=body, headers=headers, timeout=timeout
     30    ) as response:
     31        response_body = await response.read()
     32        try:
     33            data = json.loads(response_body)
     34        except JSONDecodeError as exc:
     35            log.error("json-decode", body=response_body)
     36            data = {"error": "!internal", "message": str(exc), "stacktrace": ""}
     37        wrap_screen(data)
     38        log.info(
     39            "response",
     40            url=strip_auth(full_url),
     41            method=method,
     42            body=body,
     43            response=response,
     44            data=data,
     45        )
     46        return response.status, data
     47 
     48 
     49 Connection.request = request