main-page.dox 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. @mainpage %QHttpServer Documentation
  3. @section introduction Introduction
  4. %QHttpServer is a easy to use, fast and light-weight
  5. HTTP Server suitable for C++ web applications backed
  6. by Qt. Since C++ web applications are pretty uncommon
  7. the market for this project is pretty low.
  8. But integrating this with a module like QtScript and using it to write
  9. JavaScript web applications is a tempting possibility, and something that
  10. I demonstrated at <a href="http://conf.kde.in">conf.kde.in 2011</a>. The slides
  11. can be found on <a href="https://github.com/nikhilm/confkdein11">Github</a>.
  12. %QHttpServer uses a signal-slots based mechanism
  13. for all communication, so no inheritance is required.
  14. It tries to be as asynchronous as possible, to the
  15. extent that request body data is also delivered as and
  16. when it is received over the socket via signals. This
  17. kind of programming may take some getting used to.
  18. %QHttpServer is backed by <a href="http://github.com/ry/http-parser">Ryan
  19. Dahl's secure and fast http parser</a> which makes it streaming
  20. till the lowest level.
  21. @section usage Usage
  22. Using %QHttpServer is very simple. Simply create a QHttpServer,
  23. connect a slot to the newRequest() signal and use the request and
  24. response objects.
  25. See the QHttpServer class documentation for an example.
  26. @section memorymanagement Memory Management
  27. The QHttpRequest and QHttpResponse deletion policies
  28. are such.
  29. QHttpRequest is <b>never</b> deleted by %QHttpServer.
  30. Since it is not possible to determine till what point the application
  31. may want access to its data, it is up to the application to delete it.
  32. A recommended way to handle this is to create a new responder object for
  33. every request and to delete the request in that object's destructor. The
  34. object itself can be deleted by connecting to QHttpResponse's done()
  35. slot as explained below.
  36. You should <b>not</b> delete the QHttpRequest object until it
  37. has emitted an QHttpRequest::end() signal.
  38. QHttpResponse queues itself up for auto-deletion once the application
  39. calls its end() method. Once the data has been flushed to the underlying
  40. socket, the object will emit a QHttpResponse::done() signal before queueing itself up
  41. for deletion. You should <b>not</b> interact with the response
  42. object once it has emitted QHttpResponse::done() although actual deletion does not
  43. happen until QHttpResponse::destroyed() is emitted.
  44. QHttpResponse::done() serves as a useful way to handle memory management of the
  45. application itself. For example:
  46. @code
  47. MyApp::MyApp() : QObject()
  48. {
  49. QHttpServer *server = new QHttpServer(this);
  50. connect(server, SIGNAL(newRequest(...)), this, SLOT(handle(...)));
  51. s.listen(8080);
  52. }
  53. void MyApp::handle(QHttpRequest *request, QHttpResponse *response)
  54. {
  55. if (request->path() == x) // Match a route
  56. new Responder(request, response);
  57. else
  58. new PageNotFound(request, response);
  59. }
  60. ...
  61. Responder::Responder(QHttpRequest *request, QHttpResponse *response)
  62. {
  63. m_request = request;
  64. connect(request, SIGNAL(end()), response, SLOT(end()));
  65. // Once the request is complete, the response is sent.
  66. // When the response ends, it deletes itself
  67. // the Responder object connects to done()
  68. // which will lead to it being deleted
  69. // and this will delete the request.
  70. // So all 3 are properly deleted.
  71. connect(response, SIGNAL(done()), this, SLOT(deleteLater()));
  72. response->writeHead(200);
  73. response->write("Quitting soon");
  74. }
  75. Responder::~Responder()
  76. {
  77. delete m_request;
  78. m_request = 0;
  79. }
  80. @endcode
  81. */