2014-04-28 23:33:30 +02:00
|
|
|
from httpie.plugins import FormatterPlugin
|
2014-04-27 00:07:13 +02:00
|
|
|
|
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
class HeadersFormatter(FormatterPlugin):
|
2014-04-27 00:07:13 +02:00
|
|
|
|
2020-05-27 15:58:15 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.enabled = self.format_options['headers']['sort']
|
|
|
|
|
2019-08-31 19:11:46 +02:00
|
|
|
def format_headers(self, headers: str) -> str:
|
2014-04-27 00:07:13 +02:00
|
|
|
"""
|
|
|
|
Sorts headers by name while retaining relative
|
|
|
|
order of multiple headers with the same name.
|
|
|
|
|
|
|
|
"""
|
|
|
|
lines = headers.splitlines()
|
|
|
|
headers = sorted(lines[1:], key=lambda h: h.split(':')[0])
|
|
|
|
return '\r\n'.join(lines[:1] + headers)
|