comparison deps/Platinum/Source/Core/PltHttpServerTask.h @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Platinum - HTTP Server Tasks
4 |
5 | Copyright (c) 2004-2008, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
13 |
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 |
21 | This program is distributed in the hope that it will be useful,
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | GNU General Public License for more details.
25 |
26 | You should have received a copy of the GNU General Public License
27 | along with this program; see the file LICENSE.txt. If not, write to
28 | the Free Software Foundation, Inc.,
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 | http://www.gnu.org/licenses/gpl-2.0.html
31 |
32 ****************************************************************/
33
34 #ifndef _PLT_HTTP_SERVER_TASK_H_
35 #define _PLT_HTTP_SERVER_TASK_H_
36
37 /*----------------------------------------------------------------------
38 | includes
39 +---------------------------------------------------------------------*/
40 #include "Neptune.h"
41 #include "PltHttp.h"
42 #include "PltDatagramStream.h"
43 #include "PltThreadTask.h"
44
45 /*----------------------------------------------------------------------
46 | forward declarations
47 +---------------------------------------------------------------------*/
48 template <class T> class PLT_HttpListenTask;
49 class PLT_HttpServerListener;
50 typedef PLT_HttpListenTask<PLT_HttpServerListener> PLT_HttpServerListenTask;
51
52 /*----------------------------------------------------------------------
53 | PLT_HttpServerSocketTask class
54 +---------------------------------------------------------------------*/
55 class PLT_HttpServerSocketTask : public PLT_ThreadTask
56 {
57 friend class PLT_ThreadTask;
58
59 public:
60 PLT_HttpServerSocketTask(NPT_Socket* socket, bool stay_alive_forever = false);
61
62 protected:
63 virtual ~PLT_HttpServerSocketTask();
64
65 protected:
66 // Request callback handler
67 virtual NPT_Result ProcessRequest(NPT_HttpRequest& request,
68 const NPT_HttpRequestContext& context,
69 NPT_HttpResponse*& response,
70 bool& headers_only) = 0;
71
72 // overridables
73 virtual NPT_Result GetInputStream(NPT_InputStreamReference& stream);
74 virtual NPT_Result GetInfo(NPT_SocketInfo& info);
75
76 // PLT_ThreadTask methods
77 virtual void DoAbort() { m_Socket->Disconnect(); }
78 virtual void DoRun();
79
80 private:
81 virtual NPT_Result Read(NPT_BufferedInputStreamReference& buffered_input_stream,
82 NPT_HttpRequest*& request,
83 NPT_HttpRequestContext* context = NULL);
84 virtual NPT_Result Write(NPT_HttpResponse* response,
85 bool& keep_alive,
86 bool headers_only = false);
87
88 protected:
89 NPT_Socket* m_Socket;
90 bool m_StayAliveForever;
91 };
92
93 /*----------------------------------------------------------------------
94 | PLT_HttpServerTask class
95 +---------------------------------------------------------------------*/
96 template <class T>
97 class PLT_HttpServerTask : public PLT_HttpServerSocketTask
98 {
99 public:
100 PLT_HttpServerTask<T>(T* data,
101 NPT_Socket* socket,
102 bool keep_alive = false) :
103 PLT_HttpServerSocketTask(socket, keep_alive), m_Data(data) {}
104
105 protected:
106 virtual ~PLT_HttpServerTask<T>() {}
107
108 protected:
109 NPT_Result ProcessRequest(NPT_HttpRequest& request,
110 const NPT_HttpRequestContext& context,
111 NPT_HttpResponse*& response,
112 bool& headers_only) {
113 return m_Data->ProcessHttpRequest(request, context, response, headers_only);
114 }
115
116 protected:
117 T* m_Data;
118 };
119
120 /*----------------------------------------------------------------------
121 | PLT_HttpListenTask class
122 +---------------------------------------------------------------------*/
123 template <class T>
124 class PLT_HttpListenTask : public PLT_ThreadTask
125 {
126 public:
127 PLT_HttpListenTask<T>(T* data, NPT_TcpServerSocket* socket, bool cleanup_socket = true) :
128 m_Data(data), m_Socket(socket), m_CleanupSocket(cleanup_socket) {}
129
130 protected:
131 virtual ~PLT_HttpListenTask<T>() {
132 if (m_CleanupSocket) delete m_Socket;
133 }
134
135 protected:
136 // PLT_ThreadTask methods
137 virtual void DoAbort() { m_Socket->Disconnect(); }
138 virtual void DoRun() {
139 while (!IsAborting(0)) {
140 NPT_Socket* client = NULL;
141 NPT_Result result = m_Socket->WaitForNewClient(client, 5000);
142 if (NPT_FAILED(result) && result != NPT_ERROR_TIMEOUT) {
143 if (client) delete client;
144 //NPT_LOG_WARNING_2("PLT_HttpListenTask exiting with %d (%s)", result, NPT_ResultText(result));
145 break;
146 }
147
148 if (NPT_SUCCEEDED(result)) {
149 PLT_ThreadTask* task = new PLT_HttpServerTask<T>(m_Data, client);
150 if (NPT_FAILED(m_TaskManager->StartTask(task))) {
151 task->Kill();
152 }
153 }
154 }
155 }
156
157 protected:
158 T* m_Data;
159 NPT_TcpServerSocket* m_Socket;
160 bool m_CleanupSocket;
161 };
162
163 #endif /* _PLT_HTTP_SERVER_TASK_H_ */