Skip to content

Commit

Permalink
[Session] Handle session persistance
Browse files Browse the repository at this point in the history
  • Loading branch information
unkn0wn107 committed Jun 25, 2024
1 parent 2c7af21 commit 4b4f8f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
11 changes: 2 additions & 9 deletions src/ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,13 @@ void ConnectionHandler::_processRequest() {
_request = NULL;
}
_request = new HTTPRequest(_buffer);
std::string sessionId = _request->getSessionId();
if (sessionId.empty()) {
sessionId = generateSessionId();
_request->setSessionId(sessionId);
}

if (_request->getHeader("Cache-Control") != "no-cache") {
HTTPResponse* cachedResponse = _cacheHandler.getResponse(*_request);
if (cachedResponse) {
_log.info("CONNECTION_HANDLER: Cache hit");
_response = cachedResponse;
_response->setCookie("sessionid", _request->getSessionId());
_connectionStatus = SENDING;
return;
}
Expand All @@ -203,7 +199,6 @@ void ConnectionHandler::_processRequest() {

if ((_response = vserv->checkRequest(*_request)) != NULL) {
_log.error("CONNECTION_HANDLER: Request failed");
_response->setCookie("sessionid", sessionId);
_connectionStatus = SENDING;
return;
}
Expand All @@ -214,9 +209,7 @@ void ConnectionHandler::_processRequest() {
"public, max-age=" + Utils::to_string(CacheHandler::MAX_AGE));
_cacheHandler.storeResponse(*_request, *_response);

if (_request->getHeader("Set-Cookie").empty()) {
_response->setCookie("sessionid", sessionId);
}
_response->setCookie("sessionid", _request->getSessionId());

_connectionStatus = SENDING;
}
Expand Down
40 changes: 31 additions & 9 deletions src/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
/* ::: :::::::: */
/* HTTPRequest.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mchenava <mchenava@student.42.fr> +#+ +:+ +#+ */
/* By: agaley <agaley@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 16:10:58 by agaley #+# #+# */
/* Updated: 2024/06/24 14:17:52 by mchenava ### ########.fr */
/* Updated: 2024/06/25 03:18:55 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

#include "HTTPRequest.hpp"
#include "Common.hpp"
#include "Utils.hpp"

const std::string HTTPRequest::supportedMethods[4] = {"GET", "HEAD", "DELETE",
Expand All @@ -24,9 +25,7 @@ HTTPRequest::HTTPRequest(std::string rawRequest /*, size_t readn*/)
parseRequest();
}

HTTPRequest::~HTTPRequest() {

}
HTTPRequest::~HTTPRequest() {}

void HTTPRequest::parseRequest() {
std::istringstream requestStream(_rawRequest);
Expand All @@ -40,6 +39,19 @@ void HTTPRequest::parseRequest() {
_uri = URI::decode(rawUri);
_uriComponents = URI::parse(rawUri);

_parseHeaders(requestStream);

if (_method == "POST" || _method == "PUT") {
std::stringstream bodyStream;
bodyStream << requestStream.rdbuf();
setBody(bodyStream.str());
}

_parseSession();
}

void HTTPRequest::_parseHeaders(std::istringstream& requestStream) {
std::string line;
while (std::getline(requestStream, line) && !line.empty()) {
if (line == "\r" || line.empty())
break;
Expand All @@ -52,12 +64,22 @@ void HTTPRequest::parseRequest() {
addHeader(key, value);
}
}
}

if (_method == "POST" || _method == "PUT") {
std::stringstream bodyStream;
bodyStream << requestStream.rdbuf();
setBody(bodyStream.str());
void HTTPRequest::_parseSession() {
std::string sessionId;
std::string cookieHeader = getHeader("Cookie");
std::size_t pos = cookieHeader.find("sessionid=");
if (pos != std::string::npos) {
std::size_t endPos = cookieHeader.find(";", pos);
if (endPos == std::string::npos)
sessionId = cookieHeader.substr(pos + 10);
else
sessionId = cookieHeader.substr(pos + 10, endPos - pos - 10);
}
if (sessionId.empty())
sessionId = generateSessionId();
setSessionId(sessionId);
}

void HTTPRequest::setSessionId(const std::string& sessionId) {
Expand Down
11 changes: 7 additions & 4 deletions src/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* HTTPRequest.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mchenava <mchenava@student.42.fr> +#+ +:+ +#+ */
/* By: agaley <agaley@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 16:12:02 by agaley #+# #+# */
/* Updated: 2024/06/14 13:42:10 by mchenava ### ########.fr */
/* Updated: 2024/06/25 03:18:01 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -54,8 +54,8 @@ class HTTPRequest {
void setBody(const std::string& body);

private:
std::string _sessionId;
std::string _rawRequest;
std::string _sessionId;
std::string _rawRequest;
// size_t _readn;
std::string _method;
std::string _uri;
Expand All @@ -64,6 +64,9 @@ class HTTPRequest {
std::string _body;
std::string _protocol;
LocationConfig* _config;

void _parseHeaders(std::istringstream& requestStream);
void _parseSession();
};

#endif

0 comments on commit 4b4f8f7

Please sign in to comment.