Bug 95981

Summary: XMLHttpRequest.setRequestHeader adds a linebreak after header
Product: [Applications] konqueror Reporter: Teofilis Martisius <teo>
Component: khtml ecmaAssignee: Konqueror Developers <konq-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: chris+kde, opensource, zack
Priority: NOR    
Version: 3.3.1   
Target Milestone: ---   
Platform: Debian testing   
OS: Linux   
Latest Commit: Version Fixed In:
Attachments: Make XMLHttpRequest set content type headers correctly for POST requests

Description Teofilis Martisius 2004-12-29 16:32:23 UTC
Version:           3.3.1 (using KDE KDE 3.3.1)
Installed from:    Debian testing/unstable Packages
OS:                Linux

hi, I want to do a HTTP POST and send some values via XMLHttpRequest. When I do this:

    request.open("POST", url, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send("name=value");

I get an extra CR/LF after Content-Type header, before Content-Length header. This breaks
HTTP request and application doesn't work. The request gets formed like this:

POST /fw16/xml/test.fw HTTP/1.1

Connection: Keep-Alive

User-Agent: Mozilla/5.0 (compatible; Konqueror/3.3; Linux) (KHTML, like Gecko)

Pragma: no-cache

Cache-control: no-cache

Accept: text/html, image/jpeg, image/png, text/*, image/*, */*

Accept-Encoding: x-gzip, x-deflate, gzip, deflate

Accept-Charset: iso-8859-1, utf-8;q=0.5, *;q=0.5

Accept-Language: en

Host: 192.168.1.153:8888

Cookie: JSESSIONID=aaa8IxsI9zEq1a

Content-Type: application/x-www-form-urlencoded



Content-Length: 12



name=test2
.
^ this is zero, i.e. 0x00, I'm not sure this should be there either, but this shouldn't be a problem.

Teofilis
Comment 1 Chris Lear 2005-01-21 17:46:55 UTC
I have also found the same thing in version 3.3.2 on Gentoo. It seems that this bug does not exist in Safari 1.2.4 (but Safari does appear to append the zero byte at the end). I don't know whether that is relevent.
I am not using setRequestHeader at all, and the unwanted blank line appears directly after a cookie header and before the content-length header.
Comment 2 Michael Clark 2005-01-23 01:05:52 UTC
I've tracked this down - it is a bug in xmlHttpRequest.cpp. It doesn't set the mandatory 'content-type' metadata for the http kio_slave and kio/http/http.c just print a blank field. The Content-type does however shows up although it is set as an additional header field. This of course is the problem.

Here is a test site:

  http://oss.metaparadigm.com/jsonrpc/test.jsp

I'll follow up with a patch the fixes the problem (should apply to current CVS). The patch changes setRequestHeader to stash the Content-type instead of adding it and then it is added to the job as metadata (which is the correct method accoriding to the comments in job.h).

Once you apply the patch you'll find the above test site works.
Comment 3 Michael Clark 2005-01-23 01:08:04 UTC
Created attachment 9231 [details]
Make XMLHttpRequest set content type headers correctly for POST requests
Comment 4 Christian Ullrich 2005-01-24 14:44:40 UTC
*** This bug has been confirmed by popular vote. ***
Comment 5 Andrew Mason 2005-01-26 08:36:53 UTC
On Tuesday 25 January 2005 11:49 pm, Mathieu Jobin wrote:
Voter. I'm having to use FF for this atm but i'd prefer konq.

many thanks
Andrew M

> ------- You are receiving this mail because: -------
> You are a voter for the bug, or are watching someone who is.
>
> http://bugs.kde.org/show_bug.cgi?id=95981
> somekool mytradecenter com changed:
>
>            What    |Removed                     |Added
> ---------------------------------------------------------------------------
>- CC|                            |somekool mytradecenter com

Comment 6 Stephan Kulow 2005-01-26 10:05:17 UTC
CVS commit by coolo: 

applying the patch as the logic seems correct to me (and Zack didn't protest)
BUG: 95981


  M +11 -0     xmlhttprequest.cpp   1.11
  M +1 -0      xmlhttprequest.h   1.6


--- kdelibs/khtml/ecma/xmlhttprequest.cpp  #1.10:1.11
@@ -217,4 +218,5 @@ XMLHttpRequest::XMLHttpRequest(ExecState
     doc(static_cast<DOM::DocumentImpl*>(d.handle())),
     async(true),
+    contentType(QString::null),
     job(0),
     state(Uninitialized),
@@ -309,4 +311,8 @@ void XMLHttpRequest::send(const QString&
       // FIXME: determine post encoding correctly by looking in headers for charset
       job = KIO::http_post( url, QCString(_body.utf8()), false );
+      if(contentType.isNull())
+        job->addMetaData( "content-type", "Content-type: text/plain" );
+      else
+        job->addMetaData( "content-type", contentType );
   }
   else
@@ -369,4 +375,9 @@ void XMLHttpRequest::abort()
 void XMLHttpRequest::setRequestHeader(const QString& name, const QString &value)
 {
+  // Content-type needs to be set seperately from the other headers
+  if(name.lower() == "content-type") {
+    contentType = "Content-type: " + value;
+    return;
+  }
   if (requestHeaders.length() > 0) {
     requestHeaders += "\r\n";

--- kdelibs/khtml/ecma/xmlhttprequest.h  #1.5:1.6
@@ -99,4 +99,5 @@ namespace KJS {
     bool async;
     QString requestHeaders;
+    QString contentType;
 
     KIO::TransferJob * job;