def test_comma_list1(): p = rfc7230.comma_list1(rfc7230.token) no_parse(p, b'') no_parse(p, b' \t ') no_parse(p, b' , ,, , ,') assert parse(p, b'foo') == [u'foo'] assert parse(p, b'foo,bar') == [u'foo', u'bar'] assert parse(p, b'foo, bar,') == [u'foo', u'bar'] assert parse(p, b', ,,,foo, ,bar, baz, ,, ,') == [u'foo', u'bar', u'baz'] no_parse(p, b'foo,"bar"') no_parse(p, b'foo;bar')
def test_comma_list1(): p = rfc7230.comma_list1(rfc7230.token) no_parse(p, b'') no_parse(p, b' \t ') no_parse(p, b' , ,, , ,') assert parse(p, b'foo') == [u'foo'] assert parse(p, b'foo,bar') == [u'foo', u'bar'] assert parse(p, b'foo, bar,') == [u'foo', u'bar'] assert parse(p, b', ,,,foo, ,bar, baz, ,, ,') == [u'foo', u'bar', u'baz'] no_parse(p, b'foo,"bar"') no_parse(p, b'foo;bar')
string, subst, ) from httpolice.structure import EntityTag from httpolice.syntax.common import DQUOTE from httpolice.syntax.rfc7230 import comma_list1, obs_text from httpolice.syntax.rfc7231 import HTTP_date weak = subst(True) << octet(0x57) * octet(0x2F) > auto etagc = octet(0x21) | octet_range(0x23, 0x7E) | obs_text > auto @can_complain def _no_backslashes(complain, s): if u'\\' in s: complain(1119) return s opaque_tag = _no_backslashes << DQUOTE + string(etagc) + DQUOTE > auto entity_tag = EntityTag << maybe(weak, False) * opaque_tag > pivot ETag = entity_tag > pivot Last_Modified = HTTP_date > pivot If_Match = '*' | comma_list1(entity_tag) > pivot If_None_Match = '*' | comma_list1(entity_tag) > pivot If_Modified_Since = HTTP_date > pivot If_Unmodified_Since = HTTP_date > pivot fill_names(globals(), RFC(7232))
from httpolice.citation import RFC from httpolice.parse import fill_names, pivot from httpolice.syntax.rfc7230 import comma_list1 from httpolice.syntax.rfc7231 import media_type Accept_Patch = comma_list1(media_type) > pivot fill_names(globals(), RFC(5789))
from httpolice.parse import (auto, can_complain, fill_names, maybe, octet, octet_range, pivot, string, subst) from httpolice.structure import EntityTag from httpolice.syntax.common import DQUOTE from httpolice.syntax.rfc7230 import comma_list1, obs_text from httpolice.syntax.rfc7231 import HTTP_date weak = subst(True) << octet(0x57) * octet(0x2F) > auto etagc = octet(0x21) | octet_range(0x23, 0x7E) | obs_text > auto @can_complain def _no_backslashes(complain, s): if u'\\' in s: complain(1119) return s opaque_tag = _no_backslashes << DQUOTE + string(etagc) + DQUOTE > auto entity_tag = EntityTag << maybe(weak, False) * opaque_tag > pivot ETag = entity_tag > pivot Last_Modified = HTTP_date > pivot If_Match = '*' | comma_list1(entity_tag) > pivot If_None_Match = '*' | comma_list1(entity_tag) > pivot If_Modified_Since = HTTP_date > pivot If_Unmodified_Since = HTTP_date > pivot fill_names(globals(), RFC(7232))
from httpolice.citation import RFC from httpolice.parse import (auto, can_complain, fill_names, literal, maybe, pivot, skip, string, string1, subst) from httpolice.structure import (CaseInsensitive, ContentRange, RangeSpecifier, RangeUnit) from httpolice.syntax.common import CHAR, DIGIT, SP, VCHAR from httpolice.syntax.rfc7230 import comma_list1, token__excluding from httpolice.syntax.rfc7231 import HTTP_date from httpolice.syntax.rfc7232 import entity_tag bytes_unit = RangeUnit << literal('bytes') > auto other_range_unit = RangeUnit << token__excluding(['bytes']) > auto range_unit = bytes_unit | other_range_unit > pivot acceptable_ranges = (CaseInsensitive << literal('none') | comma_list1(range_unit)) > pivot Accept_Ranges = acceptable_ranges > pivot @can_complain def _well_formed1(complain, first, last): if (last is not None) and (first > last): complain(1133) return (first, last) first_byte_pos = int << string1(DIGIT) > auto last_byte_pos = int << string1(DIGIT) > auto byte_range_spec = _well_formed1 << (first_byte_pos * skip('-') * maybe(last_byte_pos)) > pivot suffix_length = int << string1(DIGIT) > auto
weight = skip(OWS * ';' * OWS * 'q=') * qvalue > pivot accept_ext = (skip(OWS * ';' * OWS) * token * maybe(skip('=') * (token | quoted_string))) > pivot def _prepend_q(q, xs): return MultiDict([(CaseInsensitive(u'q'), q)] + xs) accept_params = _prepend_q << weight * many(accept_ext) > pivot Accept = comma_list(Parametrized << ( media_range(no_q=True) * maybe(accept_params, MultiDict()))) > pivot charset = Charset << token > pivot Accept_Charset = comma_list1(Parametrized << ( (charset | Charset << literal('*')) * maybe(weight))) > pivot codings = (content_coding | ContentCoding << literal('identity') | literal('*')) > pivot Accept_Encoding = comma_list(Parametrized << codings * maybe(weight)) > pivot Accept_Language = comma_list1(Parametrized << language_range * maybe(weight)) > pivot delay_seconds = int << string1(DIGIT) > pivot Retry_After = HTTP_date | delay_seconds > pivot Allow = comma_list(method) > pivot Content_Encoding = comma_list1(content_coding) > pivot Content_Language = comma_list1(language_tag) > pivot Content_Location = absolute_URI | partial_URI > pivot
from httpolice.syntax.common import ALPHA, DIGIT from httpolice.syntax.rfc3986 import IPv4address, IPv6address from httpolice.syntax.rfc7230 import comma_list1, quoted_string, token def _remove_empty(xs): return [x for x in xs if x is not None] obfnode = '_' + string1(ALPHA | DIGIT | '.' | '_' | '-') > pivot nodename = (IPv4address | skip('[') * IPv6address * skip(']') | 'unknown' | obfnode) > pivot port = int << string_times(1, 5, DIGIT) > pivot obfport = '_' + string1(ALPHA | DIGIT | '.' | '_' | '-') > pivot node_port = port | obfport > pivot node = nodename * maybe(skip(':') * node_port) > pivot value = token | quoted_string > pivot forwarded_pair = (ForwardedParam << token) * skip('=') * value > pivot forwarded_element = _remove_empty << ( maybe(forwarded_pair) % many(skip(';') * maybe(forwarded_pair))) > pivot Forwarded = comma_list1(forwarded_element) > pivot fill_names(globals(), RFC(7239))
weight = skip(OWS * ';' * OWS * 'q=') * qvalue > pivot accept_ext = (skip(OWS * ';' * OWS) * token * maybe(skip('=') * (token | quoted_string))) > pivot def _prepend_q(q, xs): return MultiDict([(CaseInsensitive(u'q'), q)] + xs) accept_params = _prepend_q << weight * many(accept_ext) > pivot Accept = comma_list( Parametrized << (media_range(no_q=True) * maybe(accept_params, MultiDict()))) > pivot charset = Charset << token > pivot Accept_Charset = comma_list1( Parametrized << ((charset | Charset << literal('*')) * maybe(weight))) > pivot codings = (content_coding | ContentCoding << literal('identity') | literal('*')) > pivot Accept_Encoding = comma_list(Parametrized << codings * maybe(weight)) > pivot Accept_Language = comma_list1( Parametrized << language_range * maybe(weight)) > pivot delay_seconds = int << string1(DIGIT) > pivot Retry_After = HTTP_date | delay_seconds > pivot Allow = comma_list(method) > pivot Content_Encoding = comma_list1(content_coding) > pivot
string_times) from httpolice.structure import ForwardedParam from httpolice.syntax.common import ALPHA, DIGIT from httpolice.syntax.rfc3986 import IPv4address, IPv6address from httpolice.syntax.rfc7230 import comma_list1, quoted_string, token def _remove_empty(xs): return [x for x in xs if x is not None] obfnode = '_' + string1(ALPHA | DIGIT | '.' | '_' | '-') > pivot nodename = (IPv4address | skip('[') * IPv6address * skip(']') | 'unknown' | obfnode) > pivot port = int << string_times(1, 5, DIGIT) > pivot obfport = '_' + string1(ALPHA | DIGIT | '.' | '_' | '-') > pivot node_port = port | obfport > pivot node = nodename * maybe(skip(':') * node_port) > pivot value = token | quoted_string > pivot forwarded_pair = (ForwardedParam << token) * skip('=') * value > pivot forwarded_element = _remove_empty << ( maybe(forwarded_pair) % many(skip(';') * maybe(forwarded_pair))) > pivot Forwarded = comma_list1(forwarded_element) > pivot fill_names(globals(), RFC(7239))
named, pivot, skip, string1, string_times) from httpolice.structure import (CacheDirective, CaseInsensitive, Parametrized, WarnCode, WarningValue) from httpolice.syntax.common import DIGIT, DQUOTE, SP from httpolice.syntax.rfc7230 import (comma_list, comma_list1, field_name, port, pseudonym, quoted_string, token, token__excluding, uri_host) from httpolice.syntax.rfc7231 import HTTP_date delta_seconds = int << string1(DIGIT) > pivot Age = delta_seconds > pivot cache_directive = Parametrized << ( (CacheDirective << token) * maybe(skip('=') * (mark(token) | mark(quoted_string)))) > pivot Cache_Control = comma_list1(cache_directive) > pivot # RFC 7234 does not, strictly speaking, define these productions: no_cache = comma_list(field_name) > pivot private = comma_list(field_name) > pivot Expires = HTTP_date > pivot def extension_pragma(exclude_no_cache=False): return Parametrized << ( (token__excluding(['no-cache']) if exclude_no_cache else token) * maybe(skip('=') * (token | quoted_string))) > named( u'extension-pragma', RFC(7234), is_pivot=True)
# RFC 7240 Section 2: "Empty or zero-length values on both # the preference token and within parameters are equivalent # to no value being specified at all." (name, value) = x if isinstance(x, tuple) else (x, None) return Parametrized(name, None if value == u'' else value) def preference_parameter(head=False): # The head (first) ``preference-parameter`` of a ``preference`` # contains the actual preference name, which we want to annotate. name_cls = Preference if head else CaseInsensitive return ( _normalize_empty_value << (parameter(name_cls=name_cls) | name_cls << token) ) > named(u'preference-parameter', RFC(7240, errata=4439), is_pivot=True) preference = Parametrized << ( preference_parameter(head=True) * many(skip(OWS * ';') * maybe(skip(OWS) * preference_parameter())) ) > named(u'preference', RFC(7240, errata=4439), is_pivot=True) Prefer = comma_list1(preference) > pivot Preference_Applied = comma_list1(preference_parameter(head=True)) > pivot return_ = CaseInsensitive << (literal('representation') | 'minimal') > pivot wait = delay_seconds > auto handling = CaseInsensitive << (literal('strict') | 'lenient') > pivot fill_names(globals(), RFC(7240))
from httpolice.citation import RFC from httpolice.parse import (auto, can_complain, fill_names, literal, maybe, pivot, skip, string, string1, subst) from httpolice.structure import ContentRange, RangeSpecifier, RangeUnit from httpolice.syntax.common import CHAR, DIGIT, SP, VCHAR from httpolice.syntax.rfc7230 import comma_list1, token__excluding from httpolice.syntax.rfc7231 import HTTP_date from httpolice.syntax.rfc7232 import entity_tag bytes_unit = RangeUnit << literal('bytes') > auto other_range_unit = RangeUnit << token__excluding(['bytes']) > auto range_unit = bytes_unit | other_range_unit > pivot acceptable_ranges = ( subst([]) << literal('none') | comma_list1(range_unit)) > pivot Accept_Ranges = acceptable_ranges > pivot @can_complain def _well_formed1(complain, first, last): if (last is not None) and (first > last): complain(1133) return (first, last) first_byte_pos = int << string1(DIGIT) > auto last_byte_pos = int << string1(DIGIT) > auto byte_range_spec = _well_formed1 << (first_byte_pos * skip('-') * maybe(last_byte_pos)) > pivot suffix_length = int << string1(DIGIT) > auto suffix_byte_range_spec = \
# -*- coding: utf-8; -*- from httpolice.citation import RFC from httpolice.parse import fill_names, pivot from httpolice.syntax.rfc7230 import comma_list1 from httpolice.syntax.rfc7231 import media_type Accept_Patch = comma_list1(media_type) > pivot fill_names(globals(), RFC(5789))
from httpolice.citation import Citation from httpolice.parse import (auto, case_sens, fill_names, literal, maybe_str, pivot) from httpolice.syntax.rfc3986 import host, port, scheme from httpolice.syntax.rfc7230 import (comma_list, comma_list1, field_name, method) from httpolice.syntax.rfc7234 import delta_seconds # WHATWG actually uses their own definitions for scheme, host, and port, # but that's a bit too far for HTTPolice, we can live with RFC 3986. origin = scheme + '://' + host + maybe_str(':' + port) > pivot origin_or_null = origin | case_sens('null') > pivot Origin = origin_or_null > pivot Access_Control_Request_Method = method > pivot Access_Control_Request_Headers = comma_list1(field_name) > pivot wildcard = literal('*') > auto Access_Control_Allow_Origin = origin_or_null | wildcard > pivot Access_Control_Allow_Credentials = case_sens('true') > pivot Access_Control_Expose_Headers = comma_list(field_name) > pivot Access_Control_Max_Age = delta_seconds > pivot Access_Control_Allow_Methods = comma_list(method) > pivot Access_Control_Allow_Headers = comma_list(field_name) > pivot X_Content_Type_Options = literal('nosniff') > pivot Cross_Origin_Resource_Policy = (case_sens('same-origin') | case_sens('same-site')) > pivot fill_names(globals(), Citation(u'WHATWG Fetch', u'https://fetch.spec.whatwg.org/'))
correct_encoded_id += pct_encode(c, safe='').upper() if encoded_id != correct_encoded_id: complain(1256, actual=encoded_id, correct=correct_encoded_id) return decoded_id protocol_id = _check_protocol_id << token > pivot @can_complain def _check_alt_authority(complain, value): return parse(value, maybe_str(uri_host) + ':' + port, complain, 1257, authority=value) alt_authority = _check_alt_authority << quoted_string > pivot alternative = protocol_id * skip('=') * alt_authority > pivot parameter = ((AltSvcParam << token) * skip('=') * (token | quoted_string)) > pivot alt_value = Parametrized << ( alternative * (MultiDict << many(skip(OWS * ';' * OWS) * parameter))) > pivot Alt_Svc = clear | comma_list1(alt_value) > pivot ma = delta_seconds > pivot persist = subst(True) << literal('1') > pivot Alt_Used = uri_host + maybe_str(':' + port) > pivot fill_names(globals(), RFC(7838))
protocol_id = _check_protocol_id << token > pivot @can_complain def _check_alt_authority(complain, value): return parse(value, maybe_str(uri_host) + ':' + port, complain, 1257, authority=value) alt_authority = _check_alt_authority << quoted_string > pivot alternative = protocol_id * skip('=') * alt_authority > pivot parameter = ((AltSvcParam << token) * skip('=') * (token | quoted_string)) > pivot alt_value = Parametrized << ( alternative * (MultiDict << many(skip(OWS * ';' * OWS) * parameter))) > pivot Alt_Svc = clear | comma_list1(alt_value) > pivot ma = delta_seconds > pivot persist = subst(True) << literal('1') > pivot Alt_Used = uri_host + maybe_str(':' + port) > pivot fill_names(globals(), RFC(7838))
pseudonym, quoted_string, token, token__excluding, uri_host, ) from httpolice.syntax.rfc7231 import HTTP_date delta_seconds = int << string1(DIGIT) > pivot Age = delta_seconds > pivot cache_directive = Parametrized << ( (CacheDirective << token) * maybe(skip('=') * (mark(token) | mark(quoted_string)))) > pivot Cache_Control = comma_list1(cache_directive) > pivot Expires = HTTP_date > pivot def extension_pragma(exclude_no_cache=False): return Parametrized << ( (token__excluding(['no-cache']) if exclude_no_cache else token) * maybe(skip('=') * (token | quoted_string)) ) > named(u'extension-pragma', RFC(7234), is_pivot=True) pragma_directive = (CaseInsensitive << literal('no-cache') | extension_pragma(exclude_no_cache=True)) > pivot Pragma = comma_list1(pragma_directive) > pivot warn_code = WarnCode << string_times(3, 3, DIGIT) > pivot warn_agent = uri_host + maybe_str(':' + port) | pseudonym > pivot
@can_complain def _check_realm(complain, k, v): (symbol, v) = v if k == u'realm' and symbol is not quoted_string: complain(1196) return (k, v) auth_param = _check_realm << ((CaseInsensitive << token) * skip(BWS * '=' * BWS) * (mark(token) | mark(quoted_string))) > pivot challenge = Parametrized << ( auth_scheme * maybe(skip(string1(SP)) * (token68 | MultiDict << comma_list(auth_param)), default=MultiDict())) > auto WWW_Authenticate = comma_list1(challenge) > pivot Proxy_Authenticate = comma_list1(challenge) > pivot credentials = Parametrized << ( auth_scheme * maybe(skip(string1(SP)) * (token68 | MultiDict << comma_list(auth_param)), default=MultiDict())) > auto Authorization = credentials > pivot Proxy_Authorization = credentials > pivot fill_names(globals(), RFC(7235))