PixelLightAPI  .
HttpClient.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: HttpClient.h                                   *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_HTTPCLIENT_H__
00024 #define __PLCORE_HTTPCLIENT_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/Network/Client.h"
00032 #include "PLCore/Network/Http/Http.h"
00033 #include "PLCore/Network/Http/HttpHeader.h"
00034 
00035 
00036 //[-------------------------------------------------------]
00037 //[ Namespace                                             ]
00038 //[-------------------------------------------------------]
00039 namespace PLCore {
00040 
00041 
00042 //[-------------------------------------------------------]
00043 //[ Forward declarations                                  ]
00044 //[-------------------------------------------------------]
00045 class HttpClientConnection;
00046 
00047 
00048 //[-------------------------------------------------------]
00049 //[ Classes                                               ]
00050 //[-------------------------------------------------------]
00051 /**
00052 *  @brief
00053 *    Http server
00054 */
00055 class HttpClient : protected Client {
00056 
00057 
00058     //[-------------------------------------------------------]
00059     //[ Public functions                                      ]
00060     //[-------------------------------------------------------]
00061     public:
00062         //[-------------------------------------------------------]
00063         //[ General                                               ]
00064         //[-------------------------------------------------------]
00065         /**
00066         *  @brief
00067         *    Constructor
00068         */
00069         PLCORE_API HttpClient();
00070 
00071         /**
00072         *  @brief
00073         *    Destructor
00074         */
00075         PLCORE_API virtual ~HttpClient();
00076 
00077         //[-------------------------------------------------------]
00078         //[ Options                                               ]
00079         //[-------------------------------------------------------]
00080         /**
00081         *  @brief
00082         *    Get client signature
00083         *
00084         *  @return
00085         *    Client signature
00086         */
00087         inline EClientSignature GetClientSignature() const;
00088 
00089         /**
00090         *  @brief
00091         *    Set client signature
00092         *
00093         *  @param[in] nClientSignature
00094         *    Client signature
00095         *
00096         *  @remarks
00097         *    Usually, this HTTP client will send "PixelLight HTTP Client" as it's client
00098         *    identification, so the server will know, who we are ;-) If you want to mimic
00099         *    other clients, you can set this to another value, e.g. to mimic a Mozilla client.
00100         */
00101         inline void SetClientSignature(EClientSignature nClientSignature);
00102 
00103         /**
00104         *  @brief
00105         *    Get protocol version
00106         *
00107         *  @return
00108         *    HTTP protocol version
00109         */
00110         inline EHttpProtocol GetProtocol() const;
00111 
00112         /**
00113         *  @brief
00114         *    Set protocol version
00115         *
00116         *  @param[in] nProtocol
00117         *    HTTP protocol version
00118         */
00119         inline void SetProtocol(EHttpProtocol nProtocol);
00120 
00121         /**
00122         *  @brief
00123         *    Get connection type
00124         *
00125         *  @return
00126         *    HTTP connection type
00127         */
00128         inline EHttpConnection GetConnectionType() const;
00129 
00130         /**
00131         *  @brief
00132         *    Set connection type
00133         *
00134         *  @param[in] nConnection
00135         *    HTTP connection type
00136         */
00137         inline void SetConnectionType(EHttpConnection nConnection);
00138 
00139         /**
00140         *  @brief
00141         *    Get authentication
00142         *
00143         *  @return
00144         *    HTTP authentication type
00145         */
00146         inline EHttpAuth GetAuthentication() const;
00147 
00148         /**
00149         *  @brief
00150         *    Set authentication
00151         *
00152         *  @param[in] nAuth
00153         *    HTTP authentication type
00154         *  @param[in] sUsername
00155         *    User name
00156         *  @param[in] sPassword
00157         *    Password
00158         */
00159         inline void SetAuthentication(EHttpAuth nAuth, const String &sUsername, const String &sPassword);
00160 
00161         //[-------------------------------------------------------]
00162         //[ Functions                                             ]
00163         //[-------------------------------------------------------]
00164         /**
00165         *  @brief
00166         *    Check if a connection is open
00167         *
00168         *  @return
00169         *    'true' if connection is open, else 'false'
00170         */
00171         inline bool IsConnected() const;
00172 
00173         /**
00174         *  @brief
00175         *    Open connection to server
00176         *
00177         *  @param[in] sAddress
00178         *    Server address
00179         *  @param[in] nPort
00180         *    Port number (Default: 80)
00181         *
00182         *  @return
00183         *    'true' if all went fine, else 'false'
00184         */
00185         PLCORE_API bool Open(const String &sAddress, uint32 nPort = 80);
00186 
00187         /**
00188         *  @brief
00189         *    Close HTTP session
00190         */
00191         PLCORE_API void Disconnect();
00192 
00193         /**
00194         *  @brief
00195         *    Open a HTTP resource (GET command)
00196         *
00197         *  @param[in] sURL
00198         *    URL
00199         *
00200         *  @return
00201         *    'true' if all went fine, else 'false'
00202         */
00203         PLCORE_API bool Get(const String &sURL);
00204 
00205         /**
00206         *  @brief
00207         *    Open a HTTP resource and get partial data (GET command)
00208         *
00209         *  @param[in] sURL
00210         *    URL
00211         *  @param[in] nPos
00212         *    Start offset
00213         *  @param[in] nSize
00214         *    Size of data to read (-1 for all)
00215         *
00216         *  @return
00217         *    'true' if all went fine, else 'false'
00218         */
00219         PLCORE_API bool GetPartial(const String &sURL, uint32 nPos, int32 nSize = -1);
00220 
00221         /**
00222         *  @brief
00223         *    Open a HTTP resource (POST command)
00224         *
00225         *  @param[in] sURL
00226         *    URL
00227         *  @param[in] sPostData
00228         *    Additional data for POST command
00229         *
00230         *  @return
00231         *    'true' if all went fine, else 'false'
00232         */
00233         PLCORE_API bool Post(const String &sURL, const String &sPostData);
00234 
00235         /**
00236         *  @brief
00237         *    Delete a HTTP resource (DELETE command)
00238         *
00239         *  @param[in] sURL
00240         *    URL
00241         *
00242         *  @return
00243         *    'true' if all went fine, else 'false'
00244         */
00245         PLCORE_API bool Delete(const String &sURL);
00246 
00247         /**
00248         *  @brief
00249         *    Get HTTP header sent from server
00250         *
00251         *  @return
00252         *    Header of HTTP answer from server
00253         */
00254         inline const HttpHeader &GetHeader() const;
00255 
00256         /**
00257         *  @brief
00258         *    Read data from HTTP connection
00259         *
00260         *  @param[in] pBuffer
00261         *    Data buffer
00262         *  @param[in] nSize
00263         *    Size in bytes
00264         *
00265         *  @return
00266         *    Total number of bytes read. Can be less than the requested size
00267         *    to be sent, negative value on error
00268         */
00269         PLCORE_API int Read(char *pBuffer, uint32 nSize);
00270 
00271 
00272     //[-------------------------------------------------------]
00273     //[ Protected virtual Client functions                    ]
00274     //[-------------------------------------------------------]
00275     protected:
00276         PLCORE_API virtual Connection *CreateOutgoingConnection() override;
00277         PLCORE_API virtual void OnDisconnect(Connection &cConnection) override;
00278 
00279 
00280     //[-------------------------------------------------------]
00281     //[ Private functions                                     ]
00282     //[-------------------------------------------------------]
00283     private:
00284         /**
00285         *  @brief
00286         *    Copy constructor
00287         *
00288         *  @param[in] cClient
00289         *    HTTP client
00290         */
00291         HttpClient(const HttpClient &cClient);
00292 
00293         /**
00294         *  @brief
00295         *    Copy operator
00296         *
00297         *  @param[in] cHttpClient
00298         *    HTTP client
00299         *
00300         *  @return
00301         *    Reference to this client
00302         */
00303         HttpClient &operator =(const HttpClient &cClient);
00304 
00305 
00306     //[-------------------------------------------------------]
00307     //[ Private data                                          ]
00308     //[-------------------------------------------------------]
00309     private:
00310         EClientSignature         m_nClientSignature;    /**< Used client signature (Default: ClientPixelLight) */
00311         EHttpProtocol            m_nHttpProtocol;       /**< Used protocol version (Default: Http11) */
00312         EHttpConnection          m_nConnection;         /**< Used connection type  (Default: Close) */
00313         EHttpAuth                m_nHttpAuth;           /**< Used authentication   (Default: NoAuth) */
00314         String                   m_sAddress;            /**< Host address */
00315         uint32                   m_nPort;               /**< Port number */
00316         String                   m_sUsername;           /**< User name (for authentication) */
00317         String                   m_sPassword;           /**< Password  (for authentication) */
00318         HttpClientConnection    *m_pConnection;         /**< Current connection */
00319         HttpHeader               m_cHttpHeader;         /**< Received HTTP header */
00320         int                      m_nChunkSize;          /**< Size of current chunk (only for chunked transfer-coding) */
00321         int                      m_nChunkLeft;          /**< Size of chunk remaining (only for chunked transfer-coding) */
00322 
00323 
00324 };
00325 
00326 
00327 //[-------------------------------------------------------]
00328 //[ Namespace                                             ]
00329 //[-------------------------------------------------------]
00330 } // PLCore
00331 
00332 
00333 //[-------------------------------------------------------]
00334 //[ Implementation                                        ]
00335 //[-------------------------------------------------------]
00336 #include "PLCore/Network/Http/HttpClient.inl"
00337 
00338 
00339 #endif // __PLCORE_HTTPCLIENT_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:56
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported