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 35 /** @file 36 HTTP Server Tasks 37 */ 38 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 +---------------------------------------------------------------------*/ 53 /** 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. 58 */ 59 class PLT_HttpServerSocketTask : public PLT_ThreadTask 60 { 61 friend class PLT_ThreadTask; 62 63 public: 64 PLT_HttpServerSocketTask(NPT_Socket* socket, bool stay_alive_forever = false); 65 66 protected: 67 virtual ~PLT_HttpServerSocketTask(); 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 DoAbort()80 virtual void DoAbort() { if (m_Socket) m_Socket->Cancel(); } 81 virtual void DoRun(); 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 +---------------------------------------------------------------------*/ 107 /** 108 The PLT_HttpServerTask class is a version of PLT_HttpServerSocketTask that supports 109 delegation of HTTP request handling. 110 */ 111 class PLT_HttpServerTask : public PLT_HttpServerSocketTask 112 { 113 public: 114 PLT_HttpServerTask(NPT_HttpRequestHandler* handler, 115 NPT_Socket* socket, 116 bool keep_alive = false) : PLT_HttpServerSocketTask(socket,keep_alive)117 PLT_HttpServerSocketTask(socket, keep_alive), m_Handler(handler) {} 118 119 protected: ~PLT_HttpServerTask()120 virtual ~PLT_HttpServerTask() {} 121 SetupResponse(NPT_HttpRequest & request,const NPT_HttpRequestContext & context,NPT_HttpResponse & response)122 NPT_Result SetupResponse(NPT_HttpRequest& request, 123 const NPT_HttpRequestContext& context, 124 NPT_HttpResponse& response) { 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 +---------------------------------------------------------------------*/ 135 /** 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. 138 */ 139 class PLT_HttpListenTask : public PLT_ThreadTask 140 { 141 public: 142 PLT_HttpListenTask(NPT_HttpRequestHandler* handler, 143 NPT_TcpServerSocket* socket, 144 bool owns_socket = true) : m_Handler(handler)145 m_Handler(handler), m_Socket(socket), m_OwnsSocket(owns_socket) {} 146 147 protected: ~PLT_HttpListenTask()148 virtual ~PLT_HttpListenTask() { 149 if (m_OwnsSocket && m_Socket) delete m_Socket; 150 } 151 152 protected: 153 // PLT_ThreadTask methods DoAbort()154 virtual void DoAbort() { if (m_Socket) m_Socket->Cancel(); } 155 virtual void DoRun(); 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_ */ 164