HttpRequest.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_HTTP_REQUEST_H_
  36. #define _D_HTTP_REQUEST_H_
  37. #include "common.h"
  38. #include <cassert>
  39. #include <string>
  40. #include <vector>
  41. #include "SharedHandle.h"
  42. #include "Request.h"
  43. #include "FileEntry.h"
  44. namespace aria2 {
  45. class Segment;
  46. class Range;
  47. class Option;
  48. class CookieStorage;
  49. class AuthConfigFactory;
  50. class AuthConfig;
  51. class HttpRequest {
  52. private:
  53. static const std::string USER_AGENT;
  54. SharedHandle<Request> request_;
  55. SharedHandle<FileEntry> fileEntry_;
  56. SharedHandle<Segment> segment_;
  57. bool contentEncodingEnabled_;
  58. std::string userAgent_;
  59. std::vector<std::string> headers_;
  60. std::vector<std::string> acceptTypes_;
  61. SharedHandle<CookieStorage> cookieStorage_;
  62. SharedHandle<AuthConfigFactory> authConfigFactory_;
  63. const Option* option_;
  64. SharedHandle<AuthConfig> authConfig_;
  65. SharedHandle<Request> proxyRequest_;
  66. bool noCache_;
  67. bool acceptGzip_;
  68. off_t endOffsetOverride_;
  69. std::string ifModSinceHeader_;
  70. std::pair<std::string, std::string> getProxyAuthString() const;
  71. public:
  72. HttpRequest();
  73. const SharedHandle<Segment>& getSegment() const
  74. {
  75. return segment_;
  76. }
  77. void setSegment(const SharedHandle<Segment>& segment);
  78. void setRequest(const SharedHandle<Request>& request);
  79. uint64_t getEntityLength() const
  80. {
  81. assert(!fileEntry_.isNull());
  82. return fileEntry_->getLength();
  83. }
  84. const std::string& getHost() const
  85. {
  86. return request_->getHost();
  87. }
  88. uint16_t getPort() const
  89. {
  90. return request_->getPort();
  91. }
  92. const std::string& getMethod() const
  93. {
  94. return request_->getMethod();
  95. }
  96. const std::string& getProtocol() const
  97. {
  98. return request_->getProtocol();
  99. }
  100. const std::string& getCurrentURI() const
  101. {
  102. return request_->getCurrentUri();
  103. }
  104. const std::string& getDir() const
  105. {
  106. return request_->getDir();
  107. }
  108. const std::string& getFile() const
  109. {
  110. return request_->getFile();
  111. }
  112. const std::string& getQuery() const
  113. {
  114. return request_->getQuery();
  115. }
  116. const std::string& getPreviousURI() const
  117. {
  118. return request_->getPreviousUri();
  119. }
  120. std::string getURIHost() const
  121. {
  122. return request_->getURIHost();
  123. }
  124. SharedHandle<Range> getRange() const;
  125. /**
  126. * Inspects whether the specified response range is satisfiable
  127. * with request range.
  128. */
  129. bool isRangeSatisfied(const SharedHandle<Range>& range) const;
  130. const SharedHandle<Request>& getRequest() const
  131. {
  132. return request_;
  133. }
  134. off_t getStartByte() const;
  135. off_t getEndByte() const;
  136. /**
  137. * Returns string representation of http request. It usually starts
  138. * with "GET ..." and ends with "\r\n". The AuthConfig for this
  139. * request is resolved using authConfigFactory_ and stored in
  140. * authConfig_. getAuthConfig() returns AuthConfig used in the last
  141. * invocation of createRequest().
  142. */
  143. std::string createRequest();
  144. /**
  145. * Returns string representation of http tunnel request.
  146. * It usually starts with "CONNECT ..." and ends with "\r\n".
  147. */
  148. std::string createProxyRequest() const;
  149. void enableContentEncoding();
  150. void disableContentEncoding();
  151. void setUserAgent(const std::string& userAgent)
  152. {
  153. userAgent_ = userAgent;
  154. }
  155. // accepts multiline headers, delimited by LF
  156. void addHeader(const std::string& headers);
  157. void addAcceptType(const std::string& type);
  158. template<typename InputIterator>
  159. void addAcceptType(InputIterator first, InputIterator last)
  160. {
  161. acceptTypes_.insert(acceptTypes_.end(), first, last);
  162. }
  163. void setCookieStorage(const SharedHandle<CookieStorage>& cookieStorage);
  164. const SharedHandle<CookieStorage>& getCookieStorage() const
  165. {
  166. return cookieStorage_;
  167. }
  168. void setAuthConfigFactory
  169. (const SharedHandle<AuthConfigFactory>& factory, const Option* option);
  170. /*
  171. * To use proxy, pass proxy string to Request::setUri() and set it this
  172. * object.
  173. */
  174. void setProxyRequest(const SharedHandle<Request>& proxyRequest);
  175. /*
  176. * Returns true if non-Null proxy request is set by setProxyRequest().
  177. * Otherwise, returns false.
  178. */
  179. bool isProxyRequestSet() const;
  180. // Returns true if authentication was used in the last
  181. // createRequest().
  182. bool authenticationUsed() const;
  183. // Returns AuthConfig used in the last invocation of
  184. // createRequest().
  185. const SharedHandle<AuthConfig>& getAuthConfig() const;
  186. void setFileEntry(const SharedHandle<FileEntry>& fileEntry)
  187. {
  188. fileEntry_ = fileEntry;
  189. }
  190. const SharedHandle<FileEntry>& getFileEntry() const
  191. {
  192. return fileEntry_;
  193. }
  194. void enableNoCache()
  195. {
  196. noCache_ = true;
  197. }
  198. void disableNoCache()
  199. {
  200. noCache_ = false;
  201. }
  202. void enableAcceptGZip()
  203. {
  204. acceptGzip_ = true;
  205. }
  206. void disableAcceptGZip()
  207. {
  208. acceptGzip_ = false;
  209. }
  210. bool acceptGZip() const
  211. {
  212. return acceptGzip_;
  213. }
  214. void setEndOffsetOverride(off_t offset)
  215. {
  216. endOffsetOverride_ = offset;
  217. }
  218. void setIfModifiedSinceHeader(const std::string& hd)
  219. {
  220. ifModSinceHeader_ = hd;
  221. }
  222. const std::string& getIfModifiedSinceHeader() const
  223. {
  224. return ifModSinceHeader_;
  225. }
  226. };
  227. } // namespace aria2
  228. #endif // _D_HTTP_REQUEST_H_