AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzHttp.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CZ_HTTP_H_)
00015 #define _CZ_HTTP_H_
00016 
00017 #include "CzUtil.h"
00018 #include "CzString.h"
00019 
00020 /**
00021  @addtogroup Comms
00022  @{
00023  */
00024 
00025 /**
00026  @def   CZ_HTTP_MANAGER()
00027 
00028  @brief A short cut macro that calls the global HTTP manager singleton
00029 
00030  */
00031  
00032 #define CZ_HTTP_MANAGER     CzHttpManager::getInstance()
00033 
00034 class CzHttpManager;
00035 
00036 typedef void* CzHttpObject;
00037 
00038 /**
00039  @class CzHttpHeader
00040 
00041  @brief An HTTP header.
00042 
00043  */
00044 class CzHttpHeader
00045 {
00046     // Properties
00047 private:
00048     CzString        Header;         ///< Header name
00049     CzString        HeaderData;     ///< header data
00050 public:
00051     void            setHeader(const char* header)
00052     {
00053         Header.setString(header);
00054     }
00055     CzString&       getHeader()     { return Header; }
00056     void            setHeaderData(const char* header_data)
00057     {
00058         HeaderData.setString(header_data);
00059     }
00060     const char*     getHeaderData() const { return HeaderData.c_str(); }
00061     // Properties end
00062 public:
00063     CzHttpHeader()
00064     {
00065         Header.setAutoHash(true);
00066     }
00067     virtual ~CzHttpHeader()
00068     {
00069     }
00070 };
00071 typedef CzList<CzHttpHeader*>::iterator CzHttpHeaderIterator;
00072 
00073 /**
00074  @class CzHttpPostData
00075 
00076  @brief HTTP POST data.
00077 
00078  */
00079 class CzHttpPostData
00080 {
00081 private:
00082     CzList<CzHttpHeader*>   Headers;        ///< Headers collection
00083     CzString                Body;           ///< POST body
00084 public:
00085     CzHttpPostData() {}
00086     virtual ~CzHttpPostData()
00087     {
00088         ClearHeaders();
00089     }
00090 
00091     void SetHeader(const char* header, const char* header_data)
00092     {
00093         unsigned int header_hash = CzString::CalculateHash(header);
00094 
00095         // Check to see if header already present, if so update the header info
00096         for (CzHttpHeaderIterator it = Headers.begin(); it != Headers.end(); it++)
00097         {
00098             if (header_hash == (*it)->getHeader().getHash())
00099             {
00100                 (*it)->setHeaderData(header_data);
00101                 return;
00102             }
00103         }
00104 
00105         // Header was not already present so add new header
00106         CzHttpHeader* head = new CzHttpHeader();
00107         head->setHeader(header);
00108         head->setHeaderData(header_data);
00109         Headers.push_back(head);
00110     }
00111     void ClearHeaders()
00112     {
00113         for (CzHttpHeaderIterator it = Headers.begin(); it != Headers.end();  ++it)
00114             delete (*it);
00115         Headers.clear();
00116     }
00117     void setBody(const char* body)
00118     {
00119         Body.setString(body);
00120     }
00121     const CzString& getBody() const { return Body; }
00122 
00123     void            ApplyHeaders();
00124 };
00125 
00126 /**
00127  @class CzHttpRequest
00128 
00129  @brief An HTTP request including the header, body and response.
00130 
00131  */
00132 class CzHttpRequest
00133 {
00134     // Properties
00135 private:
00136     CzString            URI;                            ///< URI end point
00137     CzString            Content;                        ///< Receieved content
00138     int                 ContentLength;                  ///< Received content lenngth
00139     bool                Processed;                      ///< Processed marker (becomes true when Http Manager has processed the request, this does not mean result is available)
00140     int                 Error;                          ///< Any errors that occured or 0 for none
00141     CzHttpPostData*     PostData;                       ///< Collection of headers and the main body to POST
00142     CzCallback          ContentAvailableCallback;       ///< Callback to be called when content is available
00143     void*               ContentAvailableCallbackData;   ///< Data to be passed to the callback
00144     bool                Post;                           ///< True if request is a POST, otherwise a GET
00145     bool                AutoDelete;                     ///< Request is auto deleted if true
00146 public:
00147     const CzString&     getURI() const                          { return URI; }
00148     void                setURI(const char* uri)
00149     {
00150         URI.setString(uri);
00151     }
00152     CzString&           getContent()                            { return Content; }
00153     void                setContent(char* content, int len)
00154     {
00155         Content.allocString(len);
00156         Content.Copy(content, 0, len);
00157         ContentLength = len;
00158     }
00159     void                allocContent(int len)
00160     {
00161         Content.allocString(len);
00162         Content.setLength(len);
00163         ContentLength = len;
00164     }
00165     void                reallocContent(int new_len)
00166     {
00167         Content.reallocString(new_len);
00168         Content.setLength(new_len);
00169         ContentLength = new_len;
00170     }
00171     int                 getContentLength() const                            { return ContentLength; }
00172     bool                getProcessed() const                                { return Processed; }
00173     void                setProcessed(bool processed)                        { Processed = processed; }
00174     void                SetHeader(const char* header, const char* header_data) { PostData->SetHeader(header, header_data); }
00175     void                ClearHeaders()                                      { PostData->ClearHeaders(); }
00176     void                setBody(const char* body)                           { PostData->setBody(body); }
00177     const CzString&     getBody() const                                     { return PostData->getBody(); }
00178     void                setContentAvailableCallback(CzCallback callback, void *data)    { ContentAvailableCallback = callback; ContentAvailableCallbackData = data; }
00179     void                setPOST()                                           { Post = true; }
00180     void                setGET()                                            { Post = false; }
00181     bool                isPOST() const                                      { return Post; }
00182     // Properties end
00183 
00184 private:
00185 
00186 public:
00187     CzHttpRequest() : ContentLength(0), Processed(false), Error(0), ContentAvailableCallback(NULL), ContentAvailableCallbackData(NULL), Post(false), AutoDelete(false) { PostData = new CzHttpPostData(); }
00188     virtual ~CzHttpRequest()
00189     {
00190         SAFE_DELETE(PostData)
00191     }
00192 
00193     void    setError(int error)                 { Error = error; }
00194     int     getError() const                    { return Error; }   
00195 
00196     void    EndRequest(int error);
00197     void    ApplyHeaders();
00198 
00199 };
00200 
00201 /**
00202  @class CzHttpManager
00203 
00204  @brief Handles the queueing of HTTP requests.
00205 
00206  */
00207 class CzHttpManager
00208 {
00209     CDEFINE_SINGLETON(CzHttpManager)
00210 
00211 protected:
00212     typedef CzList<CzHttpRequest*>::iterator _Iterator;
00213     CzList<CzHttpRequest*>  Requests;       ///< Request queue (caller owns requests)
00214 
00215     // Properties
00216     CzHttpRequest*          CurrentRequest; ///< Current request thats being processed or null if not busy
00217     CzHttpObject            HttpObject;     ///< The Marmalade SDK Http Object
00218     CzString                UserAgent;      ///< Browser style user-agent
00219     CzString                IPAddress;      ///< IP address of device
00220 public:
00221     CzHttpRequest*          getCurrentRequest()                     { return CurrentRequest; }
00222     void                    setNoRequest()                          { CurrentRequest = NULL; }
00223     bool                    GetHeader(const char* header_name, CzString& header_data);
00224     CzHttpObject            getHttpObject()                         { return HttpObject; }
00225     void                    setUserAgent(const char* user_agent)    { UserAgent = user_agent; } // Aoto determined but can be changed if needed
00226     CzString&               getUserAgent()                          { return UserAgent; }
00227     void                    setIPAddress(const char* ip_address)    { IPAddress = ip_address; }
00228     CzString&               getIPAddress()                          { return IPAddress; }
00229     // Properties end
00230 
00231 protected:
00232     CzString                DetermineUserAgent();
00233 //  bool                    DetermineIPAddress();
00234 public:
00235     void                    Init();
00236     void                    Release();
00237     void                    AddRequest(CzHttpRequest* request);
00238     void                    RemoveRequest(CzHttpRequest* request);
00239     void                    ClearRequests();
00240     void                    CancelRequests();
00241     
00242     void                    Update();
00243     bool                    isEmpty() const;
00244 
00245     // Internal
00246 };
00247 
00248 /// @}
00249 
00250 
00251 #endif // _CZ_HTTP_H_