1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SFML - Simple and Fast Multimedia Library</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="tabs.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="logo">
<img src="./logo.jpg" width="770" height="200" title="SFML home" alt="SFML logo" />
</div>
<!-- Generated by Doxygen 1.5.8 -->
<div class="navigation" id="top">
<div class="tabs">
<ul>
<li><a href="index.htm"><span>Main Page</span></a></li>
<li><a href="namespaces.htm"><span>Namespaces</span></a></li>
<li><a href="annotated.htm"><span>Classes</span></a></li>
<li class="current"><a href="files.htm"><span>Files</span></a></li>
</ul>
</div>
<div class="tabs">
<ul>
<li><a href="files.htm"><span>File List</span></a></li>
</ul>
</div>
<h1>Http.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001
<a name="l00002"></a>00002 <span class="comment">//</span>
<a name="l00003"></a>00003 <span class="comment">// SFML - Simple and Fast Multimedia Library</span>
<a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)</span>
<a name="l00005"></a>00005 <span class="comment">//</span>
<a name="l00006"></a>00006 <span class="comment">// This software is provided 'as-is', without any express or implied warranty.</span>
<a name="l00007"></a>00007 <span class="comment">// In no event will the authors be held liable for any damages arising from the use of this software.</span>
<a name="l00008"></a>00008 <span class="comment">//</span>
<a name="l00009"></a>00009 <span class="comment">// Permission is granted to anyone to use this software for any purpose,</span>
<a name="l00010"></a>00010 <span class="comment">// including commercial applications, and to alter it and redistribute it freely,</span>
<a name="l00011"></a>00011 <span class="comment">// subject to the following restrictions:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// 1. The origin of this software must not be misrepresented;</span>
<a name="l00014"></a>00014 <span class="comment">// you must not claim that you wrote the original software.</span>
<a name="l00015"></a>00015 <span class="comment">// If you use this software in a product, an acknowledgment</span>
<a name="l00016"></a>00016 <span class="comment">// in the product documentation would be appreciated but is not required.</span>
<a name="l00017"></a>00017 <span class="comment">//</span>
<a name="l00018"></a>00018 <span class="comment">// 2. Altered source versions must be plainly marked as such,</span>
<a name="l00019"></a>00019 <span class="comment">// and must not be misrepresented as being the original software.</span>
<a name="l00020"></a>00020 <span class="comment">//</span>
<a name="l00021"></a>00021 <span class="comment">// 3. This notice may not be removed or altered from any source distribution.</span>
<a name="l00022"></a>00022 <span class="comment">//</span>
<a name="l00024"></a>00024 <span class="comment"></span>
<a name="l00026"></a>00026 <span class="comment">// Headers</span>
<a name="l00028"></a>00028 <span class="comment"></span><span class="preprocessor">#include <SFML/Network/Http.hpp></span>
<a name="l00029"></a>00029 <span class="preprocessor">#include <ctype.h></span>
<a name="l00030"></a>00030 <span class="preprocessor">#include <sstream></span>
<a name="l00031"></a>00031
<a name="l00032"></a>00032
<a name="l00033"></a>00033 <span class="keyword">namespace</span>
<a name="l00034"></a>00034 {
<a name="l00036"></a>00036 <span class="comment">// Convenience function to convert a string to lower case</span>
<a name="l00038"></a>00038 <span class="comment"></span> std::string ToLower(<span class="keyword">const</span> std::string& Str)
<a name="l00039"></a>00039 {
<a name="l00040"></a>00040 std::string Ret = Str;
<a name="l00041"></a>00041 <span class="keywordflow">for</span> (std::string::iterator i = Ret.begin(); i != Ret.end(); ++i)
<a name="l00042"></a>00042 *i = static_cast<char>(tolower(*i));
<a name="l00043"></a>00043
<a name="l00044"></a>00044 <span class="keywordflow">return</span> Ret;
<a name="l00045"></a>00045 }
<a name="l00046"></a>00046 }
<a name="l00047"></a>00047
<a name="l00048"></a>00048
<a name="l00049"></a>00049 <span class="keyword">namespace </span>sf
<a name="l00050"></a>00050 {
<a name="l00054"></a><a class="code" href="classsf_1_1Http_1_1Request.htm#55e79368ff0d72568150b3823ee0eb72">00054</a> <a class="code" href="classsf_1_1Http_1_1Request.htm#55e79368ff0d72568150b3823ee0eb72" title="Default constructor.">Http::Request::Request</a>(<a class="code" href="classsf_1_1Http_1_1Request.htm#620f8bff6f43e1378f321bf53fbf5598" title="Enumerate the available HTTP methods for a request.">Method</a> RequestMethod, <span class="keyword">const</span> std::string& URI, <span class="keyword">const</span> std::string& Body) :
<a name="l00055"></a>00055 myMethod (RequestMethod),
<a name="l00056"></a>00056 myURI (URI),
<a name="l00057"></a>00057 myMajorVersion(1),
<a name="l00058"></a>00058 myMinorVersion(0),
<a name="l00059"></a>00059 myBody (Body)
<a name="l00060"></a>00060 {
<a name="l00061"></a>00061
<a name="l00062"></a>00062 }
<a name="l00063"></a>00063
<a name="l00064"></a>00064
<a name="l00068"></a><a class="code" href="classsf_1_1Http_1_1Request.htm#a48d6b67d701622333564dfa1094b41e">00068</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#a48d6b67d701622333564dfa1094b41e" title="Set the value of a field; the field is added if it doesn&#39;t exist.">Http::Request::SetField</a>(<span class="keyword">const</span> std::string& Field, <span class="keyword">const</span> std::string& Value)
<a name="l00069"></a>00069 {
<a name="l00070"></a>00070 myFields[ToLower(Field)] = Value;
<a name="l00071"></a>00071 }
<a name="l00072"></a>00072
<a name="l00073"></a>00073
<a name="l00078"></a><a class="code" href="classsf_1_1Http_1_1Request.htm#2b17bdbb01172b4bfc8bea4a661a1c53">00078</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#2b17bdbb01172b4bfc8bea4a661a1c53" title="Set the request method.">Http::Request::SetMethod</a>(<a class="code" href="classsf_1_1Http_1_1Request.htm#620f8bff6f43e1378f321bf53fbf5598" title="Enumerate the available HTTP methods for a request.">Http::Request::Method</a> RequestMethod)
<a name="l00079"></a>00079 {
<a name="l00080"></a>00080 myMethod = RequestMethod;
<a name="l00081"></a>00081 }
<a name="l00082"></a>00082
<a name="l00083"></a>00083
<a name="l00088"></a><a class="code" href="classsf_1_1Http_1_1Request.htm#2d92b4bf89606acb3e6ac8443b427aac">00088</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#2d92b4bf89606acb3e6ac8443b427aac" title="Set the target URI of the request.">Http::Request::SetURI</a>(<span class="keyword">const</span> std::string& URI)
<a name="l00089"></a>00089 {
<a name="l00090"></a>00090 myURI = URI;
<a name="l00091"></a>00091
<a name="l00092"></a>00092 <span class="comment">// Make sure it starts with a '/'</span>
<a name="l00093"></a>00093 <span class="keywordflow">if</span> (myURI.empty() || (myURI[0] != <span class="charliteral">'/'</span>))
<a name="l00094"></a>00094 myURI.insert(0, <span class="stringliteral">"/"</span>);
<a name="l00095"></a>00095 }
<a name="l00096"></a>00096
<a name="l00097"></a>00097
<a name="l00102"></a><a class="code" href="classsf_1_1Http_1_1Request.htm#a168e9482ea54c26868676b0eda6dabd">00102</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#a168e9482ea54c26868676b0eda6dabd" title="Set the HTTP version of the request.">Http::Request::SetHttpVersion</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> Major, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> Minor)
<a name="l00103"></a>00103 {
<a name="l00104"></a>00104 myMajorVersion = Major;
<a name="l00105"></a>00105 myMinorVersion = Minor;
<a name="l00106"></a>00106 }
<a name="l00107"></a>00107
<a name="l00108"></a>00108
<a name="l00114"></a><a class="code" href="classsf_1_1Http_1_1Request.htm#8f8b740b23e13d757e8dabd634eb8e46">00114</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#8f8b740b23e13d757e8dabd634eb8e46" title="Set the body of the request.">Http::Request::SetBody</a>(<span class="keyword">const</span> std::string& Body)
<a name="l00115"></a>00115 {
<a name="l00116"></a>00116 myBody = Body;
<a name="l00117"></a>00117 }
<a name="l00118"></a>00118
<a name="l00119"></a>00119
<a name="l00123"></a>00123 std::string Http::Request::ToString()<span class="keyword"> const</span>
<a name="l00124"></a>00124 <span class="keyword"></span>{
<a name="l00125"></a>00125 std::ostringstream Out;
<a name="l00126"></a>00126
<a name="l00127"></a>00127 <span class="comment">// Convert the method to its string representation</span>
<a name="l00128"></a>00128 std::string RequestMethod;
<a name="l00129"></a>00129 <span class="keywordflow">switch</span> (myMethod)
<a name="l00130"></a>00130 {
<a name="l00131"></a>00131 <span class="keywordflow">default</span> :
<a name="l00132"></a>00132 <span class="keywordflow">case</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#620f8bff6f43e1378f321bf53fbf5598f6c1b04e35501cd5f5a85207c2b37f00" title="Request in get mode, standard method to retrieve a page.">Get</a> : RequestMethod = <span class="stringliteral">"GET"</span>; <span class="keywordflow">break</span>;
<a name="l00133"></a>00133 <span class="keywordflow">case</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#620f8bff6f43e1378f321bf53fbf559887aa7a9c6147016f57c5ff1c5cac6138" title="Request in post mode, usually to send data to a page.">Post</a> : RequestMethod = <span class="stringliteral">"POST"</span>; <span class="keywordflow">break</span>;
<a name="l00134"></a>00134 <span class="keywordflow">case</span> <a class="code" href="classsf_1_1Http_1_1Request.htm#620f8bff6f43e1378f321bf53fbf5598d1642aed133fbd3cb4fcff0f558e6ee5" title="Request a page&#39;s header only.">Head</a> : RequestMethod = <span class="stringliteral">"HEAD"</span>; <span class="keywordflow">break</span>;
<a name="l00135"></a>00135 }
<a name="l00136"></a>00136
<a name="l00137"></a>00137 <span class="comment">// Write the first line containing the request type</span>
<a name="l00138"></a>00138 Out << RequestMethod << <span class="stringliteral">" "</span> << myURI << <span class="stringliteral">" "</span>;
<a name="l00139"></a>00139 Out << <span class="stringliteral">"HTTP/"</span> << myMajorVersion << <span class="stringliteral">"."</span> << myMinorVersion << <span class="stringliteral">"\r\n"</span>;
<a name="l00140"></a>00140
<a name="l00141"></a>00141 <span class="comment">// Write fields</span>
<a name="l00142"></a>00142 <span class="keywordflow">for</span> (FieldTable::const_iterator i = myFields.begin(); i != myFields.end(); ++i)
<a name="l00143"></a>00143 {
<a name="l00144"></a>00144 Out << i->first << <span class="stringliteral">": "</span> << i->second << <span class="stringliteral">"\r\n"</span>;
<a name="l00145"></a>00145 }
<a name="l00146"></a>00146
<a name="l00147"></a>00147 <span class="comment">// Use an extra \r\n to separate the header from the body</span>
<a name="l00148"></a>00148 Out << <span class="stringliteral">"\r\n"</span>;
<a name="l00149"></a>00149
<a name="l00150"></a>00150 <span class="comment">// Add the body</span>
<a name="l00151"></a>00151 Out << myBody;
<a name="l00152"></a>00152
<a name="l00153"></a>00153 <span class="keywordflow">return</span> Out.str();
<a name="l00154"></a>00154 }
<a name="l00155"></a>00155
<a name="l00156"></a>00156
<a name="l00160"></a>00160 <span class="keywordtype">bool</span> Http::Request::HasField(<span class="keyword">const</span> std::string& Field)<span class="keyword"> const</span>
<a name="l00161"></a>00161 <span class="keyword"></span>{
<a name="l00162"></a>00162 <span class="keywordflow">return</span> myFields.find(Field) != myFields.end();
<a name="l00163"></a>00163 }
<a name="l00164"></a>00164
<a name="l00165"></a>00165
<a name="l00169"></a><a class="code" href="classsf_1_1Http_1_1Response.htm#25f38ccd0ae7b485e5a061dfbbbe3dc2">00169</a> <a class="code" href="classsf_1_1Http_1_1Response.htm#25f38ccd0ae7b485e5a061dfbbbe3dc2" title="Default constructor.">Http::Response::Response</a>() :
<a name="l00170"></a>00170 myStatus (ConnectionFailed),
<a name="l00171"></a>00171 myMajorVersion(0),
<a name="l00172"></a>00172 myMinorVersion(0)
<a name="l00173"></a>00173 {
<a name="l00174"></a>00174
<a name="l00175"></a>00175 }
<a name="l00176"></a>00176
<a name="l00177"></a>00177
<a name="l00181"></a><a class="code" href="classsf_1_1Http_1_1Response.htm#d785737dcbb5c2bd686482d483f8a330">00181</a> <span class="keyword">const</span> std::string& <a class="code" href="classsf_1_1Http_1_1Response.htm#d785737dcbb5c2bd686482d483f8a330" title="Get the value of a field.">Http::Response::GetField</a>(<span class="keyword">const</span> std::string& Field)<span class="keyword"> const</span>
<a name="l00182"></a>00182 <span class="keyword"></span>{
<a name="l00183"></a>00183 FieldTable::const_iterator It = myFields.find(Field);
<a name="l00184"></a>00184 <span class="keywordflow">if</span> (It != myFields.end())
<a name="l00185"></a>00185 {
<a name="l00186"></a>00186 <span class="keywordflow">return</span> It->second;
<a name="l00187"></a>00187 }
<a name="l00188"></a>00188 <span class="keywordflow">else</span>
<a name="l00189"></a>00189 {
<a name="l00190"></a>00190 <span class="keyword">static</span> <span class="keyword">const</span> std::string Empty = <span class="stringliteral">""</span>;
<a name="l00191"></a>00191 <span class="keywordflow">return</span> Empty;
<a name="l00192"></a>00192 }
<a name="l00193"></a>00193 }
<a name="l00194"></a>00194
<a name="l00195"></a>00195
<a name="l00199"></a><a class="code" href="classsf_1_1Http_1_1Response.htm#542d55a3e0a88f0dfecb8811373669eb">00199</a> <a class="code" href="classsf_1_1Http_1_1Response.htm#663e071978e30fbbeb20ed045be874d8" title="Enumerate all the valid status codes returned in a HTTP response.">Http::Response::Status</a> <a class="code" href="classsf_1_1Http_1_1Response.htm#542d55a3e0a88f0dfecb8811373669eb" title="Get the header&#39;s status code.">Http::Response::GetStatus</a>()<span class="keyword"> const</span>
<a name="l00200"></a>00200 <span class="keyword"></span>{
<a name="l00201"></a>00201 <span class="keywordflow">return</span> myStatus;
<a name="l00202"></a>00202 }
<a name="l00203"></a>00203
<a name="l00204"></a>00204
<a name="l00208"></a><a class="code" href="classsf_1_1Http_1_1Response.htm#6a4f633420eb8a2e44b5accc4dc4e82a">00208</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classsf_1_1Http_1_1Response.htm#6a4f633420eb8a2e44b5accc4dc4e82a" title="Get the major HTTP version number of the response.">Http::Response::GetMajorHttpVersion</a>()<span class="keyword"> const</span>
<a name="l00209"></a>00209 <span class="keyword"></span>{
<a name="l00210"></a>00210 <span class="keywordflow">return</span> myMajorVersion;
<a name="l00211"></a>00211 }
<a name="l00212"></a>00212
<a name="l00213"></a>00213
<a name="l00217"></a><a class="code" href="classsf_1_1Http_1_1Response.htm#6a8663adab1be819683ec8d1ecb57a3b">00217</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classsf_1_1Http_1_1Response.htm#6a8663adab1be819683ec8d1ecb57a3b" title="Get the major HTTP version number of the response.">Http::Response::GetMinorHttpVersion</a>()<span class="keyword"> const</span>
<a name="l00218"></a>00218 <span class="keyword"></span>{
<a name="l00219"></a>00219 <span class="keywordflow">return</span> myMinorVersion;
<a name="l00220"></a>00220 }
<a name="l00221"></a>00221
<a name="l00222"></a>00222
<a name="l00230"></a><a class="code" href="classsf_1_1Http_1_1Response.htm#e9d26d9ec2e1c98b3880d7d01c49c179">00230</a> <span class="keyword">const</span> std::string& <a class="code" href="classsf_1_1Http_1_1Response.htm#e9d26d9ec2e1c98b3880d7d01c49c179" title="Get the body of the response.">Http::Response::GetBody</a>()<span class="keyword"> const</span>
<a name="l00231"></a>00231 <span class="keyword"></span>{
<a name="l00232"></a>00232 <span class="keywordflow">return</span> myBody;
<a name="l00233"></a>00233 }
<a name="l00234"></a>00234
<a name="l00235"></a>00235
<a name="l00239"></a>00239 <span class="keywordtype">void</span> Http::Response::FromString(<span class="keyword">const</span> std::string& Data)
<a name="l00240"></a>00240 {
<a name="l00241"></a>00241 std::istringstream In(Data);
<a name="l00242"></a>00242
<a name="l00243"></a>00243 <span class="comment">// Extract the HTTP version from the first line</span>
<a name="l00244"></a>00244 std::string Version;
<a name="l00245"></a>00245 <span class="keywordflow">if</span> (In >> Version)
<a name="l00246"></a>00246 {
<a name="l00247"></a>00247 <span class="keywordflow">if</span> ((Version.size() >= 8) && (Version[6] == <span class="charliteral">'.'</span>) &&
<a name="l00248"></a>00248 (ToLower(Version.substr(0, 5)) == <span class="stringliteral">"http/"</span>) &&
<a name="l00249"></a>00249 isdigit(Version[5]) && isdigit(Version[7]))
<a name="l00250"></a>00250 {
<a name="l00251"></a>00251 myMajorVersion = Version[5] - <span class="charliteral">'0'</span>;
<a name="l00252"></a>00252 myMinorVersion = Version[7] - <span class="charliteral">'0'</span>;
<a name="l00253"></a>00253 }
<a name="l00254"></a>00254 <span class="keywordflow">else</span>
<a name="l00255"></a>00255 {
<a name="l00256"></a>00256 <span class="comment">// Invalid HTTP version</span>
<a name="l00257"></a>00257 myStatus = <a class="code" href="classsf_1_1Http_1_1Response.htm#663e071978e30fbbeb20ed045be874d8acd062103f0975b1c2078bea0d91d5a8" title="Response is not a valid HTTP one.">InvalidResponse</a>;
<a name="l00258"></a>00258 <span class="keywordflow">return</span>;
<a name="l00259"></a>00259 }
<a name="l00260"></a>00260 }
<a name="l00261"></a>00261
<a name="l00262"></a>00262 <span class="comment">// Extract the status code from the first line</span>
<a name="l00263"></a>00263 <span class="keywordtype">int</span> StatusCode;
<a name="l00264"></a>00264 <span class="keywordflow">if</span> (In >> StatusCode)
<a name="l00265"></a>00265 {
<a name="l00266"></a>00266 myStatus = <span class="keyword">static_cast<</span><a class="code" href="classsf_1_1Http_1_1Response.htm#663e071978e30fbbeb20ed045be874d8" title="Enumerate all the valid status codes returned in a HTTP response.">Status</a><span class="keyword">></span>(StatusCode);
<a name="l00267"></a>00267 }
<a name="l00268"></a>00268 <span class="keywordflow">else</span>
<a name="l00269"></a>00269 {
<a name="l00270"></a>00270 <span class="comment">// Invalid status code</span>
<a name="l00271"></a>00271 myStatus = <a class="code" href="classsf_1_1Http_1_1Response.htm#663e071978e30fbbeb20ed045be874d8acd062103f0975b1c2078bea0d91d5a8" title="Response is not a valid HTTP one.">InvalidResponse</a>;
<a name="l00272"></a>00272 <span class="keywordflow">return</span>;
<a name="l00273"></a>00273 }
<a name="l00274"></a>00274
<a name="l00275"></a>00275 <span class="comment">// Ignore the end of the first line</span>
<a name="l00276"></a>00276 In.ignore(10000, <span class="charliteral">'\n'</span>);
<a name="l00277"></a>00277
<a name="l00278"></a>00278 <span class="comment">// Parse the other lines, which contain fields, one by one</span>
<a name="l00279"></a>00279 std::string Line;
<a name="l00280"></a>00280 <span class="keywordflow">while</span> (std::getline(In, Line) && (Line.size() > 2))
<a name="l00281"></a>00281 {
<a name="l00282"></a>00282 std::string::size_type Pos = Line.find(<span class="stringliteral">": "</span>);
<a name="l00283"></a>00283 <span class="keywordflow">if</span> (Pos != std::string::npos)
<a name="l00284"></a>00284 {
<a name="l00285"></a>00285 <span class="comment">// Extract the field name and its value</span>
<a name="l00286"></a>00286 std::string Field = Line.substr(0, Pos);
<a name="l00287"></a>00287 std::string Value = Line.substr(Pos + 2);
<a name="l00288"></a>00288
<a name="l00289"></a>00289 <span class="comment">// Remove any trailing \r</span>
<a name="l00290"></a>00290 <span class="keywordflow">if</span> (!Value.empty() && (*Value.rbegin() == <span class="charliteral">'\r'</span>))
<a name="l00291"></a>00291 Value.erase(Value.size() - 1);
<a name="l00292"></a>00292
<a name="l00293"></a>00293 <span class="comment">// Add the field</span>
<a name="l00294"></a>00294 myFields[ToLower(Field)] = Value;
<a name="l00295"></a>00295 }
<a name="l00296"></a>00296 }
<a name="l00297"></a>00297
<a name="l00298"></a>00298 <span class="comment">// Finally extract the body</span>
<a name="l00299"></a>00299 myBody.clear();
<a name="l00300"></a>00300 <span class="keywordflow">while</span> (std::getline(In, Line))
<a name="l00301"></a>00301 myBody += Line + <span class="stringliteral">"\n"</span>;
<a name="l00302"></a>00302 }
<a name="l00303"></a>00303
<a name="l00304"></a>00304
<a name="l00308"></a><a class="code" href="classsf_1_1Http.htm#be2360194f99bdde402c9f97a85cf067">00308</a> <a class="code" href="classsf_1_1Http.htm#be2360194f99bdde402c9f97a85cf067" title="Default constructor.">Http::Http</a>() :
<a name="l00309"></a>00309 myHost(),
<a name="l00310"></a>00310 myPort(0)
<a name="l00311"></a>00311 {
<a name="l00312"></a>00312
<a name="l00313"></a>00313 }
<a name="l00314"></a>00314
<a name="l00315"></a>00315
<a name="l00319"></a><a class="code" href="classsf_1_1Http.htm#283ce40b04ef678916834b8032796677">00319</a> <a class="code" href="classsf_1_1Http.htm#be2360194f99bdde402c9f97a85cf067" title="Default constructor.">Http::Http</a>(<span class="keyword">const</span> std::string& Host, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> Port)
<a name="l00320"></a>00320 {
<a name="l00321"></a>00321 <a class="code" href="classsf_1_1Http.htm#233fb3eabfeb22a442a0db92e38aee1f" title="Set the target host.">SetHost</a>(Host, Port);
<a name="l00322"></a>00322 }
<a name="l00323"></a>00323
<a name="l00324"></a>00324
<a name="l00328"></a><a class="code" href="classsf_1_1Http.htm#233fb3eabfeb22a442a0db92e38aee1f">00328</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1Http.htm#233fb3eabfeb22a442a0db92e38aee1f" title="Set the target host.">Http::SetHost</a>(<span class="keyword">const</span> std::string& Host, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> Port)
<a name="l00329"></a>00329 {
<a name="l00330"></a>00330 <span class="comment">// Detect the protocol used</span>
<a name="l00331"></a>00331 std::string Protocol = ToLower(Host.substr(0, 8));
<a name="l00332"></a>00332 <span class="keywordflow">if</span> (Protocol.substr(0, 7) == <span class="stringliteral">"http://"</span>)
<a name="l00333"></a>00333 {
<a name="l00334"></a>00334 <span class="comment">// HTTP protocol</span>
<a name="l00335"></a>00335 myHostName = Host.substr(7);
<a name="l00336"></a>00336 myPort = (Port != 0 ? Port : 80);
<a name="l00337"></a>00337 }
<a name="l00338"></a>00338 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (Protocol == <span class="stringliteral">"https://"</span>)
<a name="l00339"></a>00339 {
<a name="l00340"></a>00340 <span class="comment">// HTTPS protocol</span>
<a name="l00341"></a>00341 myHostName = Host.substr(8);
<a name="l00342"></a>00342 myPort = (Port != 0 ? Port : 443);
<a name="l00343"></a>00343 }
<a name="l00344"></a>00344 <span class="keywordflow">else</span>
<a name="l00345"></a>00345 {
<a name="l00346"></a>00346 <span class="comment">// Undefined protocol - use HTTP</span>
<a name="l00347"></a>00347 myHostName = Host;
<a name="l00348"></a>00348 myPort = (Port != 0 ? Port : 80);
<a name="l00349"></a>00349 }
<a name="l00350"></a>00350
<a name="l00351"></a>00351 <span class="comment">// Remove any trailing '/' from the host name</span>
<a name="l00352"></a>00352 <span class="keywordflow">if</span> (!myHostName.empty() && (*myHostName.rbegin() == <span class="charliteral">'/'</span>))
<a name="l00353"></a>00353 myHostName.erase(myHostName.size() - 1);
<a name="l00354"></a>00354
<a name="l00355"></a>00355 myHost = <a class="code" href="classsf_1_1IPAddress.htm" title="IPAddress provides easy manipulation of IP v4 addresses.">sf::IPAddress</a>(myHostName);
<a name="l00356"></a>00356 }
<a name="l00357"></a>00357
<a name="l00358"></a>00358
<a name="l00367"></a><a class="code" href="classsf_1_1Http.htm#25a88ce405f43feb9faf4480ea3c2d61">00367</a> <a class="code" href="classsf_1_1Http_1_1Response.htm" title="This class wraps an HTTP response, which is basically :a header with a status code...">Http::Response</a> <a class="code" href="classsf_1_1Http.htm#25a88ce405f43feb9faf4480ea3c2d61" title="Send a HTTP request and return the server&#39;s response.">Http::SendRequest</a>(<span class="keyword">const</span> <a class="code" href="classsf_1_1Http_1_1Request.htm" title="This class wraps an HTTP request, which is basically :a header with a method, a target...">Http::Request</a>& Req, <span class="keywordtype">float</span> Timeout)
<a name="l00368"></a>00368 {
<a name="l00369"></a>00369 <span class="comment">// First make sure the request is valid -- add missing mandatory fields</span>
<a name="l00370"></a>00370 <a class="code" href="classsf_1_1Http_1_1Request.htm" title="This class wraps an HTTP request, which is basically :a header with a method, a target...">Request</a> ToSend(Req);
<a name="l00371"></a>00371 <span class="keywordflow">if</span> (!ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#e36519e1f04dd593eed75e374f7cfb3f" title="Check if the given field has been defined.">HasField</a>(<span class="stringliteral">"From"</span>))
<a name="l00372"></a>00372 {
<a name="l00373"></a>00373 ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#a48d6b67d701622333564dfa1094b41e" title="Set the value of a field; the field is added if it doesn&#39;t exist.">SetField</a>(<span class="stringliteral">"From"</span>, <span class="stringliteral">"user@sfml-dev.org"</span>);
<a name="l00374"></a>00374 }
<a name="l00375"></a>00375 <span class="keywordflow">if</span> (!ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#e36519e1f04dd593eed75e374f7cfb3f" title="Check if the given field has been defined.">HasField</a>(<span class="stringliteral">"User-Agent"</span>))
<a name="l00376"></a>00376 {
<a name="l00377"></a>00377 ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#a48d6b67d701622333564dfa1094b41e" title="Set the value of a field; the field is added if it doesn&#39;t exist.">SetField</a>(<span class="stringliteral">"User-Agent"</span>, <span class="stringliteral">"libsfml-network/1.x"</span>);
<a name="l00378"></a>00378 }
<a name="l00379"></a>00379 <span class="keywordflow">if</span> (!ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#e36519e1f04dd593eed75e374f7cfb3f" title="Check if the given field has been defined.">HasField</a>(<span class="stringliteral">"Host"</span>))
<a name="l00380"></a>00380 {
<a name="l00381"></a>00381 ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#a48d6b67d701622333564dfa1094b41e" title="Set the value of a field; the field is added if it doesn&#39;t exist.">SetField</a>(<span class="stringliteral">"Host"</span>, myHostName);
<a name="l00382"></a>00382 }
<a name="l00383"></a>00383 <span class="keywordflow">if</span> (!ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#e36519e1f04dd593eed75e374f7cfb3f" title="Check if the given field has been defined.">HasField</a>(<span class="stringliteral">"Content-Length"</span>))
<a name="l00384"></a>00384 {
<a name="l00385"></a>00385 std::ostringstream Out;
<a name="l00386"></a>00386 Out << ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#d05e3c9b4ec87de6dfbfb5b57f2af15f" title="Body of the request.">myBody</a>.size();
<a name="l00387"></a>00387 ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#a48d6b67d701622333564dfa1094b41e" title="Set the value of a field; the field is added if it doesn&#39;t exist.">SetField</a>(<span class="stringliteral">"Content-Length"</span>, Out.str());
<a name="l00388"></a>00388 }
<a name="l00389"></a>00389
<a name="l00390"></a>00390 <span class="comment">// Prepare the response</span>
<a name="l00391"></a>00391 <a class="code" href="classsf_1_1Http_1_1Response.htm" title="This class wraps an HTTP response, which is basically :a header with a status code...">Response</a> Received;
<a name="l00392"></a>00392
<a name="l00393"></a>00393 <span class="comment">// Connect the socket to the host</span>
<a name="l00394"></a>00394 <span class="keywordflow">if</span> (myConnection.<a class="code" href="classsf_1_1SocketTCP.htm#dd590d0119fa8edbe8da1574b6c8f07f" title="Connect to another computer on a specified port.">Connect</a>(myPort, myHost, Timeout) == Socket::Done)
<a name="l00395"></a>00395 {
<a name="l00396"></a>00396 <span class="comment">// Convert the request to string and send it through the connected socket</span>
<a name="l00397"></a>00397 std::string RequestStr = ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#eb12e7a769fbcf1d9848bc008c28fa1f" title="Get the string representation of the request header.">ToString</a>();
<a name="l00398"></a>00398
<a name="l00399"></a>00399 <span class="keywordflow">if</span> (!RequestStr.empty())
<a name="l00400"></a>00400 {
<a name="l00401"></a>00401 <span class="comment">// Send it through the socket</span>
<a name="l00402"></a>00402 <span class="keywordflow">if</span> (myConnection.<a class="code" href="classsf_1_1SocketTCP.htm#4debed568966f75e3e63a70f08e67c21" title="Send an array of bytes to the host (must be connected first).">Send</a>(RequestStr.c_str(), RequestStr.size()) == sf::Socket::Done)
<a name="l00403"></a>00403 {
<a name="l00404"></a>00404 <span class="comment">// Wait for the server's response</span>
<a name="l00405"></a>00405 std::string ReceivedStr;
<a name="l00406"></a>00406 std::size_t Size = 0;
<a name="l00407"></a>00407 <span class="keywordtype">char</span> Buffer[1024];
<a name="l00408"></a>00408 <span class="keywordflow">while</span> (myConnection.<a class="code" href="classsf_1_1SocketTCP.htm#47027b00ab5589017a5c235f2deb3ab0" title="Receive an array of bytes from the host (must be connected first).">Receive</a>(Buffer, <span class="keyword">sizeof</span>(Buffer), Size) == sf::Socket::Done)
<a name="l00409"></a>00409 {
<a name="l00410"></a>00410 ReceivedStr.append(Buffer, Buffer + Size);
<a name="l00411"></a>00411 }
<a name="l00412"></a>00412
<a name="l00413"></a>00413 <span class="comment">// Build the Response object from the received data</span>
<a name="l00414"></a>00414 Received.<a class="code" href="classsf_1_1Http_1_1Response.htm#a51fa3ff7680d14eb8cb072ef6d65d1e" title="Construct the header from a response string.">FromString</a>(ReceivedStr);
<a name="l00415"></a>00415 }
<a name="l00416"></a>00416 }
<a name="l00417"></a>00417
<a name="l00418"></a>00418 <span class="comment">// Close the connection</span>
<a name="l00419"></a>00419 myConnection.<a class="code" href="classsf_1_1SocketTCP.htm#70a5111cbddd1edbfa6cbd6017c6d044" title="Close the socket.">Close</a>();
<a name="l00420"></a>00420 }
<a name="l00421"></a>00421
<a name="l00422"></a>00422 <span class="keywordflow">return</span> Received;
<a name="l00423"></a>00423 }
<a name="l00424"></a>00424
<a name="l00425"></a>00425 } <span class="comment">// namespace sf</span>
</pre></div></div>
<p id="footer">
:: Copyright © 2007-2008 Laurent Gomila, all rights reserved ::
Documentation generated by <a href="http://www.doxygen.org/" title="doxygen website">doxygen 1.5.2</a> ::
</p>
</body>
</html>
|