Beispiel #1
0
    def __call__(self, environ, start_response):
        method = environ['REQUEST_METHOD']
        if method not in self.allowed_methods:
            start_response('403 Forbidden', ())
            return ['']

        if self.strip_script_name:
            path_info = ''
        else:
            path_info = environ['SCRIPT_NAME']
        path_info += environ['PATH_INFO']

        query_string = environ['QUERY_STRING']
        if query_string:
            path_info += '?' + query_string

        host_uri = self.extract_uri(environ)
        uri = host_uri + path_info

        new_headers = {}
        for k, v in environ.items():
            if k.startswith('HTTP_'):
                k = k[5:].replace('_', '-').title()
                new_headers[k] = v

        for k, v in (('CONTENT_TYPE', None), ('CONTENT_LENGTH', '0')):
            v = environ.get(k, None)
            if v is not None:
                new_headers[k.replace('_', '-').title()] = v

        if new_headers.get('Content-Length', '0') == '-1':
            raise ValueError(WEBOB_ERROR)

        response = request(uri,
                           method,
                           body=environ['wsgi.input'],
                           headers=new_headers,
                           pool_instance=self.pool)

        if 'location' in response:
            headers = []
            for k, v in response.headerslist:
                if k == 'Location':
                    # rewrite location with a relative path. dont want to deal
                    # with complex url rebuild stuff
                    if v.startswith(host_uri):
                        v = v[len(host_uri):]
                    if self.strip_script_name:
                        v = environ['SCRIPT_NAME'] + v
                    headers.append((k, v))
        else:
            headers = response.headerslist

        start_response(response.status, headers)

        if 'content-length' in response and \
                int(response['content-length']) <= MAX_BODY:
            return [response.body]

        return ResponseIter(response)
Beispiel #2
0
    def __call__(self, environ, start_response):
        method = environ['REQUEST_METHOD']
        if method not in self.allowed_methods:
            start_response('403 Forbidden', ())
            return ['']

        if self.strip_script_name:
            path_info = ''
        else:
            path_info = environ['SCRIPT_NAME']
        path_info += environ['PATH_INFO']

        query_string = environ['QUERY_STRING']
        if query_string:
            path_info += '?' + query_string

        host_uri = self.extract_uri(environ)
        uri = host_uri+path_info

        new_headers = {}
        for k, v in environ.items():
            if k.startswith('HTTP_'):
                k = k[5:].replace('_', '-').title()
                new_headers[k] = v

        for k, v in (('CONTENT_TYPE', None), ('CONTENT_LENGTH', '0')):
            v = environ.get(k, None)
            if v is not None:
                new_headers[k.replace('_', '-').title()] = v

        if new_headers.get('Content-Length', '0') == '-1':
            raise ValueError(WEBOB_ERROR)

        response = request(uri, method,
                           body=environ['wsgi.input'], headers=new_headers,
                           pool_instance=self.pool)

        if 'location' in response:
            headers = []
            for k, v in response.headerslist:
                if k == 'Location':
                    # rewrite location with a relative path. dont want to deal
                    # with complex url rebuild stuff
                    if v.startswith(host_uri):
                        v = v[len(host_uri):]
                    if self.strip_script_name:
                        v = environ['SCRIPT_NAME'] + v
                    headers.append((k, v))
        else:
            headers = response.headerslist

        start_response(response.status, headers)

        if 'content-length' in response and \
                int(response['content-length']) <= MAX_BODY:
            return [response.body]

        return ResponseIter(response)
Beispiel #3
0
            except ValueError:
                pass


    try:
        if len(args) == 2:
            if args[1] == "-" and not opts.input:
                body = sys.stdin.read()
                headers.append(("Content-Length", str(len(body))))

        if not opts.method and opts.input:
            method = 'POST'
        else:
            method=opts.method.upper()
            
        resp = request(args[0], method=method, body=body,
                    headers=headers, follow_redirect=opts.follow_redirect)
                        
        if opts.output and opts.output != '-':
            with open(opts.output, 'wb') as f:
                if opts.server_response:
                    f.write("Server response from %s:\n" % resp.final_url)
                    for k, v in resp.headerslist:
                        f.write( "%s: %s" % (k, v))
                else:
                    with resp.body_stream() as body:
                        for block in body:
                            f.write(block)
        else:
            if opts.server_response:
                if opts.prettify:
                    print "\n\033[0m\033[95mServer response from %s:\n\033[0m" % (