HttpRequest.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <memory>
  42. #include "FileEntry.h"
  43. namespace aria2 {
  44. class Request;
  45. class Segment;
  46. struct 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. std::shared_ptr<Request> request_;
  55. std::shared_ptr<FileEntry> fileEntry_;
  56. std::shared_ptr<Segment> segment_;
  57. bool contentEncodingEnabled_;
  58. std::string userAgent_;
  59. std::vector<std::string> headers_;
  60. // If true, metalink content types are sent in Accept header field.
  61. bool acceptMetalink_;
  62. CookieStorage* cookieStorage_;
  63. AuthConfigFactory* authConfigFactory_;
  64. const Option* option_;
  65. std::shared_ptr<AuthConfig> authConfig_;
  66. std::shared_ptr<Request> proxyRequest_;
  67. bool noCache_;
  68. bool acceptGzip_;
  69. // Historically, aria2 did not specify end byte marker unless http
  70. // pipelining is enabled. Sometimes end byte is known because the
  71. // segment/piece ahead of this request was already acquired. In this
  72. // case, specifying end byte enables to reuse connection. To achieve
  73. // this, if endOffsetOverride_ is more than 0, its value - 1 is used
  74. // as an end byte. Please note that FTP protocol cannot specify end
  75. // bytes and it is also true if it is used via HTTP proxy.
  76. int64_t endOffsetOverride_;
  77. std::string ifModSinceHeader_;
  78. std::pair<std::string, std::string> getProxyAuthString() const;
  79. public:
  80. HttpRequest();
  81. ~HttpRequest();
  82. const std::shared_ptr<Segment>& getSegment() const
  83. {
  84. return segment_;
  85. }
  86. void setSegment(const std::shared_ptr<Segment>& segment);
  87. void setRequest(const std::shared_ptr<Request>& request);
  88. int64_t getEntityLength() const;
  89. const std::string& getHost() const;
  90. uint16_t getPort() const;
  91. const std::string& getMethod() const;
  92. const std::string& getProtocol() const;
  93. const std::string& getCurrentURI() const;
  94. const std::string& getDir() const;
  95. const std::string& getFile() const;
  96. const std::string& getQuery() const;
  97. const std::string& getPreviousURI() const;
  98. std::string getURIHost() const;
  99. Range getRange() const;
  100. /**
  101. * Inspects whether the specified response range is satisfiable
  102. * with request range.
  103. */
  104. bool isRangeSatisfied(const Range& range) const;
  105. const std::shared_ptr<Request>& getRequest() const
  106. {
  107. return request_;
  108. }
  109. int64_t getStartByte() const;
  110. int64_t getEndByte() const;
  111. /**
  112. * Returns string representation of http request. It usually starts
  113. * with "GET ..." and ends with "\r\n". The AuthConfig for this
  114. * request is resolved using authConfigFactory_ and stored in
  115. * authConfig_. getAuthConfig() returns AuthConfig used in the last
  116. * invocation of createRequest().
  117. */
  118. std::string createRequest();
  119. /**
  120. * Returns string representation of http tunnel request.
  121. * It usually starts with "CONNECT ..." and ends with "\r\n".
  122. */
  123. std::string createProxyRequest() const;
  124. void enableContentEncoding();
  125. void disableContentEncoding();
  126. void setUserAgent(const std::string& userAgent);
  127. // accepts multiline headers, delimited by LF
  128. void addHeader(const std::string& headers);
  129. void clearHeader();
  130. void addAcceptType(const std::string& type);
  131. void setAcceptMetalink(bool f)
  132. {
  133. acceptMetalink_ = f;
  134. }
  135. void setCookieStorage(CookieStorage* cookieStorage);
  136. CookieStorage* getCookieStorage() const;
  137. void setAuthConfigFactory(AuthConfigFactory* factory);
  138. void setOption(const Option* option);
  139. /*
  140. * To use proxy, pass proxy string to Request::setUri() and set it this
  141. * object.
  142. */
  143. void setProxyRequest(const std::shared_ptr<Request>& proxyRequest);
  144. /*
  145. * Returns true if non-Null proxy request is set by setProxyRequest().
  146. * Otherwise, returns false.
  147. */
  148. bool isProxyRequestSet() const;
  149. // Returns true if authentication was used in the last
  150. // createRequest().
  151. bool authenticationUsed() const;
  152. // Returns AuthConfig used in the last invocation of
  153. // createRequest().
  154. const std::shared_ptr<AuthConfig>& getAuthConfig() const;
  155. void setFileEntry(const std::shared_ptr<FileEntry>& fileEntry);
  156. const std::shared_ptr<FileEntry>& getFileEntry() const
  157. {
  158. return fileEntry_;
  159. }
  160. void enableNoCache()
  161. {
  162. noCache_ = true;
  163. }
  164. void disableNoCache()
  165. {
  166. noCache_ = false;
  167. }
  168. void enableAcceptGZip()
  169. {
  170. acceptGzip_ = true;
  171. }
  172. void disableAcceptGZip()
  173. {
  174. acceptGzip_ = false;
  175. }
  176. bool acceptGZip() const
  177. {
  178. return acceptGzip_;
  179. }
  180. void setEndOffsetOverride(int64_t offset)
  181. {
  182. endOffsetOverride_ = offset;
  183. }
  184. void setIfModifiedSinceHeader(const std::string& hd);
  185. const std::string& getIfModifiedSinceHeader() const
  186. {
  187. return ifModSinceHeader_;
  188. }
  189. // Returns true if request is conditional:more specifically, the
  190. // request is considered to be conditional if the client sent
  191. // "If-Modified-Since" or "If-None-Match" request-header field.
  192. bool conditionalRequest() const;
  193. };
  194. } // namespace aria2
  195. #endif // D_HTTP_REQUEST_H