00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 # ifndef QCJHTTPSERVICE_H
00024 # define QCJHTTPSERVICE_H
00025
00026 # include <QBuffer>
00027 # include <QDataStream>
00028 # include <QMap>
00029 # include <QMutex>
00030 # include <QString>
00031 # include <QStringList>
00032 # include <QTcpSocket>
00033 # include <QThread>
00034 # include <QTimer>
00035 # include <QVariant>
00036 # include <QWaitCondition>
00037
00038 class QcjHttpService : public QThread
00039 {
00040 Q_OBJECT
00041
00042 public:
00043 QcjHttpService(int socketDescripter, QObject *parent = 0, int max_req = 10,
00044 QStringList *extraMethods = 0, long ttl = 200);
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 enum ErrorCodes
00099 {
00100 OK = 200, Created, Accepted, NonAuthorizedContent, NoContent,
00101 RevertContent, PartialContent,
00102 MultipleChoices = 300, MovedPermanently, Found, SeeOther,
00103 NotModified, UseProxy, Unused, TemporyRedirect,
00104 BadRequest = 400, Unauthorized, PaymentRequired, Forbidden, NotFound,
00105 MethodNotAllowed, NotAcceptable, ProxyAuthenticationRequired,
00106 RequestTimeout, Conflict, Gone, LengthRequired,
00107 PreconditionFailed, RequestEntityTooLong, RequestURITooLong,
00108 UnsupportedMediaType, RequestedRangeNotSatisfiable,
00109 ExpectationFailed,
00110 InternalServerError = 500, NotImplimented, BadGateway, ServiceUnavailable,
00111 GatewayTimeout, HTTPVersionNotSupported
00112 };
00113
00114
00115
00116
00117
00118
00119
00120 void addMethods(QStringList *extraMethods)
00121 {
00122 if ( extraMethods != 0 )
00123 methods << *extraMethods;
00124 };
00125
00126
00127
00128
00129
00130
00131
00132 void setMaxRequests(int max_req)
00133 {
00134 maxRequests = max_req;
00135 };
00136
00137
00138
00139
00140
00141
00142
00143 void setTimeToLive(long ttl)
00144 {
00145 timeToLive = ttl;
00146 };
00147
00148 signals:
00149 void error(QAbstractSocket::SocketError);
00150 void send(QTcpSocket*, QByteArray);
00151 void closeSocket(QTcpSocket*);
00152
00153
00154 protected slots:
00155 void haveData();
00156 void haveTimeout();
00157 void haveError(QAbstractSocket::SocketError);
00158 void haveDisconnect();
00159 void errorResponse(enum ErrorCodes ec, QString msg);
00160 void processResponse(QMap<QString, QVariant> rsp);
00161 void haveTimeOut();
00162
00163 private:
00164
00165 protected:
00166 virtual void processRequest(QMap<QString, QVariant> *, QMap<QString, QVariant> *) {};
00167 virtual void clearLocks() {};
00168
00169 void sendResponse(QMap<QString, QVariant> *rsp);
00170 QMap<QString, QString> parseArguments(QString args);
00171 void run();
00172
00173 QMap<QString, QVariant> *request;
00174 QStringList methods;
00175 QByteArray inArray;
00176 QBuffer inBuffer;
00177 QDataStream inStream;
00178 QString lineBuffer;
00179
00180 QTimer *ttlTimer;
00181 QTimer *errTimer;
00182 int maxRequests;
00183 long timeToLive;
00184 int socknum;
00185 bool exitFlag;
00186 bool inBody;
00187 Qt::HANDLE myThreadId;
00188 qlonglong expectingBytes;
00189 QMutex bufLock;
00190 QWaitCondition haveInput;
00191 QTcpSocket *sock;
00192 };
00193
00194 # endif
00195