qhttpclient.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /** HTTP client class.
  2. * https://github.com/azadkuh/qhttp
  3. *
  4. * @author amir zamani
  5. * @version 2.0.0
  6. * @date 2014-07-11
  7. */
  8. #ifndef QHTTPCLIENT_HPP
  9. #define QHTTPCLIENT_HPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "qhttpfwd.hpp"
  12. #include <QTcpSocket>
  13. #include <QUrl>
  14. ///////////////////////////////////////////////////////////////////////////////
  15. namespace qhttp {
  16. namespace client {
  17. ///////////////////////////////////////////////////////////////////////////////
  18. typedef std::function<void (QHttpRequest*)> TRequstHandler;
  19. typedef std::function<void (QHttpResponse*)> TResponseHandler;
  20. /** a simple and async HTTP client class which sends a request to an HTTP server and parses the
  21. * corresponding response.
  22. * This class internally handles the memory management and life cycle of QHttpRequest and
  23. * QHttpResponse instances. you do not have to manually delete or keep their pointers.
  24. * in fact the QHttpRequest and QHttpResponse object will be deleted when the internal socket
  25. * disconnects.
  26. */
  27. class QHTTP_API QHttpClient : public QObject
  28. {
  29. Q_OBJECT
  30. Q_PROPERTY(quint32 timeOut READ timeOut WRITE setTimeOut)
  31. public:
  32. explicit QHttpClient(QObject *parent = nullptr);
  33. virtual ~QHttpClient();
  34. /** tries to connect to a HTTP server.
  35. * when the connection is made, the reqHandler will be called
  36. * and when the response is ready, resHandler will be called.
  37. * @note httpConnected() and newResponse() won't be emitted.
  38. *
  39. * @param method an HTTP method, ex: GET, POST, ...
  40. * @param url specifies server's address, port and optional path and query strings.
  41. * if url starts with socket:// the request will be made on QLocalSocket, otherwise
  42. * normal QTcpSocket will be used.
  43. * @param resHandler response handler (a lambda, std::function object, ...)
  44. * @return true if the url is valid or false (no connection will be made).
  45. */
  46. bool request(THttpMethod method, QUrl url,
  47. const TRequstHandler& reqHandler,
  48. const TResponseHandler& resHandler);
  49. /** tries to connect to a HTTP server.
  50. * when the connection is made, a default request handler is called automatically (
  51. * simply calls req->end()) and when the response is ready, resHandler will be called.
  52. * @note httpConnected() and newResponse() won't be emitted.
  53. *
  54. * @param method an HTTP method, ex: GET, POST, ...
  55. * @param url specifies server's address, port and optional path and query strings.
  56. * @param resHandler response handler (a lambda, std::function object, ...)
  57. * @return true if the url is valid or false (no connection will be made).
  58. */
  59. inline bool request(THttpMethod method, QUrl url, const TResponseHandler& resHandler) {
  60. return request(method, url, nullptr, resHandler);
  61. }
  62. /** tries to connect to a HTTP server.
  63. * when the connection is made, creates and emits a QHttpRequest instance
  64. * by @sa httpConnected(QHttpRequest*).
  65. * @note both httpConnected() and newResponse() may be emitted.
  66. *
  67. * @param method an HTTP method, ex: GET, POST, ...
  68. * @param url specifies server's address, port and optional path and query strings.
  69. * @return true if the url is valid or false (no connection will be made).
  70. */
  71. inline bool request(THttpMethod method, QUrl url) {
  72. return request(method, url, nullptr, nullptr);
  73. }
  74. /** checks if the connetion to the server is open. */
  75. bool isOpen() const;
  76. /** forcefully close the connection. */
  77. void killConnection();
  78. /** returns time-out value [mSec] for open connections (sockets).
  79. * @sa setTimeOut(). */
  80. quint32 timeOut()const;
  81. /** set time-out for new open connections in miliseconds [mSec].
  82. * each connection will be forcefully closed after this timeout.
  83. * a zero (0) value disables timer for new connections. */
  84. void setTimeOut(quint32);
  85. /** returns the backend type of this client. */
  86. TBackend backendType() const;
  87. /** returns tcp socket of the connection if backend() == ETcpSocket. */
  88. QTcpSocket* tcpSocket() const;
  89. /** returns local socket of the connection if backend() == ELocalSocket. */
  90. QLocalSocket* localSocket() const;
  91. signals:
  92. /** emitted when a new HTTP connection to the server is established.
  93. * if you overload onRequestReady this signal won't be emitted.
  94. * @sa onRequestReady
  95. * @sa QHttpRequest
  96. */
  97. void httpConnected(QHttpRequest* req);
  98. /** emitted when a new response is received from the server.
  99. * if you overload onResponseReady this signal won't be emitted.
  100. * @sa onResponseReady
  101. * @sa QHttpResponse
  102. */
  103. void newResponse(QHttpResponse* res);
  104. /** emitted when the HTTP connection drops or being disconnected. */
  105. void disconnected();
  106. protected:
  107. /** called when a new HTTP connection is established.
  108. * you can overload this method, the default implementaion only emits connected().
  109. * @param req use this request instance for assinging the
  110. * request headers and sending optional body.
  111. * @see httpConnected(QHttpRequest*)
  112. */
  113. virtual void onRequestReady(QHttpRequest* req);
  114. /** called when a new response is received from the server.
  115. * you can overload this method, the default implementaion only emits newResponse().
  116. * @param res use this instance for reading incoming response.
  117. * @see newResponse(QHttpResponse*)
  118. */
  119. virtual void onResponseReady(QHttpResponse* res);
  120. protected:
  121. explicit QHttpClient(QHttpClientPrivate&, QObject*);
  122. void timerEvent(QTimerEvent*) override;
  123. Q_DECLARE_PRIVATE(QHttpClient)
  124. Q_DISABLE_COPY(QHttpClient)
  125. QScopedPointer<QHttpClientPrivate> d_ptr;
  126. };
  127. ///////////////////////////////////////////////////////////////////////////////
  128. } // namespace client
  129. } // namespace qhttp
  130. ///////////////////////////////////////////////////////////////////////////////
  131. #endif // define QHTTPCLIENT_HPP