Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseHTTPServer's send_reponse adds extra "\r\n" when using HTTPMessage in input #56648

Closed
YoavWeiss mannequin opened this issue Jun 29, 2011 · 4 comments
Closed

BaseHTTPServer's send_reponse adds extra "\r\n" when using HTTPMessage in input #56648

YoavWeiss mannequin opened this issue Jun 29, 2011 · 4 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@YoavWeiss
Copy link
Mannequin

YoavWeiss mannequin commented Jun 29, 2011

BPO 12439
Nosy @orsenthil, @merwok, @akheron

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2011-07-25.19:02:21.828>
created_at = <Date 2011-06-29.09:35:24.826>
labels = ['invalid', 'type-bug', 'library']
title = 'BaseHTTPServer\'s send_reponse adds extra "\\r\\n" when using HTTPMessage in input'
updated_at = <Date 2011-07-25.19:02:21.827>
user = 'https://bugs.python.org/YoavWeiss'

bugs.python.org fields:

activity = <Date 2011-07-25.19:02:21.827>
actor = 'petri.lehtinen'
assignee = 'none'
closed = True
closed_date = <Date 2011-07-25.19:02:21.828>
closer = 'petri.lehtinen'
components = ['Library (Lib)']
creation = <Date 2011-06-29.09:35:24.826>
creator = 'Yoav.Weiss'
dependencies = []
files = []
hgrepos = []
issue_num = 12439
keywords = []
message_count = 4.0
messages = ['139401', '139698', '139749', '139784']
nosy_count = 4.0
nosy_names = ['orsenthil', 'eric.araujo', 'petri.lehtinen', 'Yoav.Weiss']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue12439'
versions = ['Python 2.6']

@YoavWeiss
Copy link
Mannequin Author

YoavWeiss mannequin commented Jun 29, 2011

I'm using BaseHTTPServer's send_response (from within a class that inherits BaseHTTPRequestHandler) with the following:

    self.send_response(response.code, response.headers)
    self.end_headers()
    self.wfile.write(content)
    self.wfile.flush()
When response is a httplib's HTTPResponse object, and its headers inherits from rfc822.Message.

What I see is that message is printed as is, including all the headers trailing "\r\n", after which the send_response method (BaseHTTPServer.py:381) adds another "\r\n".
Then send_response adds the "Server" and "Date" headers.
Since the headers before Server & Date include "\r\n\r\n", Date & server are considered by the browser as the content.

Am I misusing BaseHTTPServer? If not, this is a bug and "\r\n" should be removed from line 381, or added only after a check that shows they are not already there at the headers end, or in case there are no input headers.

@YoavWeiss YoavWeiss mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 29, 2011
@akheron
Copy link
Member

akheron commented Jul 3, 2011

It seems to me that you're indeed misusing it.

The correct way would be something like this (assuming response is a HTTPResponse object from httplib):

self.send_response(response.status)
for name, value in response.getheaders():
    self.send_header(name, value)
self.end_headers()

This is because send_response's second argument is the HTTP's "reason" field, i.e. invoking:

    self.send_response(123, 'FOOBAR')

results in

HTTP/1.1 123 FOOBAR\r\n

to be sent, followed by "Server" and "Date" headers. The second argument is not meant to be used for sending headers.

(When the second argument is omitted, a standard reason for the given status code is used.)

@akheron akheron added the invalid label Jul 3, 2011
@YoavWeiss
Copy link
Mannequin Author

YoavWeiss mannequin commented Jul 4, 2011

Thanks for correcting me. I guess I assumed that the "message" variable is
an HTTPMessage.
Is send_response documented somewhere? I failed to find a reference.

On Sun, Jul 3, 2011 at 9:45 PM, Petri Lehtinen <report@bugs.python.org>wrote:

Petri Lehtinen <petri@digip.org> added the comment:

It seems to me that you're indeed misusing it.

The correct way would be something like this (assuming response is a
HTTPResponse object from httplib):

self.send_response(response.status)
for name, value in response.getheaders():
self.send_header(name, value)
self.end_headers()

This is because send_response's second argument is the HTTP's "reason"
field, i.e. invoking:

self.send_response(123, 'FOOBAR')

results in

HTTP/1.1 123 FOOBAR\r\n

to be sent, followed by "Server" and "Date" headers. The second argument is
not meant to be used for sending headers.

(When the second argument is omitted, a standard reason for the given
status code is used.)

----------
nosy: +petri.lehtinen
resolution: -> invalid


Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue12439\>


@merwok
Copy link
Member

merwok commented Jul 4, 2011

@akheron akheron closed this as completed Jul 25, 2011
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants