comparison deps/Platinum/Source/Core/PltHttp.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 Helper
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_H_
35 #define _PLT_HTTP_H_
36
37 /*----------------------------------------------------------------------
38 | includes
39 +---------------------------------------------------------------------*/
40 #include "Neptune.h"
41
42 /*----------------------------------------------------------------------
43 | PLT_HttpHelper
44 +---------------------------------------------------------------------*/
45 class PLT_HttpHelper {
46 public:
47 static bool IsConnectionKeepAlive(NPT_HttpMessage& message);
48 static bool IsBodyStreamSeekable(NPT_HttpMessage& message);
49
50 static NPT_Result ToLog(NPT_LoggerReference logger, int level, NPT_HttpRequest* request);
51 static NPT_Result ToLog(NPT_LoggerReference logger, int level, NPT_HttpResponse* response);
52
53 static NPT_Result GetContentType(NPT_HttpMessage& message, NPT_String& type);
54 static void SetContentType(NPT_HttpMessage& message, const char* type);
55 static NPT_Result GetContentLength(NPT_HttpMessage& message, NPT_LargeSize& len);
56 static void SetContentLength(NPT_HttpMessage& message, NPT_LargeSize len);
57
58 static NPT_Result GetHost(NPT_HttpRequest& request, NPT_String& value);
59 static void SetHost(NPT_HttpRequest& request, const char* host);
60 static NPT_Result GetRange(NPT_HttpRequest& request, NPT_Position& start, NPT_Position& end);
61 static void SetRange(NPT_HttpRequest& request, NPT_Position start, NPT_Position end = (NPT_Position)-1);
62
63 static NPT_Result GetContentRange(NPT_HttpResponse& response, NPT_Position& start, NPT_Position& end, NPT_LargeSize& length);
64 static NPT_Result SetContentRange(NPT_HttpResponse& response, NPT_Position start, NPT_Position end, NPT_LargeSize length);
65
66 static NPT_Result SetBody(NPT_HttpMessage& message, NPT_String& body);
67 static NPT_Result SetBody(NPT_HttpMessage& message, const char* body, NPT_Size len);
68 static NPT_Result SetBody(NPT_HttpMessage& message, NPT_InputStreamReference& stream, NPT_LargeSize len = 0);
69
70
71 static NPT_Result GetBody(NPT_HttpMessage& message, NPT_String& body);
72 static NPT_Result ParseBody(NPT_HttpMessage& message, NPT_XmlElementNode*& xml);
73 };
74
75 /*----------------------------------------------------------------------
76 | PLT_HttpRequestContext
77 +---------------------------------------------------------------------*/
78 class PLT_HttpRequestContext : public NPT_HttpRequestContext {
79 public:
80 // constructors and destructor
81 PLT_HttpRequestContext(NPT_HttpRequest& request) :
82 m_Request(request) {}
83 PLT_HttpRequestContext(NPT_HttpRequest& request, const NPT_HttpRequestContext& context) :
84 NPT_HttpRequestContext(&context.GetLocalAddress(), &context.GetRemoteAddress()),
85 m_Request(request) {}
86 virtual ~PLT_HttpRequestContext() {}
87
88 NPT_HttpRequest& GetRequest() const { return m_Request; }
89
90 private:
91 NPT_HttpRequest& m_Request;
92 };
93
94 /*----------------------------------------------------------------------
95 | macros
96 +---------------------------------------------------------------------*/
97 #if defined(NPT_CONFIG_ENABLE_LOGGING)
98 #define PLT_LOG_HTTP_MESSAGE_L(_logger, _level, _msg) \
99 PLT_HttpHelper::ToLog(_logger, _level, _msg)
100 #define PLT_LOG_HTTP_MESSAGE(_level, _msg) \
101 PLT_LOG_HTTP_MESSAGE_L(_NPT_LocalLogger, _level, _msg)
102
103 #else /* NPT_CONFIG_ENABLE_LOGGING */
104 #define PLT_LOG_HTTP_MESSAGE_L(_logger, _level, _msg)
105 #define PLT_LOG_HTTP_MESSAGE(_level, _msg)
106 #endif /* NPT_CONFIG_ENABLE_LOGGING */
107
108 /*----------------------------------------------------------------------
109 | PLT_HttpClient
110 +---------------------------------------------------------------------*/
111 class PLT_HttpClient {
112 public:
113 // constructors and destructor
114 PLT_HttpClient() {}
115 virtual ~PLT_HttpClient() {}
116
117 // methods
118 NPT_Result Connect(NPT_Socket* connection,
119 NPT_HttpRequest& request,
120 NPT_Timeout timeout = NPT_TIMEOUT_INFINITE);
121
122 NPT_Result SendRequest(NPT_OutputStreamReference& output_stream,
123 NPT_HttpRequest& request,
124 NPT_Timeout timeout = NPT_TIMEOUT_INFINITE);
125
126 NPT_Result WaitForResponse(NPT_InputStreamReference& input_stream,
127 NPT_HttpRequest& request,
128 const NPT_HttpRequestContext& context,
129 NPT_HttpResponse*& response);
130
131 protected:
132 // members
133 };
134
135 /*----------------------------------------------------------------------
136 | PLT_HttpRequestHandler
137 +---------------------------------------------------------------------*/
138 template <class T>
139 class PLT_HttpRequestHandler : public NPT_HttpRequestHandler
140 {
141 public:
142 PLT_HttpRequestHandler<T>(T* data) : m_Data(data) {}
143 virtual ~PLT_HttpRequestHandler<T>() {}
144
145 // NPT_HttpRequestHandler methods
146 NPT_Result SetupResponse(NPT_HttpRequest& request,
147 const NPT_HttpRequestContext& context,
148 NPT_HttpResponse& response) {
149 return m_Data->ProcessHttpRequest(request, context, response);
150 }
151
152 private:
153 T* m_Data;
154 };
155
156
157 #endif /* _PLT_HTTP_H_ */