[pyramid] How is request.headers when multiple same headers

362 views
Skip to first unread message

Jimmy Thrasibule

unread,
Nov 27, 2014, 7:29:16 AM11/27/14
to pylons-...@googlegroups.com
Hello,

I'd like to run over the request headers and concatenate all headers with the same name in on string where each value is separated by a comma. For example:

    Host: example.com
    Content-Type: text/html
    Custom-Header: value1
    Custom-Header: value2
    Custom-Header: value3

I'd like from this have a dictionary in the following form:

    {
        'host': 'example.com',
        'content-type': 'text/html',
        'custom-header': 'value1, value2, value3',
    }

So I wonder how multiple headers with the same name are represented within Pyramid? Is it a MultiDict or a list of values?

Regards,
Jimmy

Tres Seaver

unread,
Nov 29, 2014, 4:57:02 PM11/29/14
to pylons-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Pyramid's request derives from WebOb's request, and uses its
EnvironHeaders. That class has the following docstring:

"""An object that represents the headers as present in a
WSGI environment.

This object is a wrapper (with no internal state) for a WSGI
request object, representing the CGI-style HTTP_* keys as a
dictionary. Because a CGI environment can only hold one value for
each key, this dictionary is single-valued (unlike outgoing
headers).
"""

So, you don't get access to the "raw" values, only what your front-end
WSGI server morphs into the CGI environment. As it happens, waitress
does exactly what you want, in conformance with RFC 2616[1], which says:

Multiple message-header fields with the same field-name MAY be
present in a message if and only if the entire field-value for that
header field is defined as a comma-separated list [i.e., #(values)].
It MUST be possible to combine the multiple header fields into one
"field-name: field-value" pair, without changing the semantics of
the message, by appending each subsequent field-value to the first,
each separated by a comma. The order in which header fields with the
same field-name are received is therefore significant to the
interpretation of the combined field value, and thus a proxy MUST
NOT change the order of these field values when a message is
forwarded.

The CGI spec[2] says:

If multiple headers with the same field-name are received then they
must be rewritten as a single header having the same semantics.
Similarly, a header that is received on more than one line must be
merged onto a single line. The server must, if necessary, change
the representation of the data (for example, the character set) to
be appropriate for a CGI environment variable.


[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2

[2] http://tools.ietf.org/html/draft-robinson-www-interface-00



Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 tse...@palladion.com
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlR6QRoACgkQ+gerLs4ltQ40aQCgqNocth024ptXPvMbaVGtCjn9
JpIAnRwYPrvI+KGsgy2K9TftqVwrgZmo
=/2Lg
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages