Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HTTP Header Capitalization in urllib.request.AbstractHTTPHandler (Python 3.3)

459 views
Skip to first unread message

Logan Owen

unread,
Nov 18, 2013, 9:25:02 AM11/18/13
to pytho...@python.org
Hello everyone,

I was hoping for some advice in dealing with an edge case related to
Python's HTTP Header handling. Python is correctly assuming that the
HTTP header field name (eg Content-Type) is case insensitive, but I have
a webservice I am interacting with that does not follow the standards.
I am passing in the header "SOAPAction" and do_request of
AbstractHTTPHandler is applying title() to the string, leading to the
field name being "Soapaction", which the server does not recognize.

So my question is, what does everyone suggest is the best solution for
being able to pass a case sensitive header to the server?

Thanks!
Logan

Chris Angelico

unread,
Nov 19, 2013, 2:11:04 AM11/19/13
to pytho...@python.org
So what you're asking is: How can Python be made less standards-compliant?

Well! You may be in luck. The evil part of my brain has just gone
digging for a solution. According to cpython/Lib/urllib/request.py
from Python 3.4 alpha (unlikely to be different in 3.3), the code
you're looking for is either line 1193ish:

for name, value in self.parent.addheaders:
name = name.capitalize()
if not request.has_header(name):
request.add_unredirected_header(name, value)

or line 1226:
headers = dict((name.title(), val) for name, val in headers.items())

I'm assuming you're either stuffing stuff into addheaders or passing
it headers. Well, dere's nuffink says it has to be a string...

class SOAPAction:
def capitalize(self):
return "SOAPAction"
title = capitalize

Just use SOAPAction() instead of "SOAPAction" as your key, and TYAOOYDAO!

ChrisA

[1] "there you are, out of your difficulty at once" - cf WS Gilbert's "Iolanthe"

Logan

unread,
Nov 19, 2013, 9:14:55 PM11/19/13
to pytho...@python.org
Chris,

That is genius. Thank you!

-- Logan

Chris Angelico

unread,
Nov 20, 2013, 2:05:48 AM11/20/13
to pytho...@python.org
On Wed, Nov 20, 2013 at 1:14 PM, Logan <lo...@s1network.com> wrote:
> Chris,
>
> That is genius. Thank you!

Then it works? Awesome!! (Permit me an evil laugh. Muahahahaaaa!)

This is why I love working with open source languages. Even if you
don't end up actually changing anything, you can go and snoop the code
and see what happens - sometimes you can tweak your code based on that
knowledge. And hey. This is duck typing at its best!

ChrisA

Logan

unread,
Nov 20, 2013, 6:26:01 PM11/20/13
to pytho...@python.org
Not exactly as written, but close enough to get me working.  At one point the following code is executed, turning the value into a string to be  "title"d next time it is called:
    name = name.title()

So, I worked around it with the following class, adapted from yours:

class CaseSensitiveHeader(object):
    def __init__(self, name):
        self.name = name

    def capitalize(self):
        return self

    def title(self):
        return self

    def lower(self):
        return self.name

    def encode(self, encoding):
        return self.name.encode(encoding)

With that, I am now able to post a case sensitive HTTP header.

-- Logan

Chris Angelico

unread,
Nov 20, 2013, 7:49:49 PM11/20/13
to pytho...@python.org
On Thu, Nov 21, 2013 at 10:26 AM, Logan <lo...@s1network.com> wrote:
> Not exactly as written, but close enough to get me working.

Excellent. Sometimes it's fun to be just that evil. :)

ChrisA
0 new messages