File indexing completed on 2025-01-26 04:24:52

0001 /*
0002  * Copyright 2011 Nikhil Marathe <nsm.nikhil@gmail.com>
0003  * 
0004  * Permission is hereby granted, free of charge, to any person obtaining a copy
0005  * of this software and associated documentation files (the "Software"), to
0006  * deal in the Software without restriction, including without limitation the
0007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
0008  * sell copies of the Software, and to permit persons to whom the Software is
0009  * furnished to do so, subject to the following conditions:
0010  * 
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  * 
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0019  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
0020  * IN THE SOFTWARE. 
0021  */
0022 
0023 #ifndef Q_HTTP_CONNECTION
0024 #define Q_HTTP_CONNECTION
0025 
0026 #include <QObject>
0027 #include <QHash>
0028 
0029 #include "http-parser/http_parser.h"
0030 
0031 class QTcpSocket;
0032 
0033 class QHttpRequest;
0034 class QHttpResponse;
0035 
0036 typedef QHash<QString, QString> HeaderHash;
0037 
0038 class QHttpConnection : public QObject
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     QHttpConnection(QTcpSocket *socket, QObject *parent = 0);
0044     virtual ~QHttpConnection();
0045 
0046     void write(const QByteArray &data);
0047     void flush();
0048 
0049 public Q_SLOTS:
0050     void disconnectFromHost();
0051 
0052 Q:
0053     void newRequest(QHttpRequest*, QHttpResponse*);
0054 
0055 private Q_SLOTS:
0056     void parseRequest();
0057     void socketDisconnected();
0058 
0059 private:
0060     static int MessageBegin(http_parser *parser);
0061     static int Url(http_parser *parser, const char *at, size_t length);
0062     static int HeaderField(http_parser *parser, const char *at, size_t length);
0063     static int HeaderValue(http_parser *parser, const char *at, size_t length);
0064     static int HeadersComplete(http_parser *parser);
0065     static int Body(http_parser *parser, const char *at, size_t length);
0066     static int MessageComplete(http_parser *parser);
0067 
0068 private:
0069     QTcpSocket *m_socket;
0070     http_parser_settings m_parserSettings;
0071     http_parser *m_parser;
0072 
0073     // since there can only be one request at any time
0074     // even with pipelining
0075     QHttpRequest *m_request;
0076 
0077     // the ones we are reading in from the parser
0078     HeaderHash m_currentHeaders;
0079     QString m_currentHeaderField;
0080     QString m_currentHeaderValue;
0081 };
0082 
0083 #endif