Request.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. namespace aria2 {
  42. class Request {
  43. private:
  44. std::string url;
  45. std::string currentUrl;
  46. /**
  47. * URL previously requested to the server. This is used as Referer
  48. */
  49. std::string previousUrl;
  50. /**
  51. * URL 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 parseUrl(const std::string& url);
  75. public:
  76. Request();
  77. // Parses URL and sets url, host, port, dir, file fields.
  78. // Returns true if parsing goes successful, otherwise returns false.
  79. bool setUrl(const std::string& url);
  80. // Parses URL and sets host, port, dir, file fields.
  81. // url field are not altered by this method.
  82. // Returns true if parsing goes successful, otherwise returns false.
  83. bool redirectUrl(const std::string& url);
  84. bool resetUrl();
  85. void resetTryCount() { tryCount = 0; }
  86. void addTryCount() { tryCount++; }
  87. unsigned int getTryCount() const { return tryCount; }
  88. //bool noMoreTry() const { return tryCount >= PREF_MAX_TRY; }
  89. void resetRedirectCount();
  90. unsigned int getRedirectCount() const;
  91. // Returns URI passed by setUrl()
  92. const std::string& getUrl() const { return url; }
  93. const std::string& getCurrentUrl() const { return currentUrl; }
  94. const std::string& getPreviousUrl() const { return previousUrl; }
  95. const std::string& getReferer() const { return referer; }
  96. void setReferer(const std::string& url);
  97. const std::string& getProtocol() const { return protocol; }
  98. const std::string& getHost() const { return host; }
  99. uint16_t getPort() const { return port; }
  100. const std::string& getDir() const { return dir; }
  101. const std::string& getFile() const { return file;}
  102. const std::string& getQuery() const { return _query; }
  103. void supportsPersistentConnection(bool f)
  104. {
  105. _supportsPersistentConnection = f;
  106. }
  107. bool supportsPersistentConnection()
  108. {
  109. return _supportsPersistentConnection;
  110. }
  111. bool isKeepAliveEnabled() const
  112. {
  113. return _supportsPersistentConnection && _keepAliveHint;
  114. }
  115. void setKeepAliveHint(bool keepAliveHint)
  116. {
  117. _keepAliveHint = keepAliveHint;
  118. }
  119. bool isPipeliningEnabled()
  120. {
  121. return _supportsPersistentConnection && _pipeliningHint;
  122. }
  123. void setPipeliningHint(bool pipeliningHint)
  124. {
  125. _pipeliningHint = pipeliningHint;
  126. }
  127. bool isPipeliningHint() const;
  128. void setMaxPipelinedRequest(unsigned int num);
  129. unsigned int getMaxPipelinedRequest() const;
  130. void setMethod(const std::string& method) {
  131. this->method = method;
  132. }
  133. const std::string& getUsername() const
  134. {
  135. return _username;
  136. }
  137. const std::string& getPassword() const
  138. {
  139. return _password;
  140. }
  141. const std::string& getMethod() const {
  142. return method;
  143. }
  144. static const std::string METHOD_GET;
  145. static const std::string METHOD_HEAD;
  146. static const std::string PROTO_HTTP;
  147. static const std::string PROTO_HTTPS;
  148. static const std::string PROTO_FTP;
  149. static const unsigned int MAX_REDIRECT = 20;
  150. };
  151. typedef SharedHandle<Request> RequestHandle;
  152. typedef WeakHandle<Request> RequestWeakHandle;
  153. typedef std::deque<RequestHandle> Requests;
  154. } // namespace aria2
  155. #endif // _D_REQUEST_H_