Mercurial > projects > hoofbaby
comparison deps/Platinum/Source/Core/PltUPnPHelper.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 - UPnP 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_UPNP_HELPER_H_ | |
35 #define _PLT_UPNP_HELPER_H_ | |
36 | |
37 /*---------------------------------------------------------------------- | |
38 | includes | |
39 +---------------------------------------------------------------------*/ | |
40 #include "Neptune.h" | |
41 | |
42 /*---------------------------------------------------------------------- | |
43 | NPT_StringFinder | |
44 +---------------------------------------------------------------------*/ | |
45 class NPT_StringFinder | |
46 { | |
47 public: | |
48 // methods | |
49 NPT_StringFinder(const char* value) : m_Value(value) {} | |
50 virtual ~NPT_StringFinder() {} | |
51 bool operator()(const NPT_String* const & value) const { | |
52 return value->Compare(m_Value) ? false : true; | |
53 } | |
54 bool operator()(const NPT_String& value) const { | |
55 return value.Compare(m_Value) ? false : true; | |
56 } | |
57 | |
58 private: | |
59 // members | |
60 NPT_String m_Value; | |
61 }; | |
62 | |
63 /*---------------------------------------------------------------------- | |
64 | PLT_UPnPMessageHelper class | |
65 +---------------------------------------------------------------------*/ | |
66 class PLT_UPnPMessageHelper | |
67 { | |
68 public: | |
69 // methods | |
70 static const NPT_String* GetST(NPT_HttpMessage& message) { | |
71 return message.GetHeaders().GetHeaderValue("ST"); | |
72 } | |
73 static NPT_Result SetST(NPT_HttpMessage& message, | |
74 const char* st) { | |
75 return message.GetHeaders().SetHeader("ST", st); | |
76 } | |
77 static const NPT_String* GetNT(NPT_HttpMessage& message) { | |
78 return message.GetHeaders().GetHeaderValue("NT"); | |
79 } | |
80 static NPT_Result SetNT(NPT_HttpMessage& message, | |
81 const char* nt) { | |
82 return message.GetHeaders().SetHeader("NT", nt); | |
83 } | |
84 static const NPT_String* GetNTS(NPT_HttpMessage& message) { | |
85 return message.GetHeaders().GetHeaderValue("NTS"); | |
86 } | |
87 static NPT_Result SetNTS(NPT_HttpMessage& message, | |
88 const char* nts) { | |
89 return message.GetHeaders().SetHeader("NTS", nts); | |
90 } | |
91 static const NPT_String* GetMAN(NPT_HttpMessage& message) { | |
92 return message.GetHeaders().GetHeaderValue("MAN"); | |
93 } | |
94 static NPT_Result SetMAN(NPT_HttpMessage& message, | |
95 const char* man) { | |
96 return message.GetHeaders().SetHeader("MAN", man); | |
97 } | |
98 static const NPT_String* GetLocation(NPT_HttpMessage& message) { | |
99 return message.GetHeaders().GetHeaderValue("LOCATION"); | |
100 } | |
101 static NPT_Result SetLocation(NPT_HttpMessage& message, | |
102 const char* location) { | |
103 return message.GetHeaders().SetHeader("LOCATION", location); | |
104 } | |
105 static const NPT_String* GetServer(NPT_HttpMessage& message) { | |
106 return message.GetHeaders().GetHeaderValue(NPT_HTTP_HEADER_SERVER); | |
107 } | |
108 static NPT_Result SetServer(NPT_HttpMessage& message, | |
109 const char* server, | |
110 bool replace = true) { | |
111 return message.GetHeaders().SetHeader( | |
112 NPT_HTTP_HEADER_SERVER, | |
113 server, | |
114 replace); | |
115 } | |
116 static const NPT_String* GetUSN(NPT_HttpMessage& message) { | |
117 return message.GetHeaders().GetHeaderValue("USN"); | |
118 } | |
119 static NPT_Result SetUSN(NPT_HttpMessage& message, | |
120 const char* usn) { | |
121 return message.GetHeaders().SetHeader("USN", usn); | |
122 } | |
123 static const NPT_String* GetCallbacks(NPT_HttpMessage& message) { | |
124 return message.GetHeaders().GetHeaderValue("CALLBACK"); | |
125 } | |
126 static NPT_Result SetCallbacks(NPT_HttpMessage& message, | |
127 const char* callbacks) { | |
128 return message.GetHeaders().SetHeader("CALLBACK", callbacks); | |
129 } | |
130 static const NPT_String* GetSID(NPT_HttpMessage& message) { | |
131 return message.GetHeaders().GetHeaderValue("SID"); | |
132 } | |
133 static NPT_Result SetSID(NPT_HttpMessage& message, | |
134 const char* sid) { | |
135 return message.GetHeaders().SetHeader("SID", sid); | |
136 } | |
137 static NPT_Result GetLeaseTime(NPT_HttpMessage& message, | |
138 NPT_Timeout& value) { | |
139 value = 0; | |
140 const NPT_String* cc = | |
141 message.GetHeaders().GetHeaderValue("CACHE-CONTROL"); | |
142 NPT_CHECK_POINTER(cc); | |
143 return ExtractLeaseTime(*cc, value); | |
144 } | |
145 static NPT_Result SetLeaseTime(NPT_HttpMessage& message, | |
146 const NPT_Timeout lease) { | |
147 return message.GetHeaders().SetHeader( | |
148 "CACHE-CONTROL", | |
149 "max-age="+NPT_String::FromInteger(lease)); | |
150 } | |
151 static NPT_Result GetTimeOut(NPT_HttpMessage& message, | |
152 NPT_Int32& value) { | |
153 value = 0; | |
154 const NPT_String* timeout = | |
155 message.GetHeaders().GetHeaderValue("TIMEOUT"); | |
156 NPT_CHECK_POINTER(timeout); | |
157 return ExtractTimeOut(*timeout, value); | |
158 } | |
159 static NPT_Result SetTimeOut(NPT_HttpMessage& message, | |
160 const NPT_Int32 timeout) { | |
161 if (timeout >= 0) { | |
162 return message.GetHeaders().SetHeader( | |
163 "TIMEOUT", | |
164 "Second-"+NPT_String::FromInteger(timeout)); | |
165 } else { | |
166 return message.GetHeaders().SetHeader( | |
167 "TIMEOUT", "Second-infinite"); | |
168 } | |
169 } | |
170 static NPT_Result GetMX(NPT_HttpMessage& message, | |
171 NPT_UInt32& value) { | |
172 value = 0; | |
173 const NPT_String* mx = | |
174 message.GetHeaders().GetHeaderValue("MX"); | |
175 NPT_CHECK_POINTER(mx); | |
176 return NPT_ParseInteger32U(*mx, value); | |
177 } | |
178 static NPT_Result SetMX(NPT_HttpMessage& message, | |
179 const NPT_UInt32 mx) { | |
180 return message.GetHeaders().SetHeader( | |
181 "MX", | |
182 NPT_String::FromInteger(mx)); | |
183 } | |
184 static NPT_Result GetSeq(NPT_HttpMessage& message, | |
185 NPT_UInt32& value) { | |
186 value = 0; | |
187 const NPT_String* seq = | |
188 message.GetHeaders().GetHeaderValue("SEQ"); | |
189 NPT_CHECK_POINTER(seq); | |
190 return NPT_ParseInteger32U(*seq, value); | |
191 } | |
192 static NPT_Result SetSeq(NPT_HttpMessage& message, | |
193 const NPT_UInt32 seq) { | |
194 return message.GetHeaders().SetHeader( | |
195 "SEQ", | |
196 NPT_String::FromInteger(seq)); | |
197 } | |
198 static const char* GenerateUUID(int count, | |
199 NPT_String& uuid) { | |
200 uuid = ""; | |
201 for (int i=0;i<(count<100?count:100);i++) { | |
202 int random = NPT_System::GetRandomInteger(); | |
203 uuid += (char)((random % 25) + 66); | |
204 } | |
205 return uuid; | |
206 } | |
207 static const char* GenerateGUID(NPT_String& guid) { | |
208 guid = ""; | |
209 for (int i=0;i<32;i++) { | |
210 char nibble = (char)(NPT_System::GetRandomInteger() % 16); | |
211 guid += (nibble < 10) ? ('0' + nibble) : ('a' + (nibble-10)); | |
212 if (i == 7 || i == 11 || i == 15 || i == 19) { | |
213 guid += '-'; | |
214 } | |
215 } | |
216 return guid; | |
217 } | |
218 static NPT_Result ExtractLeaseTime(const char* cache_control, | |
219 NPT_Timeout& lease) { | |
220 int value; | |
221 if (cache_control && | |
222 sscanf(cache_control, "max-age=%d", &value) == 1) { | |
223 lease = value; | |
224 return NPT_SUCCESS; | |
225 } | |
226 return NPT_FAILURE; | |
227 } | |
228 static NPT_Result ExtractTimeOut(const char* timeout, | |
229 NPT_Int32& len) { | |
230 NPT_String temp = timeout; | |
231 if (temp.CompareN("Second-", 7, true)) { | |
232 return NPT_ERROR_INVALID_FORMAT; | |
233 } | |
234 | |
235 if (temp.Compare("Second-infinite", true) == 0) { | |
236 len = NPT_TIMEOUT_INFINITE; | |
237 return NPT_SUCCESS; | |
238 } | |
239 return temp.SubString(7).ToInteger(len); | |
240 } | |
241 static NPT_Result GetIPAddresses(NPT_List<NPT_String>& ips) { | |
242 NPT_List<NPT_NetworkInterface*> if_list; | |
243 NPT_CHECK(NPT_NetworkInterface::GetNetworkInterfaces(if_list)); | |
244 | |
245 NPT_List<NPT_NetworkInterface*>::Iterator iface = | |
246 if_list.GetFirstItem(); | |
247 while (iface) { | |
248 NPT_String ip = | |
249 (*(*iface)->GetAddresses().GetFirstItem()).GetPrimaryAddress().ToString(); | |
250 if (ip.Compare("0.0.0.0") && ip.Compare("127.0.0.1")) { | |
251 ips.Add(ip); | |
252 } | |
253 ++iface; | |
254 } | |
255 | |
256 if (ips.GetItemCount() == 0) { | |
257 ips.Add("127.0.0.1"); | |
258 } | |
259 | |
260 if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>()); | |
261 return NPT_SUCCESS; | |
262 } | |
263 | |
264 static NPT_Result GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& if_list, | |
265 bool with_localhost = false) { | |
266 NPT_CHECK(_GetNetworkInterfaces(if_list, false)); | |
267 | |
268 // if no valid interfaces or if requested, add localhost capable interface | |
269 if (if_list.GetItemCount() == 0 || with_localhost) { | |
270 NPT_CHECK(_GetNetworkInterfaces(if_list, true)); | |
271 } | |
272 return NPT_SUCCESS; | |
273 } | |
274 | |
275 private: | |
276 | |
277 static NPT_Result _GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& if_list, | |
278 bool only_localhost = false) { | |
279 NPT_List<NPT_NetworkInterface*> _if_list; | |
280 NPT_CHECK(NPT_NetworkInterface::GetNetworkInterfaces(_if_list)); | |
281 | |
282 NPT_NetworkInterface* iface; | |
283 while (NPT_SUCCEEDED(_if_list.PopHead(iface))) { | |
284 NPT_String ip = | |
285 iface->GetAddresses().GetFirstItem()->GetPrimaryAddress().ToString(); | |
286 if (ip.Compare("0.0.0.0") && | |
287 ((!only_localhost && ip.Compare("127.0.0.1")) || | |
288 (only_localhost && !ip.Compare("127.0.0.1")))) { | |
289 if_list.Add(iface); | |
290 | |
291 // add localhost only once | |
292 if (only_localhost) break; | |
293 } else { | |
294 delete iface; | |
295 } | |
296 } | |
297 | |
298 // cleanup any remaining items in list if we breaked early | |
299 _if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>()); | |
300 return NPT_SUCCESS; | |
301 } | |
302 | |
303 NPT_String HttpServerHeader; | |
304 }; | |
305 | |
306 #endif /* _PLT_UPNP_HELPER_H_ */ |