Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / http / http_util.cc
blobf1e5143e701a5334499dccc9a814ae4c96d2023b
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 // The rules for parsing content-types were borrowed from Firefox:
6 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834
8 #include "net/http/http_util.h"
10 #include <algorithm>
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_tokenizer.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/time/time.h"
22 namespace net {
24 // Helpers --------------------------------------------------------------------
26 // Returns the index of the closing quote of the string, if any. |start| points
27 // at the opening quote.
28 static size_t FindStringEnd(const std::string& line, size_t start, char delim) {
29 DCHECK_LT(start, line.length());
30 DCHECK_EQ(line[start], delim);
31 DCHECK((delim == '"') || (delim == '\''));
33 const char set[] = { delim, '\\', '\0' };
34 for (size_t end = line.find_first_of(set, start + 1);
35 end != std::string::npos; end = line.find_first_of(set, end + 2)) {
36 if (line[end] != '\\')
37 return end;
39 return line.length();
43 // HttpUtil -------------------------------------------------------------------
45 // static
46 void HttpUtil::ParseContentType(const std::string& content_type_str,
47 std::string* mime_type,
48 std::string* charset,
49 bool* had_charset,
50 std::string* boundary) {
51 const std::string::const_iterator begin = content_type_str.begin();
53 // Trim leading and trailing whitespace from type. We include '(' in
54 // the trailing trim set to catch media-type comments, which are not at all
55 // standard, but may occur in rare cases.
56 size_t type_val = content_type_str.find_first_not_of(HTTP_LWS);
57 type_val = std::min(type_val, content_type_str.length());
58 size_t type_end = content_type_str.find_first_of(HTTP_LWS ";(", type_val);
59 if (type_end == std::string::npos)
60 type_end = content_type_str.length();
62 size_t charset_val = 0;
63 size_t charset_end = 0;
64 bool type_has_charset = false;
66 // Iterate over parameters
67 size_t param_start = content_type_str.find_first_of(';', type_end);
68 if (param_start != std::string::npos) {
69 base::StringTokenizer tokenizer(begin + param_start, content_type_str.end(),
70 ";");
71 tokenizer.set_quote_chars("\"");
72 while (tokenizer.GetNext()) {
73 std::string::const_iterator equals_sign =
74 std::find(tokenizer.token_begin(), tokenizer.token_end(), '=');
75 if (equals_sign == tokenizer.token_end())
76 continue;
78 std::string::const_iterator param_name_begin = tokenizer.token_begin();
79 std::string::const_iterator param_name_end = equals_sign;
80 TrimLWS(&param_name_begin, &param_name_end);
82 std::string::const_iterator param_value_begin = equals_sign + 1;
83 std::string::const_iterator param_value_end = tokenizer.token_end();
84 DCHECK(param_value_begin <= tokenizer.token_end());
85 TrimLWS(&param_value_begin, &param_value_end);
87 if (base::LowerCaseEqualsASCII(
88 base::StringPiece(param_name_begin, param_name_end), "charset")) {
89 // TODO(abarth): Refactor this function to consistently use iterators.
90 charset_val = param_value_begin - begin;
91 charset_end = param_value_end - begin;
92 type_has_charset = true;
93 } else if (base::LowerCaseEqualsASCII(
94 base::StringPiece(param_name_begin, param_name_end),
95 "boundary")) {
96 if (boundary)
97 boundary->assign(param_value_begin, param_value_end);
102 if (type_has_charset) {
103 // Trim leading and trailing whitespace from charset_val. We include
104 // '(' in the trailing trim set to catch media-type comments, which are
105 // not at all standard, but may occur in rare cases.
106 charset_val = content_type_str.find_first_not_of(HTTP_LWS, charset_val);
107 charset_val = std::min(charset_val, charset_end);
108 char first_char = content_type_str[charset_val];
109 if (first_char == '"' || first_char == '\'') {
110 charset_end = FindStringEnd(content_type_str, charset_val, first_char);
111 ++charset_val;
112 DCHECK(charset_end >= charset_val);
113 } else {
114 charset_end = std::min(content_type_str.find_first_of(HTTP_LWS ";(",
115 charset_val),
116 charset_end);
120 // if the server sent "*/*", it is meaningless, so do not store it.
121 // also, if type_val is the same as mime_type, then just update the
122 // charset. however, if charset is empty and mime_type hasn't
123 // changed, then don't wipe-out an existing charset. We
124 // also want to reject a mime-type if it does not include a slash.
125 // some servers give junk after the charset parameter, which may
126 // include a comma, so this check makes us a bit more tolerant.
127 if (content_type_str.length() != 0 &&
128 content_type_str != "*/*" &&
129 content_type_str.find_first_of('/') != std::string::npos) {
130 // Common case here is that mime_type is empty
131 bool eq = !mime_type->empty() &&
132 base::LowerCaseEqualsASCII(
133 base::StringPiece(begin + type_val, begin + type_end),
134 mime_type->data());
135 if (!eq) {
136 *mime_type = base::ToLowerASCII(
137 base::StringPiece(begin + type_val, begin + type_end));
139 if ((!eq && *had_charset) || type_has_charset) {
140 *had_charset = true;
141 *charset = base::ToLowerASCII(
142 base::StringPiece(begin + charset_val, begin + charset_end));
147 // static
148 // Parse the Range header according to RFC 2616 14.35.1
149 // ranges-specifier = byte-ranges-specifier
150 // byte-ranges-specifier = bytes-unit "=" byte-range-set
151 // byte-range-set = 1#( byte-range-spec | suffix-byte-range-spec )
152 // byte-range-spec = first-byte-pos "-" [last-byte-pos]
153 // first-byte-pos = 1*DIGIT
154 // last-byte-pos = 1*DIGIT
155 bool HttpUtil::ParseRanges(const std::string& headers,
156 std::vector<HttpByteRange>* ranges) {
157 std::string ranges_specifier;
158 HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n");
160 while (it.GetNext()) {
161 // Look for "Range" header.
162 if (!base::LowerCaseEqualsASCII(it.name(), "range"))
163 continue;
164 ranges_specifier = it.values();
165 // We just care about the first "Range" header, so break here.
166 break;
169 if (ranges_specifier.empty())
170 return false;
172 return ParseRangeHeader(ranges_specifier, ranges);
175 // static
176 bool HttpUtil::ParseRangeHeader(const std::string& ranges_specifier,
177 std::vector<HttpByteRange>* ranges) {
178 size_t equal_char_offset = ranges_specifier.find('=');
179 if (equal_char_offset == std::string::npos)
180 return false;
182 // Try to extract bytes-unit part.
183 std::string::const_iterator bytes_unit_begin = ranges_specifier.begin();
184 std::string::const_iterator bytes_unit_end = bytes_unit_begin +
185 equal_char_offset;
186 std::string::const_iterator byte_range_set_begin = bytes_unit_end + 1;
187 std::string::const_iterator byte_range_set_end = ranges_specifier.end();
189 TrimLWS(&bytes_unit_begin, &bytes_unit_end);
190 // "bytes" unit identifier is not found.
191 if (!base::LowerCaseEqualsASCII(
192 base::StringPiece(bytes_unit_begin, bytes_unit_end), "bytes"))
193 return false;
195 ValuesIterator byte_range_set_iterator(byte_range_set_begin,
196 byte_range_set_end, ',');
197 while (byte_range_set_iterator.GetNext()) {
198 size_t minus_char_offset = byte_range_set_iterator.value().find('-');
199 // If '-' character is not found, reports failure.
200 if (minus_char_offset == std::string::npos)
201 return false;
203 std::string::const_iterator first_byte_pos_begin =
204 byte_range_set_iterator.value_begin();
205 std::string::const_iterator first_byte_pos_end =
206 first_byte_pos_begin + minus_char_offset;
207 TrimLWS(&first_byte_pos_begin, &first_byte_pos_end);
208 std::string first_byte_pos(first_byte_pos_begin, first_byte_pos_end);
210 HttpByteRange range;
211 // Try to obtain first-byte-pos.
212 if (!first_byte_pos.empty()) {
213 int64 first_byte_position = -1;
214 if (!base::StringToInt64(first_byte_pos, &first_byte_position))
215 return false;
216 range.set_first_byte_position(first_byte_position);
219 std::string::const_iterator last_byte_pos_begin =
220 byte_range_set_iterator.value_begin() + minus_char_offset + 1;
221 std::string::const_iterator last_byte_pos_end =
222 byte_range_set_iterator.value_end();
223 TrimLWS(&last_byte_pos_begin, &last_byte_pos_end);
224 std::string last_byte_pos(last_byte_pos_begin, last_byte_pos_end);
226 // We have last-byte-pos or suffix-byte-range-spec in this case.
227 if (!last_byte_pos.empty()) {
228 int64 last_byte_position;
229 if (!base::StringToInt64(last_byte_pos, &last_byte_position))
230 return false;
231 if (range.HasFirstBytePosition())
232 range.set_last_byte_position(last_byte_position);
233 else
234 range.set_suffix_length(last_byte_position);
235 } else if (!range.HasFirstBytePosition()) {
236 return false;
239 // Do a final check on the HttpByteRange object.
240 if (!range.IsValid())
241 return false;
242 ranges->push_back(range);
244 return !ranges->empty();
247 // static
248 bool HttpUtil::ParseRetryAfterHeader(const std::string& retry_after_string,
249 base::Time now,
250 base::TimeDelta* retry_after) {
251 int seconds;
252 base::Time time;
253 base::TimeDelta interval;
255 if (base::StringToInt(retry_after_string, &seconds)) {
256 interval = base::TimeDelta::FromSeconds(seconds);
257 } else if (base::Time::FromUTCString(retry_after_string.c_str(), &time)) {
258 interval = time - now;
259 } else {
260 return false;
263 if (interval < base::TimeDelta::FromSeconds(0))
264 return false;
266 *retry_after = interval;
267 return true;
270 // static
271 bool HttpUtil::HasHeader(const std::string& headers, const char* name) {
272 size_t name_len = strlen(name);
273 std::string::const_iterator it =
274 std::search(headers.begin(),
275 headers.end(),
276 name,
277 name + name_len,
278 base::CaseInsensitiveCompareASCII<char>());
279 if (it == headers.end())
280 return false;
282 // ensure match is prefixed by newline
283 if (it != headers.begin() && it[-1] != '\n')
284 return false;
286 // ensure match is suffixed by colon
287 if (it + name_len >= headers.end() || it[name_len] != ':')
288 return false;
290 return true;
293 namespace {
294 // A header string containing any of the following fields will cause
295 // an error. The list comes from the XMLHttpRequest standard.
296 // http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method
297 const char* const kForbiddenHeaderFields[] = {
298 "accept-charset",
299 "accept-encoding",
300 "access-control-request-headers",
301 "access-control-request-method",
302 "connection",
303 "content-length",
304 "cookie",
305 "cookie2",
306 "content-transfer-encoding",
307 "date",
308 "expect",
309 "host",
310 "keep-alive",
311 "origin",
312 "referer",
313 "te",
314 "trailer",
315 "transfer-encoding",
316 "upgrade",
317 "user-agent",
318 "via",
320 } // anonymous namespace
322 // static
323 bool HttpUtil::IsSafeHeader(const std::string& name) {
324 std::string lower_name(base::ToLowerASCII(name));
325 if (base::StartsWith(lower_name, "proxy-", base::CompareCase::SENSITIVE) ||
326 base::StartsWith(lower_name, "sec-", base::CompareCase::SENSITIVE))
327 return false;
328 for (size_t i = 0; i < arraysize(kForbiddenHeaderFields); ++i) {
329 if (lower_name == kForbiddenHeaderFields[i])
330 return false;
332 return true;
335 // static
336 bool HttpUtil::IsValidHeaderName(const std::string& name) {
337 // Check whether the header name is RFC 2616-compliant.
338 return HttpUtil::IsToken(name);
341 // static
342 bool HttpUtil::IsValidHeaderValue(const std::string& value) {
343 // Just a sanity check: disallow NUL and CRLF.
344 return value.find('\0') == std::string::npos &&
345 value.find("\r\n") == std::string::npos;
348 // static
349 std::string HttpUtil::StripHeaders(const std::string& headers,
350 const char* const headers_to_remove[],
351 size_t headers_to_remove_len) {
352 std::string stripped_headers;
353 HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n");
355 while (it.GetNext()) {
356 bool should_remove = false;
357 for (size_t i = 0; i < headers_to_remove_len; ++i) {
358 if (base::LowerCaseEqualsASCII(
359 base::StringPiece(it.name_begin(), it.name_end()),
360 headers_to_remove[i])) {
361 should_remove = true;
362 break;
365 if (!should_remove) {
366 // Assume that name and values are on the same line.
367 stripped_headers.append(it.name_begin(), it.values_end());
368 stripped_headers.append("\r\n");
371 return stripped_headers;
374 // static
375 bool HttpUtil::IsNonCoalescingHeader(std::string::const_iterator name_begin,
376 std::string::const_iterator name_end) {
377 // NOTE: "set-cookie2" headers do not support expires attributes, so we don't
378 // have to list them here.
379 const char* const kNonCoalescingHeaders[] = {
380 "date",
381 "expires",
382 "last-modified",
383 "location", // See bug 1050541 for details
384 "retry-after",
385 "set-cookie",
386 // The format of auth-challenges mixes both space separated tokens and
387 // comma separated properties, so coalescing on comma won't work.
388 "www-authenticate",
389 "proxy-authenticate",
390 // STS specifies that UAs must not process any STS headers after the first
391 // one.
392 "strict-transport-security"
394 for (size_t i = 0; i < arraysize(kNonCoalescingHeaders); ++i) {
395 if (base::LowerCaseEqualsASCII(base::StringPiece(name_begin, name_end),
396 kNonCoalescingHeaders[i]))
397 return true;
399 return false;
402 bool HttpUtil::IsLWS(char c) {
403 return strchr(HTTP_LWS, c) != NULL;
406 void HttpUtil::TrimLWS(std::string::const_iterator* begin,
407 std::string::const_iterator* end) {
408 // leading whitespace
409 while (*begin < *end && IsLWS((*begin)[0]))
410 ++(*begin);
412 // trailing whitespace
413 while (*begin < *end && IsLWS((*end)[-1]))
414 --(*end);
417 bool HttpUtil::IsQuote(char c) {
418 // Single quote mark isn't actually part of quoted-text production,
419 // but apparently some servers rely on this.
420 return c == '"' || c == '\'';
423 // See RFC 2616 Sec 2.2 for the definition of |token|.
424 bool HttpUtil::IsToken(std::string::const_iterator begin,
425 std::string::const_iterator end) {
426 if (begin == end)
427 return false;
428 for (std::string::const_iterator iter = begin; iter != end; ++iter) {
429 unsigned char c = *iter;
430 if (c >= 0x80 || c <= 0x1F || c == 0x7F ||
431 c == '(' || c == ')' || c == '<' || c == '>' || c == '@' ||
432 c == ',' || c == ';' || c == ':' || c == '\\' || c == '"' ||
433 c == '/' || c == '[' || c == ']' || c == '?' || c == '=' ||
434 c == '{' || c == '}' || c == ' ' || c == '\t')
435 return false;
437 return true;
440 std::string HttpUtil::Unquote(std::string::const_iterator begin,
441 std::string::const_iterator end) {
442 // Empty string
443 if (begin == end)
444 return std::string();
446 // Nothing to unquote.
447 if (!IsQuote(*begin))
448 return std::string(begin, end);
450 // No terminal quote mark.
451 if (end - begin < 2 || *begin != *(end - 1))
452 return std::string(begin, end);
454 // Strip quotemarks
455 ++begin;
456 --end;
458 // Unescape quoted-pair (defined in RFC 2616 section 2.2)
459 std::string unescaped;
460 bool prev_escape = false;
461 for (; begin != end; ++begin) {
462 char c = *begin;
463 if (c == '\\' && !prev_escape) {
464 prev_escape = true;
465 continue;
467 prev_escape = false;
468 unescaped.push_back(c);
470 return unescaped;
473 // static
474 std::string HttpUtil::Unquote(const std::string& str) {
475 return Unquote(str.begin(), str.end());
478 // static
479 std::string HttpUtil::Quote(const std::string& str) {
480 std::string escaped;
481 escaped.reserve(2 + str.size());
483 std::string::const_iterator begin = str.begin();
484 std::string::const_iterator end = str.end();
486 // Esape any backslashes or quotemarks within the string, and
487 // then surround with quotes.
488 escaped.push_back('"');
489 for (; begin != end; ++begin) {
490 char c = *begin;
491 if (c == '"' || c == '\\')
492 escaped.push_back('\\');
493 escaped.push_back(c);
495 escaped.push_back('"');
496 return escaped;
499 // Find the "http" substring in a status line. This allows for
500 // some slop at the start. If the "http" string could not be found
501 // then returns -1.
502 // static
503 int HttpUtil::LocateStartOfStatusLine(const char* buf, int buf_len) {
504 const int slop = 4;
505 const int http_len = 4;
507 if (buf_len >= http_len) {
508 int i_max = std::min(buf_len - http_len, slop);
509 for (int i = 0; i <= i_max; ++i) {
510 if (base::LowerCaseEqualsASCII(base::StringPiece(buf + i, http_len),
511 "http"))
512 return i;
515 return -1; // Not found
518 static int LocateEndOfHeadersHelper(const char* buf,
519 int buf_len,
520 int i,
521 bool accept_empty_header_list) {
522 char last_c = '\0';
523 bool was_lf = false;
524 if (accept_empty_header_list) {
525 // Normally two line breaks signal the end of a header list. An empty header
526 // list ends with a single line break at the start of the buffer.
527 last_c = '\n';
528 was_lf = true;
531 for (; i < buf_len; ++i) {
532 char c = buf[i];
533 if (c == '\n') {
534 if (was_lf)
535 return i + 1;
536 was_lf = true;
537 } else if (c != '\r' || last_c != '\n') {
538 was_lf = false;
540 last_c = c;
542 return -1;
545 int HttpUtil::LocateEndOfAdditionalHeaders(const char* buf,
546 int buf_len,
547 int i) {
548 return LocateEndOfHeadersHelper(buf, buf_len, i, true);
551 int HttpUtil::LocateEndOfHeaders(const char* buf, int buf_len, int i) {
552 return LocateEndOfHeadersHelper(buf, buf_len, i, false);
555 // In order for a line to be continuable, it must specify a
556 // non-blank header-name. Line continuations are specifically for
557 // header values -- do not allow headers names to span lines.
558 static bool IsLineSegmentContinuable(const char* begin, const char* end) {
559 if (begin == end)
560 return false;
562 const char* colon = std::find(begin, end, ':');
563 if (colon == end)
564 return false;
566 const char* name_begin = begin;
567 const char* name_end = colon;
569 // Name can't be empty.
570 if (name_begin == name_end)
571 return false;
573 // Can't start with LWS (this would imply the segment is a continuation)
574 if (HttpUtil::IsLWS(*name_begin))
575 return false;
577 return true;
580 // Helper used by AssembleRawHeaders, to find the end of the status line.
581 static const char* FindStatusLineEnd(const char* begin, const char* end) {
582 size_t i = base::StringPiece(begin, end - begin).find_first_of("\r\n");
583 if (i == base::StringPiece::npos)
584 return end;
585 return begin + i;
588 // Helper used by AssembleRawHeaders, to skip past leading LWS.
589 static const char* FindFirstNonLWS(const char* begin, const char* end) {
590 for (const char* cur = begin; cur != end; ++cur) {
591 if (!HttpUtil::IsLWS(*cur))
592 return cur;
594 return end; // Not found.
597 std::string HttpUtil::AssembleRawHeaders(const char* input_begin,
598 int input_len) {
599 std::string raw_headers;
600 raw_headers.reserve(input_len);
602 const char* input_end = input_begin + input_len;
604 // Skip any leading slop, since the consumers of this output
605 // (HttpResponseHeaders) don't deal with it.
606 int status_begin_offset = LocateStartOfStatusLine(input_begin, input_len);
607 if (status_begin_offset != -1)
608 input_begin += status_begin_offset;
610 // Copy the status line.
611 const char* status_line_end = FindStatusLineEnd(input_begin, input_end);
612 raw_headers.append(input_begin, status_line_end);
614 // After the status line, every subsequent line is a header line segment.
615 // Should a segment start with LWS, it is a continuation of the previous
616 // line's field-value.
618 // TODO(ericroman): is this too permissive? (delimits on [\r\n]+)
619 base::CStringTokenizer lines(status_line_end, input_end, "\r\n");
621 // This variable is true when the previous line was continuable.
622 bool prev_line_continuable = false;
624 while (lines.GetNext()) {
625 const char* line_begin = lines.token_begin();
626 const char* line_end = lines.token_end();
628 if (prev_line_continuable && IsLWS(*line_begin)) {
629 // Join continuation; reduce the leading LWS to a single SP.
630 raw_headers.push_back(' ');
631 raw_headers.append(FindFirstNonLWS(line_begin, line_end), line_end);
632 } else {
633 // Terminate the previous line.
634 raw_headers.push_back('\n');
636 // Copy the raw data to output.
637 raw_headers.append(line_begin, line_end);
639 // Check if the current line can be continued.
640 prev_line_continuable = IsLineSegmentContinuable(line_begin, line_end);
644 raw_headers.append("\n\n", 2);
646 // Use '\0' as the canonical line terminator. If the input already contained
647 // any embeded '\0' characters we will strip them first to avoid interpreting
648 // them as line breaks.
649 raw_headers.erase(std::remove(raw_headers.begin(), raw_headers.end(), '\0'),
650 raw_headers.end());
651 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
653 return raw_headers;
656 std::string HttpUtil::ConvertHeadersBackToHTTPResponse(const std::string& str) {
657 std::string disassembled_headers;
658 base::StringTokenizer tokenizer(str, std::string(1, '\0'));
659 while (tokenizer.GetNext()) {
660 disassembled_headers.append(tokenizer.token_begin(), tokenizer.token_end());
661 disassembled_headers.append("\r\n");
663 disassembled_headers.append("\r\n");
665 return disassembled_headers;
668 // TODO(jungshik): 1. If the list is 'fr-CA,fr-FR,en,de', we have to add
669 // 'fr' after 'fr-CA' with the same q-value as 'fr-CA' because
670 // web servers, in general, do not fall back to 'fr' and may end up picking
671 // 'en' which has a lower preference than 'fr-CA' and 'fr-FR'.
672 // 2. This function assumes that the input is a comma separated list
673 // without any whitespace. As long as it comes from the preference and
674 // a user does not manually edit the preference file, it's the case. Still,
675 // we may have to make it more robust.
676 std::string HttpUtil::GenerateAcceptLanguageHeader(
677 const std::string& raw_language_list) {
678 // We use integers for qvalue and qvalue decrement that are 10 times
679 // larger than actual values to avoid a problem with comparing
680 // two floating point numbers.
681 const unsigned int kQvalueDecrement10 = 2;
682 unsigned int qvalue10 = 10;
683 base::StringTokenizer t(raw_language_list, ",");
684 std::string lang_list_with_q;
685 while (t.GetNext()) {
686 std::string language = t.token();
687 if (qvalue10 == 10) {
688 // q=1.0 is implicit.
689 lang_list_with_q = language;
690 } else {
691 DCHECK_LT(qvalue10, 10U);
692 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(),
693 qvalue10);
695 // It does not make sense to have 'q=0'.
696 if (qvalue10 > kQvalueDecrement10)
697 qvalue10 -= kQvalueDecrement10;
699 return lang_list_with_q;
702 void HttpUtil::AppendHeaderIfMissing(const char* header_name,
703 const std::string& header_value,
704 std::string* headers) {
705 if (header_value.empty())
706 return;
707 if (HttpUtil::HasHeader(*headers, header_name))
708 return;
709 *headers += std::string(header_name) + ": " + header_value + "\r\n";
712 bool HttpUtil::HasStrongValidators(HttpVersion version,
713 const std::string& etag_header,
714 const std::string& last_modified_header,
715 const std::string& date_header) {
716 if (version < HttpVersion(1, 1))
717 return false;
719 if (!etag_header.empty()) {
720 size_t slash = etag_header.find('/');
721 if (slash == std::string::npos || slash == 0)
722 return true;
724 std::string::const_iterator i = etag_header.begin();
725 std::string::const_iterator j = etag_header.begin() + slash;
726 TrimLWS(&i, &j);
727 if (!base::LowerCaseEqualsASCII(base::StringPiece(i, j), "w"))
728 return true;
731 base::Time last_modified;
732 if (!base::Time::FromString(last_modified_header.c_str(), &last_modified))
733 return false;
735 base::Time date;
736 if (!base::Time::FromString(date_header.c_str(), &date))
737 return false;
739 return ((date - last_modified).InSeconds() >= 60);
742 // Functions for histogram initialization. The code 0 is put in the map to
743 // track status codes that are invalid.
744 // TODO(gavinp): Greatly prune the collected codes once we learn which
745 // ones are not sent in practice, to reduce upload size & memory use.
747 enum {
748 HISTOGRAM_MIN_HTTP_STATUS_CODE = 100,
749 HISTOGRAM_MAX_HTTP_STATUS_CODE = 599,
752 // static
753 std::vector<int> HttpUtil::GetStatusCodesForHistogram() {
754 std::vector<int> codes;
755 codes.reserve(
756 HISTOGRAM_MAX_HTTP_STATUS_CODE - HISTOGRAM_MIN_HTTP_STATUS_CODE + 2);
757 codes.push_back(0);
758 for (int i = HISTOGRAM_MIN_HTTP_STATUS_CODE;
759 i <= HISTOGRAM_MAX_HTTP_STATUS_CODE; ++i)
760 codes.push_back(i);
761 return codes;
764 // static
765 int HttpUtil::MapStatusCodeForHistogram(int code) {
766 if (HISTOGRAM_MIN_HTTP_STATUS_CODE <= code &&
767 code <= HISTOGRAM_MAX_HTTP_STATUS_CODE)
768 return code;
769 return 0;
772 // BNF from section 4.2 of RFC 2616:
774 // message-header = field-name ":" [ field-value ]
775 // field-name = token
776 // field-value = *( field-content | LWS )
777 // field-content = <the OCTETs making up the field-value
778 // and consisting of either *TEXT or combinations
779 // of token, separators, and quoted-string>
782 HttpUtil::HeadersIterator::HeadersIterator(
783 std::string::const_iterator headers_begin,
784 std::string::const_iterator headers_end,
785 const std::string& line_delimiter)
786 : lines_(headers_begin, headers_end, line_delimiter) {
789 HttpUtil::HeadersIterator::~HeadersIterator() {
792 bool HttpUtil::HeadersIterator::GetNext() {
793 while (lines_.GetNext()) {
794 name_begin_ = lines_.token_begin();
795 values_end_ = lines_.token_end();
797 std::string::const_iterator colon(std::find(name_begin_, values_end_, ':'));
798 if (colon == values_end_)
799 continue; // skip malformed header
801 name_end_ = colon;
803 // If the name starts with LWS, it is an invalid line.
804 // Leading LWS implies a line continuation, and these should have
805 // already been joined by AssembleRawHeaders().
806 if (name_begin_ == name_end_ || IsLWS(*name_begin_))
807 continue;
809 TrimLWS(&name_begin_, &name_end_);
810 if (name_begin_ == name_end_)
811 continue; // skip malformed header
813 values_begin_ = colon + 1;
814 TrimLWS(&values_begin_, &values_end_);
816 // if we got a header name, then we are done.
817 return true;
819 return false;
822 bool HttpUtil::HeadersIterator::AdvanceTo(const char* name) {
823 DCHECK(name != NULL);
824 DCHECK_EQ(0, base::ToLowerASCII(name).compare(name))
825 << "the header name must be in all lower case";
827 while (GetNext()) {
828 if (base::LowerCaseEqualsASCII(base::StringPiece(name_begin_, name_end_),
829 name)) {
830 return true;
834 return false;
837 HttpUtil::ValuesIterator::ValuesIterator(
838 std::string::const_iterator values_begin,
839 std::string::const_iterator values_end,
840 char delimiter)
841 : values_(values_begin, values_end, std::string(1, delimiter)) {
842 values_.set_quote_chars("\'\"");
845 HttpUtil::ValuesIterator::~ValuesIterator() {
848 bool HttpUtil::ValuesIterator::GetNext() {
849 while (values_.GetNext()) {
850 value_begin_ = values_.token_begin();
851 value_end_ = values_.token_end();
852 TrimLWS(&value_begin_, &value_end_);
854 // bypass empty values.
855 if (value_begin_ != value_end_)
856 return true;
858 return false;
861 HttpUtil::NameValuePairsIterator::NameValuePairsIterator(
862 std::string::const_iterator begin,
863 std::string::const_iterator end,
864 char delimiter,
865 OptionalValues optional_values)
866 : props_(begin, end, delimiter),
867 valid_(true),
868 name_begin_(end),
869 name_end_(end),
870 value_begin_(end),
871 value_end_(end),
872 value_is_quoted_(false),
873 values_optional_(optional_values == VALUES_OPTIONAL) {}
875 HttpUtil::NameValuePairsIterator::NameValuePairsIterator(
876 std::string::const_iterator begin,
877 std::string::const_iterator end,
878 char delimiter)
879 : NameValuePairsIterator(begin, end, delimiter, VALUES_NOT_OPTIONAL) {}
881 HttpUtil::NameValuePairsIterator::~NameValuePairsIterator() {}
883 // We expect properties to be formatted as one of:
884 // name="value"
885 // name='value'
886 // name='\'value\''
887 // name=value
888 // name = value
889 // name (if values_optional_ is true)
890 // Due to buggy implementations found in some embedded devices, we also
891 // accept values with missing close quotemark (http://crbug.com/39836):
892 // name="value
893 bool HttpUtil::NameValuePairsIterator::GetNext() {
894 if (!props_.GetNext())
895 return false;
897 // Set the value as everything. Next we will split out the name.
898 value_begin_ = props_.value_begin();
899 value_end_ = props_.value_end();
900 name_begin_ = name_end_ = value_end_;
902 // Scan for the equals sign.
903 std::string::const_iterator equals = std::find(value_begin_, value_end_, '=');
904 if (equals == value_begin_)
905 return valid_ = false; // Malformed, no name
906 if (equals == value_end_ && !values_optional_)
907 return valid_ = false; // Malformed, no equals sign and values are required
909 // If an equals sign was found, verify that it wasn't inside of quote marks.
910 if (equals != value_end_) {
911 for (std::string::const_iterator it = value_begin_; it != equals; ++it) {
912 if (HttpUtil::IsQuote(*it))
913 return valid_ = false; // Malformed, quote appears before equals sign
917 name_begin_ = value_begin_;
918 name_end_ = equals;
919 value_begin_ = (equals == value_end_) ? value_end_ : equals + 1;
921 TrimLWS(&name_begin_, &name_end_);
922 TrimLWS(&value_begin_, &value_end_);
923 value_is_quoted_ = false;
924 unquoted_value_.clear();
926 if (equals != value_end_ && value_begin_ == value_end_) {
927 // Malformed; value is empty
928 return valid_ = false;
931 if (value_begin_ != value_end_ && HttpUtil::IsQuote(*value_begin_)) {
932 // Trim surrounding quotemarks off the value
933 if (*value_begin_ != *(value_end_ - 1) || value_begin_ + 1 == value_end_) {
934 // NOTE: This is not as graceful as it sounds:
935 // * quoted-pairs will no longer be unquoted
936 // (["\"hello] should give ["hello]).
937 // * Does not detect when the final quote is escaped
938 // (["value\"] should give [value"])
939 ++value_begin_; // Gracefully recover from mismatching quotes.
940 } else {
941 value_is_quoted_ = true;
942 // Do not store iterators into this. See declaration of unquoted_value_.
943 unquoted_value_ = HttpUtil::Unquote(value_begin_, value_end_);
947 return true;
950 } // namespace net