qhttpserver.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2011-2014 Nikhil Marathe <nsm.nikhil@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #ifndef Q_HTTP_SERVER
  23. #define Q_HTTP_SERVER
  24. #define QHTTPSERVER_VERSION_MAJOR 0
  25. #define QHTTPSERVER_VERSION_MINOR 1
  26. #define QHTTPSERVER_VERSION_PATCH 0
  27. #include "qhttpserverapi.h"
  28. #include "qhttpserverfwd.h"
  29. #include <QObject>
  30. #include <QHostAddress>
  31. /// Maps status codes to string reason phrases
  32. extern QHash<int, QString> STATUS_CODES;
  33. /// The QHttpServer class forms the basis of the %QHttpServer
  34. /// project. It is a fast, non-blocking HTTP server.
  35. /** These are the steps to create a server, handle and respond to requests:
  36. <ol>
  37. <li>Create an instance of QHttpServer.</li>
  38. <li>Connect a slot to the newRequest() signal.</li>
  39. <li>Create a QCoreApplication to drive the server event loop.</li>
  40. <li>Respond to clients by writing out to the QHttpResponse object.</li>
  41. </ol>
  42. <b>Here is a simple sample application on how to use this library</b>
  43. helloworld.cpp
  44. @include helloworld/helloworld.cpp
  45. helloworld.h
  46. @include helloworld/helloworld.h */
  47. class QHTTPSERVER_API QHttpServer : public QObject
  48. {
  49. Q_OBJECT
  50. public:
  51. /// Construct a new HTTP Server.
  52. /** @param parent Parent QObject for the server. */
  53. QHttpServer(QObject *parent = 0);
  54. virtual ~QHttpServer();
  55. /// Start the server by bounding to the given @c address and @c port.
  56. /** @note This function returns immediately, it does not block.
  57. @param address Address on which to listen to. Default is to listen on
  58. all interfaces which means the server can be accessed from anywhere.
  59. @param port Port number on which the server should run.
  60. @return True if the server was started successfully, false otherwise.
  61. @sa listen(quint16) */
  62. bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 80);
  63. /// Starts the server on @c port listening on all interfaces.
  64. /** @param port Port number on which the server should run.
  65. @return True if the server was started successfully, false otherwise.
  66. @sa listen(const QHostAddress&, quint16) */
  67. bool listen(quint16 port);
  68. /// Return the last error encountered.
  69. QString errorString() const;
  70. /// Stop the server and listening for new connections.
  71. void close();
  72. Q_SIGNALS:
  73. /// Emitted when a client makes a new request to the server.
  74. /** The slot should use the given @c request and @c response
  75. objects to communicate with the client.
  76. @param request New incoming request.
  77. @param response Response object to the request. */
  78. void newRequest(QHttpRequest *request, QHttpResponse *response);
  79. private Q_SLOTS:
  80. void newConnection();
  81. private:
  82. QTcpServer *m_tcpServer;
  83. QString m_errorString;
  84. };
  85. #endif