Modify the Cookie class (#296)

This commit is contained in:
An Tao 2019-11-15 10:26:21 +08:00 committed by GitHub
parent a8ae91bee0
commit d46a041cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 9 deletions

View File

@ -34,9 +34,6 @@ class Cookie
{
}
Cookie() = default;
~Cookie()
{
}
/**
* @brief Set the Expires Date
@ -71,6 +68,10 @@ class Cookie
{
_domain = domain;
}
void setDomain(std::string &&domain)
{
_domain = std::move(domain);
}
/**
* @brief Set the path of the cookie.
@ -79,6 +80,10 @@ class Cookie
{
_path = path;
}
void setPath(std::string &&path)
{
_path = std::move(path);
}
/**
* @brief Set the key of the cookie.
@ -87,7 +92,10 @@ class Cookie
{
_key = key;
}
void setKey(std::string &&key)
{
_key = std::move(key);
}
/**
* @brief Set the value of the cookie.
*/
@ -95,6 +103,10 @@ class Cookie
{
_value = value;
}
void setValue(std::string &&value)
{
_value = std::move(value);
}
/**
* @brief Get the string value of the cookie

View File

@ -201,6 +201,7 @@ class HttpResponse
/// Add a cookie
virtual void addCookie(const Cookie &cookie) = 0;
virtual void addCookie(Cookie &&cookie) = 0;
/// Get the cookie identified by the key parameter.
/// If there is no the cookie, the empty cookie is retured.
@ -300,9 +301,16 @@ class HttpResponse
const std::string &viewName,
const HttpViewData &data = HttpViewData());
/// Create a response that returns a 302 Found page, redirecting to another
/// page located in the location parameter.
static HttpResponsePtr newRedirectionResponse(const std::string &location);
/// Create a response that returns a redirection page, redirecting to
/// another page located in the location parameter.
/**
* @param location The location to redirect
* @param status The HTTP status code, k302Found by default. Users could set
* it to one of the 301, 302, 303, 307, ...
*/
static HttpResponsePtr newRedirectionResponse(
const std::string &location,
HttpStatusCode status = k302Found);
/// Create a response that returns a file to the client.
/**

View File

@ -130,10 +130,11 @@ HttpResponsePtr HttpResponse::newNotFoundResponse()
}
}
HttpResponsePtr HttpResponse::newRedirectionResponse(
const std::string &location)
const std::string &location,
HttpStatusCode status)
{
auto res = std::make_shared<HttpResponseImpl>();
res->setStatusCode(k302Found);
res->setStatusCode(status);
res->redirect(location);
return res;
}

View File

@ -195,6 +195,11 @@ class HttpResponseImpl : public HttpResponse
_cookies[cookie.key()] = cookie;
}
virtual void addCookie(Cookie &&cookie) override
{
_cookies[cookie.key()] = std::move(cookie);
}
virtual const Cookie &getCookie(const std::string &key) const override
{
static const Cookie defaultCookie;