forked from extern/httpie-cli
e6c5cd3e4b
In some special cases, to prevent against Cross Site Script Inclusion (XSSI) attacks, the JSON response body starts with a magic prefix line that must be stripped before feeding the rest of the response body to the JSON parser. Such prefix is now simply ignored from the parser but still printed in the terminal. * Fix Windows tests
32 lines
852 B
Python
32 lines
852 B
Python
import re
|
|
|
|
from pygments.lexer import bygroups, using, RegexLexer
|
|
from pygments.lexers.data import JsonLexer
|
|
from pygments.token import Token
|
|
|
|
PREFIX_TOKEN = Token.Error
|
|
PREFIX_REGEX = r'[^{\["]+'
|
|
|
|
|
|
class EnhancedJsonLexer(RegexLexer):
|
|
"""
|
|
Enhanced JSON lexer for Pygments.
|
|
|
|
It adds support for eventual data prefixing the actual JSON body.
|
|
|
|
"""
|
|
name = 'JSON'
|
|
flags = re.IGNORECASE | re.DOTALL
|
|
tokens = {
|
|
'root': [
|
|
# Eventual non-JSON data prefix followed by actual JSON body.
|
|
# FIX: data prefix + number (integer or float) are not correctly handled.
|
|
(
|
|
fr'({PREFIX_REGEX})' + r'((?:[{\["]|true|false|null).+)',
|
|
bygroups(PREFIX_TOKEN, using(JsonLexer))
|
|
),
|
|
# JSON body.
|
|
(r'.+', using(JsonLexer)),
|
|
],
|
|
}
|