1 /*****************************************************************
3 | Platinum - HTTP Server Tasks
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
7 | http://www.plutinosoft.com
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.
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 | licensing@plutinosoft.com
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
33 ****************************************************************/
39 #ifndef _PLT_HTTP_SERVER_TASK_H_
40 #define _PLT_HTTP_SERVER_TASK_H_
42 /*----------------------------------------------------------------------
44 +---------------------------------------------------------------------*/
47 #include "PltDatagramStream.h"
48 #include "PltThreadTask.h"
50 /*----------------------------------------------------------------------
51 | PLT_HttpServerSocketTask class
52 +---------------------------------------------------------------------*/
54 The PLT_HttpServerSocketTask class is a task used for handling one or more HTTP
55 requests from a client. It is created by a PLT_HttpListenTask instance upon
56 receiving a connection request. A PLT_HttpServer will handle the delegation for
57 setting up the HTTP response.
59 class PLT_HttpServerSocketTask
: public PLT_ThreadTask
61 friend class PLT_ThreadTask
;
64 PLT_HttpServerSocketTask(NPT_Socket
* socket
, bool stay_alive_forever
= false);
67 ~PLT_HttpServerSocketTask() override
;
70 // Request callback handler
71 virtual NPT_Result
SetupResponse(NPT_HttpRequest
& request
,
72 const NPT_HttpRequestContext
& context
,
73 NPT_HttpResponse
& response
) = 0;
76 virtual NPT_Result
GetInputStream(NPT_InputStreamReference
& stream
);
77 virtual NPT_Result
GetInfo(NPT_SocketInfo
& info
);
79 // PLT_ThreadTask methods
80 void DoAbort() override
{ if (m_Socket
) m_Socket
->Cancel(); }
81 void DoRun() override
;
84 virtual NPT_Result
Read(NPT_BufferedInputStreamReference
& buffered_input_stream
,
85 NPT_HttpRequest
*& request
,
86 NPT_HttpRequestContext
* context
= NULL
);
87 virtual NPT_Result
Write(NPT_HttpResponse
* response
,
89 bool headers_only
= false);
90 virtual NPT_Result
RespondToClient(NPT_HttpRequest
& request
,
91 const NPT_HttpRequestContext
& context
,
92 NPT_HttpResponse
*& response
);
93 virtual NPT_Result
SendResponseHeaders(NPT_HttpResponse
* response
,
94 NPT_OutputStream
& output_stream
,
96 virtual NPT_Result
SendResponseBody(NPT_HttpResponse
* response
,
97 NPT_OutputStream
& output_stream
);
100 NPT_Socket
* m_Socket
;
101 bool m_StayAliveForever
;
104 /*----------------------------------------------------------------------
105 | PLT_HttpServerTask class
106 +---------------------------------------------------------------------*/
108 The PLT_HttpServerTask class is a version of PLT_HttpServerSocketTask that supports
109 delegation of HTTP request handling.
111 class PLT_HttpServerTask
: public PLT_HttpServerSocketTask
114 PLT_HttpServerTask(NPT_HttpRequestHandler
* handler
,
116 bool keep_alive
= false) :
117 PLT_HttpServerSocketTask(socket
, keep_alive
), m_Handler(handler
) {}
120 ~PLT_HttpServerTask() override
{}
122 NPT_Result
SetupResponse(NPT_HttpRequest
& request
,
123 const NPT_HttpRequestContext
& context
,
124 NPT_HttpResponse
& response
) override
{
125 return m_Handler
->SetupResponse(request
, context
, response
);
129 NPT_HttpRequestHandler
* m_Handler
;
132 /*----------------------------------------------------------------------
133 | PLT_HttpListenTask class
134 +---------------------------------------------------------------------*/
136 The PLT_HttpListenTask class is used by a PLT_HttpServer to listen for incoming
137 connections and spawn a new task for handling each request.
139 class PLT_HttpListenTask
: public PLT_ThreadTask
142 PLT_HttpListenTask(NPT_HttpRequestHandler
* handler
,
143 NPT_TcpServerSocket
* socket
,
144 bool owns_socket
= true) :
145 m_Handler(handler
), m_Socket(socket
), m_OwnsSocket(owns_socket
) {}
148 ~PLT_HttpListenTask() override
{
149 if (m_OwnsSocket
&& m_Socket
) delete m_Socket
;
153 // PLT_ThreadTask methods
154 void DoAbort() override
{ if (m_Socket
) m_Socket
->Cancel(); }
155 void DoRun() override
;
158 NPT_HttpRequestHandler
* m_Handler
;
159 NPT_TcpServerSocket
* m_Socket
;
163 #endif /* _PLT_HTTP_SERVER_TASK_H_ */