Parse a raw HTTP request with Werkzeug

I am writing fuzzer for a Flask application. I have examples of requests stored as text files, for example, get.txt:

GET /docs/index.html HTTP/1.1
Host: www.w3.org

Ideally, I would parse this with an object werkzeug.wrappers.Request, something like this (psuedo-code):

from werkzeug.wrappers import Request

req = Request()

with open('get.txt') as f:
    req.parse_raw(f.read())

However, it seems that raw HTTP analysis does not occur in Werkzeug. Instead, Werkzeug takes the WSGI environment from BaseHTTPServer.BaseHTTPRequestHandler , and it requires BaseHTTPServer.HTTPServer to parse the request. This seems to be too complicated.

I also met http-parser , which is closer to what I want, but it duplicates most Werkzeug data structures with incompatible types. I would have to convert data from one to another.

HTTP- WSGI- Werkzeug ( BaseHTTPRequestHandler HTTP-)?

+4
1

, Werkzeug-Raw HTTP- WSGI ( ).

:

from flask import Flask, request
import werkzeug_raw

app = Flask(__name__)

environ = werkzeug_raw.environ('GET /foo/bar?tequila=42 HTTP/1.1')

with app.request_context(environ):
    print request.args  # ImmutableMultiDict([('tequila', u'42')])

HTTP- :

client = app.test_client()
rv = werkzeug_raw.open(client, 'GET /foo/bar?tequila=42 HTTP/1.1')
+3

Source: https://habr.com/ru/post/1612503/


All Articles