if (keyword) { nsCAutoString keywordSpec("keyword:"); char *utf8Spec = urlStr.ToNewUTF8String(); if(utf8Spec) { char* escapedUTF8Spec = nsEscape(utf8Spec, url_Path); if(escapedUTF8Spec) { keywordSpec.Append(escapedUTF8Spec); rv = NS_NewURI(getter_AddRefs(uri), keywordSpec.GetBuffer(), nsnull); nsAllocator::Free(escapedUTF8Spec); } nsAllocator::Free(utf8Spec); } } } }   PRInt32 colon = -1; if (NS_FAILED(rv)) { PRInt32 fSlash = urlSpec.FindChar('/'); PRUnichar port;  if ( ((colon=urlSpec.FindChar(':')) == -1) || ( (fSlash > -1) && (colon > fSlash) ) || ( (colon < urlSpec.Length()-1) && ((port=urlSpec.CharAt(colon+1)) <= '9') && (port > '0') )) {  PRInt32 hostPos = urlSpec.FindCharInSet("./:"); if (hostPos == -1) { hostPos = urlSpec.Length(); }   nsAutoString hostSpec; urlSpec.Left(hostSpec, hostPos);   if (hostSpec.EqualsIgnoreCase("ftp")) { urlSpec.Insert("ftp: } else { urlSpec.Insert("http: } }  rv = NS_NewURI(getter_AddRefs(uri), urlSpec, nsnull); if(NS_ERROR_UNKNOWN_PROTOCOL == rv) {  rv = InitDialogVars(); if (NS_FAILED(rv)) return rv;  nsXPIDLString messageStr; nsAutoString name("protocolNotFound"); rv = mStringBundle->GetStringFromName(name.GetUnicode(), getter_Copies(messageStr)); if (NS_FAILED(rv)) return rv;  NS_ASSERTION(colon != -1, "we shouldn't have gotten this far if we didn't have a colon");   nsAutoString scheme; urlSpec.Left(scheme, colon);  nsAutoString dnsMsg(scheme); dnsMsg.Append(' '); dnsMsg.Append(messageStr);  (void)mPrompter->Alert(dnsMsg.GetUnicode()); } } } }  if (!uri) return rv;
if(!uri) return rv;
nsWebShell::LoadURL(const PRUnichar *aURLSpec,                    const char* aCommand,                    nsIInputStream* aPostDataStream,                    PRBool aModifyHistory,                    nsLoadFlags aType,                    nsISupports * aHistoryState,                    const PRUnichar* aReferrer,                    const char * aWindowTarget){  nsresult rv = NS_OK;  PRBool keywordsEnabled = PR_FALSE;  /*      TODO This doesnt belong here... The app should be doing all this     URL play. The webshell should not have a clue about whats "mailto"      If you insist that this should be here, then put in URL parsing      optimizations here. -Gagan  */  nsAutoString urlStr(aURLSpec);  // first things first. try to create a uri out of the string.  nsCOMPtr<nsIURI> uri;  nsXPIDLCString  spec;  if(0==urlStr.Find("file:",0)) {    // if this is file url, we need to  convert the URI    // from Unicode to the FS charset    nsCAutoString inFSCharset;    rv = convertURLToFileCharset(urlStr, inFSCharset);    NS_ASSERTION(NS_SUCCEEDED(rv), "convertURLToFielCharset failed");    if (NS_SUCCEEDED(rv))       rv = NS_NewURI(getter_AddRefs(uri), inFSCharset.GetBuffer(), nsnull);  } else {    rv = NS_NewURI(getter_AddRefs(uri), urlStr, nsnull);  }  if (NS_FAILED(rv)) {    // no dice.    nsAutoString urlSpec;    urlStr.Trim(" ");    // see if we've got a file url.    convertFileToURL(urlStr, urlSpec);    nsCAutoString inFSCharset;    rv = convertURLToFileCharset(urlSpec, inFSCharset);    NS_ASSERTION(NS_SUCCEEDED(rv), "convertURLToFielCharset failed");    if (NS_SUCCEEDED(rv))       rv = NS_NewURI(getter_AddRefs(uri), inFSCharset.GetBuffer(), nsnull);    if (NS_FAILED(rv)) {      NS_ASSERTION(mPrefs, "the webshell's pref service wasn't initialized");      rv = mPrefs->GetBoolPref("keyword.enabled", &keywordsEnabled);      if (NS_FAILED(rv)) return rv;      PRInt32 qMarkLoc = -1, spaceLoc = -1;      rv = NS_ERROR_FAILURE;      if (keywordsEnabled) {          // These are keyword formatted strings          // "what is mozilla"          // "what is mozilla?"          // "?mozilla"          // "?What is mozilla"          // These are not keyword formatted strings          // "www.blah.com" - anything with a dot in it           // "nonQualifiedHost:80" - anything with a colon in it          // "nonQualifiedHost?"          // "nonQualifiedHost?args"          // "nonQualifiedHost?some args"          if (urlStr.FindChar('.') == -1 && urlStr.FindChar(':') == -1) {              qMarkLoc = urlStr.FindChar('?');              spaceLoc = urlStr.FindChar(' ');              PRBool keyword = PR_FALSE;              if (qMarkLoc == 0) {                  keyword = PR_TRUE;              } else if ( (spaceLoc > 0) &&                           ( (qMarkLoc == -1) || (spaceLoc < qMarkLoc) )) {                    keyword = PR_TRUE;              }              if (keyword) {                  nsCAutoString keywordSpec("keyword:");                  char *utf8Spec = urlStr.ToNewUTF8String();                  if(utf8Spec) {                      char* escapedUTF8Spec = nsEscape(utf8Spec, url_Path);                      if(escapedUTF8Spec) {                          keywordSpec.Append(escapedUTF8Spec);                          rv = NS_NewURI(getter_AddRefs(uri), keywordSpec.GetBuffer(), nsnull);                          nsAllocator::Free(escapedUTF8Spec);                      } // escapedUTF8Spec                      nsAllocator::Free(utf8Spec);                  } // utf8Spec              } // keyword           } // FindChar      } // keywordEnable         PRInt32 colon = -1;      if (NS_FAILED(rv)) {          PRInt32 fSlash = urlSpec.FindChar('/');          PRUnichar port;          // if no scheme (protocol) is found, assume http.          if ( ((colon=urlSpec.FindChar(':')) == -1) // no colon at all               || ( (fSlash > -1) && (colon > fSlash) ) // the only colon comes after the first slash               || ( (colon < urlSpec.Length()-1) // the first char after the first colon is a digit (i.e. a port)                    && ((port=urlSpec.CharAt(colon+1)) <= '9')                    && (port > '0') )) {            // find host name            PRInt32 hostPos = urlSpec.FindCharInSet("./:");            if (hostPos == -1) {              hostPos = urlSpec.Length();            }            // extract host name            nsAutoString hostSpec;            urlSpec.Left(hostSpec, hostPos);            // insert url spec corresponding to host name            if (hostSpec.EqualsIgnoreCase("ftp")) {              urlSpec.Insert("ftp://", 0, 6);            } else {              urlSpec.Insert("http://", 0, 7);            }          } // end if colon          rv = NS_NewURI(getter_AddRefs(uri), urlSpec, nsnull);          if(NS_ERROR_UNKNOWN_PROTOCOL == rv)             {             // we weren't able to find a protocol handler             rv = InitDialogVars();             if (NS_FAILED(rv)) return rv;             nsXPIDLString messageStr;             nsAutoString name("protocolNotFound");             rv = mStringBundle->GetStringFromName(name.GetUnicode(), getter_Copies(messageStr));             if (NS_FAILED(rv)) return rv;             NS_ASSERTION(colon != -1, "we shouldn't have gotten this far if we didn't have a colon");             // extract the scheme             nsAutoString scheme;             urlSpec.Left(scheme, colon);             nsAutoString dnsMsg(scheme);             dnsMsg.Append(' ');             dnsMsg.Append(messageStr);             (void)mPrompter->Alert(dnsMsg.GetUnicode());             } // end unknown protocol      }   }  }  if (!uri) return rv;   rv = uri->GetSpec(getter_Copies(spec));   if (NS_FAILED(rv)) return rv;  // Get hold of Root webshell  nsCOMPtr<nsIWebShell>  root;  nsCOMPtr<nsISessionHistory> shist;  PRBool  isLoadingHistory=PR_FALSE; // Is SH currently loading an entry from history?  rv = GetRootWebShell(*getter_AddRefs(root));  // Get hold of session History  if (NS_SUCCEEDED(rv) && root) {        root->GetSessionHistory(*getter_AddRefs(shist));  }  if (shist)    shist->GetLoadingFlag(&isLoadingHistory);  /*    * Save the history state for the current index iff this loadurl() request   * is not from SH. When the request comes from SH, aModifyHistory will   * be false and nsSessionHistory.cpp takes of this.   */  if (shist)   {   PRInt32  indix;     shist->GetCurrentIndex(&indix);     if (indix >= 0 && (aModifyHistory))      {       nsCOMPtr<nsILayoutHistoryState>  historyState;       //XXX For now don't do it when we have frames       if(mChildren.Count())         {         nsCOMPtr<nsIPresShell> presShell;         GetPresShell(getter_AddRefs(presShell));         if(presShell)            rv = presShell->CaptureHistoryState(getter_AddRefs(historyState));         if (NS_SUCCEEDED(rv) && historyState)           shist->SetHistoryObjectForIndex(indix, historyState);         }      }   }  /* Set the History state object for the current page in the   * presentation shell. If it is a new page being visited,   * aHistoryState is null. If the load is coming from   * session History, it will be set to the cached history object by   * session History.   */   SetHistoryState(aHistoryState);   /* If the current Uri is the same as the one to be loaded    * don't add it to session history. This avoids multiple	* entries for the same url in the  go menu 	*/   nsCOMPtr<nsIURI> currentURI;   nsresult res = GetCurrentURI(getter_AddRefs(currentURI));   if (NS_SUCCEEDED(res) && currentURI) {	  nsXPIDLCString currentUriSpec;      rv = currentURI->GetSpec(getter_Copies(currentUriSpec));      if (NS_FAILED(rv)) return rv;      nsAutoString currentURIString(currentUriSpec);	  if (currentURIString.Equals(spec)) {         /* The url to be loaded is the same as the 		  * url already in the page. Don't add it to session history		  */		  aModifyHistory = PR_FALSE;	  }   }  /* Add the page to session history */  if (aModifyHistory && shist)  {        PRInt32  ret;        nsCAutoString referrer(aReferrer);        ret = shist->Add(spec, referrer, this);  }  nsCOMPtr<nsIWebShell> parent;  res = GetParent(*getter_AddRefs(parent));  nsCOMPtr<nsIURI> newURI;  if ((isLoadingHistory)) {  /* if LoadURL() got called from SH, AND If we are going "Back/Forward"    * to a frame page,SH  will change the current uri to the right value   * for smoother redraw.    */     res = GetCurrentURI(getter_AddRefs(newURI));  }  else{  /* If the call is not from SH, use the url passed by the caller   * so that things like JS will work right. This is for bug # 1646.   * May regress in other situations.   * What a hack   */     nsAutoString urlstr = (const char *) spec;     res = NS_NewURI(getter_AddRefs(newURI), urlstr, nsnull);  }    if (NS_SUCCEEDED(res)) {    // now that we have a uri, call the REAL LoadURI method which requires a nsIURI.    return LoadURI(newURI, aCommand, aPostDataStream, aModifyHistory, aType, aHistoryState, aReferrer, aWindowTarget);  }  return rv;}