MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSDB/HTTPConnection.h
Go to the documentation of this file.
00001 
00005 #ifndef CHTTPCONNECTIONH
00006 
00007 #include <MOOSUtilityLib/MOOSThread.h>
00008 #include <map>
00009 
00010 //forward decalarations
00011 class XPCTcpSocket;
00012 class CMOOSCommClient;
00013 class CMOOSLock;
00014 
00015 
00016 
00017 
00018 
00019 class CHTTPConnection
00020 {
00021 public:
00022 
00023     /**************************************************************/
00025     struct HTTPRequest
00026     {
00027         //type of request
00028         enum eRequest
00029         {
00030             GET,
00031             POST
00032         };
00033         eRequest m_eRequest;
00034 
00036         typedef std::map<std::string,std::list<std::string> > HEADERMAP;
00037 
00039         void AddHeader(std::string sLine)
00040         {
00041             MOOSRemoveChars(sLine," ");
00042             std::string sHeader = MOOSChomp(sLine,":");
00043             std::list<std::string> Values;
00044 
00045             while(!sLine.empty())
00046             {
00047                 Values.push_back(MOOSChomp(sLine,","));
00048             }
00049             MOOSToUpper(sHeader);
00050             m_Headers[sHeader] = Values;
00051 
00052         }
00053 
00055         bool IsKeepAlive()
00056         {
00057             if(HasProperty("CONNECTION","keep-alive"))
00058                 return true;
00059             if(HasProperty("CONNECTION","close"))
00060                 return false;
00061 
00062             //default is true
00063             return true;
00064         }
00065 
00067         bool  GetKeepAliveTime(double & dfTimeOut)
00068         {
00069             std::list<std::string> Values;
00070 
00071             if(!GetHeader("Keep-Alive",Values))
00072                 return false;
00073 
00074             if(Values.empty())
00075                 return false;
00076 
00077             dfTimeOut =  atof(Values.begin()->c_str());
00078 
00079             return true;
00080         }
00081 
00083         bool HasProperty(std::string sHeader, std::string sProp)
00084         {
00085             std::list<std::string> Values;
00086 
00087             if(!GetHeader(sHeader,Values))
00088                 return false;
00089 
00090             std::list<std::string>::iterator q;
00091             for(q = Values.begin();q!=Values.end();q++)
00092             {
00093                 if(MOOSStrCmp(*q,sProp))
00094                     return true;
00095             }
00096             return false;
00097 
00098         }
00099 
00101         bool GetHeader(std::string sHeader, std::list<std::string> & Values)
00102         {
00103             MOOSToUpper(sHeader);
00104             if(m_Headers.find(sHeader)==m_Headers.end())
00105                 return false;
00106 
00107             Values = m_Headers[sHeader];
00108 
00109             return true;
00110 
00111         }
00113         void Clean()
00114         {
00115             m_Headers.clear();
00116         }
00117 
00119         HEADERMAP m_Headers;
00120     };
00121     /************ END OF HTTPRequest definition ******************/
00122 
00123 
00124 
00126     CHTTPConnection(XPCTcpSocket* pNewSocket,CMOOSCommClient* pMOOSComms,CMOOSLock* pMOOSCommsLock);
00127 
00129     bool Run();
00130 
00132     bool HasCompleted();
00133 
00135     static bool _CB(void* pParam)
00136     {
00137         CHTTPConnection* pMe = (CHTTPConnection*) pParam;
00138         return pMe->Serve();
00139     }
00140 
00141 
00142 protected:
00144     bool Serve();
00146     bool MakeWebPage();
00148     bool SendHeader();
00150     bool SendFailureHeader();
00152     bool SendWebPage();
00154     bool HandlePoke(std::string sPokeURL);
00155     /* build webpage of single variable */
00156     bool BuildSingleVariableWebPageContents( std::ostringstream & wp,MOOSMSG_LIST & MsgList);
00158     bool BuildFullDBWebPageContents( std::ostringstream & wp,MOOSMSG_LIST & MsgList);
00160     bool ReadRequest();
00161 
00163     bool ReadLine(std::string &);
00165     void SendLine(std::string sLine);
00167     void SendString(std::string sLine);
00168     
00169     
00171     std::string m_sWebPage;
00173     CMOOSThread m_ServeThread; 
00175     XPCTcpSocket* m_pSocket;
00176     
00178     std::string m_sFocusVariable;
00179 
00181     HTTPRequest m_Request;
00182 
00184     CMOOSCommClient * m_pMOOSComms;
00185 
00187     CMOOSLock * m_pMOOSCommsLock;
00188 
00189 };
00190 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines