Request.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_REQUEST_H_
  36. #define _D_REQUEST_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include "SharedHandle.h"
  41. #include "PeerStat.h"
  42. namespace aria2 {
  43. class Request {
  44. private:
  45. std::string url;
  46. std::string currentUrl;
  47. /**
  48. * URL previously requested to the server. This is used as Referer
  49. */
  50. std::string previousUrl;
  51. /**
  52. * URL used as Referer in the initial request
  53. */
  54. std::string referer;
  55. std::string protocol;
  56. std::string host;
  57. uint16_t port;
  58. std::string dir;
  59. std::string file;
  60. /* after ? mark(includes '?' itself) */
  61. std::string _query;
  62. unsigned int tryCount;
  63. unsigned int _redirectCount;
  64. // whether or not the server supports persistent connection
  65. bool _supportsPersistentConnection;
  66. // enable keep-alive if possible.
  67. bool _keepAliveHint;
  68. // enable pipelining if possible.
  69. bool _pipeliningHint;
  70. // maximum number of pipelined requests
  71. unsigned int _maxPipelinedRequest;
  72. std::string method;
  73. std::string _username;
  74. std::string _password;
  75. SharedHandle<PeerStat> _peerStat;
  76. bool parseUrl(const std::string& url);
  77. public:
  78. Request();
  79. // Parses URL and sets url, host, port, dir, file fields.
  80. // Returns true if parsing goes successful, otherwise returns false.
  81. bool setUrl(const std::string& url);
  82. // Parses URL and sets host, port, dir, file fields.
  83. // url field are not altered by this method.
  84. // Returns true if parsing goes successful, otherwise returns false.
  85. bool redirectUrl(const std::string& url);
  86. bool resetUrl();
  87. void resetTryCount() { tryCount = 0; }
  88. void addTryCount() { ++tryCount; }
  89. unsigned int getTryCount() const { return tryCount; }
  90. //bool noMoreTry() const { return tryCount >= PREF_MAX_TRY; }
  91. void resetRedirectCount();
  92. unsigned int getRedirectCount() const
  93. {
  94. return _redirectCount;
  95. }
  96. // Returns URI passed by setUrl()
  97. const std::string& getUrl() const { return url; }
  98. const std::string& getCurrentUrl() const { return currentUrl; }
  99. const std::string& getPreviousUrl() const { return previousUrl; }
  100. const std::string& getReferer() const { return referer; }
  101. void setReferer(const std::string& url);
  102. const std::string& getProtocol() const { return protocol; }
  103. const std::string& getHost() const { return host; }
  104. uint16_t getPort() const { return port; }
  105. const std::string& getDir() const { return dir; }
  106. const std::string& getFile() const { return file;}
  107. const std::string& getQuery() const { return _query; }
  108. void supportsPersistentConnection(bool f)
  109. {
  110. _supportsPersistentConnection = f;
  111. }
  112. bool supportsPersistentConnection()
  113. {
  114. return _supportsPersistentConnection;
  115. }
  116. bool isKeepAliveEnabled() const
  117. {
  118. return _supportsPersistentConnection && _keepAliveHint;
  119. }
  120. void setKeepAliveHint(bool keepAliveHint)
  121. {
  122. _keepAliveHint = keepAliveHint;
  123. }
  124. bool isPipeliningEnabled()
  125. {
  126. return _supportsPersistentConnection && _pipeliningHint;
  127. }
  128. void setPipeliningHint(bool pipeliningHint)
  129. {
  130. _pipeliningHint = pipeliningHint;
  131. }
  132. bool isPipeliningHint() const
  133. {
  134. return _pipeliningHint;
  135. }
  136. void setMaxPipelinedRequest(unsigned int num);
  137. unsigned int getMaxPipelinedRequest() const
  138. {
  139. return _maxPipelinedRequest;
  140. }
  141. void setMethod(const std::string& method) {
  142. this->method = method;
  143. }
  144. const std::string& getUsername() const
  145. {
  146. return _username;
  147. }
  148. const std::string& getPassword() const
  149. {
  150. return _password;
  151. }
  152. const std::string& getMethod() const {
  153. return method;
  154. }
  155. const SharedHandle<PeerStat>& getPeerStat() const { return _peerStat; }
  156. const SharedHandle<PeerStat>& initPeerStat();
  157. static const std::string METHOD_GET;
  158. static const std::string METHOD_HEAD;
  159. static const std::string PROTO_HTTP;
  160. static const std::string PROTO_HTTPS;
  161. static const std::string PROTO_FTP;
  162. static const unsigned int MAX_REDIRECT = 20;
  163. };
  164. typedef SharedHandle<Request> RequestHandle;
  165. typedef WeakHandle<Request> RequestWeakHandle;
  166. typedef std::deque<RequestHandle> Requests;
  167. } // namespace aria2
  168. #endif // _D_REQUEST_H_