The function drupal_http_request()
adds 'Content-Length' header even if there is no content in the request. Even if this should be ok, some web servers seem to have problems with it. For example, you cannot use aggregator to fetch feeds from http://www.yle.fi/uutiset/rss/ymparisto.xml .
The following patch makes 'Content-Length' header go away when there's no content in the request.
The patch command to run is: (run it in your drupal base dir)
patch -Np1 <drupal_http_request_content_length_0.patch
Comments
Comment #1
skiminki CreditAttribution: skiminki commentedComment #2
Damien Tournoud CreditAttribution: Damien Tournoud commentedThe patch looks good, but bugs have to be fixed in the current development version (7.x) first, than backported. Can you reroll a patch for 7.x?
Comment #3
casperbiering CreditAttribution: casperbiering commentedHere is an updated patch which should work for 7.x-dev.
Comment #4
lilou CreditAttribution: lilou commentedComment #5
c960657 CreditAttribution: c960657 commentedSome servers (e.g. Squid that is often used in a reverse proxy scenario) seems to require the Content-Length header for POST and PUT requests, even if the message body is empty. You can verify this by sending the following request to drupal.org:80 using a telnet client - the server responds with 411 Length Required:
I suggest that Content-Length is also added if the request method is POST or PUT, even if $options['data'] is empty. I assume that the problem described in this issue only occurs with GET requests.
BTW, the HTTP spec isn't very clear about whether sending a Content-Length for GET requests is allowed:
http://lists.w3.org/Archives/Public/ietf-http-wg/2006AprJun/0103.html
Comment #6
casperbiering CreditAttribution: casperbiering commentedIn my case (http://johannes.hansen.name/) it is at least HEAD and GET requests. POST and PUT works fine for me with zero-length content.
Comment #8
casperbiering CreditAttribution: casperbiering commentedPatch contained "incorrect" file path (i believe)
Comment #9
c960657 CreditAttribution: c960657 commentedI tested this, and it works.
One teeny-weeny nit: Most short strings in Drupal use single quotes (unless they contain escaped characters like \n etc.), so for consistency I suggest you use that.
Comment #11
c960657 CreditAttribution: c960657 commentedReroll.
Comment #13
c960657 CreditAttribution: c960657 commentedComment #14
Dries CreditAttribution: Dries commentedTwo possible micro optimizations:
1. It is not clear whether strlen() is a fast operation (e.g. the length is cached) or a slow operation (e.g. we need to count all characters in the string), but we are calling it twice now on the same string.
2. It might be faster to check the request type before doing a strlen().
Comment #15
c960657 CreditAttribution: c960657 commentedI couldn't find a good reference, but I think that
if (!$foo)
may be slightly faster than using strlen().Comment #16
sunPlease use Content-Length consistently here.
I don't see why that needs to be multi-line. Additionally, !empty() for $options['data'] wouldn't hurt for clarity.
Comment #17
c960657 CreditAttribution: c960657 commentedThanks for the review. This patch addresses your comments.
Comment #18
Dries CreditAttribution: Dries commentedUnfortunately, this patch no longer applies. It needs a quick re-roll. I'll commit after reroll.
Comment #19
c960657 CreditAttribution: c960657 commentedReroll.
Comment #20
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks!
Comment #21
andypostIt's easy to port first to 6.x
Comment #22
c960657 CreditAttribution: c960657 commentedshould be
Comment #23
andypostHere reroll with #22 and against current DRUPAL-6
Comment #24
c960657 CreditAttribution: c960657 commentedSeems my advice in #22 was bad. Now the Content-Length header isn't added at all.
Comment #25
andypost@c960657 This was my fault, in drupal 6 array of headers holds values same as keys
Here examples
drupal_http_request('http://www.drupal.org/');
drupal_http_request('http://www.drupal.org/', array(), 'POST');
Comment #26
c960657 CreditAttribution: c960657 commentedLooks good now.
Comment #27
pwolanin CreditAttribution: pwolanin commentedthere is a possible flaw here - I think I can't GET/DELETE/etc the character '0' as the entire entity-body.
However, that's probably irrelevant to normal operation.
if we care about that (maybe not) we want something like this change instead:
Comment #28
sunbtw, also using wrong string concatenation (the patch, not pwolanin who just copied).
Comment #29
pwolanin CreditAttribution: pwolanin commented@sun - no this is the D6 backport, why is that wrong?
Comment #30
sunoopsie. sorry, I'm slow.
Comment #31
andypostPOST with "0" is never happen because it's contents always name: value
http://en.wikipedia.org/wiki/HTTP_POST
Comment #32
Damien Tournoud CreditAttribution: Damien Tournoud commentedThe POST body in the general case is free-form, so we should allow posting '0'.
Comment #33
andypostFixed as @pwolanin proposed
Comment #34
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks!
Comment #36
pwolanin CreditAttribution: pwolanin commentedneeds to be ported for D6
Comment #37
andypostLook at #33 thare's a D6 patch
Comment #38
andypostReroll
Comment #39
c960657 CreditAttribution: c960657 commentedLooks good.
Comment #40
Gábor HojtsyCommitted to Drupal 6, thanks!
Comment #41
pwolanin CreditAttribution: pwolanin commentedbackport for Drupal 5? http://api.drupal.org/api/function/drupal_http_request/5
Comment #42
marcingy CreditAttribution: marcingy commentedMarking as won't fix as d5 is end of life.