HttpRequest.h 4.8 KB

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