Page Speed Optimization Libraries  1.2.24.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends
net_instaweb::ResponseHeaders Class Reference

Read/write API for HTTP response headers. More...

#include "response_headers.h"

Inheritance diagram for net_instaweb::ResponseHeaders:
net_instaweb::Headers< HttpResponseHeaders >

List of all members.

Public Member Functions

void FixDateHeaders (int64 now_ms)
virtual void Clear ()
void CopyFrom (const ResponseHeaders &other)
virtual void Add (const StringPiece &name, const StringPiece &value)
 Add a new header.
bool MergeContentType (const StringPiece &content_type)
virtual bool Remove (const StringPiece &name, const StringPiece &value)
 Remove headers by name and value.
virtual bool RemoveAll (const StringPiece &name)
 Remove all headers by name.
virtual bool RemoveAllFromSet (const StringSetInsensitive &names)
 Remove all headers whose name is in |names|.
virtual void Replace (const StringPiece &name, const StringPiece &value)
virtual void UpdateFrom (const Headers< HttpResponseHeaders > &other)
void UpdateFromProto (const HttpResponseHeaders &proto)
virtual bool WriteAsBinary (Writer *writer, MessageHandler *message_handler)
 Serialize HTTP response header to a binary stream.
virtual bool ReadFromBinary (const StringPiece &buf, MessageHandler *handler)
virtual bool WriteAsHttp (Writer *writer, MessageHandler *handler) const
 Serialize HTTP response header in HTTP format so it can be re-parsed.
void ComputeCaching ()
bool IsCacheable () const
bool IsProxyCacheable () const
bool IsProxyCacheableGivenRequest (const RequestHeaders &req_headers) const
int64 CacheExpirationTimeMs () const
void SetDateAndCaching (int64 date_ms, int64 ttl_ms, const StringPiece &cache_control_suffix)
void SetDateAndCaching (int64 date_ms, int64 ttl_ms)
GoogleString CacheControlValuesToPreserve ()
void SetTimeHeader (const StringPiece &header, int64 time_ms)
 Set a time-based header, converting ms since epoch to a string.
void SetDate (int64 date_ms)
void SetLastModified (int64 last_modified_ms)
void SetCacheControlMaxAge (int64 ttl_ms)
void SetOriginalContentLength (int64 content_length)
bool Sanitize ()
 Removes cookie headers, and returns true if any changes were made.
void GetSanitizedProto (HttpResponseHeaders *proto) const
bool headers_complete () const
int status_code () const
bool has_status_code () const
void set_status_code (const int code)
const char * reason_phrase () const
void set_reason_phrase (const StringPiece &reason_phrase)
int64 implicit_cache_ttl_ms () const
void set_implicit_cache_ttl_ms (const int64 ttl)
int64 last_modified_time_ms () const
int64 date_ms () const
 Timestamp from Date header.
bool has_date_ms () const
int64 cache_ttl_ms () const
bool is_implicitly_cacheable () const
GoogleString ToString () const
void SetStatusAndReason (HttpStatus::Code code)
 Sets the status code and reason_phrase based on an internal table.
void DebugPrint () const
bool IsErrorStatus ()
 Returns true if our status denotes the request failing.
bool IsServerErrorStatus ()
 Returns true if our status denotes a server side error.
bool IsGzipped () const
 Determines whether a response header is marked as gzipped.
bool WasGzippedLast () const
const ContentTypeDetermineContentType () const
bool IsHtmlLike () const
 Does this header have an HTML-like Content-Type (HTML, XHTML, ...).
GoogleString DetermineCharset () const
 Get the charset. Empty string if none set in a Content-Type header.
void DetermineContentTypeAndCharset (const ContentType **content_type_out, GoogleString *charset_out) const
bool ParseDateHeader (const StringPiece &attr, int64 *date_ms) const
bool IsDateLaterThan (int64 time_ms) const
void ParseFirstLine (const StringPiece &first_line)
 Parses the first line of an HTTP response, including the "HTTP/".
void ParseFirstLineHelper (const StringPiece &first_line)
 Parses the first line of an HTTP response, skipping the "HTTP/".
void set_first_line (int major_version, int minor_version, int status_code, const StringPiece &reason_phrase)
 Set whole first line.
bool VaryCacheable (bool request_has_cookie) const
bool FindContentLength (int64 *content_length) const
void ForceCaching (int64 ttl_ms)
bool UpdateCacheHeadersIfForceCached ()
 Update the caching headers if the response has force cached.
int64 SizeEstimate () const
bool GetCookieString (GoogleString *cookie_str) const
bool HasCookie (StringPiece name, StringPieceVector *values) const

Static Public Member Functions

static bool ParseTime (const char *time_str, int64 *time_ms)
 Parses an arbitrary string into milliseconds since 1970.

Static Public Attributes

static const int64 kImplicitCacheTtlMs = 5 * Timer::kMinuteMs

Friends

class ResponseHeadersTest

Detailed Description

Read/write API for HTTP response headers.


Member Function Documentation

Returns Cache-Control header values that we might need to preserve. This function is meant to be used with SetDateAndCaching. It currently looks for and returns no-transform and no-store if found.

Note(sligocki): I think CacheExpirationTimeMs will return 0 if !IsCacheable

Todo:
TODO(sligocki): Look through callsites and make sure this is being interpretted correctly.

Compute caching information. The current time is used to compute the absolute time when a cache resource will expire. The timestamp is in milliseconds since 1970. It is an error to call any of the accessors before ComputeCaching is called.

Get ContentType. NULL if none set or it isn't in our predefined set of known content types.

void net_instaweb::ResponseHeaders::DetermineContentTypeAndCharset ( const ContentType **  content_type_out,
GoogleString charset_out 
) const

Determine both the charset and content-type as above. See DetermineContentType() and DetermineCharset() for details. You may also pass in NULL for those of _out parameters you do not need (but in that case the individual functions would be more convenient)

bool net_instaweb::ResponseHeaders::FindContentLength ( int64 *  content_length) const

Finds Content-Length in the response headers, returning true and putting it in *content_length if successful.

This will set Date and (if supplied in the first place, Expires) header to now if the delta of date header wrt now_ms is more than a tolerance. Leaves the ComputeCaching state dirty if it came in dirty, or clean if it came in clean.

Force cache the response with the given TTL even if it is private. Note that this does not change any of the headers. The values of cache_ttl_ms, IsCacheable and IsProxyCacheable are updated once ComputeCaching() is called. Note that for responses which were originally cacheable, the effective cache TTL is the maximum of the original TTL and ttl_ms. For responses which were originally uncacheable, the new cache TTL is ttl_ms.

Returns true if the response headers have cookies and false otherwise. If cookies are found then it sets them in cookie_str in javascript array format.

void net_instaweb::ResponseHeaders::GetSanitizedProto ( HttpResponseHeaders *  proto) const

Copies the HttpResponseHeaders proto from the response headers to the given input after removing the Set-Cookie fields.

bool net_instaweb::ResponseHeaders::HasCookie ( StringPiece  name,
StringPieceVector *  values 
) const

Returns true in the response headers have a cookie attribute with the given name. values gives the associated values. name=value results in "value" in values. name= results in "" in values. name results in nothing being added to values. The return value is true in all the above cases. It is a limitation of this API that a cookie value of "name=value;name" is indistinguishable from a cookie value of "name=value".

Todo:
TODO(jmarantz): consider an alternative representation
bool net_instaweb::ResponseHeaders::IsDateLaterThan ( int64  time_ms) const [inline]

Returns true if the date header is later than time_ms. Used in invalidation of http cache.

Returns true if these response headers indicate the response is cacheable if it was fetched w/o special authorization headers.

Generally you want to use IsProxyCacheableGivenRequest() instead which will also take the request headers into account, unless you know the request was synthesized with known headers which do not include authorization.

Returns true if these response header indicate the response is cacheable if it was fetched with given 'request_headers'.

bool net_instaweb::ResponseHeaders::MergeContentType ( const StringPiece &  content_type)

Merge the new content_type with what is already in the headers. Returns true if the existing content-type header was changed.

bool net_instaweb::ResponseHeaders::ParseDateHeader ( const StringPiece &  attr,
int64 *  date_ms 
) const

Parses a date header such as HttpAttributes::kDate or HttpAttributes::kExpires, returning the timestamp as number of milliseconds since 1970.

virtual bool net_instaweb::ResponseHeaders::ReadFromBinary ( const StringPiece &  buf,
MessageHandler handler 
) [virtual]

Read HTTP response header from a binary string. Note that this is distinct from HTTP response-header parsing, which is in ResponseHeadersParser.

Reimplemented from net_instaweb::Headers< HttpResponseHeaders >.

virtual void net_instaweb::ResponseHeaders::Replace ( const StringPiece &  name,
const StringPiece &  value 
) [virtual]

Similar to RemoveAll followed by Add. Note that the attribute order may be changed as a side effect of this operation.

Reimplemented from net_instaweb::Headers< HttpResponseHeaders >.

Sets the cache-control max-age to the specified value leaving the remaining Cache-Control attributes the same. This also updates the Expires header appropriately. Note that all existing max-age values are removed.

void net_instaweb::ResponseHeaders::SetDateAndCaching ( int64  date_ms,
int64  ttl_ms,
const StringPiece &  cache_control_suffix 
)

Set Date, Cache-Control and Expires headers appropriately. If cache_control_suffix is provided it is appended onto the Cache-Control: "max-age=%d" string. For example, cache_control_suffix = ", private" or ", no-cache, no-store".

Sets the original content length header, used to relay information on the original size of optimized resources.

Returns estimated size in bytes of these headers (if transferred over HTTP, not SPDY or other protocols). This is an estimate because it may not properly account for things like spacing around : or whether multiple headers were on a single or multiple lines.

virtual void net_instaweb::ResponseHeaders::UpdateFrom ( const Headers< HttpResponseHeaders > &  other) [virtual]

Merge headers. Replaces all headers specified both here and in other with the version in other. Useful for updating headers when recieving 304 Not Modified responses. Note: We must use Headers<HttpResponseHeaders> instead of ResponseHeaders so that we don't expose the base UpdateFrom (and to avoid "hiding" errors).

Reimplemented from net_instaweb::Headers< HttpResponseHeaders >.

void net_instaweb::ResponseHeaders::UpdateFromProto ( const HttpResponseHeaders &  proto)

Initializes the response headers with the one in proto, clearing the existing fields.

bool net_instaweb::ResponseHeaders::VaryCacheable ( bool  request_has_cookie) const

Returns whether or not we can cache these headers if we take into account the Vary: headers. Note that we consider Vary: Cookie as cacheable if request_has_cookie is false.


Member Data Documentation

const int64 net_instaweb::ResponseHeaders::kImplicitCacheTtlMs = 5 * Timer::kMinuteMs [static]

The number of milliseconds of cache TTL we assign to resources that are "likely cacheable" (e.g. images, js, css, not html) and have no explicit cache ttl or expiration date.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines