Http.hpp
00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00025 #ifndef SFML_HTTP_HPP
00026 #define SFML_HTTP_HPP
00027 
00029 // Headers
00031 #include <SFML/System/NonCopyable.hpp>
00032 #include <SFML/Network/IpAddress.hpp>
00033 #include <SFML/Network/TcpSocket.hpp>
00034 #include <map>
00035 #include <string>
00036 
00037 
00038 namespace sf
00039 {
00044 class SFML_API Http : NonCopyable
00045 {
00046 public :
00047 
00052     class SFML_API Request
00053     {
00054     public :
00055 
00060         enum Method
00061         {
00062             Get,  
00063             Post, 
00064             Head  
00065         };
00066 
00078         Request(const std::string& uri = "/", Method method = Get, const std::string& body = "");
00079 
00093         void SetField(const std::string& field, const std::string& value);
00094 
00105         void SetMethod(Method method);
00106 
00117         void SetUri(const std::string& uri);
00118 
00128         void SetHttpVersion(unsigned int major, unsigned int minor);
00129 
00140         void SetBody(const std::string& body);
00141 
00142     private :
00143 
00144         friend class Http;
00145 
00155         std::string Prepare() const;
00156 
00167         bool HasField(const std::string& field) const;
00168 
00170         // Types
00172         typedef std::map<std::string, std::string> FieldTable;
00173 
00175         // Member data
00177         FieldTable   myFields;       
00178         Method       myMethod;       
00179         std::string  myURI;          
00180         unsigned int myMajorVersion; 
00181         unsigned int myMinorVersion; 
00182         std::string  myBody;         
00183     };
00184 
00189     class SFML_API Response
00190     {
00191     public :
00192 
00197         enum Status
00198         {
00199             // 2xx: success
00200             Ok             = 200, 
00201             Created        = 201, 
00202             Accepted       = 202, 
00203             NoContent      = 204, 
00204             ResetContent   = 205, 
00205             PartialContent = 206, 
00206 
00207             // 3xx: redirection
00208             MultipleChoices  = 300, 
00209             MovedPermanently = 301, 
00210             MovedTemporarily = 302, 
00211             NotModified      = 304, 
00212 
00213             // 4xx: client error
00214             BadRequest          = 400, 
00215             Unauthorized        = 401, 
00216             Forbidden           = 403, 
00217             NotFound            = 404, 
00218             RangeNotSatisfiable = 407, 
00219 
00220             // 5xx: server error
00221             InternalServerError = 500, 
00222             NotImplemented      = 501, 
00223             BadGateway          = 502, 
00224             ServiceNotAvailable = 503, 
00225             GatewayTimeout      = 504, 
00226             VersionNotSupported = 505, 
00227 
00228             // 10xx: SFML custom codes
00229             InvalidResponse  = 1000, 
00230             ConnectionFailed = 1001  
00231         };
00232 
00239         Response();
00240 
00253         const std::string& GetField(const std::string& field) const;
00254 
00266         Status GetStatus() const;
00267 
00276         unsigned int GetMajorHttpVersion() const;
00277 
00286         unsigned int GetMinorHttpVersion() const;
00287 
00300         const std::string& GetBody() const;
00301 
00302     private :
00303 
00304         friend class Http;
00305 
00315         void Parse(const std::string& data);
00316 
00318         // Types
00320         typedef std::map<std::string, std::string> FieldTable;
00321 
00323         // Member data
00325         FieldTable   myFields;       
00326         Status       myStatus;       
00327         unsigned int myMajorVersion; 
00328         unsigned int myMinorVersion; 
00329         std::string  myBody;         
00330     };
00331 
00336     Http();
00337 
00352     Http(const std::string& host, unsigned short port = 0);
00353 
00369     void SetHost(const std::string& host, unsigned short port = 0);
00370 
00389     Response SendRequest(const Request& request, Uint32 timeout = 0);
00390 
00391 private :
00392 
00394     // Member data
00396     TcpSocket      myConnection; 
00397     IpAddress      myHost;       
00398     std::string    myHostName;   
00399     unsigned short myPort;       
00400 };
00401 
00402 } // namespace sf
00403 
00404 
00405 #endif // SFML_HTTP_HPP
00406 
00407