HttpRequest.h 4.9 KB

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