Page Speed Optimization Libraries
1.13.35.1
|
Read/write API for HTTP response headers. More...
#include "response_headers.h"
Public Types | |
enum | VaryOption { kRespectVaryOnResources, kIgnoreVaryOnResources } |
enum | ValidatorOption { kHasValidator, kNoValidator } |
Public Types inherited from net_instaweb::Headers< HttpResponseHeaders > | |
typedef std::pair< StringPiece, StringPiece > | ValueAndAttributes |
typedef's for manipulating the cookie multimap. | |
typedef std::multimap < StringPiece, ValueAndAttributes > | CookieMultimap |
typedef std::multimap < StringPiece, ValueAndAttributes > ::const_iterator | CookieMultimapConstIter |
Public Member Functions | |
ResponseHeaders (const HttpOptions &options) | |
This constructor with options explicitly set should be used by all callers. | |
ResponseHeaders (const ResponseHeaders &other) | |
ResponseHeaders & | operator= (const ResponseHeaders &other) |
ResponseHeaders () | |
void | FixDateHeaders (int64 now_ms) |
bool | cache_fields_dirty () const |
virtual void | Clear () |
void | CopyFrom (const ResponseHeaders &other) |
bool | MergeContentType (const StringPiece &content_type) |
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 | IsProxyCacheable (RequestHeaders::Properties properties, VaryOption respect_vary_on_resources, ValidatorOption has_request_validator) const |
bool | IsProxyCacheable () const |
bool | IsBrowserCacheable () const |
bool | RequiresBrowserRevalidation () const |
bool | RequiresProxyRevalidation () 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 | SetCacheControlPublic () |
void | SetOriginalContentLength (int64 content_length) |
void | SetContentLength (int64 content_length) |
bool | Sanitize () |
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) |
const HttpOptions & | http_options () const |
int64 | implicit_cache_ttl_ms () const |
void | set_implicit_cache_ttl_ms (const int64 ttl) |
bool | has_last_modified_time_ms () const |
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 () const |
Returns true if our status denotes the request failing. | |
bool | IsServerErrorStatus () const |
Returns true if our status denotes a server side error. | |
bool | IsRedirectStatus () const |
Returns true if our status denotes a redirect. | |
bool | IsGzipped () const |
Determines whether a response header is marked as gzipped. | |
bool | WasGzippedLast () const |
const ContentType * | DetermineContentType () 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 |
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 | 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, StringPieceVector *attributes) const |
bool | HasAnyCookiesWithAttribute (StringPiece attribute_name, StringPiece *attribute_value) |
bool | SetQueryParamsAsCookies (const GoogleUrl &gurl, StringPiece query_params, const StringPieceVector &to_exclude, int64 expiration_time) |
bool | ClearOptionCookies (const GoogleUrl &gurl, StringPiece option_cookies, const StringPieceVector &to_exclude) |
bool | HasLinkRelCanonical () const |
void | SetSMaxAge (int s_maxage_sec) |
Public Member Functions inherited from net_instaweb::Headers< HttpResponseHeaders > | |
int | major_version () const |
bool | has_major_version () const |
int | minor_version () const |
void | set_major_version (int major_version) |
void | set_minor_version (int major_version) |
int | NumAttributes () const |
Raw access for random access to attribute name/value pairs. | |
const GoogleString & | Name (int i) const |
const GoogleString & | Value (int i) const |
void | SetValue (int i, StringPiece value) |
bool | Lookup (const StringPiece &name, ConstStringStarVector *values) const |
GoogleString | LookupJoined (StringPiece name) const |
const char * | Lookup1 (const StringPiece &name) const |
bool | Has (const StringPiece &name) const |
Does there exist a header with given name. | |
bool | HasValue (const StringPiece &name, const StringPiece &value) const |
Is value one of the values in Lookup(name)? | |
int | NumAttributeNames () const |
NumAttributeNames is also const but not thread-safe. | |
void | RemoveCookie (const StringPiece &cookie_name) |
void | Add (const StringPiece &name, const StringPiece &value) |
bool | Remove (const StringPiece &name, const StringPiece &value) |
bool | RemoveAll (const StringPiece &name) |
Removes all headers by name. Return true if anything was removed. | |
bool | RemoveAllFromSortedArray (const StringPiece *names, int names_size) |
bool | RemoveAllWithPrefix (const StringPiece &prefix) |
bool | RemoveIfNotIn (const Headers &keep) |
virtual void | Replace (const StringPiece &name, const StringPiece &value) |
void | CopyToProto (HttpResponseHeaders *proto) const |
Copy protobuf representation to "proto". | |
Static Public Member Functions | |
static bool | IsImminentlyExpiring (int64 start_date_ms, int64 expire_ms, int64 now_ms, const HttpOptions &options) |
static VaryOption | GetVaryOption (bool respect_vary) |
static bool | ParseTime (const char *time_str, int64 *time_ms) |
Parses an arbitrary string into milliseconds since 1970. | |
static GoogleString | RelCanonicalHeaderValue (StringPiece url) |
Constructs a <url>; rel="canonical" value for use with a Link header. | |
static bool | ApplySMaxAge (int s_maxage_sec, StringPiece existing_cache_control, GoogleString *updated_cache_control) |
static bool | IsHopByHopIndication (StringPiece val) |
Static Public Member Functions inherited from net_instaweb::Headers< HttpResponseHeaders > | |
static bool | RemoveFromHeaders (const StringType *names, int names_size, protobuf::RepeatedPtrField< NameValue > *headers) |
static bool | FindValueForName (const StringPieceVector &name_equals_value_vec, StringPiece name_to_find, StringPiece *optional_retval) |
static bool | ExtractNameAndValue (StringPiece input, StringPiece *name, StringPiece *optional_retval) |
Protected Member Functions | |
virtual void | UpdateHook () |
Protected Member Functions inherited from net_instaweb::Headers< HttpResponseHeaders > | |
void | SetProto (HttpResponseHeaders *proto) |
You need to know what you're doing to use these, so for subclasses only. More... | |
void | CopyProto (const HttpResponseHeaders &proto) |
void | PopulateMap () const |
const is a lie, mutates map_. | |
const CookieMultimap * | PopulateCookieMap (StringPiece header_name) const |
const HttpResponseHeaders * | proto () const |
HttpResponseHeaders * | mutable_proto () |
Friends | |
class | ResponseHeadersTest |
Read/write API for HTTP response headers.
|
inline |
This default constructor should only be used in tests.
|
static |
Stand-alone version of SetSMaxAge. If there are changes to make, returns true and sets updated_cache_control.
|
inline |
Returns whether the computed cache fields have been dirtied by a mutation since ComputeCache was called. This is exposed for debug assertions.
GoogleString net_instaweb::ResponseHeaders::CacheControlValuesToPreserve | ( | ) |
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.
int64 net_instaweb::ResponseHeaders::CacheExpirationTimeMs | ( | ) | const |
Note(sligocki): I think CacheExpirationTimeMs will return 0 if !IsCacheable
void net_instaweb::ResponseHeaders::ComputeCaching | ( | ) |
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.
const ContentType* net_instaweb::ResponseHeaders::DetermineContentType | ( | ) | const |
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.
void net_instaweb::ResponseHeaders::FixDateHeaders | ( | int64 | now_ms | ) |
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.
void net_instaweb::ResponseHeaders::ForceCaching | ( | int64 | ttl_ms | ) |
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.
bool net_instaweb::ResponseHeaders::GetCookieString | ( | GoogleString * | cookie_str | ) | const |
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::HasAnyCookiesWithAttribute | ( | StringPiece | attribute_name, |
StringPiece * | attribute_value | ||
) |
Returns true if any cookies in the response headers have an attribute with the given name, returning the value for the first one found in '*attribute_value' iff it isn't NULL.
bool net_instaweb::ResponseHeaders::HasCookie | ( | StringPiece | name, |
StringPieceVector * | values, | ||
StringPieceVector * | attributes | ||
) | const |
Returns true if the response headers have a cookie with the given name. 'values' gives the associated values. 'attributes' gives the attributes. name results in "" in values and nothing in attributes. name=; HttpOnly results in "" in values and "HttpOnly" in attributes. name=value results in "value" in values and nothing in attributes. name=value; Expires=yaddayadda; HttpOnly results in "value" in values and " Expires=yaddayadda" and " HttpOnly" in attributes. Note that the attributes are not trimmed of whitespace. The return value is true in all the above cases. It is a limitation of this API that a cookie with no value set is indistinguishable from a cookie with an empty value. Furthermore, if the cookie is set in multiple headers, values and attributes will be the union of those headers' contents.
bool net_instaweb::ResponseHeaders::HasLinkRelCanonical | ( | ) | const |
Returns true if the headers may contain a Link: rel = canonical entry. (Slightly approximate, will never say 'no' if it's there).
|
inline |
|
inline |
bool net_instaweb::ResponseHeaders::IsBrowserCacheable | ( | ) | const |
Returns true if the response is privately cacheable.
Generally you want to use IsProxyCacheable*() instead.
|
static |
Returns true if the given value should be interpreted as a header being marked as hop by hop when listed as a value in a Connection: header.
|
static |
Returns true if the resource with given date and TTL is going to expire shortly and should hence be proactively re-fetched. All the parameters are absolute times.
bool net_instaweb::ResponseHeaders::IsProxyCacheable | ( | RequestHeaders::Properties | properties, |
VaryOption | respect_vary_on_resources, | ||
ValidatorOption | has_request_validator | ||
) | const |
Returns true if these response headers indicate the response is publicly cacheable if it was fetched w/o special authorization headers.
See also RequiresProxyRevalidation(), which must be used to determine whether stale content can be re-used by a proxy.
The difference between HTML and non-HTML is tolerance for Vary:Cookie. In HTML we are willing to cache cookieless responses and serve them to other cookieless requests, but this requires the requests to be validated. Callers can indicate their ability to validate requests by passing kHasRequestValidator for has_request_validator.
respect_vary_on_resources |
|
inline |
The zero-arg version of IsProxyCacheable gives a pessimistic answer, assuming the request has cookies, there is no validator, and we respect Vary.
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. If the new content_type contains non-printable characters, the change will be rejected silently (and false will be returned).
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 |
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 >.
bool net_instaweb::ResponseHeaders::RequiresBrowserRevalidation | ( | ) | const |
Determines whether must-revalidate is in any Cache-Control setting.
Proxies such as PSOL likely want to use RequiresProxyRevalidation() instead.
bool net_instaweb::ResponseHeaders::RequiresProxyRevalidation | ( | ) | const |
Determines whether either must-revalidate or proxy-revalidate is in any Cache-Control setting. These must be checked to see whether it's OK to serve stale content while freshening in the background.
bool net_instaweb::ResponseHeaders::Sanitize | ( | ) |
Removes hop-by-hop plus cookie headers, and returns true if any changes were made.
void net_instaweb::ResponseHeaders::SetCacheControlMaxAge | ( | int64 | ttl_ms | ) |
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::SetCacheControlPublic | ( | ) |
Sets the cache-control to explicitly have 'public', as long as that's not in conflict with other CC headers.
void net_instaweb::ResponseHeaders::SetContentLength | ( | int64 | content_length | ) |
Sets the content-length attribute, removing any matching x-orginal-content-length header.
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".
void net_instaweb::ResponseHeaders::SetOriginalContentLength | ( | int64 | content_length | ) |
Sets the x-original-content-length header, used to relay information on the original size of optimized resources.
bool net_instaweb::ResponseHeaders::SetQueryParamsAsCookies | ( | const GoogleUrl & | gurl, |
StringPiece | query_params, | ||
const StringPieceVector & | to_exclude, | ||
int64 | expiration_time | ||
) |
Set or clears the given query parameters as response header cookies, skipping any in to_exclude. query_params and option_cookies are both query parameters (name=value separated by '&'s) and are treated as untrusted data. Sets the cookies' Expires attributes to the given value. Returns true if any cookies were set, false if not.
void net_instaweb::ResponseHeaders::SetSMaxAge | ( | int | s_maxage_sec | ) |
Gives a new value for the cache control header, making it more restrictive by adding s-maxage=<s_maxage_sec>. Takes into account existing s-maxage and maxage segments:
int64 net_instaweb::ResponseHeaders::SizeEstimate | ( | ) | const |
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 |
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.
|
protectedvirtual |
Called whenever a mutation occurrs. Subclasses may override to update any local copies of data.
Reimplemented from net_instaweb::Headers< HttpResponseHeaders >.