Request.h 5.2 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 "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. // whether or not the server supports persistent connection
  72. bool _supportsPersistentConnection;
  73. // enable keep-alive if possible.
  74. bool _keepAliveHint;
  75. // enable pipelining if possible.
  76. bool _pipeliningHint;
  77. std::string method;
  78. std::string _username;
  79. std::string _password;
  80. bool parseUrl(const std::string& url);
  81. bool isHexNumber(const char c) const;
  82. void urlencode(std::string& result, const std::string& src) const;
  83. public:
  84. SharedHandle<CookieBox> cookieBox;
  85. public:
  86. Request();
  87. virtual ~Request();
  88. // Parses URL and sets url, host, port, dir, file fields.
  89. // Returns true if parsing goes successful, otherwise returns false.
  90. bool setUrl(const std::string& url);
  91. // Parses URL and sets host, port, dir, file fields.
  92. // url field are not altered by this method.
  93. // Returns true if parsing goes successful, otherwise returns false.
  94. bool redirectUrl(const std::string& url);
  95. bool resetUrl();
  96. void resetTryCount() { tryCount = 0; }
  97. void addTryCount() { tryCount++; }
  98. unsigned int getTryCount() const { return tryCount; }
  99. //bool noMoreTry() const { return tryCount >= PREF_MAX_TRY; }
  100. const std::string& getUrl() const { return url; }
  101. const std::string& getCurrentUrl() const { return currentUrl; }
  102. const std::string& getPreviousUrl() const { return previousUrl; }
  103. const std::string& getReferer() const { return referer; }
  104. void setReferer(const std::string& url) { referer = previousUrl = url; }
  105. const std::string& getProtocol() const { return protocol; }
  106. const std::string& getHost() const { return host; }
  107. uint16_t getPort() const { return port; }
  108. const std::string& getDir() const { return dir; }
  109. const std::string& getFile() const { return file;}
  110. const std::string& getQuery() const { return _query; }
  111. void supportsPersistentConnection(bool f)
  112. {
  113. _supportsPersistentConnection = f;
  114. }
  115. bool supportsPersistentConnection()
  116. {
  117. return _supportsPersistentConnection;
  118. }
  119. bool isKeepAliveEnabled() const
  120. {
  121. return _supportsPersistentConnection && _keepAliveHint;
  122. }
  123. void setKeepAliveHint(bool keepAliveHint)
  124. {
  125. _keepAliveHint = keepAliveHint;
  126. }
  127. bool isPipeliningEnabled()
  128. {
  129. return _supportsPersistentConnection && _pipeliningHint;
  130. }
  131. void setPipeliningHint(bool pipeliningHint)
  132. {
  133. _pipeliningHint = pipeliningHint;
  134. }
  135. void setMethod(const std::string& method) {
  136. this->method = method;
  137. }
  138. const std::string& getUsername() const
  139. {
  140. return _username;
  141. }
  142. const std::string& getPassword() const
  143. {
  144. return _password;
  145. }
  146. const std::string& getMethod() const {
  147. return method;
  148. }
  149. static const std::string METHOD_GET;
  150. static const std::string METHOD_HEAD;
  151. static const std::string PROTO_HTTP;
  152. static const std::string PROTO_HTTPS;
  153. static const std::string PROTO_FTP;
  154. };
  155. typedef SharedHandle<Request> RequestHandle;
  156. typedef WeakHandle<Request> RequestWeakHandle;
  157. typedef std::deque<RequestHandle> Requests;
  158. } // namespace aria2
  159. #endif // _D_REQUEST_H_