From 27ffda3b67d172afca2cb85387fad27e78719480 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 29 Mar 2022 23:28:05 +0200 Subject: Split Session from HttpServlet --- NetEx/CMakeLists.txt | 1 + NetEx/HttpServlet.cpp | 165 +++++--------------------------------------------- NetEx/HttpServlet.h | 54 +++-------------- NetEx/HttpSession.cpp | 145 ++++++++++++++++++++++++++++++++++++++++++++ NetEx/HttpSession.h | 49 +++++++++++++++ 5 files changed, 218 insertions(+), 196 deletions(-) create mode 100644 NetEx/HttpSession.cpp create mode 100644 NetEx/HttpSession.h (limited to 'NetEx') diff --git a/NetEx/CMakeLists.txt b/NetEx/CMakeLists.txt index 84d8b0f..13dadc2 100644 --- a/NetEx/CMakeLists.txt +++ b/NetEx/CMakeLists.txt @@ -9,6 +9,7 @@ add_library( HttpServer.cpp HttpServlet.cpp HttpServletExec.cpp + HttpSession.cpp NetAddr.cpp NetClient.cpp NetGram.cpp diff --git a/NetEx/HttpServlet.cpp b/NetEx/HttpServlet.cpp index eb9f19e..150719c 100644 --- a/NetEx/HttpServlet.cpp +++ b/NetEx/HttpServlet.cpp @@ -3,31 +3,31 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - - OVERVIEW - ======== - Network Server Pump for HTTP Server + OVERVIEW + ======== + Network Server Pump for HTTP Server */ #include "HttpServlet.h" -#include "NetLayer.h" -#include #include -#include -// +-------------------------------------------------------------------+ +#include "HttpRequest.h" +#include "HttpResponse.h" + + +HttpServlet::HttpServlet() : + session(nullptr) +{ +} -HttpServlet::HttpServlet() - : session(0) -{ } HttpServlet::~HttpServlet() -{ } +{ +} -// +--------------------------------------------------------------------+ bool HttpServlet::Service(HttpRequest& request, HttpResponse& response) @@ -54,7 +54,6 @@ HttpServlet::Service(HttpRequest& request, HttpResponse& response) return result; } -// +--------------------------------------------------------------------+ bool HttpServlet::DoGet(HttpRequest& request, HttpResponse& response) @@ -62,12 +61,14 @@ HttpServlet::DoGet(HttpRequest& request, HttpResponse& response) return false; } + bool HttpServlet::DoPost(HttpRequest& request, HttpResponse& response) { return DoGet(request, response); } + bool HttpServlet::DoHead(HttpRequest& request, HttpResponse& response) { @@ -84,137 +85,3 @@ HttpServlet::DoHead(HttpRequest& request, HttpResponse& response) return false; } - -// +--------------------------------------------------------------------+ -// +--------------------------------------------------------------------+ -// +--------------------------------------------------------------------+ - -HttpSession::HttpSession() -{ - id = GenerateUniqueID(); - access_time = NetLayer::GetUTC(); -} - -HttpSession::~HttpSession() -{ - attributes.destroy(); -} - -// +--------------------------------------------------------------------+ - -Text -HttpSession::GetAttribute(const char* name) -{ - ListIter iter = attributes; - while (++iter) { - HttpParam* p = iter.value(); - - if (p->name == name) - return p->value; - } - - return Text(); -} - -void -HttpSession::SetAttribute(const char* name, const char* value) -{ - ListIter iter = attributes; - while (++iter) { - HttpParam* p = iter.value(); - - if (p->name == name) { - p->value = value; - return; - } - } - - HttpParam* param = new HttpParam(name, value); - if (param) - attributes.append(param); -} - -void -HttpSession::DelAttribute(const char* name) -{ - ListIter iter = attributes; - while (++iter) { - HttpParam* p = iter.value(); - - if (p->name == name) { - delete iter.removeItem(); - return; - } - } -} - -// +--------------------------------------------------------------------+ - -int -HttpSession::GetIntAttribute(const char* name) -{ - ListIter iter = attributes; - while (++iter) { - HttpParam* p = iter.value(); - - if (p->name == name) { - int result = ::atoi(p->value.data()); - return result; - } - } - - return 0; -} - -void -HttpSession::SetIntAttribute(const char* name, int value) -{ - char buf[32]; - sprintf(buf, "%d", value); - - ListIter iter = attributes; - while (++iter) { - HttpParam* p = iter.value(); - - if (p->name == name) { - p->value = buf; - return; - } - } - - HttpParam* param = new HttpParam(name, buf); - if (param) - attributes.append(param); -} - -void -HttpSession::DelIntAttribute(const char* name) -{ - DelAttribute(name); -} - -// +--------------------------------------------------------------------+ - -Text -HttpSession::GenerateUniqueID() -{ - char unique[17]; - - for (int i = 0; i < 16; i++) { - char c = rand() % 25 + 'a'; - unique[i] = c; - } - - unique[16] = 0; - return unique; -} - -// +--------------------------------------------------------------------+ - -void -HttpSession::Access() -{ - access_time = NetLayer::GetUTC(); -} - - diff --git a/NetEx/HttpServlet.h b/NetEx/HttpServlet.h index aedfb09..eb706ea 100644 --- a/NetEx/HttpServlet.h +++ b/NetEx/HttpServlet.h @@ -3,26 +3,21 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Network Server Pump for HTTP Server + OVERVIEW + ======== + Network Server Pump for HTTP Server */ - #ifndef HttpServlet_h #define HttpServlet_h -#include "HttpServer.h" - -// +-------------------------------------------------------------------+ - -class HttpServlet; -class HttpSession; +#include "HttpRequest.h" +#include "HttpResponse.h" +#include "HttpSession.h" -// +-------------------------------------------------------------------+ class HttpServlet { @@ -45,40 +40,5 @@ protected: HttpSession* session; }; -// +-------------------------------------------------------------------+ - -class HttpSession -{ -public: - static const char* TYPENAME() { return "HttpSession"; } - - HttpSession(); - virtual ~HttpSession(); - - int operator == (const HttpSession& s) const { return id == s.id; } - - Text GenerateUniqueID(); - - Text GetID() const { return id; } - void SetID(const char* i) { id = i; } - int GetLastAccess() const { return access_time;} - void Access(); - - List& GetAttributes() { return attributes; } - - Text GetAttribute(const char* name); - void SetAttribute(const char* name, const char* value); - void DelAttribute(const char* name); - - int GetIntAttribute(const char* name); - void SetIntAttribute(const char* name, int value); - void DelIntAttribute(const char* name); - -protected: - Text id; - int access_time; - List attributes; -}; - #endif // HttpServlet_h diff --git a/NetEx/HttpSession.cpp b/NetEx/HttpSession.cpp new file mode 100644 index 0000000..577b1e3 --- /dev/null +++ b/NetEx/HttpSession.cpp @@ -0,0 +1,145 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. +*/ + +#include "HttpSession.h" + +#include +#include + +#include "HttpParam.h" +#include "List.h" +#include "NetLayer.h" +#include "Text.h" + + +HttpSession::HttpSession() +{ + id = GenerateUniqueID(); + access_time = NetLayer::GetUTC(); +} + + +HttpSession::~HttpSession() +{ + attributes.destroy(); +} + + +Text +HttpSession::GetAttribute(const char* name) +{ + ListIter iter = attributes; + while (++iter) { + HttpParam* p = iter.value(); + + if (p->name == name) + return p->value; + } + + return Text(); +} + + +void +HttpSession::SetAttribute(const char* name, const char* value) +{ + ListIter iter = attributes; + while (++iter) { + HttpParam* p = iter.value(); + + if (p->name == name) { + p->value = value; + return; + } + } + + HttpParam* param = new HttpParam(name, value); + if (param) + attributes.append(param); +} + + +void +HttpSession::DelAttribute(const char* name) +{ + ListIter iter = attributes; + while (++iter) { + HttpParam* p = iter.value(); + + if (p->name == name) { + delete iter.removeItem(); + return; + } + } +} + + +int +HttpSession::GetIntAttribute(const char* name) +{ + ListIter iter = attributes; + while (++iter) { + HttpParam* p = iter.value(); + + if (p->name == name) { + int result = ::atoi(p->value.data()); + return result; + } + } + + return 0; +} + + +void +HttpSession::SetIntAttribute(const char* name, int value) +{ + char buf[32]; + sprintf(buf, "%d", value); + + ListIter iter = attributes; + while (++iter) { + HttpParam* p = iter.value(); + + if (p->name == name) { + p->value = buf; + return; + } + } + + HttpParam* param = new HttpParam(name, buf); + if (param) + attributes.append(param); +} + + +void +HttpSession::DelIntAttribute(const char* name) +{ + DelAttribute(name); +} + + +Text +HttpSession::GenerateUniqueID() +{ + char unique[17]; + + for (int i = 0; i < 16; i++) { + char c = rand() % 25 + 'a'; + unique[i] = c; + } + + unique[16] = 0; + return unique; +} + + +void +HttpSession::Access() +{ + access_time = NetLayer::GetUTC(); +} diff --git a/NetEx/HttpSession.h b/NetEx/HttpSession.h new file mode 100644 index 0000000..e8d67f8 --- /dev/null +++ b/NetEx/HttpSession.h @@ -0,0 +1,49 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. +*/ + +#ifndef HttpSession_h +#define HttpSession_h + +#include "HttpParam.h" +#include "List.h" +#include "Text.h" + + +class HttpSession +{ +public: + static const char* TYPENAME() { return "HttpSession"; } + + HttpSession(); + virtual ~HttpSession(); + + int operator == (const HttpSession& s) const { return id == s.id; } + + Text GenerateUniqueID(); + + Text GetID() const { return id; } + void SetID(const char* i) { id = i; } + int GetLastAccess() const { return access_time;} + void Access(); + + List& GetAttributes() { return attributes; } + + Text GetAttribute(const char* name); + void SetAttribute(const char* name, const char* value); + void DelAttribute(const char* name); + + int GetIntAttribute(const char* name); + void SetIntAttribute(const char* name, int value); + void DelIntAttribute(const char* name); + +protected: + Text id; + int access_time; + List attributes; +}; + + +#endif // HttpSession_h -- cgit v1.1