00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #ifndef HTTPREQUEST_H_
00012 #define HTTPREQUEST_H_
00013
00014 #include <iostream>
00015 #include <qptrlist.h>
00016
00017 using namespace std;
00018
00019 #include <qsocket.h>
00020 #include "upnputil.h"
00021 #include "bufferedsocketdevice.h"
00022
00023 #define SOAP_ENVELOPE_BEGIN "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " \
00024 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" \
00025 "<s:Body>"
00026 #define SOAP_ENVELOPE_END "</s:Body>\r\n</s:Envelope>";
00027
00028
00030
00032
00033 typedef enum
00034 {
00035 RequestTypeUnknown = 0x0000,
00036 RequestTypeGet = 0x0001,
00037 RequestTypeHead = 0x0002,
00038 RequestTypePost = 0x0004,
00039 RequestTypeMSearch = 0x0008,
00040 RequestTypeSubscribe = 0x0010,
00041 RequestTypeUnsubscribe = 0x0020,
00042 RequestTypeNotify = 0x0040,
00043 RequestTypeResponse = 0x0080
00044
00045 } RequestType;
00046
00047 typedef enum
00048 {
00049 ContentType_Unknown = 0,
00050 ContentType_Urlencoded = 1,
00051 ContentType_XML = 2
00052
00053 } ContentType;
00054
00055 typedef enum
00056 {
00057 ResponseTypeNone = -1,
00058 ResponseTypeUnknown = 0,
00059 ResponseTypeXML = 1,
00060 ResponseTypeHTML = 2,
00061 ResponseTypeFile = 3,
00062 ResponseTypeOther = 4
00063
00064 } ResponseType;
00065
00066
00067 typedef struct
00068 {
00069 char *pszExtension;
00070 char *pszType;
00071
00072 } MIMETypes;
00073
00075
00076 class IPostProcess
00077 {
00078 public:
00079 virtual void ExecutePostProcess( ) = 0;
00080 virtual ~IPostProcess() {};
00081 };
00082
00084
00086
00087 class HTTPRequest
00088 {
00089 protected:
00090
00091 static const char *m_szServerHeaders;
00092
00093
00094 QByteArray m_aBuffer;
00095
00096
00097 public:
00098
00099 RequestType m_eType;
00100 ContentType m_eContentType;
00101
00102 QString m_sRawRequest;
00103
00104 QString m_sBaseUrl;
00105 QString m_sMethod;
00106
00107 QStringMap m_mapParams;
00108 QStringMap m_mapHeaders;
00109
00110 QString m_sPayload;
00111
00112 QString m_sProtocol;
00113 int m_nMajor;
00114 int m_nMinor;
00115
00116
00117 bool m_bSOAPRequest;
00118 QString m_sNameSpace;
00119
00120
00121
00122 ResponseType m_eResponseType;
00123 QString m_sResponseTypeText;
00124
00125 long m_nResponseStatus;
00126 QStringMap m_mapRespHeaders;
00127
00128 QString m_sFileName;
00129
00130 QTextStream m_response;
00131
00132 IPostProcess *m_pPostProcess;
00133
00134 protected:
00135
00136 RequestType SetRequestType ( const QString &sType );
00137 void SetRequestProtocol ( const QString &sLine );
00138 ContentType SetContentType ( const QString &sType );
00139
00140 void SetServerHeaders ( void );
00141
00142 void ProcessRequestLine ( const QString &sLine );
00143 bool ProcessSOAPPayload ( const QString &sSOAPAction );
00144 void ExtractMethodFromURL( );
00145
00146 QString GetResponseStatus ( void );
00147 QString GetResponseType ( void );
00148 QString GetAdditionalHeaders( void );
00149
00150 bool ParseRange ( QString sRange,
00151 long long llSize,
00152 long long *pllStart,
00153 long long *pllEnd );
00154
00155 QString BuildHeader ( long long nSize );
00156
00157 public:
00158
00159 HTTPRequest ();
00160 virtual ~HTTPRequest () {};
00161
00162 void Reset ();
00163
00164 bool ParseRequest ();
00165
00166 void FormatErrorResponse ( bool bServerError,
00167 const QString &sFaultString,
00168 const QString &sDetails );
00169
00170 void FormatActionResponse( NameValueList *pArgs );
00171 void FormatFileResponse ( const QString &sFileName );
00172
00173 long SendResponse ( void );
00174 long SendResponseFile( QString sFileName );
00175
00176 static QString &Encode ( QString &sStr );
00177
00178 QString GetHeaderValue ( const QString &sKey, QString sDefault );
00179
00180 bool GetKeepAlive ();
00181
00182 static QString GetMimeType ( const QString &sFileExtension );
00183 static long GetParameters ( QString sParams, QStringMap &mapParams );
00184
00185
00186
00187 virtual Q_LONG BytesAvailable () = 0;
00188 virtual Q_ULONG WaitForMore ( int msecs, bool *timeout = NULL ) = 0;
00189 virtual bool CanReadLine () = 0;
00190 virtual QString ReadLine ( int msecs = 0 ) = 0;
00191 virtual Q_LONG ReadBlock ( char *pData, Q_ULONG nMaxLen, int msecs = 0 ) = 0;
00192 virtual Q_LONG WriteBlock ( char *pData, Q_ULONG nLen ) = 0;
00193 virtual Q_LONG WriteBlockDirect( char *pData, Q_ULONG nLen ) = 0;
00194 virtual QString GetHostAddress () = 0;
00195 virtual QString GetPeerAddress () = 0;
00196 virtual void Flush () = 0;
00197 virtual bool IsValid () = 0;
00198 virtual int getSocketHandle () = 0;
00199 virtual void SetBlocking ( bool bBlock ) = 0;
00200 virtual bool IsBlocking () = 0;
00201 };
00202
00204
00206
00207 class BufferedSocketDeviceRequest : public HTTPRequest
00208 {
00209 public:
00210
00211 BufferedSocketDevice *m_pSocket;
00212
00213 public:
00214
00215 BufferedSocketDeviceRequest( BufferedSocketDevice *pSocket );
00216 virtual ~BufferedSocketDeviceRequest() {};
00217
00218 virtual Q_LONG BytesAvailable ();
00219 virtual Q_ULONG WaitForMore ( int msecs, bool *timeout = NULL );
00220 virtual bool CanReadLine ();
00221 virtual QString ReadLine ( int msecs = 0 );
00222 virtual Q_LONG ReadBlock ( char *pData, Q_ULONG nMaxLen, int msecs = 0 );
00223 virtual Q_LONG WriteBlock ( char *pData, Q_ULONG nLen );
00224 virtual Q_LONG WriteBlockDirect( char *pData, Q_ULONG nLen );
00225 virtual QString GetHostAddress ();
00226 virtual QString GetPeerAddress ();
00227 virtual void Flush () { m_pSocket->Flush(); }
00228 virtual bool IsValid () {return( m_pSocket->IsValid() ); }
00229 virtual int getSocketHandle () {return( m_pSocket->socket() ); }
00230 virtual void SetBlocking ( bool bBlock );
00231 virtual bool IsBlocking ();
00232
00233 };
00234
00235 #endif