Request.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "SharedHandle.h"
  40. #include "TimerA2.h"
  41. namespace aria2 {
  42. class PeerStat;
  43. class Request {
  44. private:
  45. std::string uri_;
  46. std::string currentUri_;
  47. /**
  48. * URI previously requested to the server. This is used as Referer
  49. */
  50. std::string previousUri_;
  51. /**
  52. * URI 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. bool hasPassword_;
  76. bool ipv6LiteralAddress_;
  77. SharedHandle<PeerStat> peerStat_;
  78. bool removalRequested_;
  79. std::string connectedHostname_;
  80. std::string connectedAddr_;
  81. uint16_t connectedPort_;
  82. Timer wakeTime_;
  83. bool parseUri(const std::string& uri);
  84. public:
  85. Request();
  86. ~Request();
  87. // Sets uri to uri_ and parses URI. Returns true if parsing goes
  88. // successful, otherwise returns false.
  89. bool setUri(const std::string& uri);
  90. // Parses URI. uri_ field are not altered by this method. Returns
  91. // true if parsing goes successful, otherwise returns false.
  92. bool redirectUri(const std::string& uri);
  93. bool resetUri();
  94. void resetTryCount() { tryCount_ = 0; }
  95. void addTryCount() { ++tryCount_; }
  96. unsigned int getTryCount() const { return tryCount_; }
  97. void resetRedirectCount();
  98. unsigned int getRedirectCount() const { return redirectCount_; }
  99. // Returns URI passed by setUri()
  100. const std::string& getUri() const { return uri_; }
  101. const std::string& getCurrentUri() const { return currentUri_; }
  102. const std::string& getPreviousUri() const { return previousUri_; }
  103. const std::string& getReferer() const { return referer_; }
  104. void setReferer(const std::string& uri);
  105. const std::string& getProtocol() const { return protocol_; }
  106. const std::string& getHost() const { return host_; }
  107. // Same as getHost(), but for IPv6 literal addresses, enclose them
  108. // with square brackets and return.
  109. std::string getURIHost() const;
  110. uint16_t getPort() const { return port_; }
  111. const std::string& getDir() const { return dir_; }
  112. const std::string& getFile() const { return file_;}
  113. const std::string& getQuery() const { return query_; }
  114. bool isIPv6LiteralAddress() const { return ipv6LiteralAddress_; }
  115. void supportsPersistentConnection(bool f)
  116. {
  117. supportsPersistentConnection_ = f;
  118. }
  119. bool supportsPersistentConnection()
  120. {
  121. return supportsPersistentConnection_;
  122. }
  123. bool isKeepAliveEnabled() const
  124. {
  125. return supportsPersistentConnection_ && keepAliveHint_;
  126. }
  127. void setKeepAliveHint(bool keepAliveHint)
  128. {
  129. keepAliveHint_ = keepAliveHint;
  130. }
  131. bool isPipeliningEnabled()
  132. {
  133. return supportsPersistentConnection_ && pipeliningHint_;
  134. }
  135. void setPipeliningHint(bool pipeliningHint)
  136. {
  137. pipeliningHint_ = pipeliningHint;
  138. }
  139. bool isPipeliningHint() const
  140. {
  141. return pipeliningHint_;
  142. }
  143. void setMaxPipelinedRequest(unsigned int num);
  144. unsigned int getMaxPipelinedRequest() const
  145. {
  146. return maxPipelinedRequest_;
  147. }
  148. void setMethod(const std::string& method);
  149. const std::string& getUsername() const
  150. {
  151. return username_;
  152. }
  153. const std::string& getPassword() const
  154. {
  155. return password_;
  156. }
  157. // Returns true if current URI has embedded password.
  158. bool hasPassword() const
  159. {
  160. return hasPassword_;
  161. }
  162. const std::string& getMethod() const
  163. {
  164. return method_;
  165. }
  166. const SharedHandle<PeerStat>& getPeerStat() const
  167. {
  168. return peerStat_;
  169. }
  170. const SharedHandle<PeerStat>& initPeerStat();
  171. void requestRemoval()
  172. {
  173. removalRequested_ = true;
  174. }
  175. bool removalRequested() const
  176. {
  177. return removalRequested_;
  178. }
  179. void setConnectedAddrInfo
  180. (const std::string& hostname, const std::string& addr, uint16_t port);
  181. const std::string& getConnectedHostname() const
  182. {
  183. return connectedHostname_;
  184. }
  185. const std::string& getConnectedAddr() const
  186. {
  187. return connectedAddr_;
  188. }
  189. uint16_t getConnectedPort() const
  190. {
  191. return connectedPort_;
  192. }
  193. void setWakeTime(Timer timer)
  194. {
  195. wakeTime_ = timer;
  196. }
  197. const Timer& getWakeTime()
  198. {
  199. return wakeTime_;
  200. }
  201. static const std::string METHOD_GET;
  202. static const std::string METHOD_HEAD;
  203. static const std::string PROTO_HTTP;
  204. static const std::string PROTO_HTTPS;
  205. static const std::string PROTO_FTP;
  206. static const unsigned int MAX_REDIRECT = 20;
  207. };
  208. } // namespace aria2
  209. #endif // D_REQUEST_H