Beispiel #1
0
void CookieJar::addCookieHeader(const URI &uri, const std::string &request)
{
  string domain = uri.server();
  if (not acceptCookies(domain))
  {
    // this server is blocked.
    return;
  }
  string path = uri.fileName();
  addCookieHeader(domain, path, request);
  m_domain = domain;
  saveCookiesToDisk();
}
Beispiel #2
0
void CookieJar::cookiesForRequest(
    const URI &request,
    std::string &headers,
    time_t now)
{
  loadDomainCookies(request.server());
  loadDomainCookies(topLevel(request.server()));
  // printf("cookiesForRequest: %s \n",request.asString().c_str());
  string domain(request.server());
  // find a cookie in the jar that corresponds to the requested domain...
  string tmp("Cookie: ");
  bool needSep(false);
  const static string sep("; ");
  bool haveExpired(false);
  std::vector<Cookie *>::const_iterator it(m_cookies.begin());
  for (; it != m_cookies.end(); ++it)
  {
    Cookie *c(*it);
    if (c->name() == DOMAIN_STR)
      continue;
    if (c->matchesDomain(domain)) {
      if (c->expired(now)) {
        haveExpired = true;
        continue;
      }

      if (not c->path().empty())
      {
        // check to see if the requested path is a sub dir of path
        string value = c->path();
        string file = request.fileName();
        size_t pos = file.find(value);
        if (pos != 0)
        {
          continue;
        }
      }
      // do not send secure cookies to unsecure domains
      if (c->secure() and request.protocol() != URI::HTTPS_PROTOCOL)
      {
        continue;
      }

      if (needSep)
      {
        tmp += sep;
      }
      string value(c->value());
      bool needQuotes = value.find_first_of(" ")!=string::npos;
      tmp += c->name();
      tmp += "=";
      if (needQuotes) {
        tmp += "\"";
      }
      tmp += c->value();
      if (needQuotes) {
        tmp += "\"";
      }
      needSep = true;
    }
  }
  if (needSep) {
    tmp += "\r\n";
    headers = tmp;
  }
  if (haveExpired) {
    gcExpiredCookies(now);
  }
}