def __call__(self, environ, start_response): # Base64 encode auth header http_auth = 'HTTP_AUTHORIZATION' if http_auth in environ: environ[http_auth] = authHeader(environ[http_auth]) publish = publish_module if self.browser.handleErrors: publish = HTTPExceptionHandler(publish) wsgi_result = publish(environ, start_response) # Sync transaction self.app._p_jar.sync() return wsgi_result
def __call__(self, environ, start_response): # This is similar to # Testing.ZopeTestCase.zopedoctest.functional.http # Commit previously done work transaction.commit() # Base64 encode auth header http_auth = 'HTTP_AUTHORIZATION' if http_auth in environ: environ[http_auth] = auth_header(environ[http_auth]) publish = publish_module if self.browser.handleErrors: publish = HTTPExceptionHandler(publish) wsgi_result = publish(environ, start_response) # Sync transaction AppZapper().app()._p_jar.sync() return wsgi_result
def publish(self, path, basic=None, env=None, extra=None, request_method='GET', stdin=None, handle_errors=True): '''Publishes the object at 'path' returning a response object.''' from io import BytesIO from ZPublisher.HTTPRequest import WSGIRequest as Request from ZPublisher.HTTPResponse import WSGIResponse from ZPublisher.WSGIPublisher import publish_module # Commit the sandbox for good measure transaction.commit() if env is None: env = {} if extra is None: extra = {} request = self.app.REQUEST env['SERVER_NAME'] = request['SERVER_NAME'] env['SERVER_PORT'] = request['SERVER_PORT'] env['SERVER_PROTOCOL'] = 'HTTP/1.1' env['REQUEST_METHOD'] = request_method p = path.split('?') if len(p) == 1: env['PATH_INFO'] = p[0] elif len(p) == 2: [env['PATH_INFO'], env['QUERY_STRING']] = p else: raise TypeError('') if basic: env['HTTP_AUTHORIZATION'] = basic_auth_encode(basic) if not handle_errors: # Tell the publisher to skip exception views env['x-wsgiorg.throw_errors'] = True if stdin is None: stdin = BytesIO() outstream = BytesIO() response = WSGIResponse(stdout=outstream, stderr=sys.stderr) request = Request(stdin, env, response) for k, v in extra.items(): request[k] = v wsgi_headers = BytesIO() def start_response(status, headers): # Keep the fake response in-sync with the actual values # from the WSGI start_response call. response.setStatus(status.split()[0]) for key, value in headers: response.setHeader(key, value) wsgi_headers.write( b'HTTP/1.1 ' + status.encode('ascii') + b'\r\n') headers = b'\r\n'.join([ (k + ': ' + v).encode('ascii') for k, v in headers]) wsgi_headers.write(headers) wsgi_headers.write(b'\r\n\r\n') publish = partial(publish_module, _request=request, _response=response) if handle_errors: publish = HTTPExceptionHandler(publish) wsgi_result = publish(env, start_response) return ResponseWrapper(response, outstream, path, wsgi_result, wsgi_headers)
def http(request_string, handle_errors=True): """Execute an HTTP request string via the publisher This is used for HTTP doc tests. """ from urllib.parse import unquote from ZPublisher.HTTPRequest import WSGIRequest as Request from ZPublisher.HTTPResponse import WSGIResponse from ZPublisher.WSGIPublisher import publish_module # Commit work done by previous python code. transaction.commit() # Discard leading white space to make call layout simpler request_string = request_string.lstrip() # Split off and parse the command line newline = request_string.find('\n') command_line = request_string[:newline].rstrip() request_string = request_string[newline + 1:] method, path, protocol = command_line.split() path = unquote(path) env = { 'HTTP_HOST': 'localhost', 'HTTP_REFERER': 'localhost', 'REQUEST_METHOD': method, 'SERVER_PROTOCOL': protocol, } p = path.split('?', 1) if len(p) == 1: env['PATH_INFO'] = p[0] elif len(p) == 2: [env['PATH_INFO'], env['QUERY_STRING']] = p else: raise TypeError('') header_output = HTTPHeaderOutput( protocol, ('x-content-type-warning', 'x-powered-by')) # With a HeaderParser the payload is always a string, Parser would create a # list of messages for multipart messages. parser = email.parser.HeaderParser() msg = parser.parsestr(request_string) headers = msg.items() body = msg.get_payload() if isinstance(body, str): body = body.encode('utf-8') # Store request body without headers instream = BytesIO(body) for name, value in headers: name = ('_'.join(name.upper().split('-'))) if name not in ('CONTENT_TYPE', 'CONTENT_LENGTH'): name = 'HTTP_' + name env[name] = value.rstrip() if 'HTTP_AUTHORIZATION' in env: env['HTTP_AUTHORIZATION'] = auth_header(env['HTTP_AUTHORIZATION']) if not handle_errors: # Tell the publisher to skip exception views env['x-wsgiorg.throw_errors'] = True outstream = BytesIO() response = WSGIResponse(stdout=outstream, stderr=sys.stderr) request = Request(instream, env, response) env['wsgi.input'] = instream wsgi_headers = BytesIO() def start_response(status, headers): wsgi_headers.write( b'HTTP/1.1 ' + status.encode('ascii') + b'\r\n') headers = b'\r\n'.join([ (k + ': ' + v).encode('ascii') for k, v in headers]) wsgi_headers.write(headers) wsgi_headers.write(b'\r\n\r\n') publish = partial(publish_module, _request=request, _response=response) if handle_errors: publish = HTTPExceptionHandler(publish) wsgi_result = publish(env, start_response) header_output.setResponseStatus(response.getStatus(), response.errmsg) header_output.setResponseHeaders(response.headers) header_output.headersl.extend(response._cookie_list()) header_output.appendResponseHeaders(response.accumulated_headers) sync() return DocResponseWrapper( response, outstream, path, header_output, wsgi_result, wsgi_headers)
def __call__(self, environ, start_response): publish = HTTPExceptionHandler(publish_module) wsgi_result = publish(environ, start_response) return wsgi_result
def http(request_string, handle_errors=True): """Execute an HTTP request string via the publisher This is used for HTTP doc tests. """ import rfc822 from io import BytesIO from six.moves.urllib.parse import unquote from ZPublisher.HTTPRequest import WSGIRequest as Request from ZPublisher.HTTPResponse import WSGIResponse from ZPublisher.WSGIPublisher import publish_module # Commit work done by previous python code. transaction.commit() # Discard leading white space to make call layout simpler request_string = request_string.lstrip() # Split off and parse the command line l = request_string.find('\n') command_line = request_string[:l].rstrip() request_string = request_string[l + 1:] method, path, protocol = command_line.split() path = unquote(path) instream = BytesIO(request_string) env = { 'HTTP_HOST': 'localhost', 'HTTP_REFERER': 'localhost', 'REQUEST_METHOD': method, 'SERVER_PROTOCOL': protocol, } p = path.split('?', 1) if len(p) == 1: env['PATH_INFO'] = p[0] elif len(p) == 2: [env['PATH_INFO'], env['QUERY_STRING']] = p else: raise TypeError('') header_output = HTTPHeaderOutput( protocol, ('x-content-type-warning', 'x-powered-by', 'bobo-exception-type', 'bobo-exception-file', 'bobo-exception-value', 'bobo-exception-line')) headers = [ split_header(header) for header in rfc822.Message(instream).headers ] # Store request body without headers instream = BytesIO(instream.read()) for name, value in headers: name = ('_'.join(name.upper().split('-'))) if name not in ('CONTENT_TYPE', 'CONTENT_LENGTH'): name = 'HTTP_' + name env[name] = value.rstrip() if 'HTTP_AUTHORIZATION' in env: env['HTTP_AUTHORIZATION'] = auth_header(env['HTTP_AUTHORIZATION']) outstream = BytesIO() response = WSGIResponse(stdout=outstream, stderr=sys.stderr) request = Request(instream, env, response) env['wsgi.input'] = instream wsgi_headers = BytesIO() def start_response(status, headers): wsgi_headers.write('HTTP/1.1 %s\r\n' % status) headers = '\r\n'.join([': '.join(x) for x in headers]) wsgi_headers.write(headers) wsgi_headers.write('\r\n\r\n') publish = partial(publish_module, _request=request, _response=response) if handle_errors: publish = HTTPExceptionHandler(publish) wsgi_result = publish(env, start_response) header_output.setResponseStatus(response.getStatus(), response.errmsg) header_output.setResponseHeaders(response.headers) header_output.headersl.extend(response._cookie_list()) header_output.appendResponseHeaders(response.accumulated_headers) sync() return DocResponseWrapper(response, outstream, path, header_output, wsgi_result, wsgi_headers)
def publish(self, path, basic=None, env=None, extra=None, request_method='GET', stdin=None, handle_errors=True): '''Publishes the object at 'path' returning a response object.''' from io import BytesIO from ZPublisher.HTTPRequest import WSGIRequest as Request from ZPublisher.HTTPResponse import WSGIResponse from ZPublisher.WSGIPublisher import publish_module # Commit the sandbox for good measure transaction.commit() if env is None: env = {} if extra is None: extra = {} request = self.app.REQUEST env['SERVER_NAME'] = request['SERVER_NAME'] env['SERVER_PORT'] = request['SERVER_PORT'] env['SERVER_PROTOCOL'] = 'HTTP/1.1' env['REQUEST_METHOD'] = request_method p = path.split('?') if len(p) == 1: env['PATH_INFO'] = p[0] elif len(p) == 2: [env['PATH_INFO'], env['QUERY_STRING']] = p else: raise TypeError('') if basic: env['HTTP_AUTHORIZATION'] = "Basic %s" % base64.encodestring(basic) if stdin is None: stdin = BytesIO() outstream = BytesIO() response = WSGIResponse(stdout=outstream, stderr=sys.stderr) request = Request(stdin, env, response) for k, v in extra.items(): request[k] = v wsgi_headers = BytesIO() def start_response(status, headers): wsgi_headers.write('HTTP/1.1 %s\r\n' % status) headers = '\r\n'.join([': '.join(x) for x in headers]) wsgi_headers.write(headers) wsgi_headers.write('\r\n\r\n') publish = partial(publish_module, _request=request, _response=response) if handle_errors: publish = HTTPExceptionHandler(publish) wsgi_result = publish(env, start_response) return ResponseWrapper(response, outstream, path, wsgi_result, wsgi_headers)