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
Родитель d4f565ccd1
Коммит 41091979e8
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;
} }
// Now that we know the real content length we can create the header // If no content type header was set by the client, we set it to text/xml.
PR_snprintf(postData, nsXPIDLCString header;
MAX_HEADER_SIZE, if( NS_OK != mChannel->GetRequestHeader("Content-Type", getter_Copies(header)) )
"Content-type: text/xml\015\012Content-Length: %d\015\012\015\012", mChannel->SetRequestHeader("Content-Type", "text/xml" );
charLength);
PRInt32 headerSize = nsCRT::strlen(postData);
// Copy the post data to immediately follow the header // set the content length header
nsCRT::memcpy(postData+headerSize, postData+MAX_HEADER_SIZE, charLength); char charLengthBuf [32];
PR_snprintf(charLengthBuf, sizeof(charLengthBuf), "%d", charLength);
mChannel->SetRequestHeader("Content-Length", charLengthBuf );
// Shove in the trailing CRLF // Shove in the trailing and leading CRLF
postData[headerSize+charLength] = nsCRT::CR; postData[0] = nsCRT::CR;
postData[headerSize+charLength+1] = nsCRT::LF; postData[1] = nsCRT::LF;
postData[headerSize+charLength+2] = '\0'; postData[2+charLength] = nsCRT::CR;
postData[2+charLength+1] = nsCRT::LF;
postData[2+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;