Cookie.cc 4.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2010 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "Cookie.h"

#include <sstream>

#include "A2STR.h"
#include "a2functional.h"
#include "cookie_helper.h"

namespace aria2 {

45 46 47 48
Cookie::Cookie(std::string name, std::string value, time_t expiryTime,
               bool persistent, std::string domain, bool hostOnly,
               std::string path, bool secure, bool httpOnly,
               time_t creationTime)
49 50 51 52 53 54 55 56 57 58 59 60 61
    : expiryTime_(expiryTime),
      creationTime_(creationTime),
      lastAccessTime_(creationTime),
      name_(std::move(name)),
      value_(std::move(value)),
      domain_(std::move(domain)),
      path_(std::move(path)),
      persistent_(persistent),
      hostOnly_(hostOnly),
      secure_(secure),
      httpOnly_(httpOnly)
{
}
62

63
Cookie::Cookie()
64 65 66 67 68 69 70 71 72
    : expiryTime_(0),
      creationTime_(0),
      lastAccessTime_(0),
      persistent_(false),
      hostOnly_(false),
      secure_(false),
      httpOnly_(false)
{
}
73 74 75 76 77 78 79 80 81

std::string Cookie::toString() const
{
  std::string s = name_;
  s += "=";
  s += value_;
  return s;
}

82
bool Cookie::match(const std::string& requestHost,
83 84
                   const std::string& requestPath, time_t date,
                   bool secure) const
85
{
86 87
  if ((secure_ && !secure) || isExpired(date) ||
      !cookie::pathMatch(requestPath, path_)) {
88 89
    return false;
  }
90 91 92 93
  if (hostOnly_) {
    return requestHost == domain_;
  }
  else {
94 95 96 97 98 99 100
    return cookie::domainMatch(requestHost, domain_);
  }
}

bool Cookie::operator==(const Cookie& cookie) const
{
  return domain_ == cookie.domain_ && path_ == cookie.path_ &&
101
         name_ == cookie.name_;
102 103
}

104 105 106 107 108
bool Cookie::operator!=(const Cookie& cookie) const
{
  return !(*this == cookie);
}

109 110 111 112 113 114 115 116
bool Cookie::isExpired(time_t base) const
{
  return persistent_ && base > expiryTime_;
}

std::string Cookie::toNsCookieFormat() const
{
  std::stringstream ss;
117
  if (!hostOnly_) {
118
    ss << ".";
119 120
  }
  ss << domain_ << "\t";
121
  if (hostOnly_) {
122
    ss << "FALSE";
123 124
  }
  else {
125 126 127 128
    ss << "TRUE";
  }
  ss << "\t";
  ss << path_ << "\t";
129
  if (secure_) {
130
    ss << "TRUE";
131 132
  }
  else {
133 134 135
    ss << "FALSE";
  }
  ss << "\t";
136
  if (persistent_) {
137
    ss << expiryTime_;
138 139
  }
  else {
140 141 142 143 144 145 146 147
    ss << 0;
  }
  ss << "\t";
  ss << name_ << "\t";
  ss << value_;
  return ss.str();
}

148
void Cookie::setName(std::string name) { name_ = std::move(name); }
149

150
void Cookie::setValue(std::string value) { value_ = std::move(value); }
151

152
void Cookie::setDomain(std::string domain) { domain_ = std::move(domain); }
153

154
void Cookie::setPath(std::string path) { path_ = std::move(path); }
155 156

} // namespace aria2