From 4517d2cbaee2158d3dc2307987fe43cd961784bd Mon Sep 17 00:00:00 2001 From: "blizzard%redhat.com" Date: Mon, 2 Jul 2001 19:35:48 +0000 Subject: [PATCH] bug #87534. multipart/mixed handler doesn't parse content-type handler properly. r=dougt, sr=darin --- .../protocol/http/src/nsHttpResponseHead.cpp | 3 ++ .../converters/nsMultiMixedConv.cpp | 53 ++++++++++++++++++- .../streamconv/converters/nsMultiMixedConv.h | 2 + 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/netwerk/protocol/http/src/nsHttpResponseHead.cpp b/netwerk/protocol/http/src/nsHttpResponseHead.cpp index e934ff14f24..f3f0c373fb3 100644 --- a/netwerk/protocol/http/src/nsHttpResponseHead.cpp +++ b/netwerk/protocol/http/src/nsHttpResponseHead.cpp @@ -486,6 +486,9 @@ nsHttpResponseHead::ParseVersion(const char *str) mVersion = NS_HTTP_VERSION_1_0; } +// This code is duplicated in nsMultiMixedConv.cpp. If you change it +// here, change it there, too! + nsresult nsHttpResponseHead::ParseContentType(char *type) { diff --git a/netwerk/streamconv/converters/nsMultiMixedConv.cpp b/netwerk/streamconv/converters/nsMultiMixedConv.cpp index a7b179a6e04..e1ebe52cf15 100644 --- a/netwerk/streamconv/converters/nsMultiMixedConv.cpp +++ b/netwerk/streamconv/converters/nsMultiMixedConv.cpp @@ -827,7 +827,9 @@ nsMultiMixedConv::ParseHeaders(nsIChannel *aChannel, char *&aPtr, // examine header if (headerStr.EqualsIgnoreCase("content-type")) { - mContentType = headerVal; + char *tmpString = headerVal.ToNewCString(); + ParseContentType(tmpString); + nsCRT::free(tmpString); } else if (headerStr.EqualsIgnoreCase("content-length")) { mContentLength = atoi(headerVal.get()); } else if (headerStr.EqualsIgnoreCase("set-cookie")) { @@ -884,6 +886,55 @@ nsMultiMixedConv::ParseHeaders(nsIChannel *aChannel, char *&aPtr, return rv; } +// This code is duplicated in nsHttpResponseHead.cpp. If you change it +// here, change it there, too! + +nsresult +nsMultiMixedConv::ParseContentType(char *type) +{ + char *p = PL_strchr(type, '('); + + if (p) + // we don't care about comments (although they are invalid here) + *p = 0; + + // check if the content-type has additional fields... + if ((p = PL_strchr(type, ';')) != nsnull) { + char *p2, *p3; + // is there a charset field? + if ((p2 = PL_strcasestr(p, "charset=")) != nsnull) { + p2 += 8; + + // check end of charset parameter + if ((p3 = PL_strchr(p2, ';')) == nsnull) + p3 = p2 + PL_strlen(p2); + + // trim any trailing whitespace + do { + --p3; + } while ((*p3 == ' ') || (*p3 == '\t')); + *++p3 = 0; // overwrite first char after the charset field + + mContentCharset = p2; + } + } + else + p = type + PL_strlen(type); + + // trim any trailing whitespace + while (--p >= type && ((*p == ' ') || (*p == '\t'))) + ; + *++p = 0; // overwrite first char after the media type + + // force the content-type to lowercase + while (--p >= type) + *p = nsCRT::ToLower(*p); + + mContentType = type; + + return NS_OK; +} + char * nsMultiMixedConv::FindToken(char *aCursor, PRUint32 aLen) { // strnstr without looking for null termination diff --git a/netwerk/streamconv/converters/nsMultiMixedConv.h b/netwerk/streamconv/converters/nsMultiMixedConv.h index 5df1596f02a..a3d89b42935 100644 --- a/netwerk/streamconv/converters/nsMultiMixedConv.h +++ b/netwerk/streamconv/converters/nsMultiMixedConv.h @@ -92,6 +92,7 @@ protected: nsresult SendData(char *aBuffer, PRUint32 aLen); nsresult ParseHeaders(nsIChannel *aChannel, char *&aPtr, PRUint32 &aLen, PRBool *_retval); + nsresult ParseContentType(char *type); PRInt32 PushOverLine(char *&aPtr, PRUint32 &aLen); char *FindToken(char *aCursor, PRUint32 aLen); nsresult BufferData(char *aData, PRUint32 aLen); @@ -108,6 +109,7 @@ protected: // one channel per part. nsCOMPtr mContext; nsCString mContentType; + nsCString mContentCharset; PRInt32 mContentLength; char *mBuffer;