Ejemplo n.º 1
0
 def start_response(status, headers, exc_info=None):
     if exc_info:
         if state["headers_sent"]:
             six.reraise(*exc_info)
     elif state["status"]:
         raise AssertionError('Response already started')
     state["status"] = status
     state["headers"] = http.Headers([[strutils.always_bytes(k), strutils.always_bytes(v)] for k, v in headers])
     if exc_info:
         self.error_page(soc, state["headers_sent"], traceback.format_tb(exc_info[2]))
         state["headers_sent"] = True
Ejemplo n.º 2
0
 def start_response(status, headers, exc_info=None):
     if exc_info:
         if state["headers_sent"]:
             raise exc_info[1]
     elif state["status"]:
         raise AssertionError('Response already started')
     state["status"] = status
     state["headers"] = http.Headers(
         [[strutils.always_bytes(k),
           strutils.always_bytes(v)] for k, v in headers])
     if exc_info:
         self.error_page(soc, state["headers_sent"],
                         traceback.format_tb(exc_info[2]))
         state["headers_sent"] = True
Ejemplo n.º 3
0
def parse_upstream_auth(auth):
    pattern = re.compile(".+:")
    if pattern.search(auth) is None:
        raise exceptions.OptionsError(
            "Invalid upstream auth specification: %s" % auth
        )
    return b"Basic" + b" " + base64.b64encode(strutils.always_bytes(auth))
Ejemplo n.º 4
0
 def __init__(
         self,
         headers,
         flows,
         exit,
         nopop,
         ignore_params,
         ignore_content,
         ignore_payload_params,
         ignore_host):
     """
         headers: Case-insensitive list of request headers that should be
         included in request-response matching.
     """
     self.headers = headers
     self.exit = exit
     self.nopop = nopop
     self.ignore_params = ignore_params
     self.ignore_content = ignore_content
     self.ignore_payload_params = [strutils.always_bytes(x) for x in (ignore_payload_params or ())]
     self.ignore_host = ignore_host
     self.fmap = {}
     for i in flows:
         if i.response:
             l = self.fmap.setdefault(self._hash(i), [])
             l.append(i)
Ejemplo n.º 5
0
def parse_upstream_auth(auth):
    pattern = re.compile(".+:")
    if pattern.search(auth) is None:
        raise exceptions.OptionsError(
            "Invalid upstream auth specification: %s" % auth
        )
    return b"Basic" + b" " + base64.b64encode(strutils.always_bytes(auth))
Ejemplo n.º 6
0
def test_always_bytes():
    assert strutils.always_bytes(bytes(bytearray(range(256)))) == bytes(bytearray(range(256)))
    assert strutils.always_bytes("foo") == b"foo"
    with tutils.raises(ValueError):
        strutils.always_bytes(u"\u2605", "ascii")
    with tutils.raises(TypeError):
        strutils.always_bytes(42, "ascii")
Ejemplo n.º 7
0
def test_always_bytes():
    assert strutils.always_bytes(bytes(bytearray(range(256)))) == bytes(
        bytearray(range(256)))
    assert strutils.always_bytes("foo") == b"foo"
    with tutils.raises(ValueError):
        strutils.always_bytes(u"\u2605", "ascii")
    with tutils.raises(TypeError):
        strutils.always_bytes(42, "ascii")
Ejemplo n.º 8
0
    def establish_ssl(self, clientcerts, sni, **kwargs):
        clientcert = None
        if clientcerts:
            if os.path.isfile(clientcerts):
                clientcert = clientcerts
            else:
                path = os.path.join(
                    clientcerts,
                    self.address.host.encode("idna").decode()) + ".pem"
                if os.path.exists(path):
                    clientcert = path

        self.convert_to_ssl(cert=clientcert, sni=strutils.always_bytes(sni), **kwargs)
        self.sni = sni
        self.timestamp_ssl_setup = time.time()
Ejemplo n.º 9
0
    def assemble_response(self, response):
        assert isinstance(response, netlib.http.response.Response)

        headers = response.headers.copy()

        if ':status' not in headers:
            headers.insert(0, b':status', strutils.always_bytes(response.status_code))

        if hasattr(response, 'stream_id'):
            stream_id = response.stream_id
        else:
            stream_id = self._next_stream_id()

        return list(itertools.chain(
            self._create_headers(headers, stream_id, end_stream=(response.body is None or len(response.body) == 0)),
            self._create_body(response.body, stream_id),
        ))
Ejemplo n.º 10
0
    def _hash(self, flow):
        """
            Calculates a loose hash of the flow request.
        """
        r = flow.request

        _, _, path, _, query, _ = urllib.parse.urlparse(r.url)
        queriesArray = urllib.parse.parse_qsl(query, keep_blank_values=True)

        key = [str(r.port), str(r.scheme), str(r.method), str(path)]
        if not self.options.server_replay_ignore_content:
            form_contents = r.urlencoded_form or r.multipart_form
            if self.options.server_replay_ignore_payload_params and form_contents:
                params = [
                    strutils.always_bytes(i)
                    for i in self.options.server_replay_ignore_payload_params
                ]
                for p in form_contents.items(multi=True):
                    if p[0] not in params:
                        key.append(p)
            else:
                key.append(str(r.raw_content))

        if not self.options.server_replay_ignore_host:
            key.append(r.host)

        filtered = []
        ignore_params = self.options.server_replay_ignore_params or []
        for p in queriesArray:
            if p[0] not in ignore_params:
                filtered.append(p)
        for p in filtered:
            key.append(p[0])
            key.append(p[1])

        if self.options.server_replay_use_headers:
            headers = []
            for i in self.options.server_replay_use_headers:
                v = r.headers.get(i)
                headers.append((i, v))
            key.append(headers)
        return hashlib.sha256(
            repr(key).encode("utf8", "surrogateescape")
        ).digest()
Ejemplo n.º 11
0
    def _hash(self, flow):
        """
            Calculates a loose hash of the flow request.
        """
        r = flow.request

        _, _, path, _, query, _ = urllib.parse.urlparse(r.url)
        queriesArray = urllib.parse.parse_qsl(query, keep_blank_values=True)

        key = [str(r.port), str(r.scheme), str(r.method), str(path)]
        if not self.options.server_replay_ignore_content:
            form_contents = r.urlencoded_form or r.multipart_form
            if self.options.server_replay_ignore_payload_params and form_contents:
                params = [
                    strutils.always_bytes(i)
                    for i in self.options.server_replay_ignore_payload_params
                ]
                for p in form_contents.items(multi=True):
                    if p[0] not in params:
                        key.append(p)
            else:
                key.append(str(r.raw_content))

        if not self.options.server_replay_ignore_host:
            key.append(r.host)

        filtered = []
        ignore_params = self.options.server_replay_ignore_params or []
        for p in queriesArray:
            if p[0] not in ignore_params:
                filtered.append(p)
        for p in filtered:
            key.append(p[0])
            key.append(p[1])

        if self.options.server_replay_use_headers:
            headers = []
            for i in self.options.server_replay_use_headers:
                v = r.headers.get(i)
                headers.append((i, v))
            key.append(headers)
        return hashlib.sha256(repr(key).encode("utf8",
                                               "surrogateescape")).digest()
Ejemplo n.º 12
0
 def values(self, settings):
     if self.body:
         bodygen = self.body.value.get_generator(settings)
         length = len(self.body.value.get_generator(settings))
     elif self.rawbody:
         bodygen = self.rawbody.value.get_generator(settings)
         length = len(self.rawbody.value.get_generator(settings))
     elif self.nested_frame:
         bodygen = NESTED_LEADER + strutils.always_bytes(self.nested_frame.parsed.spec())
         length = len(bodygen)
     else:
         bodygen = None
         length = 0
     if self.toklength:
         length = int(self.toklength.value)
     frameparts = dict(
         payload_length=length
     )
     if self.mask and self.mask.value:
         frameparts["mask"] = True
     if self.knone:
         frameparts["masking_key"] = None
     elif self.key:
         key = self.key.values(settings)[0][:]
         frameparts["masking_key"] = key
     for i in ["opcode", "fin", "rsv1", "rsv2", "rsv3", "mask"]:
         v = getattr(self, i, None)
         if v is not None:
             frameparts[i] = v.value
     frame = netlib.websockets.FrameHeader(**frameparts)
     vals = [bytes(frame)]
     if bodygen:
         if frame.masking_key and not self.rawbody:
             masker = netlib.websockets.Masker(frame.masking_key)
             vals.append(
                 generators.TransformGenerator(
                     bodygen,
                     masker.mask
                 )
             )
         else:
             vals.append(bodygen)
     return vals
Ejemplo n.º 13
0
 def __init__(self, headers, flows, exit, nopop, ignore_params,
              ignore_content, ignore_payload_params, ignore_host):
     """
         headers: Case-insensitive list of request headers that should be
         included in request-response matching.
     """
     self.headers = headers
     self.exit = exit
     self.nopop = nopop
     self.ignore_params = ignore_params
     self.ignore_content = ignore_content
     self.ignore_payload_params = [
         strutils.always_bytes(x) for x in (ignore_payload_params or ())
     ]
     self.ignore_host = ignore_host
     self.fmap = {}
     for i in flows:
         if i.response:
             l = self.fmap.setdefault(self._hash(i), [])
             l.append(i)
Ejemplo n.º 14
0
    def assemble_response(self, response):
        assert isinstance(response, netlib.http.response.Response)

        headers = response.headers.copy()

        if ':status' not in headers:
            headers.insert(0, b':status',
                           strutils.always_bytes(response.status_code))

        if hasattr(response, 'stream_id'):
            stream_id = response.stream_id
        else:
            stream_id = self._next_stream_id()

        return list(
            itertools.chain(
                self._create_headers(headers,
                                     stream_id,
                                     end_stream=(response.body is None
                                                 or len(response.body) == 0)),
                self._create_body(response.body, stream_id),
            ))
Ejemplo n.º 15
0
 def values(self, settings):
     if self.body:
         bodygen = self.body.value.get_generator(settings)
         length = len(self.body.value.get_generator(settings))
     elif self.rawbody:
         bodygen = self.rawbody.value.get_generator(settings)
         length = len(self.rawbody.value.get_generator(settings))
     elif self.nested_frame:
         bodygen = NESTED_LEADER + strutils.always_bytes(
             self.nested_frame.parsed.spec())
         length = len(bodygen)
     else:
         bodygen = None
         length = 0
     if self.toklength:
         length = int(self.toklength.value)
     frameparts = dict(payload_length=length)
     if self.mask and self.mask.value:
         frameparts["mask"] = True
     if self.knone:
         frameparts["masking_key"] = None
     elif self.key:
         key = self.key.values(settings)[0][:]
         frameparts["masking_key"] = key
     for i in ["opcode", "fin", "rsv1", "rsv2", "rsv3", "mask"]:
         v = getattr(self, i, None)
         if v is not None:
             frameparts[i] = v.value
     frame = netlib.websockets.FrameHeader(**frameparts)
     vals = [bytes(frame)]
     if bodygen:
         if frame.masking_key and not self.rawbody:
             masker = netlib.websockets.Masker(frame.masking_key)
             vals.append(generators.TransformGenerator(
                 bodygen, masker.mask))
         else:
             vals.append(bodygen)
     return vals
Ejemplo n.º 16
0
 def _always_bytes(x):
     strutils.always_bytes(
         x, "utf-8",
         "replace")  # raises a TypeError if x != str/bytes/None.
     return x
Ejemplo n.º 17
0
 def create_server_nonce(self, client_nonce):
     return base64.b64encode(hashlib.sha1(strutils.always_bytes(client_nonce) + websockets_magic).digest())
Ejemplo n.º 18
0
def create_server_nonce(client_nonce):
    return base64.b64encode(
        hashlib.sha1(strutils.always_bytes(client_nonce) + MAGIC).digest())
Ejemplo n.º 19
0
 def _always_bytes(x):
     return strutils.always_bytes(x, "utf-8", "surrogateescape")
Ejemplo n.º 20
0
def create_server_nonce(client_nonce):
    return base64.b64encode(hashlib.sha1(strutils.always_bytes(client_nonce) + MAGIC).digest())
Ejemplo n.º 21
0
 def _always_bytes(x):
     return strutils.always_bytes(x, "utf-8", "surrogateescape")
Ejemplo n.º 22
0
 def create_server_nonce(self, client_nonce):
     return base64.b64encode(
         hashlib.sha1(
             strutils.always_bytes(client_nonce) +
             websockets_magic).digest())
Ejemplo n.º 23
0
 def _always_bytes(x):
     strutils.always_bytes(x, "utf-8", "replace")  # raises a TypeError if x != str/bytes/None.
     return x