def test_dup_var_should_be_removed(self): fields_qs = [ ('a', 1), ('b', 1), ] fields_form = [ ('a', 1), ('b', 2), ] qs = urlencode(fields_qs) body = urlencode(fields_form) stdin = StringIO(body) env = { 'SERVER_PROTOCOL': 'HTTP/1.0', 'REQUEST_METHOD': 'POST', 'PATH_INFO': '/', 'CONTENT_TYPE': "application/x-www-form-urlencoded", 'CONTENT_LENGTH': str(len(body)), 'QUERY_STRING': qs, } req = HTTPRequest(stdin, env) req.process_inputs() self.assertEqual(req.form, {'a': ['1', '1'], 'b': ['2', '1']}) self.assertEqual(req.get_form_var('a'), '1') self.assertEqual(req.get_form_list_var('a'), ['1', '1']) self.assertEqual(req.get_form_var('b'), ['2','1']) self.assertEqual(req.get_form_list_var('b'), ['2','1'])
def continue_request(self, data, request): msg = rfc822.Message(StringIO('\n'.join(request.header))) remote_addr, remote_port = request.channel.addr if '#' in request.uri: # MSIE is buggy and sometimes includes fragments in URLs [request.uri, fragment] = request.uri.split('#', 1) if '?' in request.uri: [path, query_string] = request.uri.split('?', 1) else: path = request.uri query_string = '' path = urllib.unquote(path) server_port = str(self.server.port) http_host = msg.get("Host") if http_host: if ":" in http_host: server_name, server_port = http_host.split(":", 1) else: server_name = http_host else: server_name = (self.server.ip or socket.gethostbyaddr(socket.gethostname())[0]) environ = { 'REQUEST_METHOD': request.command, 'ACCEPT_ENCODING': msg.get('Accept-encoding', ''), 'CONTENT_TYPE': msg.get('Content-type', ''), 'CONTENT_LENGTH': len(data), "GATEWAY_INTERFACE": "CGI/1.1", 'PATH_INFO': path, 'QUERY_STRING': query_string, 'REMOTE_ADDR': remote_addr, 'REMOTE_PORT': str(remote_port), 'REQUEST_URI': request.uri, 'SCRIPT_NAME': '', "SCRIPT_FILENAME": '', 'SERVER_NAME': server_name, 'SERVER_PORT': server_port, 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'Quixote/%s' % quixote.__version__, } for title, header in msg.items(): envname = 'HTTP_' + title.replace('-', '_').upper() environ[envname] = header stdin = StringIO(data) qrequest = HTTPRequest(stdin, environ) qresponse = self.publisher.process_request(qrequest) # Copy headers from Quixote's HTTP response for name, value in qresponse.generate_headers(): # XXX Medusa's HTTP request is buggy, and only allows unique # headers. request[name] = value request.response(qresponse.status_code) request.push(StreamProducer(qresponse.generate_body_chunks())) request.done()
def run(publisher, req): from quixote.http_request import HTTPRequest request = HTTPRequest(apache.CGIStdin(req), apache.build_cgi_env(req)) response = publisher.process_request(request) try: response.write(apache.CGIStdout(req)) except IOError, err: publisher.log("IOError while sending response ignored: %s" % err)
def process(self, env): request = HTTPRequest(self.rfile, env) response = get_publisher().process_request(request) try: self.send_response(response.get_status_code(), response.get_reason_phrase()) response.write(self.wfile, include_status=False) except IOError, err: print "IOError while sending response ignored: %s" % err
def _process(rfile, env, include_body): """Process a single request, in background Quixote thread.""" request = HTTPRequest(rfile, env, seekable=True) response = get_publisher().process_request(request) status, reason = response.get_status_code(), response.get_reason_phrase() # write response body to temporary file, this ensures that write() runs in # the correct thread and we are not blocked by slow clients. body = tempfile.SpooledTemporaryFile() response.write(body, include_status=False, include_body=include_body) body.seek(0) return (status, reason, body)
def run(create_publisher): publisher = create_publisher() while _fcgi.isFCGI(): f = _fcgi.FCGI() request = HTTPRequest(f.inp, f.env) response = publisher.process_request(request) try: response.write(f.out) except IOError, err: publisher.log("IOError while sending response ignored: %s" % err) f.Finish()
def process(self, stdin, env): """(stdin : stream, env : dict) -> HTTPResponse Process a single request, given a stream, stdin, containing the incoming request and a dictionary, env, containing the web server's environment. An HTTPRequest object is created and the process_request() method is called and passed the request object. """ request = HTTPRequest(stdin, env) return self.process_request(request)
def run(create_publisher): if sys.platform == "win32": # on Windows, stdin and stdout are in text mode by default import msvcrt msvcrt.setmode(sys.__stdin__.fileno(), os.O_BINARY) msvcrt.setmode(sys.__stdout__.fileno(), os.O_BINARY) publisher = create_publisher() request = HTTPRequest(sys.__stdin__, os.environ) response = publisher.process_request(request) try: response.write(sys.__stdout__) except IOError, err: publisher.log("IOError while sending response ignored: %s" % err)
def create_request(self, stdin, env): ctype = get_content_type(env) if ctype == "multipart/form-data" and ( # workaround for safari bug, see ticket #1556 env.get('REQUEST_METHOD') != 'GET' or env.get('CONTENT_LENGTH', '0') != '0'): req = HTTPUploadRequest(stdin, env, content_type=ctype) req.set_upload_dir(self.config.upload_dir, self.config.upload_dir_mode) return req elif self.config.support_application_json and ctype == "application/json": return HTTPJSONRequest(stdin, env, content_type=ctype) else: return HTTPRequest(stdin, env, content_type=ctype)
def process(self): environ = self.create_environment() # this seek is important, it doesn't work without it (it doesn't # matter for GETs, but POSTs will not work properly without it.) self.content.seek(0, 0) qxrequest = HTTPRequest(self.content, environ) qxresponse = self.channel.factory.publisher.process_request(qxrequest) self.setResponseCode(qxresponse.status_code) for name, value in qxresponse.generate_headers(): if name != 'Set-Cookie': self.setHeader(name, value) # Cookies get special treatment since it seems Twisted cannot handle # multiple Set-Cookie headers. for name, attrs in qxresponse.cookies.items(): attrs = attrs.copy() value = attrs.pop('value') self.addCookie(name, value, **attrs) QuixoteProducer(qxresponse, self)
def handle_connection(self, conn): input = conn.makefile("r") output = conn.makefile("w") env = self.read_env(input) if self.script_name is not None: # mod_scgi doesn't know SCRIPT_NAME :-( prefix = self.script_name path = env['SCRIPT_NAME'] assert path[:len(prefix)] == prefix, ( "path %r doesn't start with script_name %r" % (path, prefix)) env['SCRIPT_NAME'] = prefix env['PATH_INFO'] = path[len(prefix):] + env.get('PATH_INFO', '') request = HTTPRequest(input, env) response = self.publisher.process_request(request) try: response.write(output) input.close() output.close() conn.close() except IOError, err: self.publisher.log("IOError while sending response " "ignored: %s" % err)
#!/usr/bin/env python # Test Quixote's ability to parse the "User-Agent" header, ie. # the 'guess_browser_version()' method of HTTPRequest. # # Reads User-Agent strings on stdin, and writes Quixote's interpretation # of each on stdout. This is *not* an automated test! import sys, os from copy import copy from quixote.http_request import HTTPRequest env = copy(os.environ) file = sys.stdin while 1: line = file.readline() if not line: break if line[-1] == "\n": line = line[:-1] env["HTTP_USER_AGENT"] = line req = HTTPRequest(None, env) (name, version) = req.guess_browser_version() if name is None: print "%s -> ???" % line else: print "%s -> (%s, %s)" % (line, name, version)
def __init__(self, stdin, environ, content_type=None): HTTPRequest.__init__(self, stdin, environ, content_type) self.upload_dir = None self.upload_dir_mode = 0775