47 Commits

Author SHA1 Message Date
Mikhail Svetkin
e2c63563d5 Fix crash for passing lambda with captures to QHttpServer::route
QHttpServer::route accepted the lambda by universal reference, but did
not pass it to QHttpServerRouter::addRule by std::forward.

We can not initialize lambda captures with C++11.
So we always need to copy the lambda by value.

Task-number: QTBUG-74882
Change-Id: I3e2dc6003624414d0ff45ba2e2f1af472e219ff1
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-05-15 12:41:43 +00:00
Mikhail Svetkin
3a811fc0cc Use Qt5TestMacros for run cmake tests
Change-Id: I54486c20d047b5aa73e6012d43c9ca5019f9a51f
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-05-15 11:32:34 +00:00
Mikhail Svetkin
6ac5d57697 Remove unused signal
Change-Id: I28fb6f9f45e43d21cace1978c03acd330a9e6faa
Reviewed-by: Tasuku Suzuki <tasuku.suzuki@qbc.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2019-05-09 17:36:53 +00:00
Tasuku Suzuki
26b4df2582 Change QString for headers in QHttpServerRequest to QByteArray
This is because QHttpServerResponder uses QByteArray for headers' key/
value.

Change-Id: I21b5af4d08e43ee58a1edc95b714c6da0ae10790
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
2019-05-09 15:14:33 +00:00
Tasuku Suzuki
9f7bf6654b Remove a member variable unused
Change-Id: Iacb3259742ca1d3e352624d953b5f5581cb72102
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2019-05-09 14:27:21 +00:00
Tasuku Suzuki
f72ced1671 Fix tests failure introduced by the mime type detection
Commit 4f64330b39bd528a84241e976baa464c6dc89de1 changes some mime types
of responses to text/plain.
The commit also FIXED the "No newline at end of file" warning in a test
data for QHttpServerResponder.

Change-Id: I9b6b1878a2b61bf80db1e39b81ae75c4cedce615
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2019-05-09 13:57:18 +00:00
Jesus Fernandez
d542f7e13d Add a fork() test
Change-Id: Ic7c797f9757ad14803154f6125298bfc8fe03baa
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2019-04-29 13:00:23 +00:00
Tasuku Suzuki
4f64330b39 Find proper mime type for QByteArray data
QByteArray can be used for any data that QMimeDatabase understands

Change-Id: I17d8f0060065c0a93fc4a8cf6450bdd11aed49d0
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2019-04-26 16:41:53 +00:00
Mikhail Svetkin
5147076e5e Fix support for keep-alive connection
QAbstractHttpServer did not clear a request's internal properties (headers,
url, body).
If a request has a keep-alive header, QAbstractHttpServer should clear the
request's internal properties.

Change-Id: I2dfd0565369bd3291cd8d9900045c5a7f9d43ca3
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-04-08 13:49:38 +00:00
Mikhail Svetkin
be06bd66b8 Accept a string as request method in QHttpServer::route()
Allow writing simpler source code.

For example:

httpserver.route("/", "GET|POST", [] () { return ""; })

Instead of:

httpserver.route("/", QHttpServerRequest::Method::Post | QHttpServerRequest::Method::Get, [] () { return ""; })

Change-Id: Id0a754eccaba6b5f9f3be6a3b975383eb94840a0
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-04-04 20:50:09 +00:00
Mikhail Svetkin
6f7e8d28b4 Fix HTTP chunked request body handling
Task: QTBUG-74843
Change-Id: I4978c80195246c99a5e6d1e608b3980f56b39207
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2019-04-03 15:26:38 +00:00
Mikhail Svetkin
5ad0dd4050 Reuse HTTP status codes from nodejs http-parser
Change-Id: Id5f224ff797a56e379ec37c6f57ccd422b098f16
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-04-02 14:40:22 +00:00
Jesus Fernandez
71b37f8588 Use multi-arg version of QString::arg
Change-Id: Ib16a2c3feee10bbd9c784c5303fc0e4e97e5ef5b
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2019-03-19 10:52:18 +00:00
Mikhail Svetkin
2459725682 Allow use of custom data structures as router callback arguments
Change-Id: I6dcc66a95b72bb461f237cade0352a0177065227
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-03-06 09:41:39 +00:00
Maurice Kalinowski
572e0b1f6c Fix build on Windows
Change-Id: I7bb287dedfc146ba717a69170cec529d7ef7fadb
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-02-28 10:08:19 +00:00
Jesus Fernandez
11fb154a84 Fix several build issues and fix the unit tests
It is required to have continuous integration enabled in the
repository.

Change-Id: I71728a6e89841640e77c50a05cf59f34d697f897
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2019-02-28 09:47:48 +00:00
Jesus Fernandez
db45e5a9fa Use QStringLiteral
To avoid using the deprecated QString implicit constructor

Change-Id: If6f29952b0dbec96437e0b940c3cb52a801c0d32
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2019-02-12 08:42:09 +00:00
Jesus Fernandez
21eb76d94a Update http-parser submodule to version 2.9.0
Change-Id: I965ecc876213e91ebd57faae676334a1fb14efc2
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
2019-02-08 10:53:53 +00:00
Jesus Fernandez
fe96330ca0 Fix license headers
Change-Id: Ie15244adb917e2938fe7897c30d4616218d26ac0
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-01-31 11:31:21 +00:00
Jesus Fernandez
b317105242 Fix -Wmismatched-tags warnings
The forward declaration and the definition was not matching. Some
were forward declared as class and defined as struct.

Change-Id: I8912e6373e24126befba52df0bef8795299e50f7
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-01-30 14:56:20 +00:00
Jesus Fernandez
e5e1edc718 Fix build
Change-Id: I9a4e3f9c3bea160af4affbda600ee292cb131a13
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2019-01-30 14:16:55 +00:00
Mikhail Svetkin
540daf882f Introduce QHttpServer
QHttpServer provides a simplified interface to QAbstractHttpServer and
QHttpServerRouter.

Change-Id: I884204aa8140bbab4c0d6a8ab4c798e0b2470e29
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2019-01-25 11:49:44 +00:00
Mikhail Svetkin
a856556784 Introduce QHttpServerRouter
Provide simple API for routing, parsing, capture and call callback

Change-Id: Ibd7c37282d00bd56f96d841db92b473a65a2bf5c
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2019-01-23 10:46:43 +00:00
Mikhail Svetkin
74b22d36f8 Update .gitignore
Allow examples have subfolders for depth more than four

Change-Id: Ia85602393034f68bd9b0214be240d17d1a751e1e
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2018-12-20 14:48:56 +00:00
Mikhail Svetkin
9a41ddd648 Fix tst_QAbstractHttpServer::request
We have undefined behavior when we check server.method.
Because HttpServer::method was uninitialized.

Also start checking a return value from QTcpServer::listen.

Change-Id: Ib7c31f33be956582a6ef264743ca9e44d4de24b1
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2018-12-20 14:08:08 +00:00
Mikhail Svetkin
0763423618 Fix build the test without the websocket support
Change-Id: If32f6a7a42258f9a5a8bed15c776ffa18240da24
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2018-12-20 14:08:00 +00:00
Mikhail Svetkin
f81e7c8562 Add .gitignore
Change-Id: I6e22ef8fa4f906e3e2fd0803b2cb2d8b8334d37a
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2018-12-20 14:07:53 +00:00
Jesus Fernandez
02b15fdd8a Fix CMake tests
Change-Id: I5a79cfc3bab647f9bec29e8b08f9262b211c83b1
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
2018-10-22 07:46:43 +00:00
Tasuku Suzuki
7df45b8ea6 Refactor QAbstractHttpServer::servers()
Change-Id: I11e7100dd5f25d6c02bef28f4c53ce3acc257390
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-10-22 01:26:44 +00:00
Mårten Nordheim
c20d230bf2 Fix compile error: "redefinition; different linkage"
qhttpserverrequest.cpp(56): error C2375: 'operator <<': redefinition;
different linkage

Change-Id: I3fa4c058030e9f81f9c914af10cf740a546a90cc
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-10-18 07:33:41 +00:00
Mårten Nordheim
da100c110a Don't depend on QtGui
Change-Id: I1d3f51d6694a4fa050b6e716b814df17b0c39fc9
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-10-18 07:33:39 +00:00
Mikhail Svetkin
2e7a824526 Refactor QHttpServerResponder::write(QIODevice, ...)
The current implementation has several problems.

1. The function takes an ownership the QIODevice and puts it into a smartpointer.
Also we conntected socket's destroyed signal to lambda which has captured the
smartpointer.
So if responder does not find the file, the smartpointer will
call deleteLater which then will call socket::destroyed and then lambda
will be called and try to access the smartpointer which does not exist
anymore.

2. The function takes an ownership the file(QIODevice)
and puts it into a smartpointer.
Also we conntected the QTemporaryFile's aboutToClose signal to lambda which has captured the
smartpointer.
So when the QTemporaryFile calls destructor it will emit aboutToClose
signal which will call the lambda and this lambda will try to access the smartpointer
which does not exist anymore.

3. If we send a file smaller than chunksize,
IOChunkedTransfer we will never be deleted.

4. Does not check a socket connection type (keep-alive).

5. Does not send anything if file is not found or opened in wrong
mode.

Change-Id: I699e7d5a462c4b8d195908747bf0386132b19973
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-10-04 08:12:29 +00:00
Jesus Fernandez
f53818c8ef Introduce QHttpServerResponder
It encapsulates the socket and gives an API to answer the received
requests.

Change-Id: Ic95db2c50224a650a02b206faca9a0ff8d1cc62b
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Ryan Chu <ryan.chu@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-09-26 12:38:56 +00:00
Ryan Chu
9856170359 Fix build error about missing QTcpSocket header file
The compiler will complain the invalid use of incomplete type 'class
QTcpSocket' when calling the functions by the socket pointer. Such as
connect(), isTransactionStarted(), startTransaction(), and disconnect().

When connecting the signals of QTcpSocket (QTcpSocket::readyRead,
QTcpSocket::disconnected), the compiler will complain the incomplete
type 'QTcpSocket' used in nested name specifier.

Change-Id: I0e631218b0446e72b994718850f6886bec979a2d
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-08-22 10:16:20 +00:00
Ryan Chu
4fd2176252 Fix build error when SSL configuration is disabled
When QT_NO_SSL is used, the value of QT_FEATURE_ssl is also defined to
'-1' in qtnetwork-config.h. The "#if defined(QT_FEATURE_ssl)" block will
be compiled, and then it implies that 'QSslSocket' was not declared in
this scope.

The QT_CONFIG macro implements a compile-time check for features of Qt.
If QT_FEATURE_ssl is '0' or undefined, QT_ CONFIG(ssl) will lead to a
compile error. If QT_FEATURE_ssl is '-1' (unavailable), the
constant-expression of QT_ CONFIG(ssl) will be false.

Change-Id: I80bef731e8246f9206601527435a43195989331b
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-08-22 10:15:52 +00:00
Jesus Fernandez
4fc73da284 Reduce project dependencies
Change-Id: Ic693da5fb70e3352a06ca4f42643ee3b1f7c2625
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2018-07-23 11:40:44 +00:00
Mikhail Svetkin
a35767dd84 Allow build tst_qabstracthttpserver without qtConfig(private_tests)
Change-Id: I6d0c6e9f4bbdf203fb29ae611e064aa284d52ebe
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-07-20 14:00:00 +00:00
Mikhail Svetkin
232a3047f5 Remove unused QNetworkReply
Change-Id: I536226270db1534b0bda1a2205898c291285721a
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
2018-07-20 08:46:07 +00:00
Mikhail Svetkin
1bc95822cf Fix Qt code style
Change-Id: Ie2a5eb3cbfce8742ebdd5973078a665463306466
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-07-20 08:28:13 +00:00
Jesus Fernandez
419e7a17f6 Return the server port in listen()
It allows knowing the server port in case the user chooses to use a
random port. In the previous implementation listen() was returning a
boolean value making it impossible to know the port number.

Change-Id: I73384188b3b2eb57816eb6c6c9a7ac1a511b7456
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-07-19 15:28:32 +00:00
Mikhail Svetkin
cccece0a61 Fix inccorect parsing url with spaces
Change-Id: I942bd80cf3cefaa9f0194ce3281640a3b5077ed2
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
2018-07-18 11:03:33 +00:00
Mikhail Svetkin
8eb3678616 Fix compilation warning
qhttpserverrequest.cpp: In function ‘QDebug operator<<(QDebug, const http_parser*)’:
qhttpserverrequest.cpp:70:89: warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]
     debug.nospace() << "http_parser(" << reinterpret_cast<const void * const>(httpParser) << ": ";

Change-Id: Idf1e92d19033e58634638f3c1a1ccf3a9c3b81d8
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-07-17 09:25:04 +00:00
Mikhail Svetkin
1ba50028d2 Allow inherits from QAbstractHttpServer with custom private class
Change-Id: I35ba1c7cf460d51beecad3427976f1e8a710aef0
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-07-16 15:19:48 +00:00
Jesus Fernandez
6956aa7b77 Add Qt namespace
Change-Id: I0693da9b8d3b71dfc820899b2be5da0697414ade
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-07-16 15:07:39 +00:00
Jesus Fernandez
d4711a0f8e Add public header
Change-Id: Id475a420d783008675ac71fd883035682644603d
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-07-16 15:07:22 +00:00
Jesus Fernandez
b24745265d Introduce Qt HttpServer framework
Small, Qt integrated framework for creating specialized http server.

Goals of the project:
- Create a framework allowing creation of a specialized web server
  running in non public networks (home and company networks, stealth
  or hidden services)
- Create an easy tool for developers to embed http servers in their
  apps.
- Playground to narrow down problems in Qt, related to network
  stack, but also to explore general usability.
- Potentially reduce code duplication in Qt.

Not goals:
- Standalone server, in particular not Apache or nginx replacement

Change-Id: I0d8b83e50992b9a95c88f4735539329279cf5425
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2018-07-16 14:36:08 +00:00
Oswald Buddenhagen
050abe1f33 Initial empty repository 2018-02-02 11:37:47 +00:00