1// Copyright (c) 2012 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
5#ifndef NET_HTTP_HTTP_UTIL_H_
6#define NET_HTTP_HTTP_UTIL_H_
7
8#include <string>
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/strings/string_tokenizer.h"
13#include "net/base/net_export.h"
14#include "net/http/http_byte_range.h"
15#include "net/http/http_version.h"
16#include "url/gurl.h"
17
18// This is a macro to support extending this string literal at compile time.
19// Please excuse me polluting your global namespace!
20#define HTTP_LWS " \t"
21
22namespace net {
23
24class NET_EXPORT HttpUtil {
25 public:
26  // Returns the absolute path of the URL, to be used for the http request.
27  // The absolute path starts with a '/' and may contain a query.
28  static std::string PathForRequest(const GURL& url);
29
30  // Returns the absolute URL, to be used for the http request. This url is
31  // made up of the protocol, host, [port], path, [query]. Everything else
32  // is stripped (username, password, reference).
33  static std::string SpecForRequest(const GURL& url);
34
35  // Locates the next occurance of delimiter in line, skipping over quoted
36  // strings (e.g., commas will not be treated as delimiters if they appear
37  // within a quoted string).  Returns the offset of the found delimiter or
38  // line.size() if no delimiter was found.
39  static size_t FindDelimiter(const std::string& line,
40                              size_t search_start,
41                              char delimiter);
42
43  // Parses the value of a Content-Type header.  The resulting mime_type and
44  // charset values are normalized to lowercase.  The mime_type and charset
45  // output values are only modified if the content_type_str contains a mime
46  // type and charset value, respectively.  The boundary output value is
47  // optional and will be assigned the (quoted) value of the boundary
48  // paramter, if any.
49  static void ParseContentType(const std::string& content_type_str,
50                               std::string* mime_type,
51                               std::string* charset,
52                               bool* had_charset,
53                               std::string* boundary);
54
55  // Scans the headers and look for the first "Range" header in |headers|,
56  // if "Range" exists and the first one of it is well formatted then returns
57  // true, |ranges| will contain a list of valid ranges. If return
58  // value is false then values in |ranges| should not be used. The format of
59  // "Range" header is defined in RFC 7233 Section 2.1.
60  // https://tools.ietf.org/html/rfc7233#section-2.1
61  static bool ParseRanges(const std::string& headers,
62                          std::vector<HttpByteRange>* ranges);
63
64  // Same thing as ParseRanges except the Range header is known and its value
65  // is directly passed in, rather than requiring searching through a string.
66  static bool ParseRangeHeader(const std::string& range_specifier,
67                               std::vector<HttpByteRange>* ranges);
68
69  // Scans the '\r\n'-delimited headers for the given header name.  Returns
70  // true if a match is found.  Input is assumed to be well-formed.
71  // TODO(darin): kill this
72  static bool HasHeader(const std::string& headers, const char* name);
73
74  // Returns true if it is safe to allow users and scripts to specify the header
75  // named |name|.
76  static bool IsSafeHeader(const std::string& name);
77
78  // Returns true if |name| is a valid HTTP header name.
79  static bool IsValidHeaderName(const std::string& name);
80
81  // Returns false if |value| contains NUL or CRLF. This method does not perform
82  // a fully RFC-2616-compliant header value validation.
83  static bool IsValidHeaderValue(const std::string& value);
84
85  // Strips all header lines from |headers| whose name matches
86  // |headers_to_remove|. |headers_to_remove| is a list of null-terminated
87  // lower-case header names, with array length |headers_to_remove_len|.
88  // Returns the stripped header lines list, separated by "\r\n".
89  static std::string StripHeaders(const std::string& headers,
90                                  const char* const headers_to_remove[],
91                                  size_t headers_to_remove_len);
92
93  // Multiple occurances of some headers cannot be coalesced into a comma-
94  // separated list since their values are (or contain) unquoted HTTP-date
95  // values, which may contain a comma (see RFC 2616 section 3.3.1).
96  static bool IsNonCoalescingHeader(std::string::const_iterator name_begin,
97                                    std::string::const_iterator name_end);
98  static bool IsNonCoalescingHeader(const std::string& name) {
99    return IsNonCoalescingHeader(name.begin(), name.end());
100  }
101
102  // Return true if the character is HTTP "linear white space" (SP | HT).
103  // This definition corresponds with the HTTP_LWS macro, and does not match
104  // newlines.
105  static bool IsLWS(char c);
106
107  // Trim HTTP_LWS chars from the beginning and end of the string.
108  static void TrimLWS(std::string::const_iterator* begin,
109                      std::string::const_iterator* end);
110
111  // Whether the character is the start of a quotation mark.
112  static bool IsQuote(char c);
113
114  // Whether the string is a valid |token| as defined in RFC 2616 Sec 2.2.
115  static bool IsToken(std::string::const_iterator begin,
116                      std::string::const_iterator end);
117  static bool IsToken(const std::string& str) {
118    return IsToken(str.begin(), str.end());
119  }
120
121  // RFC 2616 Sec 2.2:
122  // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
123  // Unquote() strips the surrounding quotemarks off a string, and unescapes
124  // any quoted-pair to obtain the value contained by the quoted-string.
125  // If the input is not quoted, then it works like the identity function.
126  static std::string Unquote(std::string::const_iterator begin,
127                             std::string::const_iterator end);
128
129  // Same as above.
130  static std::string Unquote(const std::string& str);
131
132  // The reverse of Unquote() -- escapes and surrounds with "
133  static std::string Quote(const std::string& str);
134
135  // Returns the start of the status line, or -1 if no status line was found.
136  // This allows for 4 bytes of junk to precede the status line (which is what
137  // mozilla does too).
138  static int LocateStartOfStatusLine(const char* buf, int buf_len);
139
140  // Returns index beyond the end-of-headers marker or -1 if not found.  RFC
141  // 2616 defines the end-of-headers marker as a double CRLF; however, some
142  // servers only send back LFs (e.g., Unix-based CGI scripts written using the
143  // ASIS Apache module).  This function therefore accepts the pattern LF[CR]LF
144  // as end-of-headers (just like Mozilla).
145  // The parameter |i| is the offset within |buf| to begin searching from.
146  static int LocateEndOfHeaders(const char* buf, int buf_len, int i = 0);
147
148  // Assemble "raw headers" in the format required by HttpResponseHeaders.
149  // This involves normalizing line terminators, converting [CR]LF to \0 and
150  // handling HTTP line continuations (i.e., lines starting with LWS are
151  // continuations of the previous line).  |buf_len| indicates the position of
152  // the end-of-headers marker as defined by LocateEndOfHeaders.
153  // If a \0 appears within the headers themselves, it will be stripped. This
154  // is a workaround to avoid later code from incorrectly interpreting it as
155  // a line terminator.
156  //
157  // TODO(eroman): we should use \n as the canonical line separator rather than
158  //               \0 to avoid this problem. Unfortunately the persistence layer
159  //               is already dependent on newlines being replaced by NULL so
160  //               this is hard to change without breaking things.
161  static std::string AssembleRawHeaders(const char* buf, int buf_len);
162
163  // Converts assembled "raw headers" back to the HTTP response format. That is
164  // convert each \0 occurence to CRLF. This is used by DevTools.
165  // Since all line continuations info is already lost at this point, the result
166  // consists of status line and then one line for each header.
167  static std::string ConvertHeadersBackToHTTPResponse(const std::string& str);
168
169  // Given a comma separated ordered list of language codes, return
170  // the list with a qvalue appended to each language.
171  // The way qvalues are assigned is rather simple. The qvalue
172  // starts with 1.0 and is decremented by 0.2 for each successive entry
173  // in the list until it reaches 0.2. All the entries after that are
174  // assigned the same qvalue of 0.2. Also, note that the 1st language
175  // will not have a qvalue added because the absence of a qvalue implicitly
176  // means q=1.0.
177  //
178  // When making a http request, this should be used to determine what
179  // to put in Accept-Language header. If a comma separated list of language
180  // codes *without* qvalue is sent, web servers regard all
181  // of them as having q=1.0 and pick one of them even though it may not
182  // be at the beginning of the list (see http://crbug.com/5899).
183  static std::string GenerateAcceptLanguageHeader(
184      const std::string& raw_language_list);
185
186  // Helper. If |*headers| already contains |header_name| do nothing,
187  // otherwise add <header_name> ": " <header_value> to the end of the list.
188  static void AppendHeaderIfMissing(const char* header_name,
189                                    const std::string& header_value,
190                                    std::string* headers);
191
192  // Returns true if the parameters describe a response with a strong etag or
193  // last-modified header.  See section 13.3.3 of RFC 2616.
194  static bool HasStrongValidators(HttpVersion version,
195                                  const std::string& etag_header,
196                                  const std::string& last_modified_header,
197                                  const std::string& date_header);
198
199  // Gets a vector of common HTTP status codes for histograms of status
200  // codes.  Currently returns everything in the range [100, 600), plus 0
201  // (for invalid responses/status codes).
202  static std::vector<int> GetStatusCodesForHistogram();
203
204  // Maps an HTTP status code to one of the status codes in the vector
205  // returned by GetStatusCodesForHistogram.
206  static int MapStatusCodeForHistogram(int code);
207
208  // Used to iterate over the name/value pairs of HTTP headers.  To iterate
209  // over the values in a multi-value header, use ValuesIterator.
210  // See AssembleRawHeaders for joining line continuations (this iterator
211  // does not expect any).
212  class NET_EXPORT HeadersIterator {
213   public:
214    HeadersIterator(std::string::const_iterator headers_begin,
215                    std::string::const_iterator headers_end,
216                    const std::string& line_delimiter);
217    ~HeadersIterator();
218
219    // Advances the iterator to the next header, if any.  Returns true if there
220    // is a next header.  Use name* and values* methods to access the resultant
221    // header name and values.
222    bool GetNext();
223
224    // Iterates through the list of headers, starting with the current position
225    // and looks for the specified header.  Note that the name _must_ be
226    // lower cased.
227    // If the header was found, the return value will be true and the current
228    // position points to the header.  If the return value is false, the
229    // current position will be at the end of the headers.
230    bool AdvanceTo(const char* lowercase_name);
231
232    void Reset() {
233      lines_.Reset();
234    }
235
236    std::string::const_iterator name_begin() const {
237      return name_begin_;
238    }
239    std::string::const_iterator name_end() const {
240      return name_end_;
241    }
242    std::string name() const {
243      return std::string(name_begin_, name_end_);
244    }
245
246    std::string::const_iterator values_begin() const {
247      return values_begin_;
248    }
249    std::string::const_iterator values_end() const {
250      return values_end_;
251    }
252    std::string values() const {
253      return std::string(values_begin_, values_end_);
254    }
255
256   private:
257    base::StringTokenizer lines_;
258    std::string::const_iterator name_begin_;
259    std::string::const_iterator name_end_;
260    std::string::const_iterator values_begin_;
261    std::string::const_iterator values_end_;
262  };
263
264  // Iterates over delimited values in an HTTP header.  HTTP LWS is
265  // automatically trimmed from the resulting values.
266  //
267  // When using this class to iterate over response header values, be aware that
268  // for some headers (e.g., Last-Modified), commas are not used as delimiters.
269  // This iterator should be avoided for headers like that which are considered
270  // non-coalescing (see IsNonCoalescingHeader).
271  //
272  // This iterator is careful to skip over delimiters found inside an HTTP
273  // quoted string.
274  //
275  class NET_EXPORT_PRIVATE ValuesIterator {
276   public:
277    ValuesIterator(std::string::const_iterator values_begin,
278                   std::string::const_iterator values_end,
279                   char delimiter);
280    ~ValuesIterator();
281
282    // Advances the iterator to the next value, if any.  Returns true if there
283    // is a next value.  Use value* methods to access the resultant value.
284    bool GetNext();
285
286    std::string::const_iterator value_begin() const {
287      return value_begin_;
288    }
289    std::string::const_iterator value_end() const {
290      return value_end_;
291    }
292    std::string value() const {
293      return std::string(value_begin_, value_end_);
294    }
295
296   private:
297    base::StringTokenizer values_;
298    std::string::const_iterator value_begin_;
299    std::string::const_iterator value_end_;
300  };
301
302  // Iterates over a delimited sequence of name-value pairs in an HTTP header.
303  // Each pair consists of a token (the name), an equals sign, and either a
304  // token or quoted-string (the value). Arbitrary HTTP LWS is permitted outside
305  // of and between names, values, and delimiters.
306  //
307  // String iterators returned from this class' methods may be invalidated upon
308  // calls to GetNext() or after the NameValuePairsIterator is destroyed.
309  class NET_EXPORT NameValuePairsIterator {
310   public:
311    NameValuePairsIterator(std::string::const_iterator begin,
312                           std::string::const_iterator end,
313                           char delimiter);
314    ~NameValuePairsIterator();
315
316    // Advances the iterator to the next pair, if any.  Returns true if there
317    // is a next pair.  Use name* and value* methods to access the resultant
318    // value.
319    bool GetNext();
320
321    // Returns false if there was a parse error.
322    bool valid() const { return valid_; }
323
324    // The name of the current name-value pair.
325    std::string::const_iterator name_begin() const { return name_begin_; }
326    std::string::const_iterator name_end() const { return name_end_; }
327    std::string name() const { return std::string(name_begin_, name_end_); }
328
329    // The value of the current name-value pair.
330    std::string::const_iterator value_begin() const {
331      return value_is_quoted_ ? unquoted_value_.begin() : value_begin_;
332    }
333    std::string::const_iterator value_end() const {
334      return value_is_quoted_ ? unquoted_value_.end() : value_end_;
335    }
336    std::string value() const {
337      return value_is_quoted_ ? unquoted_value_ : std::string(value_begin_,
338                                                              value_end_);
339    }
340
341    // The value before unquoting (if any).
342    std::string raw_value() const { return std::string(value_begin_,
343                                                       value_end_); }
344
345   private:
346    HttpUtil::ValuesIterator props_;
347    bool valid_;
348
349    std::string::const_iterator name_begin_;
350    std::string::const_iterator name_end_;
351
352    std::string::const_iterator value_begin_;
353    std::string::const_iterator value_end_;
354
355    // Do not store iterators into this string. The NameValuePairsIterator
356    // is copyable/assignable, and if copied the copy's iterators would point
357    // into the original's unquoted_value_ member.
358    std::string unquoted_value_;
359
360    bool value_is_quoted_;
361  };
362};
363
364}  // namespace net
365
366#endif  // NET_HTTP_HTTP_UTIL_H_
367