qhttpclient.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 ESTABLISHED connections (sockets).
  79. * @sa setTimeOut(). */
  80. quint32 timeOut()const;
  81. /** set time-out for ESTABLISHED connections in miliseconds [mSec].
  82. * each (already opened) connection will be forcefully closed after this timeout.
  83. * a zero (0) value disables timer for new connections. */
  84. void setTimeOut(quint32);
  85. /** set a time-out [mSec] for making a new connection (make a request).
  86. * if connecting to server takes more than this time-out value,
  87. * the @sa timedOut(quint32) signal will be emitted and connection will be killed.
  88. * 0 (default) timeout value means to disable this timer.
  89. */
  90. void setConnectingTimeOut(quint32);
  91. template<class Handler>
  92. void setConnectingTimeOut(quint32 timeout, Handler&& func) {
  93. setConnectingTimeOut(timeout);
  94. QObject::connect(this, &QHttpClient::connectingTimeOut,
  95. std::forward<Handler&&>(func)
  96. );
  97. }
  98. /** returns the backend type of this client. */
  99. TBackend backendType() const;
  100. /** returns tcp socket of the connection if backend() == ETcpSocket. */
  101. QTcpSocket* tcpSocket() const;
  102. /** returns local socket of the connection if backend() == ELocalSocket. */
  103. QLocalSocket* localSocket() const;
  104. signals:
  105. /** emitted when a new HTTP connection to the server is established.
  106. * if you overload onRequestReady this signal won't be emitted.
  107. * @sa onRequestReady
  108. * @sa QHttpRequest
  109. */
  110. void httpConnected(QHttpRequest* req);
  111. /** emitted when a new response is received from the server.
  112. * if you overload onResponseReady this signal won't be emitted.
  113. * @sa onResponseReady
  114. * @sa QHttpResponse
  115. */
  116. void newResponse(QHttpResponse* res);
  117. /** emitted when the HTTP connection drops or being disconnected. */
  118. void disconnected();
  119. /// emitted when fails to connect to a HTTP server. @sa setConnectingTimeOut()
  120. void connectingTimeOut();
  121. protected:
  122. /** called when a new HTTP connection is established.
  123. * you can overload this method, the default implementaion only emits connected().
  124. * @param req use this request instance for assinging the
  125. * request headers and sending optional body.
  126. * @see httpConnected(QHttpRequest*)
  127. */
  128. virtual void onRequestReady(QHttpRequest* req);
  129. /** called when a new response is received from the server.
  130. * you can overload this method, the default implementaion only emits newResponse().
  131. * @param res use this instance for reading incoming response.
  132. * @see newResponse(QHttpResponse*)
  133. */
  134. virtual void onResponseReady(QHttpResponse* res);
  135. protected:
  136. explicit QHttpClient(QHttpClientPrivate&, QObject*);
  137. void timerEvent(QTimerEvent*) override;
  138. Q_DECLARE_PRIVATE(QHttpClient)
  139. Q_DISABLE_COPY(QHttpClient)
  140. QScopedPointer<QHttpClientPrivate> d_ptr;
  141. };
  142. ///////////////////////////////////////////////////////////////////////////////
  143. } // namespace client
  144. } // namespace qhttp
  145. ///////////////////////////////////////////////////////////////////////////////
  146. #endif // define QHTTPCLIENT_HPP