diff options
author | Christoph Egger <Christoph.Egger@gmx.de> | 2010-04-07 22:00:13 +0200 |
---|---|---|
committer | Christoph Egger <Christoph.Egger@gmx.de> | 2010-04-07 22:00:13 +0200 |
commit | 46bf1cf82d855660c212ee3421dff3050ec8ffea (patch) | |
tree | f0fadfc004a86242ad096dee015ed7c20cc7f554 /doc/html/Http_8cpp-source.htm | |
parent | 25177f23f35afa0e6ebdd63d581003c298d3a7df (diff) |
Imported Upstream version 1.6+repack1
Diffstat (limited to 'doc/html/Http_8cpp-source.htm')
-rwxr-xr-x | doc/html/Http_8cpp-source.htm | 271 |
1 files changed, 139 insertions, 132 deletions
diff --git a/doc/html/Http_8cpp-source.htm b/doc/html/Http_8cpp-source.htm index a084182..dd4b1d3 100755 --- a/doc/html/Http_8cpp-source.htm +++ b/doc/html/Http_8cpp-source.htm @@ -51,33 +51,33 @@ <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="l00030"></a>00030 <span class="preprocessor">#include <algorithm></span> +<a name="l00031"></a>00031 <span class="preprocessor">#include <iterator></span> +<a name="l00032"></a>00032 <span class="preprocessor">#include <sstream></span> +<a name="l00033"></a>00033 +<a name="l00034"></a>00034 +<a name="l00035"></a>00035 <span class="keyword">namespace</span> +<a name="l00036"></a>00036 { +<a name="l00038"></a>00038 <span class="comment">// Convenience function to convert a string to lower case</span> +<a name="l00040"></a>00040 <span class="comment"></span> std::string ToLower(<span class="keyword">const</span> std::string& Str) +<a name="l00041"></a>00041 { +<a name="l00042"></a>00042 std::string Ret = Str; +<a name="l00043"></a>00043 <span class="keywordflow">for</span> (std::string::iterator i = Ret.begin(); i != Ret.end(); ++i) +<a name="l00044"></a>00044 *i = static_cast<char>(tolower(*i)); +<a name="l00045"></a>00045 +<a name="l00046"></a>00046 <span class="keywordflow">return</span> Ret; +<a name="l00047"></a>00047 } +<a name="l00048"></a>00048 } +<a name="l00049"></a>00049 +<a name="l00050"></a>00050 +<a name="l00051"></a>00051 <span class="keyword">namespace </span>sf +<a name="l00052"></a>00052 { +<a name="l00056"></a><a class="code" href="classsf_1_1Http_1_1Request.htm#55e79368ff0d72568150b3823ee0eb72">00056</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="l00057"></a>00057 { +<a name="l00058"></a>00058 <a class="code" href="classsf_1_1Http_1_1Request.htm#2b17bdbb01172b4bfc8bea4a661a1c53" title="Set the request method.">SetMethod</a>(RequestMethod); +<a name="l00059"></a>00059 <a class="code" href="classsf_1_1Http_1_1Request.htm#2d92b4bf89606acb3e6ac8443b427aac" title="Set the target URI of the request.">SetURI</a>(URI); +<a name="l00060"></a>00060 <a class="code" href="classsf_1_1Http_1_1Request.htm#a168e9482ea54c26868676b0eda6dabd" title="Set the HTTP version of the request.">SetHttpVersion</a>(1, 0); +<a name="l00061"></a>00061 <a class="code" href="classsf_1_1Http_1_1Request.htm#8f8b740b23e13d757e8dabd634eb8e46" title="Set the body of the request.">SetBody</a>(Body); <a name="l00062"></a>00062 } <a name="l00063"></a>00063 <a name="l00064"></a>00064 @@ -167,7 +167,7 @@ <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="l00183"></a>00183 FieldTable::const_iterator It = myFields.find(ToLower(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; @@ -265,115 +265,122 @@ <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="l00300"></a>00300 std::copy(std::istreambuf_iterator<char>(In), std::istreambuf_iterator<char>(), std::back_inserter(myBody)); +<a name="l00301"></a>00301 } +<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="l00307"></a><a class="code" href="classsf_1_1Http.htm#be2360194f99bdde402c9f97a85cf067">00307</a> <a class="code" href="classsf_1_1Http.htm#be2360194f99bdde402c9f97a85cf067" title="Default constructor.">Http::Http</a>() : +<a name="l00308"></a>00308 myHost(), +<a name="l00309"></a>00309 myPort(0) +<a name="l00310"></a>00310 { +<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="l00318"></a><a class="code" href="classsf_1_1Http.htm#283ce40b04ef678916834b8032796677">00318</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="l00319"></a>00319 { +<a name="l00320"></a>00320 <a class="code" href="classsf_1_1Http.htm#233fb3eabfeb22a442a0db92e38aee1f" title="Set the target host.">SetHost</a>(Host, Port); +<a name="l00321"></a>00321 } +<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="l00327"></a><a class="code" href="classsf_1_1Http.htm#233fb3eabfeb22a442a0db92e38aee1f">00327</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="l00328"></a>00328 { +<a name="l00329"></a>00329 <span class="comment">// Detect the protocol used</span> +<a name="l00330"></a>00330 std::string Protocol = ToLower(Host.substr(0, 8)); +<a name="l00331"></a>00331 <span class="keywordflow">if</span> (Protocol.substr(0, 7) == <span class="stringliteral">"http://"</span>) +<a name="l00332"></a>00332 { +<a name="l00333"></a>00333 <span class="comment">// HTTP protocol</span> +<a name="l00334"></a>00334 myHostName = Host.substr(7); +<a name="l00335"></a>00335 myPort = (Port != 0 ? Port : 80); +<a name="l00336"></a>00336 } +<a name="l00337"></a>00337 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (Protocol == <span class="stringliteral">"https://"</span>) +<a name="l00338"></a>00338 { +<a name="l00339"></a>00339 <span class="comment">// HTTPS protocol</span> +<a name="l00340"></a>00340 myHostName = Host.substr(8); +<a name="l00341"></a>00341 myPort = (Port != 0 ? Port : 443); +<a name="l00342"></a>00342 } +<a name="l00343"></a>00343 <span class="keywordflow">else</span> +<a name="l00344"></a>00344 { +<a name="l00345"></a>00345 <span class="comment">// Undefined protocol - use HTTP</span> +<a name="l00346"></a>00346 myHostName = Host; +<a name="l00347"></a>00347 myPort = (Port != 0 ? Port : 80); +<a name="l00348"></a>00348 } +<a name="l00349"></a>00349 +<a name="l00350"></a>00350 <span class="comment">// Remove any trailing '/' from the host name</span> +<a name="l00351"></a>00351 <span class="keywordflow">if</span> (!myHostName.empty() && (*myHostName.rbegin() == <span class="charliteral">'/'</span>)) +<a name="l00352"></a>00352 myHostName.erase(myHostName.size() - 1); +<a name="l00353"></a>00353 +<a name="l00354"></a>00354 myHost = <a class="code" href="classsf_1_1IPAddress.htm" title="IPAddress provides easy manipulation of IP v4 addresses.">sf::IPAddress</a>(myHostName); +<a name="l00355"></a>00355 } +<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="l00366"></a><a class="code" href="classsf_1_1Http.htm#25a88ce405f43feb9faf4480ea3c2d61">00366</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="l00367"></a>00367 { +<a name="l00368"></a>00368 <span class="comment">// First make sure the request is valid -- add missing mandatory fields</span> +<a name="l00369"></a>00369 <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="l00370"></a>00370 <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="l00371"></a>00371 { +<a name="l00372"></a>00372 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="l00373"></a>00373 } +<a name="l00374"></a>00374 <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="l00375"></a>00375 { +<a name="l00376"></a>00376 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="l00377"></a>00377 } +<a name="l00378"></a>00378 <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="l00379"></a>00379 { +<a name="l00380"></a>00380 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="l00381"></a>00381 } +<a name="l00382"></a>00382 <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="l00383"></a>00383 { +<a name="l00384"></a>00384 std::ostringstream Out; +<a name="l00385"></a>00385 Out << ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#d05e3c9b4ec87de6dfbfb5b57f2af15f" title="Body of the request.">myBody</a>.size(); +<a name="l00386"></a>00386 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="l00387"></a>00387 } +<a name="l00388"></a>00388 <span class="keywordflow">if</span> ((ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#12ed125a83b352316266471e9e64e77c" title="Method to use for the request.">myMethod</a> == <a class="code" href="classsf_1_1Http_1_1Request.htm#620f8bff6f43e1378f321bf53fbf559887aa7a9c6147016f57c5ff1c5cac6138" title="Request in post mode, usually to send data to a page.">Request::Post</a>) && !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-Type"</span>)) +<a name="l00389"></a>00389 { +<a name="l00390"></a>00390 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-Type"</span>, <span class="stringliteral">"application/x-www-form-urlencoded"</span>); +<a name="l00391"></a>00391 } +<a name="l00392"></a>00392 <span class="keywordflow">if</span> ((ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#6e4db25ee0455e2eb87f5d5c62c830f5" title="Major HTTP version.">myMajorVersion</a> * 10 + ToSend.<a class="code" href="classsf_1_1Http_1_1Request.htm#d9201bb503871d3989975cbdd5ab494a" title="Minor HTTP version.">myMinorVersion</a> >= 11) && !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">"Connection"</span>)) +<a name="l00393"></a>00393 { +<a name="l00394"></a>00394 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">"Connection"</span>, <span class="stringliteral">"close"</span>); +<a name="l00395"></a>00395 } +<a name="l00396"></a>00396 +<a name="l00397"></a>00397 <span class="comment">// Prepare the response</span> +<a name="l00398"></a>00398 <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="l00399"></a>00399 +<a name="l00400"></a>00400 <span class="comment">// Connect the socket to the host</span> +<a name="l00401"></a>00401 <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="l00402"></a>00402 { +<a name="l00403"></a>00403 <span class="comment">// Convert the request to string and send it through the connected socket</span> +<a name="l00404"></a>00404 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="l00405"></a>00405 +<a name="l00406"></a>00406 <span class="keywordflow">if</span> (!RequestStr.empty()) +<a name="l00407"></a>00407 { +<a name="l00408"></a>00408 <span class="comment">// Send it through the socket</span> +<a name="l00409"></a>00409 <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="l00410"></a>00410 { +<a name="l00411"></a>00411 <span class="comment">// Wait for the server's response</span> +<a name="l00412"></a>00412 std::string ReceivedStr; +<a name="l00413"></a>00413 std::size_t Size = 0; +<a name="l00414"></a>00414 <span class="keywordtype">char</span> Buffer[1024]; +<a name="l00415"></a>00415 <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="l00416"></a>00416 { +<a name="l00417"></a>00417 ReceivedStr.append(Buffer, Buffer + Size); +<a name="l00418"></a>00418 } +<a name="l00419"></a>00419 +<a name="l00420"></a>00420 <span class="comment">// Build the Response object from the received data</span> +<a name="l00421"></a>00421 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="l00422"></a>00422 } +<a name="l00423"></a>00423 } <a name="l00424"></a>00424 -<a name="l00425"></a>00425 } <span class="comment">// namespace sf</span> +<a name="l00425"></a>00425 <span class="comment">// Close the connection</span> +<a name="l00426"></a>00426 myConnection.<a class="code" href="classsf_1_1SocketTCP.htm#70a5111cbddd1edbfa6cbd6017c6d044" title="Close the socket.">Close</a>(); +<a name="l00427"></a>00427 } +<a name="l00428"></a>00428 +<a name="l00429"></a>00429 <span class="keywordflow">return</span> Received; +<a name="l00430"></a>00430 } +<a name="l00431"></a>00431 +<a name="l00432"></a>00432 } <span class="comment">// namespace sf</span> </pre></div></div> <p id="footer"> |