if(timeToExpire) { expires = timeToExpire; } else { expires = cookie_ParseDate(date); }
if(timeToExpire == 0) { timeToExpire = cookie_ParseDate(date); }
cookie_SetCookieString(char * curURL, char * setCookieHeader, time_t timeToExpire) {  cookie_CookieStruct * prev_cookie;  char *path_from_header=NULL, *host_from_header=NULL;  char *name_from_header=NULL, *cookie_from_header=NULL;  time_t expires=0;  char *cur_path = cookie_ParseURL(curURL, GET_PATH_PART);  char *cur_host = cookie_ParseURL(curURL, GET_HOST_PART);  char *semi_colon, *ptr, *equal;  PRBool xxx=PR_FALSE, isDomain=PR_FALSE;  PRBool bCookieAdded;  PRBool pref_scd = PR_FALSE;  /* Only allow cookies to be set in the listed contexts.   * We don't want cookies being set in html mail.    *//* We need to come back and work on this - Neeti   type = context->type;  if(!((type==MWContextBrowser) || (type==MWContextHTMLHelp) || (type==MWContextPane))) {    PR_Free(cur_path);    PR_Free(cur_host);    return;  }*/  if(COOKIE_GetBehaviorPref() == COOKIE_DontUse) {    PR_Free(cur_path);    PR_Free(cur_host);    return;  }  /* Don't enter this routine if it is already in use by another     thread.  Otherwise the "remember this decision" result of the     other cookie (which came first) won't get applied to this cookie.   */  if (cookie_SetCookieStringInUse) {    PR_Free(cur_path);    PR_Free(cur_host);    cookie_Defer(curURL, setCookieHeader, timeToExpire);    return;  }  cookie_SetCookieStringInUse = PR_TRUE;//HG87358 -- @@??  /* terminate at any carriage return or linefeed */  for(ptr=setCookieHeader; *ptr; ptr++) {    if(*ptr == LF || *ptr == CR) {      *ptr = '\0';      break;    }  }  /* parse path and expires attributes from header if present */  semi_colon = PL_strchr(setCookieHeader, ';');  if(semi_colon) {    /* truncate at semi-colon and advance */    *semi_colon++ = '\0';    /* there must be some attributes. (hopefully) */    if ((ptr=PL_strcasestr(semi_colon, "secure="))) {      char cPre=*(ptr-1), cPost=*(ptr+6);      if (((cPre==' ') || (cPre==';')) && (!cPost || (cPost==' ') || (cPost==';'))) {        xxx = PR_TRUE;      }     }    /* look for the path attribute */    ptr = PL_strcasestr(semi_colon, "path=");    if(ptr) {      /* allocate more than we need */      StrAllocCopy(path_from_header, XP_StripLine(ptr+5));      /* terminate at first space or semi-colon */      for(ptr=path_from_header; *ptr != '\0'; ptr++) {        if(COOKIE_IS_SPACE(*ptr) || *ptr == ';' || *ptr == ',') {          *ptr = '\0';          break;        }      }    }    /* look for the URI or URL attribute     *     * This might be a security hole so I'm removing     * it for now.     */    /* look for a domain */    ptr = PL_strcasestr(semi_colon, "domain=");    if(ptr) {      char *domain_from_header=NULL;      char *dot, *colon;      int domain_length, cur_host_length;      /* allocate more than we need */      StrAllocCopy(domain_from_header, XP_StripLine(ptr+7));      /* terminate at first space or semi-colon */      for(ptr=domain_from_header; *ptr != '\0'; ptr++) {        if(COOKIE_IS_SPACE(*ptr) || *ptr == ';' || *ptr == ',') {          *ptr = '\0';          break;        }      }      /* verify that this host has the authority to set for this domain.   We do       * this by making sure that the host is in the domain.  We also require       * that a domain have at least two periods to prevent domains of the form       * ".com" and ".edu"       *       * Also make sure that there is more stuff after       * the second dot to prevent ".com."       */      dot = PL_strchr(domain_from_header, '.');      if(dot) {        dot = PL_strchr(dot+1, '.');      }      if(!dot || *(dot+1) == '\0') {        /* did not pass two dot test. FAIL */        PR_FREEIF(path_from_header);        PR_Free(domain_from_header);        PR_Free(cur_path);        PR_Free(cur_host);        // TRACEMSG(("DOMAIN failed two dot test"));        cookie_SetCookieStringInUse = PR_FALSE;        cookie_Undefer();        return;      }      /* strip port numbers from the current host for the domain test */      colon = PL_strchr(cur_host, ':');      if(colon) {        *colon = '\0';      }      domain_length   = PL_strlen(domain_from_header);      cur_host_length = PL_strlen(cur_host);      /* check to see if the host is in the domain */      if (!cookie_IsInDomain(domain_from_header, cur_host, cur_host_length)) {          // TRACEMSG(("DOMAIN failed host within domain test."//        " Domain: %s, Host: %s", domain_from_header, cur_host));        PR_FREEIF(path_from_header);        PR_Free(domain_from_header);        PR_Free(cur_path);        PR_Free(cur_host);        cookie_SetCookieStringInUse = PR_FALSE;        cookie_Undefer();        return;      }      /*       * check that portion of host not in domain does not contain a dot       *    This satisfies the fourth requirement in section 4.3.2 of the cookie       *    spec rfc 2109 (see www.cis.ohio-state.edu/htbin/rfc/rfc2109.html).       *    It prevents host of the form x.y.co.nz from setting cookies in the       *    entire .co.nz domain.  Note that this doesn't really solve the problem,       *    it justs makes it more unlikely.  Sites such as y.co.nz can still set       *    cookies for the entire .co.nz domain.       */      /*       *  Although this is the right thing to do(tm), it breaks too many sites.         *  So only do it if the restrictCookieDomains pref is PR_TRUE.       *       */      nsresult rv;      NS_WITH_SERVICE(nsIPref, prefs, "component://netscape/preferences", &rv);      if (NS_FAILED(prefs->GetBoolPref(cookie_strictDomainsPref, &pref_scd))) {        pref_scd = PR_FALSE;      }      if ( pref_scd == PR_TRUE ) {        cur_host[cur_host_length-domain_length] = '\0';        dot = PL_strchr(cur_host, '.');        cur_host[cur_host_length-domain_length] = '.';        if (dot) {        // TRACEMSG(("host minus domain failed no-dot test."//          " Domain: %s, Host: %s", domain_from_header, cur_host));          PR_FREEIF(path_from_header);          PR_Free(domain_from_header);          PR_Free(cur_path);          PR_Free(cur_host);          cookie_SetCookieStringInUse = PR_FALSE;          cookie_Undefer();          return;        }      }      /* all tests passed, copy in domain to hostname field */      StrAllocCopy(host_from_header, domain_from_header);      isDomain = PR_TRUE;      // TRACEMSG(("Accepted domain: %s", host_from_header));      PR_Free(domain_from_header);    }    /* now search for the expires header      * NOTE: that this part of the parsing     * destroys the original part of the string     */    ptr = PL_strcasestr(semi_colon, "expires=");    if(ptr) {      char *date =  ptr+8;      /* terminate the string at the next semi-colon */      for(ptr=date; *ptr != '\0'; ptr++) {        if(*ptr == ';') {          *ptr = '\0';          break;        }      }      if(timeToExpire) {        expires = timeToExpire;      } else {        expires = cookie_ParseDate(date);      }      // TRACEMSG(("Have expires date: %ld", expires));    }  }  if(!path_from_header) {    /* strip down everything after the last slash to get the path. */    char * slash = PL_strrchr(cur_path, '/');    if(slash) {      *slash = '\0';    }    path_from_header = cur_path;  } else {    PR_Free(cur_path);  }  if(!host_from_header) {    host_from_header = cur_host;  } else {    PR_Free(cur_host);  }  /* keep cookies under the max bytes limit */  if(PL_strlen(setCookieHeader) > MAX_BYTES_PER_COOKIE) {    setCookieHeader[MAX_BYTES_PER_COOKIE-1] = '\0';  }  /* separate the name from the cookie */  equal = PL_strchr(setCookieHeader, '=');  if(equal) {    *equal = '\0';    StrAllocCopy(name_from_header, XP_StripLine(setCookieHeader));    StrAllocCopy(cookie_from_header, XP_StripLine(equal+1));  } else {    // TRACEMSG(("Warning: no name found for cookie"));    StrAllocCopy(cookie_from_header, XP_StripLine(setCookieHeader));    StrAllocCopy(name_from_header, "");  }  /* generate the message for the nag box */  PRUnichar * new_string=0;  int count = cookie_Count(host_from_header);  cookie_LockList();  prev_cookie = cookie_CheckForPrevCookie    (path_from_header, host_from_header, name_from_header);  cookie_UnlockList();  PRUnichar * message;  if (prev_cookie) {    message = cookie_Localize("PermissionToModifyCookie");    new_string = nsTextFormatter::smprintf(message, host_from_header ? host_from_header : "");  } else if (count>1) {    message = cookie_Localize("PermissionToSetAnotherCookie");    new_string = nsTextFormatter::smprintf(message, host_from_header ? host_from_header : "", count);  } else if (count==1){    message = cookie_Localize("PermissionToSetSecondCookie");    new_string = nsTextFormatter::smprintf(message, host_from_header ? host_from_header : "");  } else {    message = cookie_Localize("PermissionToSetACookie");    new_string = nsTextFormatter::smprintf(message, host_from_header ? host_from_header : "");  }  Recycle(message);  /* use common code to determine if we can set the cookie */  PRBool permission;  permission_Check    (host_from_header, permission, COOKIEPERMISSION, cookie_GetWarningPref(), new_string);  PR_FREEIF(new_string);  if (!permission) {    PR_FREEIF(path_from_header);    PR_FREEIF(host_from_header);    PR_FREEIF(name_from_header);    PR_FREEIF(cookie_from_header);    cookie_SetCookieStringInUse = PR_FALSE;    cookie_Undefer();    return;  }  /*    * We have decided we are going to insert a cookie into the list   *   Get the cookie lock so that we can munge on the list   */  cookie_LockList();  /* limit the number of cookies from a specific host or domain */  cookie_CheckForMaxCookiesFromHo(host_from_header);  if (cookie_cookieList) {    if(cookie_cookieList->Count() > MAX_NUMBER_OF_COOKIES-1) {      cookie_RemoveOldestCookie();    }  }  prev_cookie = cookie_CheckForPrevCookie (path_from_header, host_from_header, name_from_header);  if(prev_cookie) {    prev_cookie->expires = expires;    PR_FREEIF(prev_cookie->cookie);    PR_FREEIF(prev_cookie->path);    PR_FREEIF(prev_cookie->host);    PR_FREEIF(prev_cookie->name);    prev_cookie->cookie = cookie_from_header;    prev_cookie->path = path_from_header;    prev_cookie->host = host_from_header;    prev_cookie->name = name_from_header;    prev_cookie->xxx = xxx;    prev_cookie->isDomain = isDomain;    prev_cookie->lastAccessed = get_current_time();  } else {    cookie_CookieStruct * tmp_cookie_ptr;    size_t new_len;    /* construct a new cookie_struct */    prev_cookie = PR_NEW(cookie_CookieStruct);    if(!prev_cookie) {      PR_FREEIF(path_from_header);      PR_FREEIF(host_from_header);      PR_FREEIF(name_from_header);      PR_FREEIF(cookie_from_header);      cookie_UnlockList();      cookie_SetCookieStringInUse = PR_FALSE;      cookie_Undefer();      return;    }        /* copy */    prev_cookie->cookie = cookie_from_header;    prev_cookie->name = name_from_header;    prev_cookie->path = path_from_header;    prev_cookie->host = host_from_header;    prev_cookie->expires = expires;    prev_cookie->xxx = xxx;    prev_cookie->isDomain = isDomain;    prev_cookie->lastAccessed = get_current_time();    if(!cookie_cookieList) {      cookie_cookieList = new nsVoidArray();      if(!cookie_cookieList) {        PR_FREEIF(path_from_header);        PR_FREEIF(name_from_header);        PR_FREEIF(host_from_header);        PR_FREEIF(cookie_from_header);        PR_Free(prev_cookie);        cookie_UnlockList();        cookie_SetCookieStringInUse = PR_FALSE;        cookie_Undefer();        return;      }    }    /* add it to the list so that it is before any strings of smaller length */    bCookieAdded = PR_FALSE;    new_len = PL_strlen(prev_cookie->path);    PRInt32 count = cookie_cookieList->Count();    for (PRInt32 i = 0; i < count; ++i) {      tmp_cookie_ptr = NS_STATIC_CAST(cookie_CookieStruct*, cookie_cookieList->ElementAt(i));      if (tmp_cookie_ptr) {        if(new_len > PL_strlen(tmp_cookie_ptr->path)) {          cookie_cookieList->InsertElementAt(prev_cookie, i);          bCookieAdded = PR_TRUE;          break;        }      }    }    if ( !bCookieAdded ) {      /* no shorter strings found in list */      cookie_cookieList->AppendElement(prev_cookie);    }  }  /* At this point we know a cookie has changed. Write the cookies to file. */  cookie_cookiesChanged = PR_TRUE;  cookie_Save();  cookie_UnlockList();  cookie_SetCookieStringInUse = PR_FALSE;  cookie_Undefer();  return;}