Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2006-2009 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_RESPONSE_HEADERS_H_
      6 #define NET_HTTP_HTTP_RESPONSE_HEADERS_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/hash_tables.h"
     13 #include "base/ref_counted.h"
     14 #include "net/http/http_version.h"
     15 
     16 class Pickle;
     17 
     18 namespace base {
     19 class Time;
     20 class TimeDelta;
     21 }
     22 
     23 namespace net {
     24 
     25 // HttpResponseHeaders: parses and holds HTTP response headers.
     26 class HttpResponseHeaders
     27     : public base::RefCountedThreadSafe<HttpResponseHeaders> {
     28  public:
     29   // Parses the given raw_headers.  raw_headers should be formatted thus:
     30   // includes the http status response line, each line is \0-terminated, and
     31   // it's terminated by an empty line (ie, 2 \0s in a row).
     32   // (Note that line continuations should have already been joined;
     33   // see HttpUtil::AssembleRawHeaders)
     34   //
     35   // NOTE: For now, raw_headers is not really 'raw' in that this constructor is
     36   // called with a 'NativeMB' string on Windows because WinHTTP does not allow
     37   // us to access the raw byte sequence as sent by a web server.  In any case,
     38   // HttpResponseHeaders does not perform any encoding changes on the input.
     39   //
     40   explicit HttpResponseHeaders(const std::string& raw_headers);
     41 
     42   // Initializes from the representation stored in the given pickle.  The data
     43   // for this object is found relative to the given pickle_iter, which should
     44   // be passed to the pickle's various Read* methods.
     45   HttpResponseHeaders(const Pickle& pickle, void** pickle_iter);
     46 
     47   // Persist options.
     48   typedef int PersistOptions;
     49   static const PersistOptions PERSIST_RAW = -1;  // Raw, unparsed headers.
     50   static const PersistOptions PERSIST_ALL = 0;  // Parsed headers.
     51   static const PersistOptions PERSIST_SANS_COOKIES = 1 << 0;
     52   static const PersistOptions PERSIST_SANS_CHALLENGES = 1 << 1;
     53   static const PersistOptions PERSIST_SANS_HOP_BY_HOP = 1 << 2;
     54   static const PersistOptions PERSIST_SANS_NON_CACHEABLE = 1 << 3;
     55   static const PersistOptions PERSIST_SANS_RANGES = 1 << 4;
     56 
     57   // Appends a representation of this object to the given pickle.
     58   // The options argument can be a combination of PersistOptions.
     59   void Persist(Pickle* pickle, PersistOptions options);
     60 
     61   // Performs header merging as described in 13.5.3 of RFC 2616.
     62   void Update(const HttpResponseHeaders& new_headers);
     63 
     64   // Removes all instances of a particular header.
     65   void RemoveHeader(const std::string& name);
     66 
     67   // Adds a particular header.  |header| has to be a single header without any
     68   // EOL termination, just [<header-name>: <header-values>]
     69   // If a header with the same name is already stored, the two headers are not
     70   // merged together by this method; the one provided is simply put at the
     71   // end of the list.
     72   void AddHeader(const std::string& header);
     73 
     74   // Replaces the current status line with the provided one (|new_status| should
     75   // not have any EOL).
     76   void ReplaceStatusLine(const std::string& new_status);
     77 
     78   // Creates a normalized header string.  The output will be formatted exactly
     79   // like so:
     80   //     HTTP/<version> <status_code> <status_text>\n
     81   //     [<header-name>: <header-values>\n]*
     82   // meaning, each line is \n-terminated, and there is no extra whitespace
     83   // beyond the single space separators shown (of course, values can contain
     84   // whitespace within them).  If a given header-name appears more than once
     85   // in the set of headers, they are combined into a single line like so:
     86   //     <header-name>: <header-value1>, <header-value2>, ...<header-valueN>\n
     87   //
     88   // DANGER: For some headers (e.g., "Set-Cookie"), the normalized form can be
     89   // a lossy format.  This is due to the fact that some servers generate
     90   // Set-Cookie headers that contain unquoted commas (usually as part of the
     91   // value of an "expires" attribute).  So, use this function with caution.  Do
     92   // not expect to be able to re-parse Set-Cookie headers from this output.
     93   //
     94   // NOTE: Do not make any assumptions about the encoding of this output
     95   // string.  It may be non-ASCII, and the encoding used by the server is not
     96   // necessarily known to us.  Do not assume that this output is UTF-8!
     97   //
     98   // TODO(darin): remove this method
     99   //
    100   void GetNormalizedHeaders(std::string* output) const;
    101 
    102   // Fetch the "normalized" value of a single header, where all values for the
    103   // header name are separated by commas.  See the GetNormalizedHeaders for
    104   // format details.  Returns false if this header wasn't found.
    105   //
    106   // NOTE: Do not make any assumptions about the encoding of this output
    107   // string.  It may be non-ASCII, and the encoding used by the server is not
    108   // necessarily known to us.  Do not assume that this output is UTF-8!
    109   //
    110   // TODO(darin): remove this method
    111   //
    112   bool GetNormalizedHeader(const std::string& name, std::string* value) const;
    113 
    114   // Returns the normalized status line.  For HTTP/0.9 responses (i.e.,
    115   // responses that lack a status line), this is the manufactured string
    116   // "HTTP/0.9 200 OK".
    117   std::string GetStatusLine() const;
    118 
    119   // Get the HTTP version of the normalized status line.
    120   HttpVersion GetHttpVersion() const {
    121     return http_version_;
    122   }
    123 
    124   // Get the HTTP version determined while parsing; or (0,0) if parsing failed
    125   HttpVersion GetParsedHttpVersion() const {
    126     return parsed_http_version_;
    127   }
    128 
    129   // Get the HTTP status text of the normalized status line.
    130   std::string GetStatusText() const;
    131 
    132   // Enumerate the "lines" of the response headers.  This skips over the status
    133   // line.  Use GetStatusLine if you are interested in that.  Note that this
    134   // method returns the un-coalesced response header lines, so if a response
    135   // header appears on multiple lines, then it will appear multiple times in
    136   // this enumeration (in the order the header lines were received from the
    137   // server).  Initialize a 'void*' variable to NULL and pass it by address to
    138   // EnumerateHeaderLines.  Call EnumerateHeaderLines repeatedly until it
    139   // returns false.  The out-params 'name' and 'value' are set upon success.
    140   bool EnumerateHeaderLines(void** iter,
    141                             std::string* name,
    142                             std::string* value) const;
    143 
    144   // Enumerate the values of the specified header.   If you are only interested
    145   // in the first header, then you can pass NULL for the 'iter' parameter.
    146   // Otherwise, to iterate across all values for the specified header,
    147   // initialize a 'void*' variable to NULL and pass it by address to
    148   // EnumerateHeader.  Call EnumerateHeader repeatedly until it returns false.
    149   bool EnumerateHeader(void** iter,
    150                        const std::string& name,
    151                        std::string* value) const;
    152 
    153   // Returns true if the response contains the specified header-value pair.
    154   // Both name and value are compared case insensitively.
    155   bool HasHeaderValue(const std::string& name, const std::string& value) const;
    156 
    157   // Get the mime type and charset values in lower case form from the headers.
    158   // Empty strings are returned if the values are not present.
    159   void GetMimeTypeAndCharset(std::string* mime_type,
    160                              std::string* charset) const;
    161 
    162   // Get the mime type in lower case from the headers.  If there's no mime
    163   // type, returns false.
    164   bool GetMimeType(std::string* mime_type) const;
    165 
    166   // Get the charset in lower case from the headers.  If there's no charset,
    167   // returns false.
    168   bool GetCharset(std::string* charset) const;
    169 
    170   // Returns true if this response corresponds to a redirect.  The target
    171   // location of the redirect is optionally returned if location is non-null.
    172   bool IsRedirect(std::string* location) const;
    173 
    174   // Returns true if the HTTP response code passed in corresponds to a
    175   // redirect.
    176   static bool IsRedirectResponseCode(int response_code);
    177 
    178   // Returns true if the response cannot be reused without validation.  The
    179   // result is relative to the current_time parameter, which is a parameter to
    180   // support unit testing.  The request_time parameter indicates the time at
    181   // which the request was made that resulted in this response, which was
    182   // received at response_time.
    183   bool RequiresValidation(const base::Time& request_time,
    184                           const base::Time& response_time,
    185                           const base::Time& current_time) const;
    186 
    187   // Returns the amount of time the server claims the response is fresh from
    188   // the time the response was generated.  See section 13.2.4 of RFC 2616.  See
    189   // RequiresValidation for a description of the response_time parameter.
    190   base::TimeDelta GetFreshnessLifetime(const base::Time& response_time) const;
    191 
    192   // Returns the age of the response.  See section 13.2.3 of RFC 2616.
    193   // See RequiresValidation for a description of this method's parameters.
    194   base::TimeDelta GetCurrentAge(const base::Time& request_time,
    195                                 const base::Time& response_time,
    196                                 const base::Time& current_time) const;
    197 
    198   // The following methods extract values from the response headers.  If a
    199   // value is not present, then false is returned.  Otherwise, true is returned
    200   // and the out param is assigned to the corresponding value.
    201   bool GetMaxAgeValue(base::TimeDelta* value) const;
    202   bool GetAgeValue(base::TimeDelta* value) const;
    203   bool GetDateValue(base::Time* value) const;
    204   bool GetLastModifiedValue(base::Time* value) const;
    205   bool GetExpiresValue(base::Time* value) const;
    206 
    207   // Extracts the time value of a particular header.  This method looks for the
    208   // first matching header value and parses its value as a HTTP-date.
    209   bool GetTimeValuedHeader(const std::string& name, base::Time* result) const;
    210 
    211   // Determines if this response indicates a keep-alive connection.
    212   bool IsKeepAlive() const;
    213 
    214   // Returns true if this response has a strong etag or last-modified header.
    215   // See section 13.3.3 of RFC 2616.
    216   bool HasStrongValidators() const;
    217 
    218   // Extracts the value of the Content-Length header or returns -1 if there is
    219   // no such header in the response.
    220   int64 GetContentLength() const;
    221 
    222   // Extracts the values in a Content-Range header and returns true if they are
    223   // valid for a 206 response; otherwise returns false.
    224   // The following values will be outputted:
    225   // |*first_byte_position| = inclusive position of the first byte of the range
    226   // |*last_byte_position| = inclusive position of the last byte of the range
    227   // |*instance_length| = size in bytes of the object requested
    228   // If any of the above values is unknown, its value will be -1.
    229   bool GetContentRange(int64* first_byte_position,
    230                        int64* last_byte_position,
    231                        int64* instance_length) const;
    232 
    233   // Returns the HTTP response code.  This is 0 if the response code text seems
    234   // to exist but could not be parsed.  Otherwise, it defaults to 200 if the
    235   // response code is not found in the raw headers.
    236   int response_code() const { return response_code_; }
    237 
    238   // Returns the raw header string.
    239   const std::string& raw_headers() const { return raw_headers_; }
    240 
    241  private:
    242   friend class base::RefCountedThreadSafe<HttpResponseHeaders>;
    243 
    244   typedef base::hash_set<std::string> HeaderSet;
    245 
    246   HttpResponseHeaders() {}
    247   ~HttpResponseHeaders() {}
    248 
    249   // Initializes from the given raw headers.
    250   void Parse(const std::string& raw_input);
    251 
    252   // Helper function for ParseStatusLine.
    253   // Tries to extract the "HTTP/X.Y" from a status line formatted like:
    254   //    HTTP/1.1 200 OK
    255   // with line_begin and end pointing at the begin and end of this line.  If the
    256   // status line is malformed, returns HttpVersion(0,0).
    257   static HttpVersion ParseVersion(std::string::const_iterator line_begin,
    258                                   std::string::const_iterator line_end);
    259 
    260   // Tries to extract the status line from a header block, given the first
    261   // line of said header block.  If the status line is malformed, we'll
    262   // construct a valid one.  Example input:
    263   //    HTTP/1.1 200 OK
    264   // with line_begin and end pointing at the begin and end of this line.
    265   // Output will be a normalized version of this, with a trailing \n.
    266   void ParseStatusLine(std::string::const_iterator line_begin,
    267                        std::string::const_iterator line_end,
    268                        bool has_headers);
    269 
    270   // Find the header in our list (case-insensitive) starting with parsed_ at
    271   // index |from|.  Returns string::npos if not found.
    272   size_t FindHeader(size_t from, const std::string& name) const;
    273 
    274   // Add a header->value pair to our list.  If we already have header in our
    275   // list, append the value to it.
    276   void AddHeader(std::string::const_iterator name_begin,
    277                  std::string::const_iterator name_end,
    278                  std::string::const_iterator value_begin,
    279                  std::string::const_iterator value_end);
    280 
    281   // Add to parsed_ given the fields of a ParsedHeader object.
    282   void AddToParsed(std::string::const_iterator name_begin,
    283                    std::string::const_iterator name_end,
    284                    std::string::const_iterator value_begin,
    285                    std::string::const_iterator value_end);
    286 
    287   // Replaces the current headers with the merged version of |raw_headers| and
    288   // the current headers without the headers in |headers_to_remove|. Note that
    289   // |headers_to_remove| are removed from the current headers (before the
    290   // merge), not after the merge.
    291   void MergeWithHeaders(const std::string& raw_headers,
    292                         const HeaderSet& headers_to_remove);
    293 
    294   // Adds the values from any 'cache-control: no-cache="foo,bar"' headers.
    295   void AddNonCacheableHeaders(HeaderSet* header_names) const;
    296 
    297   // Adds the set of header names that contain cookie values.
    298   static void AddSensitiveHeaders(HeaderSet* header_names);
    299 
    300   // Adds the set of rfc2616 hop-by-hop response headers.
    301   static void AddHopByHopHeaders(HeaderSet* header_names);
    302 
    303   // Adds the set of challenge response headers.
    304   static void AddChallengeHeaders(HeaderSet* header_names);
    305 
    306   // Adds the set of cookie response headers.
    307   static void AddCookieHeaders(HeaderSet* header_names);
    308 
    309   // Adds the set of content range response headers.
    310   static void AddHopContentRangeHeaders(HeaderSet* header_names);
    311 
    312   // The members of this structure point into raw_headers_.
    313   struct ParsedHeader {
    314     std::string::const_iterator name_begin;
    315     std::string::const_iterator name_end;
    316     std::string::const_iterator value_begin;
    317     std::string::const_iterator value_end;
    318 
    319     // A header "continuation" contains only a subsequent value for the
    320     // preceding header.  (Header values are comma separated.)
    321     bool is_continuation() const { return name_begin == name_end; }
    322   };
    323   typedef std::vector<ParsedHeader> HeaderList;
    324 
    325   // We keep a list of ParsedHeader objects.  These tell us where to locate the
    326   // header-value pairs within raw_headers_.
    327   HeaderList parsed_;
    328 
    329   // The raw_headers_ consists of the normalized status line (terminated with a
    330   // null byte) and then followed by the raw null-terminated headers from the
    331   // input that was passed to our constructor.  We preserve the input [*] to
    332   // maintain as much ancillary fidelity as possible (since it is sometimes
    333   // hard to tell what may matter down-stream to a consumer of XMLHttpRequest).
    334   // [*] The status line may be modified.
    335   std::string raw_headers_;
    336 
    337   // This is the parsed HTTP response code.
    338   int response_code_;
    339 
    340   // The normalized http version (consistent with what GetStatusLine() returns).
    341   HttpVersion http_version_;
    342 
    343   // The parsed http version number (not normalized).
    344   HttpVersion parsed_http_version_;
    345 
    346   DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders);
    347 };
    348 
    349 }  // namespace net
    350 
    351 #endif  // NET_HTTP_HTTP_RESPONSE_HEADERS_H_
    352