/home/croftj/photogrotto/QcjHttpService.h

00001 /*********************************************************************************
00002 **
00003 **   $Id: //depot/WorkInProgress/QcjData/QcjHttpService.h#7 $
00004 **   Copyright (c) 2006 Joe Croft joe@croftj.net
00005 **   
00006 **   This file is part of Qcj Classes.
00007 **
00008 **   QcjHttpService.h is free software; you can redistribute it and/or modify
00009 **   it under the terms of the GNU General Public License as published by
00010 **   the Free Software Foundation; either version 2 of the License, or
00011 **   (at your option) any later version.
00012 **
00013 **   Foobar 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 General Public License for more details.
00017 **
00018 **   You should have received a copy of the GNU General Public License
00019 **   along with Foobar; if not, write to the Free Software
00020 **   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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           \enum ErrorCodes
00049           
00050           These names provide a symbolic method to reference the various
00051           status codes which are defined the the w3c.
00052 
00053           \li \c 200 OK
00054           \li \c 201 Created            
00055           \li \c 202 Accepted           
00056           \li \c 203 NoAuthorizedContent
00057           \li \c 204 NoContent          
00058           \li \c 205 RevertContent      
00059           \li \c 206 PartialContent     
00060 
00061           \li \c 300 MultipleChoices
00062           \li \c 301 MovedPermanently 
00063           \li \c 302 Found            
00064           \li \c 303 SeeOther         
00065           \li \c 304 NotModified      
00066           \li \c 305 UseProxy         
00067           \li \c 306 Unused           
00068           \li \c 307 TemporaryRedirect
00069 
00070           \li \c 400 BadRequest
00071           \li \c 401 Unauthorized
00072           \li \c 402 PaymentRequired
00073           \li \c 403 Forbidden
00074           \li \c 404 NotFound
00075           \li \c 405 MethodNotAllowed
00076           \li \c 406 NotAcceptable
00077           \li \c 407 ProxyAuthenticationRequired
00078           \li \c 408 RequestTimeout
00079           \li \c 409 Conflict
00080           \li \c 410 Gone
00081           \li \c 411 LengthRequired
00082           \li \c 412 PreconditionFailed
00083           \li \c 413 RequestEntityTooLong
00084           \li \c 414 RequestURITooLong
00085           \li \c 415 UnsupportedMediaType
00086           \li \c 416 RequestedRangeNotSatisfiable
00087           \li \c 417 ExpectationFailed
00088 
00089           \li \c 500 InternalServerError 
00090           \li \c 501 NotImplimented
00091           \li \c 502 BadGateway
00092           \li \c 503 ServiceUnavailable
00093           \li \c 504 GatewayTimeout
00094           \li \c 505 HTTPVersionNotSupported
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           \fn void addMethods(QStringList *extraMethods)
00116           
00117           This  function  allows  the  caller  to  add additional HTTP
00118           methods which will be recognized by this object.
00119    */ 
00120    void addMethods(QStringList *extraMethods)
00121    {
00122       if ( extraMethods != 0 ) 
00123          methods << *extraMethods;
00124    };
00125 
00126    /*!
00127           void setMaxRequests(int max_req)
00128           
00129           This  function  sets  the  maximum  number of requests to be
00130           processed by this object.
00131    */ 
00132    void setMaxRequests(int max_req)
00133    {
00134       maxRequests = max_req;
00135    };
00136 
00137    /*!
00138           \fn void setTimeToLive(long ttl)
00139           
00140           This  function  sets  the  time,  in  milliseconds, for this
00141           object to process requests before dying.
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 //   void disconnected();
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 //   QTimer                           ttlTimer;
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 

Generated on Fri May 4 11:21:12 2007 for PhotoGrotto by  doxygen 1.5.0