Bug 90530, make it possible to set content-type header. Patch from garths@oeone.com. r=heikki, sr=jst.

This commit is contained in:
heikki%netscape.com 2006-04-20 03:37:28 +00:00
Родитель b675f03deb
Коммит 60912a41e7
1 изменённых файлов: 20 добавлений и 19 удалений

Просмотреть файл

@ -782,39 +782,40 @@ nsXMLHttpRequest::GetStreamForWString(const PRUnichar* aStr,
rv = encoder->GetMaxLength(unicodeBuf, unicodeLength, &charLength); rv = encoder->GetMaxLength(unicodeBuf, unicodeLength, &charLength);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE; if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
#define MAX_HEADER_SIZE 128
// Allocate extra space for the header and trailing CRLF // Allocate extra space for the trailing and leading CRLF
postData = (char*)nsMemory::Alloc(MAX_HEADER_SIZE + charLength + 3); postData = (char*)nsMemory::Alloc(charLength + 5);
if (!postData) { if (!postData) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
rv = encoder->Convert(unicodeBuf, rv = encoder->Convert(unicodeBuf,
&unicodeLength, postData+MAX_HEADER_SIZE, &charLength); &unicodeLength, postData+2, &charLength);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
nsMemory::Free(postData); nsMemory::Free(postData);
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
// If no content type header was set by the client, we set it to text/xml.
nsXPIDLCString header;
if( NS_OK != mChannel->GetRequestHeader("Content-Type", getter_Copies(header)) )
mChannel->SetRequestHeader("Content-Type", "text/xml" );
// Now that we know the real content length we can create the header // set the content length header
PR_snprintf(postData, char charLengthBuf [32];
MAX_HEADER_SIZE, PR_snprintf(charLengthBuf, sizeof(charLengthBuf), "%d", charLength);
"Content-type: text/xml\015\012Content-Length: %d\015\012\015\012", mChannel->SetRequestHeader("Content-Length", charLengthBuf );
charLength);
PRInt32 headerSize = nsCRT::strlen(postData);
// Copy the post data to immediately follow the header // Shove in the trailing and leading CRLF
nsCRT::memcpy(postData+headerSize, postData+MAX_HEADER_SIZE, charLength); postData[0] = nsCRT::CR;
postData[1] = nsCRT::LF;
// Shove in the trailing CRLF postData[2+charLength] = nsCRT::CR;
postData[headerSize+charLength] = nsCRT::CR; postData[2+charLength+1] = nsCRT::LF;
postData[headerSize+charLength+1] = nsCRT::LF; postData[2+charLength+2] = '\0';
postData[headerSize+charLength+2] = '\0';
// The new stream takes ownership of the buffer // The new stream takes ownership of the buffer
rv = NS_NewByteArrayInputStream((nsIByteArrayInputStream**)aStream, rv = NS_NewByteArrayInputStream((nsIByteArrayInputStream**)aStream,
postData, postData,
headerSize+charLength+2); charLength+4);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
nsMemory::Free(postData); nsMemory::Free(postData);
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
@ -914,7 +915,7 @@ NS_IMETHODIMP
nsXMLHttpRequest::Send(nsISupports *body) nsXMLHttpRequest::Send(nsISupports *body)
{ {
nsresult rv; nsresult rv;
// Return error if we're already processing a request // Return error if we're already processing a request
if (XML_HTTP_REQUEST_SENT == mStatus) { if (XML_HTTP_REQUEST_SENT == mStatus) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;