Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "src/qjson-backport"]
path = src/qjson-backport
url = https://github.com/5in4/qjson-backport.git
[submodule "src/mimetypes"]
path = src/mimetypes
url = http://code.qt.io/cgit/playground/mimetypes.git/
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,25 @@ set(CMAKECONFIG_INSTALL_DIR "${LIB_INSTALL_DIR}/cmake/${PROJECT_NAME}" CACHE STR
set(DOC_INSTALL_DIR share/doc/${PROJECT_NAME} CACHE STRING "Documentation installation directory relative to the install prefix")
set(EXAMPLES_INSTALL_DIR "${LIB_INSTALL_DIR}/${PROJECT_NAME}/examples" CACHE STRING "Examples installation directory relative to the install prefix")

find_package(Qt5Network 5.1 REQUIRED)
find_package(Qt5Network 5.1 QUIET)
if (NOT Qt5Network_FOUND)
message("-- QT minimum version 5.1 not found, trying with minimum version 4.8")

find_package(Qt4 4.8 QUIET COMPONENTS QtGui QtCore QtNetwork)
if (NOT Qt4_FOUND)
message("-- QT minimum version 4.8 not found")

message(FATAL_ERROR "Qt minimum versio 4.8 is required")
endif()
endif()

message("-- Using QT version ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH}")

if (Qt4_FOUND)
find_package(Git REQUIRED)
message("-- Updating git submodules for QT4 compilation ${Qt4_VERSION}")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive)
endif()

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ QHttpEngine requires a modern C++ compiler supported by the Qt framework. Some e
- GCC (including MinGW-w64)
- Clang

CMake 2.8.11+ and Qt 5.1+ are required to build the library.
CMake 2.8.11+ and Qt 4.8+ are required to build the library. If Qt 5.1+ is not available, then two external projects are used as submodules, which provide missing Qt5 functionalities: qjson-backport and mimetypes.

### Build Instructions

Expand Down
6 changes: 5 additions & 1 deletion examples/chatserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ set(SRC
main.cpp
)

qt5_add_resources(QRC resources.qrc)
if (Qt4_FOUND)
QT4_ADD_RESOURCES(QRC resources.qrc)
else()
qt5_add_resources(QRC resources.qrc)
endif()

add_executable(chatserver ${SRC} ${QRC})
target_link_libraries(chatserver QHttpEngine)
Expand Down
10 changes: 10 additions & 0 deletions examples/chatserver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
* IN THE SOFTWARE.
*/

#include <QtGlobal>

#if QT_VERSION >= 0x050000
#include <QCommandLineOption>
#include <QCommandLineParser>
#endif

#include <QCoreApplication>
#include <QHostAddress>
#include <QRegExp>
Expand All @@ -37,6 +42,7 @@ int main(int argc, char * argv[])
{
QCoreApplication a(argc, argv);

#if QT_VERSION >= 0x050000
// Build the command-line options
QCommandLineParser parser;
QCommandLineOption addressOption(
Expand All @@ -61,6 +67,10 @@ int main(int argc, char * argv[])
// Obtain the values
QHostAddress address = QHostAddress(parser.value(addressOption));
quint16 port = parser.value(portOption).toInt();
#else
QHostAddress address("127.0.0.1");
quint16 port = 8000;
#endif

// Build the hierarchy of handlers
QFilesystemHandler handler(":/static");
Expand Down
11 changes: 11 additions & 0 deletions examples/fileserver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
* IN THE SOFTWARE.
*/

#include <QtGlobal>

#if QT_VERSION >= 0x050000
#include <QCommandLineOption>
#include <QCommandLineParser>
#endif

#include <QCoreApplication>
#include <QDir>
#include <QHostAddress>
Expand All @@ -34,6 +39,7 @@ int main(int argc, char * argv[])
{
QCoreApplication a(argc, argv);

#if QT_VERSION >= 0x050000
// Build the command-line options
QCommandLineParser parser;
QCommandLineOption addressOption(
Expand Down Expand Up @@ -66,6 +72,11 @@ int main(int argc, char * argv[])
QHostAddress address = QHostAddress(parser.value(addressOption));
quint16 port = parser.value(portOption).toInt();
QString dir = parser.value(dirOption);
#else
QHostAddress address("127.0.0.1");
quint16 port = 8000;
QString dir = QDir::homePath();
#endif

// Create the filesystem handler and server
QFilesystemHandler handler(dir);
Expand Down
37 changes: 35 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ set(SRC
qlocalfile.cpp
qobjecthandler.cpp
)
if (Qt4_FOUND)
set(SRC "${SRC}"
qjson-backport/qjsonarray.cpp
qjson-backport/qjson.cpp
qjson-backport/qjsondocument.cpp
qjson-backport/qjsonobject.cpp
qjson-backport/qjsonparser.cpp
qjson-backport/qjsonvalue.cpp
qjson-backport/qjsonwriter.cpp

mimetypes/src/mimetypes/qmimedatabase.cpp
mimetypes/src/mimetypes/qmimeglobpattern.cpp
mimetypes/src/mimetypes/qmimemagicrule.cpp
mimetypes/src/mimetypes/qmimemagicrulematcher.cpp
mimetypes/src/mimetypes/qmimeprovider.cpp
mimetypes/src/mimetypes/qmimetype.cpp
mimetypes/src/mimetypes/qmimetypeparser.cpp
mimetypes/src/mimetypes/inqt5/qstandardpaths.cpp
mimetypes/src/mimetypes/inqt5/qstandardpaths_unix.cpp
)
endif()

if(WIN32)
configure_file(resource.rc.in "${CMAKE_CURRENT_BINARY_DIR}/resource.rc")
Expand All @@ -26,14 +47,24 @@ else()
add_library(QHttpEngine SHARED ${HEADERS} ${SRC})
endif()

qt5_use_modules(QHttpEngine Network)

target_include_directories(QHttpEngine PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
"$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>"
)

if (Qt4_FOUND)
target_include_directories(QHttpEngine PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/mimetypes/include/QtMimeTypes>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/mimetypes/src/mimetypes/inqt5>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/qjson-backport>"
)

target_link_libraries(QHttpEngine Qt4::QtCore Qt4::QtNetwork Qt4::QtGui)
else()
qt5_use_modules(QHttpEngine Network)
endif()

set_target_properties(QHttpEngine PROPERTIES
DEFINE_SYMBOL QT_NO_SIGNALS_SLOTS_KEYWORDS
DEFINE_SYMBOL QHTTPENGINE_LIBRARY
Expand Down Expand Up @@ -68,3 +99,5 @@ configure_file(${PROJECT_NAME}.pc.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
DESTINATION "${LIB_INSTALL_DIR}/pkgconfig"
)


12 changes: 12 additions & 0 deletions src/QHttpEngine/qibytearray.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ class QHTTPENGINE_EXPORT QIByteArray : public QByteArray
*/
QIByteArray(const QByteArray &other);

#if QT_VERSION >= 0x050000
/**
* @brief Create a QIByteArray from a const char *
*/
QIByteArray(const char *data, int size = -1);
#else
/**
* @brief Create a QIByteArray from a const char *
*/
QIByteArray(const char *data);

/**
* @brief Create a QIByteArray from a const char *
*/
QIByteArray(const char *data, int size);
#endif
};

QHTTPENGINE_EXPORT bool operator==(const QIByteArray &a1, const QIByteArray &a2);
Expand Down
1 change: 1 addition & 0 deletions src/mimetypes
Submodule mimetypes added at 1ab65d
11 changes: 9 additions & 2 deletions src/qfilesystemhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,26 @@ void QFilesystemHandlerPrivate::processFile(QHttpSocket *socket, const QString &
copier->start();
}

#if QT_VERSION >= 0x050000
# define HTMLESCAPE(x) ((x).toHtmlEscaped())
#else
# include <QtGui/qtextdocument.h>
# define HTMLESCAPE(x) (Qt::escape(x))
#endif

void QFilesystemHandlerPrivate::processDirectory(QHttpSocket *socket, const QString &path, const QString &absolutePath)
{
// Add entries for each of the files
QString listing;
foreach(QFileInfo info, QDir(absolutePath).entryInfoList()) {
listing.append(QString("<li><a href=\"%1%2\">%1%2</a></li>")
.arg(info.fileName().toHtmlEscaped())
.arg(HTMLESCAPE(info.fileName()))
.arg(info.isDir() ? "/" : ""));
}

// Build the response and convert the string to UTF-8
QByteArray data = ListTemplate
.arg("/" + path.toHtmlEscaped())
.arg("/" + HTMLESCAPE(path))
.arg(listing)
.arg(QHTTPENGINE_VERSION)
.toUtf8();
Expand Down
2 changes: 2 additions & 0 deletions src/qhttphandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ void QHttpHandler::addSubHandler(const QRegExp &pattern, QHttpHandler *handler)
d->subHandlers.append(SubHandler(pattern, handler));
}

#include <QStringList>

void QHttpHandler::route(QHttpSocket *socket, const QString &path)
{
// Check each of the redirects for a match
Expand Down
6 changes: 6 additions & 0 deletions src/qibytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ QIByteArray::QIByteArray(const QByteArray &other)
: QByteArray(other)
{}

#if QT_VERSION < 0x050000
QIByteArray::QIByteArray(const char *data)
: QByteArray(data)
{}
#endif

QIByteArray::QIByteArray(const char *data, int size)
: QByteArray(data, size)
{}
Expand Down
1 change: 1 addition & 0 deletions src/qjson-backport
Submodule qjson-backport added at d2596f
10 changes: 7 additions & 3 deletions src/qobjecthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
* IN THE SOFTWARE.
*/

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <QMetaMethod>
#include <QMetaObject>
#include <QMetaType>
Expand Down Expand Up @@ -107,7 +106,12 @@ void QObjectHandler::process(QHttpSocket *socket, const QString &path)

// Ensure that the return type of the slot is QVariantMap
QMetaMethod method = metaObject()->method(index);

#if QT_VERSION >= 0x050000
if(method.returnType() != QMetaType::QVariantMap) {
#else
if(!method.typeName() || strcmp(method.typeName(), "QVariantMap") != 0) {
#endif
socket->writeError(QHttpSocket::InternalServerError);
return;
}
Expand Down
37 changes: 30 additions & 7 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
find_package(Qt5Test 5.1 REQUIRED)
if (Qt4_FOUND)
find_package(Qt4 4.8 REQUIRED QtTest)
else()
find_package(Qt5Test 5.1 REQUIRED)
endif()

add_subdirectory(common)

set(TESTS
TestQFilesystemHandler
TestQHttpHandler
TestQHttpParser
TestQHttpServer
TestQHttpSocket
TestQIByteArray
TestQIODeviceCopier
TestQLocalFile
TestQObjectHandler
)

# These don't work with Qt4
if (NOT Qt4_FOUND)
set(TESTS ${TESTS}
TestQFilesystemHandler
TestQHttpHandler
TestQHttpServer
TestQHttpSocket
TestQIODeviceCopier
TestQObjectHandler
)
endif()

foreach(TEST ${TESTS})
add_executable(${TEST} ${TEST}.cpp)

if (Qt4_FOUND)
target_link_libraries(${TEST} Qt4::QtTest)

target_include_directories(${TEST} PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src/mimetypes/include/QtMimeTypes>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src/mimetypes/src/mimetypes/inqt5>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src/qjson-backport>"
)
else()
qt5_use_modules(${TEST} Test)
endif()

target_link_libraries(${TEST} QHttpEngine common)

add_test(NAME ${TEST}
COMMAND ${TEST}
)
Expand Down
1 change: 1 addition & 0 deletions tests/TestQHttpParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
typedef QList<QByteArray> QByteArrayList;

Q_DECLARE_METATYPE(QHttpHeaderMap)
Q_DECLARE_METATYPE(QList<QByteArray>)

const QIByteArray Key1 = "a";
const QByteArray Value1 = "b";
Expand Down
4 changes: 2 additions & 2 deletions tests/TestQObjectHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* IN THE SOFTWARE.
*/

#include <QJsonDocument>
#include <QJsonObject>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <QObject>
#include <QTest>
#include <QVariantMap>
Expand Down
6 changes: 5 additions & 1 deletion tests/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ set(SRC
add_library(common STATIC ${SRC})
target_link_libraries(common QHttpEngine)

qt5_use_modules(common Network)
if (Qt4_FOUND)
target_link_libraries(common Qt4::QtNetwork)
else()
qt5_use_modules(common Network)
endif()