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