kodi
PltHttpServerTask.h
Go to the documentation of this file.
1 /*****************************************************************
2 |
3 | Platinum - HTTP Server Tasks
4 |
5 | Copyright (c) 2004-2010, 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 | licensing@plutinosoft.com
21 |
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.
26 |
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
32 |
33 ****************************************************************/
34 
39 #ifndef _PLT_HTTP_SERVER_TASK_H_
40 #define _PLT_HTTP_SERVER_TASK_H_
41 
42 /*----------------------------------------------------------------------
43 | includes
44 +---------------------------------------------------------------------*/
45 #include "Neptune.h"
46 #include "PltHttp.h"
47 #include "PltDatagramStream.h"
48 #include "PltThreadTask.h"
49 
50 /*----------------------------------------------------------------------
51 | PLT_HttpServerSocketTask class
52 +---------------------------------------------------------------------*/
60 {
61  friend class PLT_ThreadTask;
62 
63 public:
64  PLT_HttpServerSocketTask(NPT_Socket* socket, bool stay_alive_forever = false);
65 
66 protected:
67  ~PLT_HttpServerSocketTask() override;
68 
69 protected:
70  // Request callback handler
71  virtual NPT_Result SetupResponse(NPT_HttpRequest& request,
72  const NPT_HttpRequestContext& context,
73  NPT_HttpResponse& response) = 0;
74 
75  // overridables
76  virtual NPT_Result GetInputStream(NPT_InputStreamReference& stream);
77  virtual NPT_Result GetInfo(NPT_SocketInfo& info);
78 
79  // PLT_ThreadTask methods
80  void DoAbort() override { if (m_Socket) m_Socket->Cancel(); }
81  void DoRun() override;
82 
83 private:
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,
88  bool& keep_alive,
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,
95  bool& keep_alive);
96  virtual NPT_Result SendResponseBody(NPT_HttpResponse* response,
97  NPT_OutputStream& output_stream);
98 
99 protected:
100  NPT_Socket* m_Socket;
101  bool m_StayAliveForever;
102 };
103 
104 /*----------------------------------------------------------------------
105 | PLT_HttpServerTask class
106 +---------------------------------------------------------------------*/
112 {
113 public:
115  NPT_Socket* socket,
116  bool keep_alive = false) :
117  PLT_HttpServerSocketTask(socket, keep_alive), m_Handler(handler) {}
118 
119 protected:
120  ~PLT_HttpServerTask() override {}
121 
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);
126  }
127 
128 protected:
129  NPT_HttpRequestHandler* m_Handler;
130 };
131 
132 /*----------------------------------------------------------------------
133 | PLT_HttpListenTask class
134 +---------------------------------------------------------------------*/
140 {
141 public:
144  bool owns_socket = true) :
145  m_Handler(handler), m_Socket(socket), m_OwnsSocket(owns_socket) {}
146 
147 protected:
148  ~PLT_HttpListenTask() override {
149  if (m_OwnsSocket && m_Socket) delete m_Socket;
150  }
151 
152 protected:
153  // PLT_ThreadTask methods
154  void DoAbort() override { if (m_Socket) m_Socket->Cancel(); }
155  void DoRun() override;
156 
157 protected:
158  NPT_HttpRequestHandler* m_Handler;
159  NPT_TcpServerSocket* m_Socket;
160  bool m_OwnsSocket;
161 };
162 
163 #endif /* _PLT_HTTP_SERVER_TASK_H_ */
Runnable Task.
Definition: NptHttp.h:566
Definition: NptSockets.h:190
Definition: NptSockets.h:317
The PLT_HttpServerSocketTask class is a task used for handling one or more HTTP requests from a clien...
Definition: PltHttpServerTask.h:59
Definition: NptHttp.h:315
Definition: NptHttp.h:282
Definition: NptHttp.h:593
void DoAbort() override
This method to override in derived classes is called when the task is about to stop.
Definition: PltHttpServerTask.h:80
Definition: NptStreams.h:92
The PLT_HttpListenTask class is used by a PLT_HttpServer to listen for incoming connections and spawn...
Definition: PltHttpServerTask.h:139
Definition: NptSockets.h:115
Datagram Input/Output Neptune streams.
void DoAbort() override
This method to override in derived classes is called when the task is about to stop.
Definition: PltHttpServerTask.h:154
The PLT_HttpServerTask class is a version of PLT_HttpServerSocketTask that supports delegation of HTT...
Definition: PltHttpServerTask.h:111
The PLT_ThreadTask class is a base class for executing a given task in a worker thread.
Definition: PltThreadTask.h:56
void DoRun() override
This method to override in derived classes is the main task loop.
Definition: PltHttpServerTask.cpp:81
HTTP utilities.