How to re-order HTTP headers?

青春壹個敷衍的年華 提交于 2019-12-06 02:01:29

For the record, the order of the HTTP headers should not matter at all according to RFC 7230. But now that you have asked... this can be done in mitmproxy as follows:

import random

def request(context, flow):
    # flow.request.headers.fields is a tuple of (name, value) header tuples.
    h = list(flow.request.headers.fields)
    random.shuffle(h)
    flow.request.headers.fields = tuple(h)

See the mitmproxy documentation on netlib.http.Headers for more details.

There are tons of way to reorder them as you wish:

def reorder(headers, header_order=["Host","User-Agent","Accept"]):
    lines = []
    for name in header_order:  # add existing headers in the specified order
        if name in headers:
            lines.extend(headers.get_all(name))
            del headers[name]
    lines.extend(headers.fields)  # all other headers
    return lines
request.headers.fields = reorder(request.headers)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!