Request.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "SharedHandle.h"
  39. #include <string>
  40. #include <deque>
  41. #define SAFE_CHARS "abcdefghijklmnopqrstuvwxyz"\
  42. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"\
  43. "0123456789"\
  44. ":/?[]@"\
  45. "!$&'()*+,;="\
  46. "-._~"\
  47. "%"\
  48. "#"
  49. namespace aria2 {
  50. class CookieBox;
  51. class Request {
  52. private:
  53. std::string url;
  54. std::string currentUrl;
  55. /**
  56. * URL previously requested to the server. This is used as Referer
  57. */
  58. std::string previousUrl;
  59. /**
  60. * URL used as Referer in the initial request
  61. */
  62. std::string referer;
  63. std::string protocol;
  64. std::string host;
  65. uint16_t port;
  66. std::string dir;
  67. std::string file;
  68. /* after ? mark(includes '?' itself) */
  69. std::string _query;
  70. unsigned int tryCount;
  71. unsigned int _redirectCount;
  72. // whether or not the server supports persistent connection
  73. bool _supportsPersistentConnection;
  74. // enable keep-alive if possible.
  75. bool _keepAliveHint;
  76. // enable pipelining if possible.
  77. bool _pipeliningHint;
  78. std::string method;
  79. std::string _username;
  80. std::string _password;
  81. bool parseUrl(const std::string& url);
  82. bool isHexNumber(const char c) const;
  83. void urlencode(std::string& result, const std::string& src) const;
  84. public:
  85. SharedHandle<CookieBox> cookieBox;
  86. public:
  87. Request();
  88. virtual ~Request();
  89. // Parses URL and sets url, host, port, dir, file fields.
  90. // Returns true if parsing goes successful, otherwise returns false.
  91. bool setUrl(const std::string& url);
  92. // Parses URL and sets host, port, dir, file fields.
  93. // url field are not altered by this method.
  94. // Returns true if parsing goes successful, otherwise returns false.
  95. bool redirectUrl(const std::string& url);
  96. bool resetUrl();
  97. void resetTryCount() { tryCount = 0; }
  98. void addTryCount() { tryCount++; }
  99. unsigned int getTryCount() const { return tryCount; }
  100. //bool noMoreTry() const { return tryCount >= PREF_MAX_TRY; }
  101. void resetRedirectCount();
  102. unsigned int getRedirectCount() const;
  103. const std::string& getUrl() const { return url; }
  104. const std::string& getCurrentUrl() const { return currentUrl; }
  105. const std::string& getPreviousUrl() const { return previousUrl; }
  106. const std::string& getReferer() const { return referer; }
  107. void setReferer(const std::string& url) { referer = previousUrl = url; }
  108. const std::string& getProtocol() const { return protocol; }
  109. const std::string& getHost() const { return host; }
  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. void supportsPersistentConnection(bool f)
  115. {
  116. _supportsPersistentConnection = f;
  117. }
  118. bool supportsPersistentConnection()
  119. {
  120. return _supportsPersistentConnection;
  121. }
  122. bool isKeepAliveEnabled() const
  123. {
  124. return _supportsPersistentConnection && _keepAliveHint;
  125. }
  126. void setKeepAliveHint(bool keepAliveHint)
  127. {
  128. _keepAliveHint = keepAliveHint;
  129. }
  130. bool isPipeliningEnabled()
  131. {
  132. return _supportsPersistentConnection && _pipeliningHint;
  133. }
  134. void setPipeliningHint(bool pipeliningHint)
  135. {
  136. _pipeliningHint = pipeliningHint;
  137. }
  138. void setMethod(const std::string& method) {
  139. this->method = method;
  140. }
  141. const std::string& getUsername() const
  142. {
  143. return _username;
  144. }
  145. const std::string& getPassword() const
  146. {
  147. return _password;
  148. }
  149. const std::string& getMethod() const {
  150. return method;
  151. }
  152. static const std::string METHOD_GET;
  153. static const std::string METHOD_HEAD;
  154. static const std::string PROTO_HTTP;
  155. static const std::string PROTO_HTTPS;
  156. static const std::string PROTO_FTP;
  157. static const unsigned int MAX_REDIRECT = 20;
  158. };
  159. typedef SharedHandle<Request> RequestHandle;
  160. typedef WeakHandle<Request> RequestWeakHandle;
  161. typedef std::deque<RequestHandle> Requests;
  162. } // namespace aria2
  163. #endif // _D_REQUEST_H_