15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_HTTP_HTTP_UTIL_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_HTTP_HTTP_UTIL_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_tokenizer.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_byte_range.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_version.h" 167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is a macro to support extending this string literal at compile time. 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Please excuse me polluting your global namespace! 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define HTTP_LWS " \t" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT HttpUtil { 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the absolute path of the URL, to be used for the http request. 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The absolute path starts with a '/' and may contain a query. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string PathForRequest(const GURL& url); 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the absolute URL, to be used for the http request. This url is 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // made up of the protocol, host, [port], path, [query]. Everything else 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is stripped (username, password, reference). 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string SpecForRequest(const GURL& url); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Locates the next occurance of delimiter in line, skipping over quoted 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // strings (e.g., commas will not be treated as delimiters if they appear 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // within a quoted string). Returns the offset of the found delimiter or 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // line.size() if no delimiter was found. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static size_t FindDelimiter(const std::string& line, 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t search_start, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char delimiter); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Parses the value of a Content-Type header. The resulting mime_type and 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // charset values are normalized to lowercase. The mime_type and charset 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // output values are only modified if the content_type_str contains a mime 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // type and charset value, respectively. The boundary output value is 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // optional and will be assigned the (quoted) value of the boundary 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // paramter, if any. 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void ParseContentType(const std::string& content_type_str, 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* mime_type, 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* charset, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool* had_charset, 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* boundary); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Scans the headers and look for the first "Range" header in |headers|, 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // if "Range" exists and the first one of it is well formatted then returns 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // true, |ranges| will contain a list of valid ranges. If return 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // value is false then values in |ranges| should not be used. The format of 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // "Range" header is defined in RFC 2616 Section 14.35.1. 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool ParseRanges(const std::string& headers, 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<HttpByteRange>* ranges); 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Same thing as ParseRanges except the Range header is known and its value 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is directly passed in, rather than requiring searching through a string. 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool ParseRangeHeader(const std::string& range_specifier, 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<HttpByteRange>* ranges); 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Scans the '\r\n'-delimited headers for the given header name. Returns 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // true if a match is found. Input is assumed to be well-formed. 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(darin): kill this 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool HasHeader(const std::string& headers, const char* name); 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if it is safe to allow users and scripts to specify the header 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // named |name|. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsSafeHeader(const std::string& name); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Strips all header lines from |headers| whose name matches 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |headers_to_remove|. |headers_to_remove| is a list of null-terminated 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // lower-case header names, with array length |headers_to_remove_len|. 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the stripped header lines list, separated by "\r\n". 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string StripHeaders(const std::string& headers, 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* const headers_to_remove[], 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t headers_to_remove_len); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Multiple occurances of some headers cannot be coalesced into a comma- 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // separated list since their values are (or contain) unquoted HTTP-date 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // values, which may contain a comma (see RFC 2616 section 3.3.1). 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsNonCoalescingHeader(std::string::const_iterator name_begin, 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_end); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsNonCoalescingHeader(const std::string& name) { 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return IsNonCoalescingHeader(name.begin(), name.end()); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Return true if the character is HTTP "linear white space" (SP | HT). 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This definition corresponds with the HTTP_LWS macro, and does not match 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // newlines. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsLWS(char c); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Trim HTTP_LWS chars from the beginning and end of the string. 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void TrimLWS(std::string::const_iterator* begin, 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator* end); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Whether the character is the start of a quotation mark. 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsQuote(char c); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Whether the string is a valid |token| as defined in RFC 2616 Sec 2.2. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsToken(std::string::const_iterator begin, 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator end); 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsToken(const std::string& str) { 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return IsToken(str.begin(), str.end()); 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // RFC 2616 Sec 2.2: 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Unquote() strips the surrounding quotemarks off a string, and unescapes 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // any quoted-pair to obtain the value contained by the quoted-string. 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the input is not quoted, then it works like the identity function. 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string Unquote(std::string::const_iterator begin, 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator end); 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Same as above. 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string Unquote(const std::string& str); 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The reverse of Unquote() -- escapes and surrounds with " 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string Quote(const std::string& str); 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the start of the status line, or -1 if no status line was found. 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This allows for 4 bytes of junk to precede the status line (which is what 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // mozilla does too). 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static int LocateStartOfStatusLine(const char* buf, int buf_len); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns index beyond the end-of-headers marker or -1 if not found. RFC 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 2616 defines the end-of-headers marker as a double CRLF; however, some 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // servers only send back LFs (e.g., Unix-based CGI scripts written using the 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ASIS Apache module). This function therefore accepts the pattern LF[CR]LF 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // as end-of-headers (just like Mozilla). 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The parameter |i| is the offset within |buf| to begin searching from. 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static int LocateEndOfHeaders(const char* buf, int buf_len, int i = 0); 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Assemble "raw headers" in the format required by HttpResponseHeaders. 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This involves normalizing line terminators, converting [CR]LF to \0 and 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // handling HTTP line continuations (i.e., lines starting with LWS are 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // continuations of the previous line). |buf_len| indicates the position of 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the end-of-headers marker as defined by LocateEndOfHeaders. 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If a \0 appears within the headers themselves, it will be stripped. This 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is a workaround to avoid later code from incorrectly interpreting it as 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a line terminator. 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(eroman): we should use \n as the canonical line separator rather than 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // \0 to avoid this problem. Unfortunately the persistence layer 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is already dependent on newlines being replaced by NULL so 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this is hard to change without breaking things. 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string AssembleRawHeaders(const char* buf, int buf_len); 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Converts assembled "raw headers" back to the HTTP response format. That is 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // convert each \0 occurence to CRLF. This is used by DevTools. 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Since all line continuations info is already lost at this point, the result 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // consists of status line and then one line for each header. 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string ConvertHeadersBackToHTTPResponse(const std::string& str); 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Given a comma separated ordered list of language codes, return 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the list with a qvalue appended to each language. 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The way qvalues are assigned is rather simple. The qvalue 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // starts with 1.0 and is decremented by 0.2 for each successive entry 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // in the list until it reaches 0.2. All the entries after that are 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // assigned the same qvalue of 0.2. Also, note that the 1st language 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // will not have a qvalue added because the absence of a qvalue implicitly 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // means q=1.0. 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // When making a http request, this should be used to determine what 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to put in Accept-Language header. If a comma separated list of language 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // codes *without* qvalue is sent, web servers regard all 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of them as having q=1.0 and pick one of them even though it may not 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // be at the beginning of the list (see http://crbug.com/5899). 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string GenerateAcceptLanguageHeader( 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& raw_language_list); 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Helper. If |*headers| already contains |header_name| do nothing, 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // otherwise add <header_name> ": " <header_value> to the end of the list. 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void AppendHeaderIfMissing(const char* header_name, 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& header_value, 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* headers); 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the parameters describe a response with a strong etag or 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // last-modified header. See section 13.3.3 of RFC 2616. 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool HasStrongValidators(HttpVersion version, 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& etag_header, 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& last_modified_header, 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& date_header); 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Gets a vector of common HTTP status codes for histograms of status 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // codes. Currently returns everything in the range [100, 600), plus 0 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (for invalid responses/status codes). 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::vector<int> GetStatusCodesForHistogram(); 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Maps an HTTP status code to one of the status codes in the vector 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // returned by GetStatusCodesForHistogram. 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static int MapStatusCodeForHistogram(int code); 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Used to iterate over the name/value pairs of HTTP headers. To iterate 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // over the values in a multi-value header, use ValuesIterator. 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // See AssembleRawHeaders for joining line continuations (this iterator 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // does not expect any). 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class NET_EXPORT HeadersIterator { 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HeadersIterator(std::string::const_iterator headers_begin, 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator headers_end, 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& line_delimiter); 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~HeadersIterator(); 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Advances the iterator to the next header, if any. Returns true if there 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is a next header. Use name* and values* methods to access the resultant 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // header name and values. 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool GetNext(); 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Iterates through the list of headers, starting with the current position 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and looks for the specified header. Note that the name _must_ be 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // lower cased. 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the header was found, the return value will be true and the current 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // position points to the header. If the return value is false, the 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // current position will be at the end of the headers. 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool AdvanceTo(const char* lowercase_name); 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Reset() { 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) lines_.Reset(); 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_begin() const { 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return name_begin_; 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_end() const { 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return name_end_; 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string name() const { 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string(name_begin_, name_end_); 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator values_begin() const { 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return values_begin_; 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator values_end() const { 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return values_end_; 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string values() const { 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string(values_begin_, values_end_); 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::StringTokenizer lines_; 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_begin_; 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_end_; 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator values_begin_; 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator values_end_; 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Iterates over delimited values in an HTTP header. HTTP LWS is 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // automatically trimmed from the resulting values. 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // When using this class to iterate over response header values, be aware that 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // for some headers (e.g., Last-Modified), commas are not used as delimiters. 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This iterator should be avoided for headers like that which are considered 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // non-coalescing (see IsNonCoalescingHeader). 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This iterator is careful to skip over delimiters found inside an HTTP 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // quoted string. 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class NET_EXPORT_PRIVATE ValuesIterator { 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ValuesIterator(std::string::const_iterator values_begin, 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator values_end, 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char delimiter); 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~ValuesIterator(); 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Advances the iterator to the next value, if any. Returns true if there 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is a next value. Use value* methods to access the resultant value. 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool GetNext(); 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_begin() const { 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return value_begin_; 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_end() const { 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return value_end_; 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string value() const { 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string(value_begin_, value_end_); 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::StringTokenizer values_; 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_begin_; 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_end_; 2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Iterates over a delimited sequence of name-value pairs in an HTTP header. 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Each pair consists of a token (the name), an equals sign, and either a 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // token or quoted-string (the value). Arbitrary HTTP LWS is permitted outside 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of and between names, values, and delimiters. 2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // String iterators returned from this class' methods may be invalidated upon 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // calls to GetNext() or after the NameValuePairsIterator is destroyed. 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class NET_EXPORT NameValuePairsIterator { 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NameValuePairsIterator(std::string::const_iterator begin, 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator end, 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char delimiter); 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~NameValuePairsIterator(); 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Advances the iterator to the next pair, if any. Returns true if there 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is a next pair. Use name* and value* methods to access the resultant 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // value. 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool GetNext(); 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns false if there was a parse error. 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool valid() const { return valid_; } 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The name of the current name-value pair. 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_begin() const { return name_begin_; } 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_end() const { return name_end_; } 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string name() const { return std::string(name_begin_, name_end_); } 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The value of the current name-value pair. 3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_begin() const { 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return value_is_quoted_ ? unquoted_value_.begin() : value_begin_; 3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_end() const { 3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return value_is_quoted_ ? unquoted_value_.end() : value_end_; 3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string value() const { 3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return value_is_quoted_ ? unquoted_value_ : std::string(value_begin_, 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) value_end_); 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The value before unquoting (if any). 3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string raw_value() const { return std::string(value_begin_, 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) value_end_); } 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HttpUtil::ValuesIterator props_; 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool valid_; 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_begin_; 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator name_end_; 3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_begin_; 3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string::const_iterator value_end_; 3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Do not store iterators into this string. The NameValuePairsIterator 3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is copyable/assignable, and if copied the copy's iterators would point 3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // into the original's unquoted_value_ member. 3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string unquoted_value_; 3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool value_is_quoted_; 3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace net 3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // NET_HTTP_HTTP_UTIL_H_ 360