HttpRequest.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <string>
  39. #include <deque>
  40. #include "SharedHandle.h"
  41. namespace aria2 {
  42. class Request;
  43. class Segment;
  44. class Range;
  45. class Option;
  46. class CookieStorage;
  47. class AuthConfigFactory;
  48. class HttpRequest {
  49. private:
  50. static const std::string USER_AGENT;
  51. SharedHandle<Request> request;
  52. SharedHandle<Segment> segment;
  53. uint64_t entityLength;
  54. bool _contentEncodingEnabled;
  55. std::string userAgent;
  56. std::deque<std::string> _headers;
  57. std::deque<std::string> _acceptTypes;
  58. SharedHandle<CookieStorage> _cookieStorage;
  59. SharedHandle<AuthConfigFactory> _authConfigFactory;
  60. SharedHandle<Request> _proxyRequest;
  61. std::string getHostText(const std::string& host, uint16_t port) const;
  62. std::string getProxyAuthString() const;
  63. public:
  64. HttpRequest();
  65. SharedHandle<Segment> getSegment() const;
  66. void setSegment(const SharedHandle<Segment>& segment);
  67. void setRequest(const SharedHandle<Request>& request);
  68. /**
  69. * entityLength is used in isRangeSatisfied() method.
  70. */
  71. void setEntityLength(uint64_t entityLength)
  72. {
  73. this->entityLength = entityLength;
  74. }
  75. uint64_t getEntityLength() const
  76. {
  77. return entityLength;
  78. }
  79. const std::string& getHost() const;
  80. uint16_t getPort() const;
  81. const std::string& getMethod() const;
  82. const std::string& getProtocol() const;
  83. const std::string& getCurrentURI() const;
  84. const std::string& getDir() const;
  85. const std::string& getFile() const;
  86. const std::string& getQuery() const;
  87. const std::string& getPreviousURI() const;
  88. SharedHandle<Range> getRange() const;
  89. /**
  90. * Inspects whether the specified response range is satisfiable
  91. * with request range.
  92. */
  93. bool isRangeSatisfied(const SharedHandle<Range>& range) const;
  94. SharedHandle<Request> getRequest() const;
  95. off_t getStartByte() const;
  96. off_t getEndByte() const;
  97. /**
  98. * Returns string representation of http request.
  99. * It usually starts with "GET ..." and ends with "\r\n".
  100. */
  101. std::string createRequest() const;
  102. /**
  103. * Returns string representation of http tunnel request.
  104. * It usually starts with "CONNECT ..." and ends with "\r\n".
  105. */
  106. std::string createProxyRequest() const;
  107. void enableContentEncoding();
  108. void disableContentEncoding();
  109. void setUserAgent(const std::string& userAgent)
  110. {
  111. this->userAgent = userAgent;
  112. }
  113. // accepts multiline headers, deliminated by LF
  114. void addHeader(const std::string& headers);
  115. void addAcceptType(const std::string& type);
  116. template<typename InputIterator>
  117. void addAcceptType(InputIterator first, InputIterator last)
  118. {
  119. _acceptTypes.insert(_acceptTypes.end(), first, last);
  120. }
  121. void setCookieStorage(const SharedHandle<CookieStorage>& cookieStorage);
  122. SharedHandle<CookieStorage> getCookieStorage() const;
  123. void setAuthConfigFactory(const SharedHandle<AuthConfigFactory>& factory);
  124. /*
  125. * To use proxy, pass proxy string to Request::setUrl() and set it this
  126. * object.
  127. */
  128. void setProxyRequest(const SharedHandle<Request>& proxyRequest);
  129. /*
  130. * Returns true if non-Null proxy request is set by setProxyRequest().
  131. * Otherwise, returns false.
  132. */
  133. bool isProxyRequestSet() const;
  134. };
  135. typedef SharedHandle<HttpRequest> HttpRequestHandle;
  136. typedef std::deque<HttpRequestHandle> HttpRequests;
  137. } // namespace aria2
  138. #endif // _D_HTTP_REQUEST_H_