Request.h 6.2 KB

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