diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index 1569481..e196f1d 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -815,7 +815,7 @@ EXCLUDE_PATTERNS = # Note that the wildcards are matched against the file with absolute path, so to # exclude all test directories use the pattern */test/* -EXCLUDE_SYMBOLS = +EXCLUDE_SYMBOLS = *Private # The EXAMPLE_PATH tag can be used to specify one or more files or directories # that contain example code fragments that are included (see the \include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3ab09c1..18ab5e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,15 +3,13 @@ configure_file(qhttpengine.pc.in "${CMAKE_CURRENT_BINARY_DIR}/qhttpengine.pc" @O set(INCLUDE "${CMAKE_CURRENT_BINARY_DIR}/config.h" - qhttprequest.h - qhttpresponse.h qhttpserver.h + qhttpsocket.h ) set(SRC - qhttprequest.cpp - qhttpresponse.cpp qhttpserver.cpp + qhttpsocket.cpp ) add_library(qhttpengine SHARED ${SRC}) diff --git a/src/qhttprequest.cpp b/src/qhttprequest.cpp deleted file mode 100644 index 1a2e72c..0000000 --- a/src/qhttprequest.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2015 Nathan Osman - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - **/ - -#include "qhttprequest.h" diff --git a/src/qhttpresponse.cpp b/src/qhttpresponse.cpp deleted file mode 100644 index 0bd7418..0000000 --- a/src/qhttpresponse.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2015 Nathan Osman - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - **/ - -#include "qhttpresponse.h" diff --git a/src/qhttpresponse.h b/src/qhttpsocket.cpp similarity index 59% rename from src/qhttpresponse.h rename to src/qhttpsocket.cpp index 06fa42b..ec64999 100644 --- a/src/qhttpresponse.h +++ b/src/qhttpsocket.cpp @@ -22,23 +22,52 @@ * IN THE SOFTWARE. **/ -#ifndef QHTTPENGINE_QHTTPRESPONSE_H -#define QHTTPENGINE_QHTTPRESPONSE_H +#include "qhttpsocket.h" +#include "qhttpsocket_p.h" -#include - -#include "config.h" - -/** - * @brief HTTP response sent to a client - */ -class QHTTPENGINE_EXPORT QHttpResponse : public QObject +QHttpSocketPrivate::QHttpSocketPrivate(QHttpSocket *socket) + : q(socket) { - Q_OBJECT +} -public: +QHttpSocket::QHttpSocket(qintptr socketDescriptor, QObject *parent) + : QIODevice(parent), + d(new QHttpSocketPrivate(this)) +{ + d->socket.setSocketDescriptor(socketDescriptor); +} +QHttpSocket::~QHttpSocket() +{ + delete d; +} + +QByteArray QHttpSocket::method() const +{ + return d->method; +} + +QByteArray QHttpSocket::path() const +{ + return d->path; +} + +QByteArray QHttpSocket::statusCode() const +{ + return d->statusCode; +} + +void QHttpSocket::setStatusCode(const QByteArray &code) +{ + d->statusCode = code; +} + +qint64 QHttpSocket::readData(char *data, qint64 maxlen) +{ //... -}; +} -#endif // QHTTPENGINE_QHTTPRESPONSE_H +qint64 QHttpSocket::writeData(const char *data, qint64 len) +{ + //... +} diff --git a/src/qhttpsocket.h b/src/qhttpsocket.h new file mode 100644 index 0000000..8139613 --- /dev/null +++ b/src/qhttpsocket.h @@ -0,0 +1,115 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Nathan Osman + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + **/ + +#ifndef QHTTPENGINE_QHTTPSOCKET_H +#define QHTTPENGINE_QHTTPSOCKET_H + +#include +#include + +#include "config.h" + +class QHttpSocketPrivate; + +/** + * @brief Socket for communicating with a client + * + * The QHttpSocket class provides an interface for communicating with an HTTP + * client. Method, path, and headers from the request can be obtained through + * the appropriate properties once the requestHeadersParsed() signal is + * emitted. The request data can be read using QIODevice's read() method. + * + * Response code and headers can be set through the appropriate properties. + * The response data can be written directly using QIODevice's write() method. + * All response headers must be set before writing any data. + */ +class QHTTPENGINE_EXPORT QHttpSocket : public QIODevice +{ + Q_OBJECT + Q_PROPERTY(QByteArray method READ method) + Q_PROPERTY(QByteArray path READ path) + Q_PROPERTY(QByteArray statusCode READ statusCode WRITE setStatusCode) + +public: + + /** + * @brief Create a new QHttpSocket instance from a socket descriptor + */ + explicit QHttpSocket(qintptr socketDescriptor, QObject *parent = 0); + + /** + * @brief Destroy the QHttpSocket instance + */ + virtual ~QHttpSocket(); + + /** + * @brief Retrieve the request method + */ + QByteArray method() const; + + /** + * @brief Retrieve the request path + */ + QByteArray path() const; + + /** + * @brief Retrieve the value of a request header + */ + QByteArray requestHeader(const QByteArray &header) const; + + /** + * @brief Retrieve the response code + */ + QByteArray statusCode() const; + + /** + * @brief Set the response code + */ + void setStatusCode(const QByteArray & code); + + /** + * @brief Set a response header to the specified value + */ + void setResponseHeader(const QByteArray &header, const QByteArray &value); + +Q_SIGNALS: + + /** + * @brief Indicate that request headers have been parsed + * + * Once this signal is emitted, it is safe to begin reading request data. + * The readyRead() signal will be emitted as request data is received. + */ + void requestHeadersParsed(); + +private: + + qint64 readData(char *data, qint64 maxlen); + qint64 writeData(const char *data, qint64 len); + + QHttpSocketPrivate *const d; + friend class QHttpSocketPrivate; +}; + +#endif // QHTTPENGINE_QHTTPSOCKET_H diff --git a/src/qhttprequest.h b/src/qhttpsocket_p.h similarity index 70% rename from src/qhttprequest.h rename to src/qhttpsocket_p.h index c49c8e6..4ca33fe 100644 --- a/src/qhttprequest.h +++ b/src/qhttpsocket_p.h @@ -22,23 +22,36 @@ * IN THE SOFTWARE. **/ -#ifndef QHTTPENGINE_QHTTPREQUEST_H -#define QHTTPENGINE_QHTTPREQUEST_H +#ifndef QHTTPENGINE_QHTTPSOCKETPRIVATE_H +#define QHTTPENGINE_QHTTPSOCKETPRIVATE_H #include +#include +#include -#include "config.h" +#include "qhttpsocket.h" -/** - * @brief HTTP request received from a client - */ -class QHTTPENGINE_EXPORT QHttpRequest : public QObject +class QHttpSocketPrivate : public QObject { Q_OBJECT public: - //... + explicit QHttpSocketPrivate(QHttpSocket *socket); + + QTcpSocket socket; + QByteArray buffer; + + QByteArray method; + QByteArray path; + QVariantMap requestHeaders; + + QByteArray statusCode; + QVariantMap responseHeaders; + +private: + + QHttpSocket *const q; }; -#endif // QHTTPENGINE_QHTTPREQUEST_H +#endif // QHTTPENGINE_QHTTPSOCKETPRIVATE_H