Request.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <memory>
  40. #include "TimerA2.h"
  41. #include "uri.h"
  42. namespace aria2 {
  43. class PeerStat;
  44. class Request {
  45. private:
  46. uri::UriStruct us_;
  47. std::string uri_;
  48. std::string currentUri_;
  49. /**
  50. * URI used as Referer header field
  51. */
  52. std::string referer_;
  53. std::string method_;
  54. std::string connectedHostname_;
  55. std::string connectedAddr_;
  56. int tryCount_;
  57. int redirectCount_;
  58. // whether or not the server supports persistent connection
  59. bool supportsPersistentConnection_;
  60. // enable keep-alive if possible.
  61. bool keepAliveHint_;
  62. // enable pipelining if possible.
  63. bool pipeliningHint_;
  64. // maximum number of pipelined requests
  65. int maxPipelinedRequest_;
  66. std::shared_ptr<PeerStat> peerStat_;
  67. bool removalRequested_;
  68. uint16_t connectedPort_;
  69. Timer wakeTime_;
  70. bool parseUri(const std::string& uri);
  71. public:
  72. Request();
  73. ~Request();
  74. // Sets uri to uri_ and parses URI. Returns true if parsing goes
  75. // successful, otherwise returns false.
  76. bool setUri(const std::string& uri);
  77. // Parses URI. uri_ field are not altered by this method. Returns
  78. // true if parsing goes successful, otherwise returns false.
  79. bool redirectUri(const std::string& uri);
  80. bool resetUri();
  81. void resetTryCount() { tryCount_ = 0; }
  82. void addTryCount() { ++tryCount_; }
  83. int getTryCount() const { return tryCount_; }
  84. void resetRedirectCount();
  85. int getRedirectCount() const { return redirectCount_; }
  86. // Returns URI passed by setUri()
  87. const std::string& getUri() const { return uri_; }
  88. const std::string& getCurrentUri() const { return currentUri_; }
  89. const std::string& getReferer() const { return referer_; }
  90. void setReferer(const std::string& uri);
  91. const std::string& getProtocol() const { return us_.protocol; }
  92. const std::string& getHost() const { return us_.host; }
  93. // Same as getHost(), but for IPv6 literal addresses, enclose them
  94. // with square brackets and return.
  95. std::string getURIHost() const;
  96. uint16_t getPort() const { return us_.port; }
  97. const std::string& getDir() const { return us_.dir; }
  98. const std::string& getFile() const { return us_.file; }
  99. const std::string& getQuery() const { return us_.query; }
  100. bool isIPv6LiteralAddress() const { return us_.ipv6LiteralAddress; }
  101. void supportsPersistentConnection(bool f)
  102. {
  103. supportsPersistentConnection_ = f;
  104. }
  105. bool supportsPersistentConnection() { return supportsPersistentConnection_; }
  106. bool isKeepAliveEnabled() const
  107. {
  108. return supportsPersistentConnection_ && keepAliveHint_;
  109. }
  110. void setKeepAliveHint(bool keepAliveHint) { keepAliveHint_ = keepAliveHint; }
  111. bool isPipeliningEnabled()
  112. {
  113. return supportsPersistentConnection_ && pipeliningHint_;
  114. }
  115. void setPipeliningHint(bool pipeliningHint)
  116. {
  117. pipeliningHint_ = pipeliningHint;
  118. }
  119. bool isPipeliningHint() const { return pipeliningHint_; }
  120. void setMaxPipelinedRequest(int num);
  121. int getMaxPipelinedRequest() const { return maxPipelinedRequest_; }
  122. void setMethod(const std::string& method);
  123. const std::string& getUsername() const { return us_.username; }
  124. const std::string& getPassword() const { return us_.password; }
  125. // Returns true if current URI has embedded password.
  126. bool hasPassword() const { return us_.hasPassword; }
  127. const std::string& getMethod() const { return method_; }
  128. const std::shared_ptr<PeerStat>& getPeerStat() const { return peerStat_; }
  129. const std::shared_ptr<PeerStat>& initPeerStat();
  130. void requestRemoval() { removalRequested_ = true; }
  131. bool removalRequested() const { return removalRequested_; }
  132. void setConnectedAddrInfo(const std::string& hostname,
  133. const std::string& addr, uint16_t port);
  134. const std::string& getConnectedHostname() const { return connectedHostname_; }
  135. const std::string& getConnectedAddr() const { return connectedAddr_; }
  136. uint16_t getConnectedPort() const { return connectedPort_; }
  137. void setWakeTime(Timer timer) { wakeTime_ = timer; }
  138. const Timer& getWakeTime() { return wakeTime_; }
  139. static const std::string METHOD_GET;
  140. static const std::string METHOD_HEAD;
  141. static const int MAX_REDIRECT = 20;
  142. static const std::string DEFAULT_FILE;
  143. };
  144. } // namespace aria2
  145. #endif // D_REQUEST_H