Request.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "PeerStat.h"
  41. #include "a2functional.h"
  42. namespace aria2 {
  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. bool parseUri(const std::string& uri);
  80. public:
  81. Request();
  82. // Sets uri to uri_ and parses URI. Returns true if parsing goes
  83. // successful, otherwise returns false.
  84. bool setUri(const std::string& uri);
  85. // Parses URI. uri_ field are not altered by this method. Returns
  86. // true if parsing goes successful, otherwise returns false.
  87. bool redirectUri(const std::string& uri);
  88. bool resetUri();
  89. void resetTryCount() { tryCount_ = 0; }
  90. void addTryCount() { ++tryCount_; }
  91. unsigned int getTryCount() const { return tryCount_; }
  92. void resetRedirectCount();
  93. unsigned int getRedirectCount() const
  94. {
  95. return redirectCount_;
  96. }
  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. {
  109. if(isIPv6LiteralAddress()) {
  110. return strconcat("[", getHost(), "]");
  111. } else {
  112. return getHost();
  113. }
  114. }
  115. uint16_t getPort() const { return port_; }
  116. const std::string& getDir() const { return dir_; }
  117. const std::string& getFile() const { return file_;}
  118. const std::string& getQuery() const { return query_; }
  119. bool isIPv6LiteralAddress() const { return ipv6LiteralAddress_; }
  120. void supportsPersistentConnection(bool f)
  121. {
  122. supportsPersistentConnection_ = f;
  123. }
  124. bool supportsPersistentConnection()
  125. {
  126. return supportsPersistentConnection_;
  127. }
  128. bool isKeepAliveEnabled() const
  129. {
  130. return supportsPersistentConnection_ && keepAliveHint_;
  131. }
  132. void setKeepAliveHint(bool keepAliveHint)
  133. {
  134. keepAliveHint_ = keepAliveHint;
  135. }
  136. bool isPipeliningEnabled()
  137. {
  138. return supportsPersistentConnection_ && pipeliningHint_;
  139. }
  140. void setPipeliningHint(bool pipeliningHint)
  141. {
  142. pipeliningHint_ = pipeliningHint;
  143. }
  144. bool isPipeliningHint() const
  145. {
  146. return pipeliningHint_;
  147. }
  148. void setMaxPipelinedRequest(unsigned int num);
  149. unsigned int getMaxPipelinedRequest() const
  150. {
  151. return maxPipelinedRequest_;
  152. }
  153. void setMethod(const std::string& method) {
  154. method_ = method;
  155. }
  156. const std::string& getUsername() const
  157. {
  158. return username_;
  159. }
  160. const std::string& getPassword() const
  161. {
  162. return password_;
  163. }
  164. // Returns true if current URI has embedded password.
  165. bool hasPassword() const
  166. {
  167. return hasPassword_;
  168. }
  169. const std::string& getMethod() const {
  170. return method_;
  171. }
  172. const SharedHandle<PeerStat>& getPeerStat() const { return peerStat_; }
  173. const SharedHandle<PeerStat>& initPeerStat();
  174. void requestRemoval()
  175. {
  176. removalRequested_ = true;
  177. }
  178. bool removalRequested() const
  179. {
  180. return removalRequested_;
  181. }
  182. static const std::string METHOD_GET;
  183. static const std::string METHOD_HEAD;
  184. static const std::string PROTO_HTTP;
  185. static const std::string PROTO_HTTPS;
  186. static const std::string PROTO_FTP;
  187. static const unsigned int MAX_REDIRECT = 20;
  188. };
  189. } // namespace aria2
  190. #endif // _D_REQUEST_H_