nsIAtom* contentTypeKey = NS_NewAtom("content-type");
HTMLContentSink::OnStreamComplete(nsIStreamLoader* aLoader,                                  nsISupports* aContext,                                  nsresult aStatus,                                  PRUint32 stringLen,                                  const char* string){  nsresult rv = NS_OK;    if (stringLen) {    PRUnichar *unicodeString = nsnull;    PRInt32 unicodeLength = 0;    nsAutoString characterSet;    nsICharsetConverterManager  *charsetConv = nsnull;    nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;    nsIAtom* contentTypeKey = NS_NewAtom("content-type");    nsXPIDLCString contenttypeheader;    nsCOMPtr<nsIHTTPChannel> httpChannel;    nsCOMPtr<nsIChannel> channel;    nsCOMPtr<nsIRequest> request;    rv = aLoader->GetRequest(getter_AddRefs(request));        NS_ASSERTION(request, "StreamLoader's request went away prematurely");    if (NS_FAILED(rv)) return rv;    channel = do_QueryInterface(request);    if (channel) {      httpChannel = do_QueryInterface(channel);      if (httpChannel) {        rv = httpChannel->GetResponseHeader(contentTypeKey, getter_Copies(contenttypeheader));        NS_RELEASE(contentTypeKey);      }    }    if (NS_SUCCEEDED(rv)) {        nsAutoString contentType; contentType.AssignWithConversion( NS_STATIC_CAST(const char*, contenttypeheader) );        PRInt32 start = contentType.RFind("charset=", PR_TRUE ) ;        if(kNotFound != start)        {          start += 8; // 8 = "charset=".length          PRInt32 end = contentType.FindCharInSet(";\n\r ", start  );          if(kNotFound == end ) end = contentType.Length();          contentType.Mid(characterSet, start, end - start);          NS_WITH_SERVICE(nsICharsetAlias, calias, kCharsetAliasCID, &rv);          if(NS_SUCCEEDED(rv) && (nsnull != calias) )          {           nsAutoString preferred;           rv = calias->GetPreferred(characterSet, preferred);           if(NS_SUCCEEDED(rv))           {              characterSet = preferred;           }          }        }      }          if (NS_FAILED(rv) || (characterSet.Length() == 0)) {      //charset from script charset tag      characterSet.Assign(mScriptCharset);    }    if (NS_FAILED(rv) || (characterSet.Length() == 0) ) {      // charset from document default      rv = mDocument->GetDocumentCharacterSet(characterSet);    }    NS_ASSERTION(NS_SUCCEEDED(rv), "Could not get document charset!");    rv = nsServiceManager::GetService(kCharsetConverterManagerCID,                    NS_GET_IID(nsICharsetConverterManager),                    (nsISupports**)&charsetConv);    if (NS_SUCCEEDED(rv) && (charsetConv))    {       rv = charsetConv->GetUnicodeDecoder(&characterSet,           getter_AddRefs(unicodeDecoder));       NS_RELEASE(charsetConv);    }    // converts from the charset to unicode    if (NS_SUCCEEDED(rv)) {      rv = unicodeDecoder->GetMaxLength(string, stringLen, &unicodeLength);      if (NS_SUCCEEDED(rv)) {          mUnicodeXferBuf.SetCapacity(unicodeLength);          unicodeString = (PRUnichar *) mUnicodeXferBuf.GetUnicode();          rv = unicodeDecoder->Convert(string, (PRInt32 *) &stringLen, unicodeString, &unicodeLength);          if (NS_SUCCEEDED(rv)) {            mUnicodeXferBuf.SetLength(unicodeLength);          } else {            mUnicodeXferBuf.SetLength(0);          }      }    }    NS_ASSERTION(NS_SUCCEEDED(rv), "Could not convert external JavaScript to Unicode!");    if ((NS_OK == aStatus) && (NS_SUCCEEDED(rv))) {      nsAutoString jsUnicodeBuffer(CBufDescriptor(unicodeString, PR_TRUE, unicodeLength+1, unicodeLength));      PRBool bodyPresent = PreEvaluateScript();      //-- Merge the principal of the script file with that of the document      nsCOMPtr<nsISupports> owner;      channel->GetOwner(getter_AddRefs(owner));      nsCOMPtr<nsIPrincipal> prin;      if (owner)      {        prin = do_QueryInterface(owner, &rv);        if (NS_FAILED(rv)) return rv;      }      rv = mDocument->AddPrincipal(prin);      if (NS_FAILED(rv)) return rv;      if(mParser) {        mParser->UnblockParser(); // make sure to unblock the parser before evaluating the script      }      rv = EvaluateScript(jsUnicodeBuffer, mScriptURI, 1, mScriptLanguageVersion);      if (NS_FAILED(rv)) return rv;      PostEvaluateScript(bodyPresent);    }  }  if(mParser && mParser->IsParserEnabled()){    rv=mParser->ContinueParsing();  }  if (NS_FAILED(rv)) return rv;  //invalidate Xfer buffer content  mUnicodeXferBuf.SetLength(0);   return rv;}