mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-02-20 15:14:26 +01:00
Added cookie support to the request. Implemented it for the drogon/trantor backend.
This commit is contained in:
parent
2a2e40aed6
commit
714eb02819
18
core/http/cookie.cpp
Normal file
18
core/http/cookie.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
#include "cookie.h"
|
||||||
|
|
||||||
|
Cookie::Cookie(const std::string &p_key, const std::string &p_value) {
|
||||||
|
http_only = true;
|
||||||
|
secure = false;
|
||||||
|
|
||||||
|
key = p_key;
|
||||||
|
value = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cookie::Cookie() {
|
||||||
|
http_only = true;
|
||||||
|
secure = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cookie::~Cookie() {
|
||||||
|
}
|
21
core/http/cookie.h
Normal file
21
core/http/cookie.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef COOKIE_H
|
||||||
|
#define COOKIE_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Cookie {
|
||||||
|
public:
|
||||||
|
//todo date
|
||||||
|
std::string domain;
|
||||||
|
std::string path;
|
||||||
|
std::string key;
|
||||||
|
std::string value;
|
||||||
|
bool http_only;
|
||||||
|
bool secure;
|
||||||
|
|
||||||
|
Cookie();
|
||||||
|
Cookie(const std::string &p_key, const std::string &p_value);
|
||||||
|
~Cookie();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -1,7 +1,17 @@
|
|||||||
#include "request.h"
|
#include "request.h"
|
||||||
|
|
||||||
|
#include "core/http/cookie.h"
|
||||||
#include "web_application.h"
|
#include "web_application.h"
|
||||||
|
|
||||||
|
const std::string &Request::get_cookie(const std::string &key) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void Request::add_cookie(const ::Cookie &cookie) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void Request::remove_cookie(const std::string &key) {
|
||||||
|
}
|
||||||
|
|
||||||
void Request::compile_body() {
|
void Request::compile_body() {
|
||||||
compiled_body.reserve(body.size() + head.size() + 13 + 14 + 15);
|
compiled_body.reserve(body.size() + head.size() + 13 + 14 + 15);
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include "handler_instance.h"
|
#include "handler_instance.h"
|
||||||
|
|
||||||
class WebApplication;
|
class WebApplication;
|
||||||
|
class Cookie;
|
||||||
|
|
||||||
class Request {
|
class Request {
|
||||||
public:
|
public:
|
||||||
@ -29,6 +30,10 @@ public:
|
|||||||
|
|
||||||
bool connection_closed;
|
bool connection_closed;
|
||||||
|
|
||||||
|
virtual const std::string &get_cookie(const std::string &key);
|
||||||
|
virtual void add_cookie(const ::Cookie &cookie);
|
||||||
|
virtual void remove_cookie(const std::string &key);
|
||||||
|
|
||||||
virtual void compile_body();
|
virtual void compile_body();
|
||||||
virtual void compile_and_send_body();
|
virtual void compile_and_send_body();
|
||||||
virtual void next_stage();
|
virtual void next_stage();
|
||||||
@ -61,7 +66,7 @@ protected:
|
|||||||
uint32_t _path_stack_pointer;
|
uint32_t _path_stack_pointer;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
class RequestPool {
|
class RequestPool {
|
||||||
public:
|
public:
|
||||||
T *get_request();
|
T *get_request();
|
||||||
@ -75,7 +80,7 @@ protected:
|
|||||||
std::vector<T *> _requests;
|
std::vector<T *> _requests;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
T *RequestPool<T>::get_request() {
|
T *RequestPool<T>::get_request() {
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
@ -99,18 +104,18 @@ T *RequestPool<T>::get_request() {
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
void RequestPool<T>::return_request(T *request) {
|
void RequestPool<T>::return_request(T *request) {
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
_requests.push_back(request);
|
_requests.push_back(request);
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
RequestPool<T>::RequestPool() {
|
RequestPool<T>::RequestPool() {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
RequestPool<T>::~RequestPool() {
|
RequestPool<T>::~RequestPool() {
|
||||||
for (uint32_t i = 0; i < _requests.size(); ++i) {
|
for (uint32_t i = 0; i < _requests.size(); ++i) {
|
||||||
delete _requests[i];
|
delete _requests[i];
|
||||||
@ -119,6 +124,4 @@ RequestPool<T>::~RequestPool() {
|
|||||||
_requests.clear();
|
_requests.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -2,13 +2,27 @@
|
|||||||
|
|
||||||
#include "web_application.h"
|
#include "web_application.h"
|
||||||
|
|
||||||
|
#include "core/http/cookie.h"
|
||||||
|
|
||||||
|
const std::string &DRequest::get_cookie(const std::string &key) {
|
||||||
|
return request->getCookie(key);
|
||||||
|
}
|
||||||
|
void DRequest::add_cookie(const ::Cookie &cookie) {
|
||||||
|
_added_cookies.push_back(cookie);
|
||||||
|
}
|
||||||
|
void DRequest::remove_cookie(const std::string &key) {
|
||||||
|
_removed_cookies.push_back(key);
|
||||||
|
}
|
||||||
|
|
||||||
void DRequest::send() {
|
void DRequest::send() {
|
||||||
//if (connection_closed) {
|
//if (connection_closed) {
|
||||||
// DRequestPool::return_request(this);
|
// DRequestPool::return_request(this);
|
||||||
// return;
|
// return;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
HttpResponsePtr response = HttpResponse::newHttpResponse();
|
drogon::HttpResponsePtr response = drogon::HttpResponse::newHttpResponse();
|
||||||
|
|
||||||
|
_response_additional_setup(response);
|
||||||
|
|
||||||
response->setBody(compiled_body);
|
response->setBody(compiled_body);
|
||||||
|
|
||||||
@ -19,7 +33,9 @@ void DRequest::send() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DRequest::send_file(const std::string &p_file_path) {
|
void DRequest::send_file(const std::string &p_file_path) {
|
||||||
HttpResponsePtr response = HttpResponse::newFileResponse(p_file_path, "", drogon::getContentType(p_file_path));
|
drogon::HttpResponsePtr response = drogon::HttpResponse::newFileResponse(p_file_path, "", drogon::getContentType(p_file_path));
|
||||||
|
|
||||||
|
_response_additional_setup(response);
|
||||||
|
|
||||||
callback(response);
|
callback(response);
|
||||||
|
|
||||||
@ -122,4 +138,25 @@ void DRequest::_file_chunk_sent() {
|
|||||||
file_next = true;
|
file_next = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DRequest::_response_additional_setup(const drogon::HttpResponsePtr &req) {
|
||||||
|
for (int i = 0; i < _added_cookies.size(); ++i) {
|
||||||
|
::Cookie &co = _added_cookies[i];
|
||||||
|
drogon::Cookie c;
|
||||||
|
c.setDomain(co.domain);
|
||||||
|
//todo
|
||||||
|
//c.setExpiresDate
|
||||||
|
c.setHttpOnly(co.http_only);
|
||||||
|
c.setKey(co.key);
|
||||||
|
c.setPath(co.path);
|
||||||
|
c.setSecure(co.secure);
|
||||||
|
c.setValue(co.value);
|
||||||
|
|
||||||
|
req->addCookie(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < _removed_cookies.size(); ++i) {
|
||||||
|
req->removeCookie(_removed_cookies[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RequestPool<DRequest> DRequest::_request_pool;
|
RequestPool<DRequest> DRequest::_request_pool;
|
@ -11,14 +11,18 @@
|
|||||||
|
|
||||||
#include "core/http/handler_instance.h"
|
#include "core/http/handler_instance.h"
|
||||||
|
|
||||||
using namespace drogon;
|
//using namespace drogon;
|
||||||
|
|
||||||
class DWebApplication;
|
class DWebApplication;
|
||||||
|
|
||||||
class DRequest : public Request {
|
class DRequest : public Request {
|
||||||
public:
|
public:
|
||||||
HttpRequestImplPtr request;
|
drogon::HttpRequestImplPtr request;
|
||||||
std::function<void(const HttpResponsePtr &)> callback;
|
std::function<void(const drogon::HttpResponsePtr &)> callback;
|
||||||
|
|
||||||
|
const std::string &get_cookie(const std::string &key);
|
||||||
|
void add_cookie(const ::Cookie &cookie);
|
||||||
|
void remove_cookie(const std::string &key);
|
||||||
|
|
||||||
void send();
|
void send();
|
||||||
void send_file(const std::string &p_file_path);
|
void send_file(const std::string &p_file_path);
|
||||||
@ -37,10 +41,14 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void _progress_send_file();
|
void _progress_send_file();
|
||||||
void _file_chunk_sent();
|
void _file_chunk_sent();
|
||||||
|
void _response_additional_setup(const drogon::HttpResponsePtr &req);
|
||||||
|
|
||||||
std::vector<std::string> _path_stack;
|
std::vector<std::string> _path_stack;
|
||||||
uint32_t _path_stack_pointer;
|
uint32_t _path_stack_pointer;
|
||||||
|
|
||||||
|
std::vector<::Cookie> _added_cookies;
|
||||||
|
std::vector<std::string> _removed_cookies;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static RequestPool<DRequest> _request_pool;
|
static RequestPool<DRequest> _request_pool;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user