HttpRequest.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "Segment.h"
  39. #include "Range.h"
  40. #include "Request.h"
  41. #include "Option.h"
  42. class HttpRequest {
  43. private:
  44. RequestHandle request;
  45. SegmentHandle segment;
  46. int64_t entityLength;
  47. bool authEnabled;
  48. bool proxyEnabled;
  49. bool proxyAuthEnabled;
  50. string userAgent;
  51. string getHostText(const string& host, int32_t port) const;
  52. string getProxyAuthString() const;
  53. public:
  54. HttpRequest():request(0),
  55. segment(0),
  56. entityLength(0),
  57. authEnabled(false),
  58. proxyEnabled(false),
  59. proxyAuthEnabled(false),
  60. userAgent(USER_AGENT)
  61. {}
  62. SegmentHandle getSegment() const
  63. {
  64. return segment;
  65. }
  66. void setSegment(const SegmentHandle& segment)
  67. {
  68. this->segment = segment;
  69. }
  70. void setRequest(const RequestHandle& request)
  71. {
  72. this->request = request;
  73. }
  74. /**
  75. * entityLength is used in isRangeSatisfied() method.
  76. */
  77. void setEntityLength(int64_t entityLength)
  78. {
  79. this->entityLength = entityLength;
  80. }
  81. int64_t getEntityLength() const
  82. {
  83. return entityLength;
  84. }
  85. string getHost() const
  86. {
  87. return request->getHost();
  88. }
  89. int32_t getPort() const
  90. {
  91. return request->getPort();
  92. }
  93. string getMethod() const
  94. {
  95. return request->getMethod();
  96. }
  97. string getProtocol() const
  98. {
  99. return request->getProtocol();
  100. }
  101. string getCurrentURI() const
  102. {
  103. return request->getCurrentUrl();
  104. }
  105. string getDir() const
  106. {
  107. return request->getDir();
  108. }
  109. string getFile() const
  110. {
  111. return request->getFile();
  112. }
  113. string getPreviousURI() const
  114. {
  115. return request->getPreviousUrl();
  116. }
  117. RangeHandle getRange() const;
  118. /**
  119. * Inspects whether the specified response range is satisfiable
  120. * with request range.
  121. */
  122. bool isRangeSatisfied(const RangeHandle& range) const;
  123. RequestHandle getRequest() const
  124. {
  125. return request;
  126. }
  127. int64_t getStartByte() const
  128. {
  129. if(segment.isNull()) {
  130. return 0;
  131. } else {
  132. return segment->getPositionToWrite();
  133. }
  134. }
  135. int64_t getEndByte() const
  136. {
  137. if(segment.isNull() || request.isNull()) {
  138. return 0;
  139. } else {
  140. if(request->isKeepAlive()) {
  141. return segment->getPosition()+segment->getLength()-1;
  142. } else {
  143. return 0;
  144. }
  145. }
  146. }
  147. /**
  148. * Returns string representation of http request.
  149. * It usually starts with "GET ..." and ends with "\r\n".
  150. */
  151. string createRequest() const;
  152. /**
  153. * Returns string representation of http tunnel request.
  154. * It usually starts with "CONNECT ..." and ends with "\r\n".
  155. */
  156. string createProxyRequest() const;
  157. /**
  158. * Configures this object with option.
  159. * Following values are evaluated:
  160. * PREF_HTTP_AUTH_ENABLED, PREF_HTTP_PROXY_ENABLED,
  161. * PREF_HTTP_PROXY_METHOD, PREF_HTTP_PROXY_AUTH_ENABLED,
  162. * PREF_HTTP_USER, PREF_HTTP_PASSWD,
  163. * PREF_HTTP_PROXY_USER, PREF_HTTP_PROXY_PASSWD
  164. * The evaluation results are stored in instance variables.
  165. */
  166. void configure(const Option* option);
  167. void setProxyEnabled(bool proxyEnabled)
  168. {
  169. this->proxyEnabled = proxyEnabled;
  170. }
  171. void setProxyAuthEnabled(bool proxyAuthEnabled)
  172. {
  173. this->proxyAuthEnabled = proxyAuthEnabled;
  174. }
  175. void setAuthEnabled(bool authEnabled)
  176. {
  177. this->authEnabled = authEnabled;
  178. }
  179. void setUserAgent(const string& userAgent)
  180. {
  181. this->userAgent = userAgent;
  182. }
  183. };
  184. typedef SharedHandle<HttpRequest> HttpRequestHandle;
  185. typedef deque<HttpRequestHandle> HttpRequests;
  186. #endif // _D_HTTP_REQUEST_H_