OPSI.web2.http_headers module

class OPSI.web2.http_headers.Cookie(name, value, path=None, domain=None, ports=None, expires=None, discard=False, secure=False, comment=None, commenturl=None, version=0)

Bases: object

class OPSI.web2.http_headers.ETag(tag, weak=False)

Bases: object

generate()
match(other, strongCompare)
static parse(tokens)
class OPSI.web2.http_headers.HeaderHandler(parsers=None, generators=None)

Bases: object

HeaderHandler manages header generating and parsing functions.

HTTPGenerators = {'content-length': (<type 'str'>, <function singleHeader>), 'accept-language': (<function <lambda>>, <function listGeneratorHelper>, <function singleHeader>), 'vary': (<function generateList>, <function singleHeader>), 'accept': (<function <lambda>>, <function listGeneratorHelper>, <function singleHeader>), 'retry-after': (<function generateRetryAfter>, <function singleHeader>), 'accept-charset': (<function <lambda>>, <function listGeneratorHelper>, <function singleHeader>), 'content-range': (<function generateContentRange>, <function singleHeader>), 'expect': (<function <lambda>>, <function listGeneratorHelper>, <function singleHeader>), 'if-none-match': (<function listGeneratorHelper>, <function singleHeader>), 'overwrite': (), 'depth': <function singleHeader>, 'from': (<type 'str'>, <function singleHeader>), 'content-language': (<function generateList>, <function singleHeader>), 'destination': <function singleHeader>, 'connection': (<function generateList>, <function singleHeader>), 'user-agent': (<type 'str'>, <function singleHeader>), 'set-cookie2': (<function generateSetCookie2>,), 'if-range': (<function generateIfRange>, <function singleHeader>), 'etag': (<unbound method ETag.generate>, <function singleHeader>), 'location': (<type 'str'>, <function singleHeader>), 'cache-control': (<function <lambda>>, <function listGeneratorHelper>, <function singleHeader>), 'te': (<function <lambda>>, <function listGeneratorHelper>, <function singleHeader>), 'if-unmodified-since': (<function generateDateTime>, <function singleHeader>), 'www-authenticate': (<function generateWWWAuthenticate>,), 'authorization': (<function generateAuthorization>,), 'if-match': (<function listGeneratorHelper>, <function singleHeader>), 'accept-encoding': (<function <lambda>>, <function removeDefaultEncoding>, <function listGeneratorHelper>, <function singleHeader>), 'transfer-encoding': (<function generateList>, <function singleHeader>), 'set-cookie': (<function generateSetCookie>,), 'accept-ranges': (<function generateList>, <function singleHeader>), 'expires': (<function generateDateTime>, <function singleHeader>), 'content-md5': (<function encodestring>, <function <lambda>>, <function singleHeader>), 'last-modified': (<function generateDateTime>, <function singleHeader>), 'host': (<type 'str'>, <function singleHeader>), 'cookie': (<function generateCookie>, <function singleHeader>), 'if-modified-since': (<function generateDateTime>, <function singleHeader>), 'date': (<function generateDateTime>, <function singleHeader>), 'max-forwards': (<type 'str'>, <function singleHeader>), 'content-location': (<type 'str'>, <function singleHeader>), 'content-encoding': (<function generateList>, <function singleHeader>), 'age': (<type 'str'>, <function singleHeader>), 'dav': (<function generateList>, <function singleHeader>), 'server': (<type 'str'>, <function singleHeader>), 'range': (<function generateRange>, <function singleHeader>), 'allow': (<function generateList>, <function singleHeader>), 'referer': (<type 'str'>, <function singleHeader>), 'content-type': (<function generateContentType>, <function singleHeader>)}
HTTPParsers = {'content-length': (<function last>, <type 'int'>), 'accept-language': (<function tokenize>, <function listParserHelper>, <type 'dict'>), 'vary': (<function tokenize>, <function filterTokens>), 'accept': (<function tokenize>, <function listParserHelper>, <type 'dict'>), 'retry-after': (<function last>, <function parseRetryAfter>), 'accept-charset': (<function tokenize>, <function listParserHelper>, <type 'dict'>, <function addDefaultCharset>), 'content-range': (<function last>, <function parseContentRange>), 'expect': (<function tokenize>, <function listParserHelper>, <type 'dict'>), 'if-none-match': (<function tokenize>, <function listParserHelper>, <type 'list'>), 'overwrite': (<function last>, <function parseOverWrite>), 'depth': (<function last>, <function parseDepth>), 'from': (<function last>,), 'content-language': (<function tokenize>, <function filterTokens>), 'destination': (<function last>,), 'connection': (<function tokenize>, <function filterTokens>), 'user-agent': (<function last>, <type 'str'>), 'set-cookie2': (<function tokenize>, <function parseSetCookie2>), 'if-range': (<function parseIfRange>,), 'etag': (<function tokenize>, <function parse>), 'location': (<function last>,), 'cache-control': (<function tokenize>, <function listParserHelper>, <type 'dict'>), 'te': (<function tokenize>, <function listParserHelper>, <type 'dict'>), 'if-unmodified-since': (<function last>, <function parseDateTime>), 'www-authenticate': (<function <lambda>>, <function parseWWWAuthenticate>), 'authorization': (<function last>, <function parseAuthorization>), 'if-match': (<function tokenize>, <function listParserHelper>, <type 'list'>), 'accept-encoding': (<function tokenize>, <function listParserHelper>, <type 'dict'>, <function addDefaultEncoding>), 'transfer-encoding': (<function tokenize>, <function filterTokens>), 'set-cookie': (<function parseSetCookie>,), 'accept-ranges': (<function tokenize>, <function filterTokens>), 'expires': (<function last>, <function parseExpires>), 'content-md5': (<function last>, <function parseContentMD5>), 'last-modified': (<function last>, <function parseDateTime>), 'host': (<function last>,), 'cookie': (<function parseCookie>,), 'if-modified-since': (<function last>, <function parseIfModifiedSince>), 'date': (<function last>, <function parseDateTime>), 'max-forwards': (<function last>, <type 'int'>), 'content-location': (<function last>,), 'content-encoding': (<function tokenize>, <function filterTokens>), 'age': (<function last>, <type 'int'>), 'dav': (<function tokenize>, <type 'list'>), 'server': (<function last>,), 'range': (<function tokenize>, <function parseRange>), 'allow': (<function <lambda>>, <function filterTokens>), 'referer': (<function last>, <type 'str'>), 'content-type': (<function <lambda>>, <function parseContentType>)}
addGenerators(name, value)

Add an individual generator chain for the given header.

@param name: Name of the header to add @type name: C{str}

@param value: The generator chain @type value: C{str}

addParser(name, value)

Add an individual parser chain for the given header.

@param name: Name of the header to add @type name: C{str}

@param value: The parser chain @type value: C{str}

generate(name, header)

Generate the given header based on its given name.

@param name: The header name to generate. @type name: C{str}

@param header: A parsed header, such as the output of
L{HeaderHandler}.parse.

@return: C{list} of C{str} each representing a generated HTTP header.

parse(name, header)

Parse the given header based on its given name.

@param name: The header name to parse. @type name: C{str}

@param header: A list of unparsed headers. @type header: C{list} of C{str}

@return: The return value is the parsed header representation,
it is dependent on the header. See the HTTP Headers document.
update(parsers, generators)

Conveniently update parsers and generators all at once.

updateGenerators(generators)

Update en masse the generator maps.

@param parsers: Map of header names to generator chains. @type parsers: C{dict}

updateParsers(parsers)

Update en masse the parser maps.

@param parsers: Map of header names to parser chains. @type parsers: C{dict}

class OPSI.web2.http_headers.Headers(headers=None, rawHeaders=None, handler=<OPSI.web2.http_headers.HeaderHandler object>)

Bases: object

This class stores the HTTP headers as both a parsed representation and the raw string representation. It converts between the two on demand.

addRawHeader(name, value)

Add a raw value to a header that may or may not already exist. If it exists, add it as a separate header to output; do not replace anything.

canonicalNameCaps(name)

Return the name with the canonical capitalization, if known, otherwise, Caps-After-Dashes

getAllRawHeaders()

Return an iterator of key,value pairs of all headers contained in this object, as strings. The keys are capitalized in canonical capitalization.

getHeader(name, default=None)

Ret9urns the parsed representation of the given header. The exact form of the return value depends on the header in question.

If no parser for the header exists, raise ValueError.

If the header doesn’t exist, return default (or None if not specified)

getRawHeaders(name, default=None)

Returns a list of headers matching the given name as the raw string given.

hasHeader(name)

Does a header with the given name exist?

makeImmutable()

Make this header set immutable. All mutating operations will raise an exception.

removeHeader(name)

Removes the header named.

setHeader(name, value)

Sets the parsed representation of the given header. Value should be a list of objects whose exact form depends on the header in question.

setRawHeaders(name, value)

Sets the raw representation of the given header. Value should be a list of strings, each being one header of the given name.

class OPSI.web2.http_headers.MimeType(mediaType, mediaSubtype, params={}, **kwargs)

Bases: object

classmethod fromString(klass, mimeTypeString)

Generate a MimeType object from the given string.

@param mimeTypeString: The mimetype to parse

@return: L{MimeType}

class OPSI.web2.http_headers.Token

Bases: str

tokens = {}
OPSI.web2.http_headers.addDefaultCharset(charsets)
OPSI.web2.http_headers.addDefaultEncoding(encodings)
OPSI.web2.http_headers.casemappingify(d)
OPSI.web2.http_headers.checkSingleToken(tokens)
OPSI.web2.http_headers.dashCapitalize(s)

Capitalize a string, making sure to treat - as a word seperator

OPSI.web2.http_headers.filterTokens(seq)

Filter out instances of Token, leaving only a list of strings.

Used instead of a more specific parsing method (e.g. splitting on commas) when only strings are expected, so as to be a little lenient.

Apache does it this way and has some comments about broken clients which forget commas (?), so I’m doing it the same way. It shouldn’t hurt anything, in any case.

OPSI.web2.http_headers.generateAccept(accept)
OPSI.web2.http_headers.generateAcceptQvalue(keyvalue)
OPSI.web2.http_headers.generateAuthorization(seq)
OPSI.web2.http_headers.generateCacheControl()
OPSI.web2.http_headers.generateContentRange(tup)

tup is (type, start, end, len) len can be None.

OPSI.web2.http_headers.generateContentType(mimeType)
OPSI.web2.http_headers.generateCookie(cookies)
OPSI.web2.http_headers.generateDateTime(secSinceEpoch)

Convert seconds since epoch to HTTP datetime string.

OPSI.web2.http_headers.generateExpect(item)
OPSI.web2.http_headers.generateIfRange(dateOrETag)
OPSI.web2.http_headers.generateKeyValues(kvs)
OPSI.web2.http_headers.generateList(seq)
OPSI.web2.http_headers.generateOverWrite(overwrite)
OPSI.web2.http_headers.generateRange(range)
OPSI.web2.http_headers.generateRetryAfter(when)
OPSI.web2.http_headers.generateSetCookie(cookies)
OPSI.web2.http_headers.generateSetCookie2(cookies)
OPSI.web2.http_headers.generateStarOrETag(etag)
OPSI.web2.http_headers.generateWWWAuthenticate(headers)
OPSI.web2.http_headers.iteritems(x)
OPSI.web2.http_headers.last(seq)

Return seq[-1]

OPSI.web2.http_headers.listGenerator(fun)

Return a function which applies ‘fun’ to every element in the given list, then joins the result with generateList

OPSI.web2.http_headers.listParser(fun)

Return a function which applies ‘fun’ to every element in the comma-separated list

OPSI.web2.http_headers.lowerify(d)
OPSI.web2.http_headers.makeCookieFromList(tup, netscapeFormat)
OPSI.web2.http_headers.parseAccept(field)
OPSI.web2.http_headers.parseAcceptQvalue(field)
OPSI.web2.http_headers.parseArgs(field)
OPSI.web2.http_headers.parseAuthorization(header)
OPSI.web2.http_headers.parseCacheControl(kv)
OPSI.web2.http_headers.parseContentMD5(header)
OPSI.web2.http_headers.parseContentRange(header)

Parse a content-range header into (kind, start, end, realLength).

realLength might be None if real length is not known (‘*’). start and end might be None if start,end unspecified (for response code 416)

OPSI.web2.http_headers.parseContentType(header)
OPSI.web2.http_headers.parseCookie(headers)

Bleargh, the cookie spec sucks. This surely needs interoperability testing. There are two specs that are supported: Version 0) http://wp.netscape.com/newsref/std/cookie_spec.html Version 1) http://www.faqs.org/rfcs/rfc2965.html

OPSI.web2.http_headers.parseDateTime(dateString)

Convert an HTTP date string (one of three formats) to seconds since epoch.

OPSI.web2.http_headers.parseDepth(depth)
OPSI.web2.http_headers.parseExpect(field)
OPSI.web2.http_headers.parseExpires(header)
OPSI.web2.http_headers.parseIfModifiedSince(header)
OPSI.web2.http_headers.parseIfRange(headers)
OPSI.web2.http_headers.parseKeyValue(val)
OPSI.web2.http_headers.parseOverWrite(overwrite)
OPSI.web2.http_headers.parseRange(range)
OPSI.web2.http_headers.parseRetryAfter(header)
OPSI.web2.http_headers.parseSetCookie(headers)
OPSI.web2.http_headers.parseSetCookie2(toks)
OPSI.web2.http_headers.parseStarOrETag(tokens)
OPSI.web2.http_headers.parseWWWAuthenticate(tokenized)
OPSI.web2.http_headers.quoteString(s)
OPSI.web2.http_headers.removeDefaultEncoding(seq)
OPSI.web2.http_headers.singleHeader(item)
OPSI.web2.http_headers.split(seq, delim)

The same as str.split but works on arbitrary sequences. Too bad it’s not builtin to python!

OPSI.web2.http_headers.tokenize(header, foldCase=True)

Tokenize a string according to normal HTTP header parsing rules.

In particular:
  • Whitespace is irrelevant and eaten next to special separator tokens. Its existance (but not amount) is important between character strings.

  • Quoted string support including embedded backslashes.

  • Case is insignificant (and thus lowercased), except in quoted strings.

    (unless foldCase=False)

  • Multiple headers are concatenated with ‘,’

NOTE: not all headers can be parsed with this function.

Takes a raw header value (list of strings), and Returns a generator of strings and Token class instances.