def handle_http_connection(self, conn): # Read the trailing HTTP request and process it with protocol_switcher. # We can't rely on ioloop to trigger read because it has been already # triggered for SSL handshake. payload = yield conn.stream.read_bytes(1024, partial=True) logger.debug("Received %r", payload[:128]) # Simulate conn._read_message side effect. This is required by # HTTP1Connection.write_headers() conn._request_start_line = parse_request_start_line('GET / HTTP/1.1') try: start_line, headers = parse_http_headers(payload) conn._request_start_line = start_line request = HTTPRequest( connection=conn, headers=headers, start_line=start_line, ) request.config = self.request_callback.config response = protocol_switcher(request) except Exception as e: logger.error("Failed to switch to HTTPS: %s", e) response = HTTPResponse( request=object(), code=500, headers=HTTPHeaders({'Content-Length': '0'}), effective_url='https://useless_effective_url') yield conn.write_headers( start_line=ResponseStartLine( 'HTTP/1.1', response.code, response.reason, ), headers=response.headers, )
def test_set_avatar_from_url(self): return client = HTTPClient() req = HTTPRequest('post', '/login') response = client.fetch(req) req = HTTPRequest('post', '/u/avatar?v=') response = client.fetch(req) self.assertEqual(response.code, 200) self.assertEqual(response.body, 'failed')
def test_exception_plugin(): # type: () -> None test_logger = ExceptionLoggerTestPlugin() proxy = PluginProxy([test_logger]) try: raise RandomException("some string") except RandomException: proxy.log_exception(None, None, *sys.exc_info()) assert test_logger.request is None assert test_logger.status is None assert test_logger.exc_type == RandomException assert str(test_logger.exc_value) == "some string" assert test_logger.exc_tb is not None # Reinitializing test_logger unconfuses mypy, which otherwise thinks that test_logger.request # must still be None. See mypy/issues/4168. test_logger = ExceptionLoggerTestPlugin() proxy = PluginProxy([test_logger]) try: raise RandomException("with request") except RandomException: request = HTTPRequest("GET", "/foo") proxy.log_exception(request, 200, *sys.exc_info()) assert test_logger.request is not None assert test_logger.request.uri == "/foo" assert test_logger.status == 200 assert test_logger.exc_type == RandomException assert str(test_logger.exc_value) == "with request" assert test_logger.exc_tb is not None
def _begin_request(self): """ Actually start executing this request. """ headers = self.m2req.headers self._request = HTTPRequest(connection=self, method=headers.get("METHOD"), uri=self.m2req.path, version=headers.get("VERSION"), headers=headers, remote_ip=headers.get("x-forwarded-for")) if len(self.m2req.body) > 0: self._request.body = self.m2req.body if self.m2req.is_disconnect(): self.finish() elif headers.get("x-mongrel2-upload-done", None): # there has been a file upload. expected = headers.get("x-mongrel2-upload-start", "BAD") upload = headers.get("x-mongrel2-upload-done", None) if expected == upload: self.request_callback(self._request) elif headers.get("x-mongrel2-upload-start", None): # this is just a notification that a file upload has started. Do # nothing for now! pass else: self.request_callback(self._request)
def test_exception_plugin(): # type: () -> None test_logger = ExceptionLoggerTestPlugin() proxy = PluginProxy([test_logger]) try: raise RandomException("some string") except RandomException: proxy.log_exception(None, None, *sys.exc_info()) assert test_logger.request is None assert test_logger.status is None assert test_logger.exc_type == RandomException assert str(test_logger.exc_value) == "some string" assert test_logger.exc_tb is not None try: raise RandomException("with request") except RandomException: request = HTTPRequest("GET", "/foo") proxy.log_exception(request, 200, *sys.exc_info()) assert test_logger.request is not None assert test_logger.request.uri == "/foo" assert test_logger.status == 200 assert test_logger.exc_type == RandomException assert str(test_logger.exc_value) == "with request" assert test_logger.exc_tb is not None
def mockedStatusHandler(method='GET', uri='/status', headers=None, body=None): """StatsusHandler with appropriate Application and HTTPRequest mocking.""" headers = HTTPHeaders({} if headers is None else headers) request = HTTPRequest(method=method, uri=uri, headers=headers, body=body, connection=Mock()) return StatusHandler(Application(), request)
def handle_http_connection(self, conn): # Read the trailing HTTP request and process it with protocol_switcher. # We can't rely on ioloop to trigger read because it has been already # triggered for SSL handshake. addr, port = conn.stream.socket.getsockname() try: # This is not blocking. Just read available bytes. payload = conn.stream.socket.recv(1024) except Exception: # Exception includes EWOULDBLOCK, when no bytes are available. In # this case just skip. payload = "" else: logger.debug("Received %r", payload[:128]) # Simulate conn._read_message side effect. This is required by # HTTP1Connection.write_headers() conn._request_start_line = parse_request_start_line('GET / HTTP/1.1') conn._request_headers = HTTPHeaders() try: start_line, headers = parse_http_headers(payload) conn._request_start_line = start_line request = HTTPRequest( connection=conn, headers=headers, start_line=start_line, ) request.config = self.request_callback.config response = protocol_switcher(request) except Exception as e: logger.error("Failed to switch to HTTPS: %s", e) response = HTTPResponse( request=object(), code=500, headers=HTTPHeaders({'Content-Length': '0'}), effective_url='https://useless_effective_url') yield conn.write_headers( start_line=ResponseStartLine( 'HTTP/1.1', response.code, response.reason, ), headers=response.headers, )
def post_handler(self, body_dict, app=None): if app is None: app = self.mock_application body = json.dumps(body_dict).encode('utf-8') payload_request = HTTPRequest(method='POST', uri="/api/bookstore/fs-clone", headers=None, body=body, connection=Mock()) return BookstoreFSCloneAPIHandler(app, payload_request)
def test_invalid_email_customers(self): params = {"email": "test.test.com"} url = url_concat("/customer/", params) mock_application = Mock(spec=Application) payload_request = HTTPRequest(method='GET', uri=url, headers=None, body=None) handler = customer_handler(mock_application, payload_request) with self.assertRaises(400): yield handler.get()
def get_handler(self, uri, app=None): if app is None: app = self.mock_application connection = Mock(context=Mock(protocol="https")) payload_request = HTTPRequest( method='GET', uri=uri, headers={"Host": "localhost:8888"}, body=None, connection=connection, ) return BookstoreCloneHandler(app, payload_request)
def test_missing_total(self): params = {"email": "test.test.com", "order_total": ""} url = url_concat("/processOrder/", params) mock_application = Mock(spec=Application) payload_request = HTTPRequest(method='POST', uri=url, headers=None, body=None) handler = processOrder_handler(mock_application, payload_request) with self.assertRaises(400): yield handler.get()
def put_handler(self, uri, body_dict=None, app=None): if body_dict is None: body_dict = {} if app is None: app = self.mock_application connection = Mock(context=Mock(protocol="https")) body = json.dumps(body_dict).encode('utf-8') payload_request = HTTPRequest( method='PUT', uri=uri, headers={"Host": "localhost:8888"}, body=body, connection=connection, ) return BookstorePublishAPIHandler(app, payload_request)
def _run_request(self, name): service, application_factory = self._get_application_factory(name) application = application_factory(self.settings) application._ctx = self.application._ctx application.ctx = HookContext(service, self.application._ctx.bot) application.name = name uri = self.request.uri[len(name) + 1:] application(HTTPRequest(self.request.method, uri, self.request.version, self.request.headers, self.request.body, self.request.remote_ip, self.request.protocol, self.request.host, self.request.files, self.request.connection)) self._handled = True
def mockedLDPHandler(method='GET', uri='/test', headers=None, body=None, base_uri='http://localhost/'): """LDPHandler with appropriate Application and HTTPRequest mocking, and a Store.""" headers = HTTPHeaders({} if headers is None else headers) request = HTTPRequest(method=method, uri=uri, headers=headers, body=body, connection=Mock()) h = LDPHandler(Application(), request) h.base_uri = base_uri h.store = Store(h.base_uri) h.store.add(LDPC(), uri=h.base_uri) return h
def _on_headers(self, data): # Request body is not read here try: data = native_str(data.decode('latin1')) eol = data.find("\r\n") start_line = data[:eol] try: method, uri, version = start_line.split(" ") except ValueError: raise _BadRequestException("Malformed HTTP request line") if not version.startswith("HTTP/"): raise _BadRequestException( "Malformed HTTP version in HTTP Request-Line") headers = httputil.HTTPHeaders.parse(data[eol:]) # HTTPRequest wants an IP, not a full socket address if self.address_family in (socket.AF_INET, socket.AF_INET6): remote_ip = self.address[0] else: # Unix (or other) socket; fake the remote address remote_ip = '0.0.0.0' self._request = HTTPRequest(connection=self, method=method, uri=uri, version=version, headers=headers, remote_ip=remote_ip, protocol=self.protocol) content_length = headers.get("Content-Length") if content_length: content_length = int(content_length) if content_length > self.stream.max_buffer_size: pass if headers.get("Expect") == "100-continue": self.stream.write(b"HTTP/1.1 100 (Continue)\r\n\r\n") self.request_callback(self._request) except _BadRequestException as e: gen_log.info("Malformed HTTP request from %s: %s", self.address[0], e) self.close() return
def create_request(self, uri, method="GET", headers={}, body=None, remote_ip=None): request = HTTPRequest(uri=uri, method=method, headers=headers, body=body, remote_ip=remote_ip) if body: arguments = parse_qs_bytes(native_str(body)) for name, values in arguments.iteritems(): values = [v for v in values if v] if values: request.arguments.setdefault(name, []).extend(values) return request
def get(self): try: token = yield AccessToken.get_access_token() except Exception as e: self.write("errmsg: %s" % e) else: client = AsyncHTTPClient() url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s" % token menu = { "button": [{ "name": "缴费", "type": "view", "url": "http://heimamba.com/wechat/profiles" }, { "type": "view", "name": "报修", "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4ccc7659371cb250&redirect_uri=http://heimamba.com/wechat/baoxiu&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect" }, { "type": "view", "name": "我的", "key": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4ccc7659371cb250&redirect_uri=http://heimamba.com/wechat/profile&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect" }] } req = HTTPRequest(url=url, method='POST', body=json.dumps(menu, ensure_ascii=False)) resp = yield client.fetch(req) dict_data = json.loads(resp.body.decode('utf-8')) print(dict_data) if dict_data['errcode'] == 0: self.write('OK') else: self.write('error')
def http_cb(r): body = r.input_buffer.read() treq = HTTPRequest( r.typestr, # method r.uri, # uri headers=dict(r.get_input_headers( )), # need to transform from list of tuples to dict body=body, remote_ip=r.remote_host, protocol="http", # or https host=None, # 127.0.0.1? files=None, # ?? connection=FakeConnection(r)) parse_post_body(treq, body) """ print "http request = ", r for m in dir(r): o = eval("r." + m) if type(o) in (str, list, int, tuple): print "r.%s = %r" % (m, o) """ t_app(treq)
def setUp(self): self.test_values = HTTPRequest( 'GET', 'http://localhost?a=Apple&b=Banana&a=Cherry') self.empty_mdict = TornadoInputWrapper({}) self.filled_mdict = TornadoInputWrapper(self.test_values.arguments)
def _tornado_request_wrapper(data): method, uri, version, headers, body = msgpack.unpackb(data) return HTTPRequest(method, uri, version, dict(headers), body)