Esempio n. 1
0
    def data_received(self, data):
        '''Implements :meth:`~.ProtocolConsumer.data_received` method.

        Once we have a full HTTP message, build the wsgi ``environ`` and
        delegate the response to the :func:`wsgi_callable` function.
        '''
        parser = self.parser
        processed = parser.execute(data, len(data))
        if not self._stream and parser.is_headers_complete():
            headers = Headers(parser.get_headers(), kind='client')
            self._stream = StreamReader(headers, parser, self.transport)
            self._response(self.wsgi_environ())
        #
        if parser.is_message_complete():
            #
            # Stream has the whole body
            if not self._stream.on_message_complete.done():
                self._stream.on_message_complete.set_result(None)

            if processed < len(data):
                if not self._buffer:
                    self._buffer = data[processed:]
                    self.bind_event('post_request', self._new_request)
                else:
                    self._buffer += data[processed:]
        #
        elif processed < len(data):
            # This is a parsing error, the client must have sent
            # bogus data
            raise ProtocolError
 def __init__(self, url, username, password, **kwargs):
     super(GerritSession, self).__init__(
         headers=Headers([('Content-Type', 'application/json')]),
         **kwargs
     )
     self.url = url
     self.auth = HTTPDigestAuth(username, password)
Esempio n. 3
0
 def test_CacheControl(self):
     headers = Headers()
     c = CacheControl()
     self.assertFalse(c.private)
     self.assertFalse(c.maxage)
     c(headers)
     self.assertEqual(headers['cache-control'], 'no-cache')
     c = CacheControl(maxage=3600)
     c(headers)
     self.assertEqual(headers['cache-control'], 'max-age=3600, public')
     c = CacheControl(maxage=3600, private=True)
     c(headers)
     self.assertEqual(headers['cache-control'], 'max-age=3600, private')
     c = CacheControl(maxage=3600, must_revalidate=True)
     c(headers)
     self.assertEqual(headers['cache-control'],
                      'max-age=3600, public, must-revalidate')
     c = CacheControl(maxage=3600, proxy_revalidate=True)
     c(headers)
     self.assertEqual(headers['cache-control'],
                      'max-age=3600, public, proxy-revalidate')
     c = CacheControl(maxage=3600, proxy_revalidate=True,
                      nostore=True)
     c(headers)
     self.assertEqual(headers['cache-control'],
                      'no-store, no-cache, must-revalidate, max-age=0')
Esempio n. 4
0
    def request_start_response(self,
                               method,
                               path,
                               HTTP_ACCEPT=None,
                               headers=None,
                               data=None,
                               json=None,
                               content_type=None,
                               token=None,
                               oauth=None,
                               jwt=None,
                               cookie=None,
                               params=None,
                               **extra):
        method = method.upper()
        extra['REQUEST_METHOD'] = method.upper()
        path = path or '/'
        extra['HTTP_ACCEPT'] = HTTP_ACCEPT or '*/*'
        extra['pulsar.connection'] = mock.MagicMock()
        heads = []
        if headers:
            heads.extend(headers)
        if json is not None:
            content_type = 'application/json'
            assert not data
            data = json
        if content_type:
            heads.append(('content-type', content_type))
        if token:
            heads.append(('Authorization', 'Bearer %s' % token))
        elif oauth:
            heads.append(('Authorization', 'OAuth %s' % oauth))
        elif jwt:
            heads.append(('Authorization', 'JWT %s' % jwt))
        if cookie:
            heads.append(('Cookie', cookie))

        if params:
            path = full_url(path, params)

        # Encode data
        if (method in ENCODE_BODY_METHODS and data is not None
                and not isinstance(data, bytes)):
            content_type = Headers(heads).get('content-type')
            if content_type is None:
                data, content_type = encode_multipart_formdata(data)
                heads.append(('content-type', content_type))
            elif content_type == 'application/json':
                data = _json.dumps(data).encode('utf-8')

        request = self.app.wsgi_request(path=path,
                                        headers=heads,
                                        body=data,
                                        **extra)
        request.environ['SERVER_NAME'] = 'localhost'
        start_response = mock.MagicMock()
        return request, start_response
Esempio n. 5
0
 def __init__(self, status=None, content=None, response_headers=None,
              content_type=None, encoding=None, environ=None):
     self.environ = environ
     self.status_code = status or self.DEFAULT_STATUS_CODE
     self.encoding = encoding
     self.cookies = SimpleCookie()
     self.headers = Headers(response_headers, kind='server')
     self.content = content
     if content_type is not None:
         self.content_type = content_type
Esempio n. 6
0
def test_wsgi_environ(path=None,
                      method=None,
                      headers=None,
                      extra=None,
                      secure=False,
                      loop=None,
                      body=None):
    '''An function to create a WSGI environment dictionary for testing.

    :param url: the resource in the ``PATH_INFO``.
    :param method: the ``REQUEST_METHOD``.
    :param headers: optional request headers
    :params secure: a secure connection?
    :param extra: additional dictionary of parameters to add.
    :return: a valid WSGI environ dictionary.
    '''
    parser = http_parser(kind=0)
    method = (method or 'GET').upper()
    path = iri_to_uri(path or '/')
    request_headers = Headers(headers, kind='client')
    # Add Host if not available
    parsed = urlparse(path)
    if 'host' not in request_headers:
        if not parsed.netloc:
            path = '%s%s' % ('https://:443' if secure else 'http://:80', path)
        else:
            request_headers['host'] = parsed.netloc
    #
    data = '%s %s HTTP/1.1\r\n\r\n' % (method, path)
    data = data.encode('latin1')
    parser.execute(data, len(data))
    #
    headers = Headers()
    stream = StreamReader(request_headers, parser)
    stream.buffer = body or b''
    stream.on_message_complete.set_result(None)
    extra = extra or {}
    return wsgi_environ(stream, ('127.0.0.1', 8060),
                        '777.777.777.777:8080',
                        headers,
                        https=secure,
                        extra=extra)
Esempio n. 7
0
 def test_client_header(self):
     h = Headers()
     self.assertEqual(len(h), 0)
     h['content-type'] = 'text/html'
     self.assertEqual(h.get_all('content-type'), ['text/html'])
     self.assertEqual(len(h), 1)
     h['server'] = 'bla'
     self.assertEqual(len(h), 2)
     del h['content-type']
     self.assertEqual(len(h), 1)
     self.assertEqual(h.get_all('content-type', []), [])
Esempio n. 8
0
 def request_headers(self, environ):
     '''Fill request headers from the environ dictionary and
     modify them via the list of :attr:`headers_middleware`.
     The returned headers will be sent to the target uri.
     '''
     headers = Headers(kind='client')
     for k in environ:
         if k.startswith('HTTP_'):
             head = k[5:].replace('_', '-')
             headers[head] = environ[k]
     for head in ENVIRON_HEADERS:
         k = head.replace('-', '_').upper()
         v = environ.get(k)
         if v:
             headers[head] = v
     return headers
Esempio n. 9
0
 def data_processed(self, response, exc=None, **kw):
     '''Receive data from the requesting HTTP client.'''
     status = response.get_status()
     if status == '100 Continue':
         stream = self.environ.get('wsgi.input') or io.BytesIO()
         body = yield stream.read()
         response.transport.write(body)
     if response.parser.is_headers_complete():
         if self._headers is None:
             headers = self.remove_hop_headers(response.headers)
             self._headers = Headers(headers, kind='server')
             # start the response
             self.start_response(status, list(self._headers))
         body = response.recv_body()
         if response.parser.is_message_complete():
             self._done = True
         self.queue.put_nowait(body)
Esempio n. 10
0
    def data_received(self, data):
        '''Implements :meth:`~.ProtocolConsumer.data_received` method.

        Once we have a full HTTP message, build the wsgi ``environ`` and
        delegate the response to the :func:`wsgi_callable` function.
        '''
        p = self.parser
        if p.execute(bytes(data), len(data)) == len(data):
            if self._request_headers is None and p.is_headers_complete():
                self._request_headers = Headers(p.get_headers(), kind='client')
                stream = StreamReader(self._request_headers, p, self.transport)
                self.bind_event('data_processed', stream.data_processed)
                environ = self.wsgi_environ(stream)
                self.event_loop. async (self._response(environ))
        else:
            # This is a parsing error, the client must have sent
            # bogus data
            raise ProtocolError
Esempio n. 11
0
    def data_received(self, data):
        '''Implements :meth:`~.ProtocolConsumer.data_received` method.

        Once we have a full HTTP message, build the wsgi ``environ`` and
        delegate the response to the :func:`wsgi_callable` function.
        '''
        parser = self.parser
        processed = parser.execute(data, len(data))
        if parser.is_headers_complete():
            if not self._body_reader:
                headers = Headers(parser.get_headers(), kind='client')
                self._body_reader = HttpBodyReader(headers,
                                                   parser,
                                                   self.transport,
                                                   self.cfg.stream_buffer,
                                                   loop=self._loop)
                ensure_future(self._response(self.wsgi_environ()),
                              loop=self._loop)
            body = parser.recv_body()
            if body:
                self._body_reader.feed_data(body)
        #
        if parser.is_message_complete():
            #
            self._body_reader.feed_eof()

            if processed < len(data):
                if not self._buffer:
                    self._buffer = data[processed:]
                    self.bind_event('post_request', self._new_request)
                else:
                    self._buffer += data[processed:]
        #
        elif processed < len(data):
            # This is a parsing error, the client must have sent
            # bogus data
            raise ProtocolError