PageRenderTime 174ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/external/chromium/net/http/http_util.h

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C Header | 320 lines | 146 code | 51 blank | 123 comment | 0 complexity | d32d03a18ad3ffffe7eb98a0bf58ec02 MD5 | raw file
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_HTTP_HTTP_UTIL_H_
  5. #define NET_HTTP_HTTP_UTIL_H_
  6. #pragma once
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/string_tokenizer.h"
  11. #include "googleurl/src/gurl.h"
  12. #include "net/http/http_byte_range.h"
  13. // This is a macro to support extending this string literal at compile time.
  14. // Please excuse me polluting your global namespace!
  15. #define HTTP_LWS " \t"
  16. namespace net {
  17. class UploadDataStream;
  18. class HttpUtil {
  19. public:
  20. // Returns the absolute path of the URL, to be used for the http request.
  21. // The absolute path starts with a '/' and may contain a query.
  22. static std::string PathForRequest(const GURL& url);
  23. // Returns the absolute URL, to be used for the http request. This url is
  24. // made up of the protocol, host, [port], path, [query]. Everything else
  25. // is stripped (username, password, reference).
  26. static std::string SpecForRequest(const GURL& url);
  27. // Locates the next occurance of delimiter in line, skipping over quoted
  28. // strings (e.g., commas will not be treated as delimiters if they appear
  29. // within a quoted string). Returns the offset of the found delimiter or
  30. // line.size() if no delimiter was found.
  31. static size_t FindDelimiter(const std::string& line,
  32. size_t search_start,
  33. char delimiter);
  34. // Parses the value of a Content-Type header. The resulting mime_type and
  35. // charset values are normalized to lowercase. The mime_type and charset
  36. // output values are only modified if the content_type_str contains a mime
  37. // type and charset value, respectively.
  38. static void ParseContentType(const std::string& content_type_str,
  39. std::string* mime_type,
  40. std::string* charset,
  41. bool *had_charset);
  42. // Scans the headers and look for the first "Range" header in |headers|,
  43. // if "Range" exists and the first one of it is well formatted then returns
  44. // true, |ranges| will contain a list of valid ranges. If return
  45. // value is false then values in |ranges| should not be used. The format of
  46. // "Range" header is defined in RFC 2616 Section 14.35.1.
  47. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1
  48. static bool ParseRanges(const std::string& headers,
  49. std::vector<HttpByteRange>* ranges);
  50. // Same thing as ParseRanges except the Range header is known and its value
  51. // is directly passed in, rather than requiring searching through a string.
  52. static bool ParseRangeHeader(const std::string& range_specifier,
  53. std::vector<HttpByteRange>* ranges);
  54. // Scans the '\r\n'-delimited headers for the given header name. Returns
  55. // true if a match is found. Input is assumed to be well-formed.
  56. // TODO(darin): kill this
  57. static bool HasHeader(const std::string& headers, const char* name);
  58. // Strips all header lines from |headers| whose name matches
  59. // |headers_to_remove|. |headers_to_remove| is a list of null-terminated
  60. // lower-case header names, with array length |headers_to_remove_len|.
  61. // Returns the stripped header lines list, separated by "\r\n".
  62. static std::string StripHeaders(const std::string& headers,
  63. const char* const headers_to_remove[],
  64. size_t headers_to_remove_len);
  65. // Multiple occurances of some headers cannot be coalesced into a comma-
  66. // separated list since their values are (or contain) unquoted HTTP-date
  67. // values, which may contain a comma (see RFC 2616 section 3.3.1).
  68. static bool IsNonCoalescingHeader(std::string::const_iterator name_begin,
  69. std::string::const_iterator name_end);
  70. static bool IsNonCoalescingHeader(const std::string& name) {
  71. return IsNonCoalescingHeader(name.begin(), name.end());
  72. }
  73. // Return true if the character is HTTP "linear white space" (SP | HT).
  74. // This definition corresponds with the HTTP_LWS macro, and does not match
  75. // newlines.
  76. static bool IsLWS(char c);
  77. // Trim HTTP_LWS chars from the beginning and end of the string.
  78. static void TrimLWS(std::string::const_iterator* begin,
  79. std::string::const_iterator* end);
  80. // Whether the character is the start of a quotation mark.
  81. static bool IsQuote(char c);
  82. // RFC 2616 Sec 2.2:
  83. // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
  84. // Unquote() strips the surrounding quotemarks off a string, and unescapes
  85. // any quoted-pair to obtain the value contained by the quoted-string.
  86. // If the input is not quoted, then it works like the identity function.
  87. static std::string Unquote(std::string::const_iterator begin,
  88. std::string::const_iterator end);
  89. // Same as above.
  90. static std::string Unquote(const std::string& str);
  91. // The reverse of Unquote() -- escapes and surrounds with "
  92. static std::string Quote(const std::string& str);
  93. // Returns the start of the status line, or -1 if no status line was found.
  94. // This allows for 4 bytes of junk to precede the status line (which is what
  95. // mozilla does too).
  96. static int LocateStartOfStatusLine(const char* buf, int buf_len);
  97. // Returns index beyond the end-of-headers marker or -1 if not found. RFC
  98. // 2616 defines the end-of-headers marker as a double CRLF; however, some
  99. // servers only send back LFs (e.g., Unix-based CGI scripts written using the
  100. // ASIS Apache module). This function therefore accepts the pattern LF[CR]LF
  101. // as end-of-headers (just like Mozilla).
  102. // The parameter |i| is the offset within |buf| to begin searching from.
  103. static int LocateEndOfHeaders(const char* buf, int buf_len, int i = 0);
  104. // Assemble "raw headers" in the format required by HttpResponseHeaders.
  105. // This involves normalizing line terminators, converting [CR]LF to \0 and
  106. // handling HTTP line continuations (i.e., lines starting with LWS are
  107. // continuations of the previous line). |buf_len| indicates the position of
  108. // the end-of-headers marker as defined by LocateEndOfHeaders.
  109. static std::string AssembleRawHeaders(const char* buf, int buf_len);
  110. // Given a comma separated ordered list of language codes, return
  111. // the list with a qvalue appended to each language.
  112. // The way qvalues are assigned is rather simple. The qvalue
  113. // starts with 1.0 and is decremented by 0.2 for each successive entry
  114. // in the list until it reaches 0.2. All the entries after that are
  115. // assigned the same qvalue of 0.2. Also, note that the 1st language
  116. // will not have a qvalue added because the absence of a qvalue implicitly
  117. // means q=1.0.
  118. //
  119. // When making a http request, this should be used to determine what
  120. // to put in Accept-Language header. If a comma separated list of language
  121. // codes *without* qvalue is sent, web servers regard all
  122. // of them as having q=1.0 and pick one of them even though it may not
  123. // be at the beginning of the list (see http://crbug.com/5899).
  124. static std::string GenerateAcceptLanguageHeader(
  125. const std::string& raw_language_list);
  126. // Given a charset, return the list with a qvalue. If charset is utf-8,
  127. // it will return 'utf-8,*;q=0.5'. Otherwise (e.g. 'euc-jp'), it'll return
  128. // 'euc-jp,utf-8;q=0.7,*;q=0.3'.
  129. static std::string GenerateAcceptCharsetHeader(const std::string& charset);
  130. // Helper. If |*headers| already contains |header_name| do nothing,
  131. // otherwise add <header_name> ": " <header_value> to the end of the list.
  132. static void AppendHeaderIfMissing(const char* header_name,
  133. const std::string& header_value,
  134. std::string* headers);
  135. // Used to iterate over the name/value pairs of HTTP headers. To iterate
  136. // over the values in a multi-value header, use ValuesIterator.
  137. // See AssembleRawHeaders for joining line continuations (this iterator
  138. // does not expect any).
  139. class HeadersIterator {
  140. public:
  141. HeadersIterator(std::string::const_iterator headers_begin,
  142. std::string::const_iterator headers_end,
  143. const std::string& line_delimiter);
  144. ~HeadersIterator();
  145. // Advances the iterator to the next header, if any. Returns true if there
  146. // is a next header. Use name* and values* methods to access the resultant
  147. // header name and values.
  148. bool GetNext();
  149. // Iterates through the list of headers, starting with the current position
  150. // and looks for the specified header. Note that the name _must_ be
  151. // lower cased.
  152. // If the header was found, the return value will be true and the current
  153. // position points to the header. If the return value is false, the
  154. // current position will be at the end of the headers.
  155. bool AdvanceTo(const char* lowercase_name);
  156. void Reset() {
  157. lines_.Reset();
  158. }
  159. std::string::const_iterator name_begin() const {
  160. return name_begin_;
  161. }
  162. std::string::const_iterator name_end() const {
  163. return name_end_;
  164. }
  165. std::string name() const {
  166. return std::string(name_begin_, name_end_);
  167. }
  168. std::string::const_iterator values_begin() const {
  169. return values_begin_;
  170. }
  171. std::string::const_iterator values_end() const {
  172. return values_end_;
  173. }
  174. std::string values() const {
  175. return std::string(values_begin_, values_end_);
  176. }
  177. private:
  178. StringTokenizer lines_;
  179. std::string::const_iterator name_begin_;
  180. std::string::const_iterator name_end_;
  181. std::string::const_iterator values_begin_;
  182. std::string::const_iterator values_end_;
  183. };
  184. // Iterates over delimited values in an HTTP header. HTTP LWS is
  185. // automatically trimmed from the resulting values.
  186. //
  187. // When using this class to iterate over response header values, be aware that
  188. // for some headers (e.g., Last-Modified), commas are not used as delimiters.
  189. // This iterator should be avoided for headers like that which are considered
  190. // non-coalescing (see IsNonCoalescingHeader).
  191. //
  192. // This iterator is careful to skip over delimiters found inside an HTTP
  193. // quoted string.
  194. //
  195. class ValuesIterator {
  196. public:
  197. ValuesIterator(std::string::const_iterator values_begin,
  198. std::string::const_iterator values_end,
  199. char delimiter);
  200. ~ValuesIterator();
  201. // Advances the iterator to the next value, if any. Returns true if there
  202. // is a next value. Use value* methods to access the resultant value.
  203. bool GetNext();
  204. std::string::const_iterator value_begin() const {
  205. return value_begin_;
  206. }
  207. std::string::const_iterator value_end() const {
  208. return value_end_;
  209. }
  210. std::string value() const {
  211. return std::string(value_begin_, value_end_);
  212. }
  213. private:
  214. StringTokenizer values_;
  215. std::string::const_iterator value_begin_;
  216. std::string::const_iterator value_end_;
  217. };
  218. // Iterates over a delimited sequence of name-value pairs in an HTTP header.
  219. // Each pair consists of a token (the name), an equals sign, and either a
  220. // token or quoted-string (the value). Arbitrary HTTP LWS is permitted outside
  221. // of and between names, values, and delimiters.
  222. //
  223. // String iterators returned from this class' methods may be invalidated upon
  224. // calls to GetNext() or after the NameValuePairsIterator is destroyed.
  225. class NameValuePairsIterator {
  226. public:
  227. NameValuePairsIterator(std::string::const_iterator begin,
  228. std::string::const_iterator end,
  229. char delimiter);
  230. ~NameValuePairsIterator();
  231. // Advances the iterator to the next pair, if any. Returns true if there
  232. // is a next pair. Use name* and value* methods to access the resultant
  233. // value.
  234. bool GetNext();
  235. // Returns false if there was a parse error.
  236. bool valid() const { return valid_; }
  237. // The name of the current name-value pair.
  238. std::string::const_iterator name_begin() const { return name_begin_; }
  239. std::string::const_iterator name_end() const { return name_end_; }
  240. std::string name() const { return std::string(name_begin_, name_end_); }
  241. // The value of the current name-value pair.
  242. std::string::const_iterator value_begin() const {
  243. return value_is_quoted_ ? unquoted_value_.begin() : value_begin_;
  244. }
  245. std::string::const_iterator value_end() const {
  246. return value_is_quoted_ ? unquoted_value_.end() : value_end_;
  247. }
  248. std::string value() const {
  249. return value_is_quoted_ ? unquoted_value_ : std::string(value_begin_,
  250. value_end_);
  251. }
  252. private:
  253. HttpUtil::ValuesIterator props_;
  254. bool valid_;
  255. std::string::const_iterator begin_;
  256. std::string::const_iterator end_;
  257. std::string::const_iterator name_begin_;
  258. std::string::const_iterator name_end_;
  259. std::string::const_iterator value_begin_;
  260. std::string::const_iterator value_end_;
  261. // Do not store iterators into this string. The NameValuePairsIterator
  262. // is copyable/assignable, and if copied the copy's iterators would point
  263. // into the original's unquoted_value_ member.
  264. std::string unquoted_value_;
  265. bool value_is_quoted_;
  266. };
  267. };
  268. } // namespace net
  269. #endif // NET_HTTP_HTTP_UTIL_H_