2021-12-19 11:41:42 +01:00
|
|
|
import re
|
2021-09-21 11:15:43 +02:00
|
|
|
import pygments
|
2021-12-23 21:13:25 +01:00
|
|
|
from httpie.output.lexers.common import precise
|
2021-09-21 11:15:43 +02:00
|
|
|
|
2022-03-03 17:28:04 +01:00
|
|
|
RE_STATUS_LINE = re.compile(r'(\d{3})( +)?(.+)?')
|
2021-12-19 11:41:42 +01:00
|
|
|
|
|
|
|
STATUS_TYPES = {
|
|
|
|
'1': pygments.token.Number.HTTP.INFO,
|
|
|
|
'2': pygments.token.Number.HTTP.OK,
|
|
|
|
'3': pygments.token.Number.HTTP.REDIRECT,
|
|
|
|
'4': pygments.token.Number.HTTP.CLIENT_ERR,
|
|
|
|
'5': pygments.token.Number.HTTP.SERVER_ERR,
|
|
|
|
}
|
|
|
|
|
|
|
|
RESPONSE_TYPES = {
|
|
|
|
'GET': pygments.token.Name.Function.HTTP.GET,
|
|
|
|
'HEAD': pygments.token.Name.Function.HTTP.HEAD,
|
|
|
|
'POST': pygments.token.Name.Function.HTTP.POST,
|
|
|
|
'PUT': pygments.token.Name.Function.HTTP.PUT,
|
|
|
|
'PATCH': pygments.token.Name.Function.HTTP.PATCH,
|
|
|
|
'DELETE': pygments.token.Name.Function.HTTP.DELETE,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def http_response_type(lexer, match, ctx):
|
|
|
|
status_match = RE_STATUS_LINE.match(match.group())
|
|
|
|
if status_match is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
status_code, text, reason = status_match.groups()
|
|
|
|
status_type = precise(
|
|
|
|
lexer,
|
|
|
|
STATUS_TYPES.get(status_code[0]),
|
|
|
|
pygments.token.Number
|
|
|
|
)
|
|
|
|
|
|
|
|
groups = pygments.lexer.bygroups(
|
|
|
|
status_type,
|
|
|
|
pygments.token.Text,
|
|
|
|
status_type
|
|
|
|
)
|
|
|
|
yield from groups(lexer, status_match, ctx)
|
|
|
|
|
|
|
|
|
|
|
|
def request_method(lexer, match, ctx):
|
|
|
|
response_type = precise(
|
|
|
|
lexer,
|
|
|
|
RESPONSE_TYPES.get(match.group()),
|
|
|
|
pygments.token.Name.Function
|
|
|
|
)
|
|
|
|
yield match.start(), response_type, match.group()
|
|
|
|
|
|
|
|
|
2021-09-21 11:15:43 +02:00
|
|
|
class SimplifiedHTTPLexer(pygments.lexer.RegexLexer):
|
|
|
|
"""Simplified HTTP lexer for Pygments.
|
|
|
|
|
|
|
|
It only operates on headers and provides a stronger contrast between
|
|
|
|
their names and values than the original one bundled with Pygments
|
|
|
|
(:class:`pygments.lexers.text import HttpLexer`), especially when
|
|
|
|
Solarized color scheme is used.
|
|
|
|
|
|
|
|
"""
|
|
|
|
name = 'HTTP'
|
|
|
|
aliases = ['http']
|
|
|
|
filenames = ['*.http']
|
|
|
|
tokens = {
|
|
|
|
'root': [
|
|
|
|
# Request-Line
|
|
|
|
(r'([A-Z]+)( +)([^ ]+)( +)(HTTP)(/)(\d+\.\d+)',
|
|
|
|
pygments.lexer.bygroups(
|
2021-12-19 11:41:42 +01:00
|
|
|
request_method,
|
2021-09-21 11:15:43 +02:00
|
|
|
pygments.token.Text,
|
|
|
|
pygments.token.Name.Namespace,
|
|
|
|
pygments.token.Text,
|
|
|
|
pygments.token.Keyword.Reserved,
|
|
|
|
pygments.token.Operator,
|
|
|
|
pygments.token.Number
|
|
|
|
)),
|
|
|
|
# Response Status-Line
|
2021-12-19 11:41:42 +01:00
|
|
|
(r'(HTTP)(/)(\d+\.\d+)( +)(.+)',
|
2021-09-21 11:15:43 +02:00
|
|
|
pygments.lexer.bygroups(
|
|
|
|
pygments.token.Keyword.Reserved, # 'HTTP'
|
|
|
|
pygments.token.Operator, # '/'
|
|
|
|
pygments.token.Number, # Version
|
|
|
|
pygments.token.Text,
|
2021-12-19 11:41:42 +01:00
|
|
|
http_response_type, # Status code and Reason
|
2021-09-21 11:15:43 +02:00
|
|
|
)),
|
|
|
|
# Header
|
|
|
|
(r'(.*?)( *)(:)( *)(.+)', pygments.lexer.bygroups(
|
|
|
|
pygments.token.Name.Attribute, # Name
|
|
|
|
pygments.token.Text,
|
|
|
|
pygments.token.Operator, # Colon
|
|
|
|
pygments.token.Text,
|
|
|
|
pygments.token.String # Value
|
|
|
|
))
|
|
|
|
]
|
|
|
|
}
|