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.
5 #ifndef NET_HTTP_HTTP_UTIL_H_
6 #define NET_HTTP_HTTP_UTIL_H_
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"
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"
24 class NET_EXPORT HttpUtil
{
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
);
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
);
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
,
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
49 static void ParseContentType(const std::string
& content_type_str
,
50 std::string
* mime_type
,
53 std::string
* boundary
);
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
);
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
);
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
);
74 // Returns true if it is safe to allow users and scripts to specify the header
76 static bool IsSafeHeader(const std::string
& name
);
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
);
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());
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
98 static bool IsLWS(char c
);
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
);
104 // Whether the character is the start of a quotation mark.
105 static bool IsQuote(char c
);
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());
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
);
123 static std::string
Unquote(const std::string
& str
);
125 // The reverse of Unquote() -- escapes and surrounds with "
126 static std::string
Quote(const std::string
& str
);
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
);
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);
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.
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
);
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
);
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
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
);
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
);
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
);
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();
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
);
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
{
207 HeadersIterator(std::string::const_iterator headers_begin
,
208 std::string::const_iterator headers_end
,
209 const std::string
& line_delimiter
);
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.
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
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
);
229 std::string::const_iterator
name_begin() const {
232 std::string::const_iterator
name_end() const {
235 std::string
name() const {
236 return std::string(name_begin_
, name_end_
);
239 std::string::const_iterator
values_begin() const {
240 return values_begin_
;
242 std::string::const_iterator
values_end() const {
245 std::string
values() const {
246 return std::string(values_begin_
, values_end_
);
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_
;
257 // Iterates over delimited values in an HTTP header. HTTP LWS is
258 // automatically trimmed from the resulting values.
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).
265 // This iterator is careful to skip over delimiters found inside an HTTP
268 class NET_EXPORT_PRIVATE ValuesIterator
{
270 ValuesIterator(std::string::const_iterator values_begin
,
271 std::string::const_iterator values_end
,
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.
279 std::string::const_iterator
value_begin() const {
282 std::string::const_iterator
value_end() const {
285 std::string
value() const {
286 return std::string(value_begin_
, value_end_
);
290 base::StringTokenizer values_
;
291 std::string::const_iterator value_begin_
;
292 std::string::const_iterator value_end_
;
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.
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
{
304 NameValuePairsIterator(std::string::const_iterator begin
,
305 std::string::const_iterator end
,
307 ~NameValuePairsIterator();
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
314 // Returns false if there was a parse error.
315 bool valid() const { return valid_
; }
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_
); }
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_
;
326 std::string::const_iterator
value_end() const {
327 return value_is_quoted_
? unquoted_value_
.end() : value_end_
;
329 std::string
value() const {
330 return value_is_quoted_
? unquoted_value_
: std::string(value_begin_
,
334 // The value before unquoting (if any).
335 std::string
raw_value() const { return std::string(value_begin_
,
339 HttpUtil::ValuesIterator props_
;
342 std::string::const_iterator name_begin_
;
343 std::string::const_iterator name_end_
;
345 std::string::const_iterator value_begin_
;
346 std::string::const_iterator value_end_
;
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_
;
353 bool value_is_quoted_
;
359 #endif // NET_HTTP_HTTP_UTIL_H_