Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
response_headers.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc.
14 
16 
17 #ifndef PAGESPEED_KERNEL_HTTP_RESPONSE_HEADERS_H_
18 #define PAGESPEED_KERNEL_HTTP_RESPONSE_HEADERS_H_
19 
28 
29 namespace net_instaweb {
30 
31 class GoogleUrl;
32 class HttpResponseHeaders;
33 class MessageHandler;
34 class Writer;
35 
37 class ResponseHeaders : public Headers<HttpResponseHeaders> {
38  public:
39  enum VaryOption { kRespectVaryOnResources, kIgnoreVaryOnResources };
40  enum ValidatorOption { kHasValidator, kNoValidator };
41 
43  explicit ResponseHeaders(const HttpOptions& options) { Init(options); }
44 
45  explicit ResponseHeaders(const ResponseHeaders& other);
46  ResponseHeaders& operator=(const ResponseHeaders& other);
47 
51 
52  virtual ~ResponseHeaders();
53 
57  static bool IsImminentlyExpiring(
58  int64 start_date_ms, int64 expire_ms, int64 now_ms,
59  const HttpOptions& options);
60 
65  void FixDateHeaders(int64 now_ms);
66 
70  bool cache_fields_dirty() const { return cache_fields_dirty_; }
71 
72  virtual void Clear();
73 
74  void CopyFrom(const ResponseHeaders& other);
75 
80  bool MergeContentType(const StringPiece& content_type);
81 
87  virtual void UpdateFrom(const Headers<HttpResponseHeaders>& other);
88 
91  void UpdateFromProto(const HttpResponseHeaders& proto);
92 
94  virtual bool WriteAsBinary(Writer* writer, MessageHandler* message_handler);
95 
99  virtual bool ReadFromBinary(const StringPiece& buf, MessageHandler* handler);
100 
102  virtual bool WriteAsHttp(Writer* writer, MessageHandler* handler) const;
103 
108  void ComputeCaching();
109 
125  VaryOption respect_vary_on_resources,
126  ValidatorOption has_request_validator) const;
127 
128  static VaryOption GetVaryOption(bool respect_vary) {
129  return respect_vary ? kRespectVaryOnResources : kIgnoreVaryOnResources;
130  }
131 
135  bool IsProxyCacheable() const {
136  return IsProxyCacheable(
137  RequestHeaders::Properties(), kRespectVaryOnResources, kNoValidator);
138  }
139 
143  bool IsBrowserCacheable() const;
144 
149  bool RequiresBrowserRevalidation() const;
150 
154  bool RequiresProxyRevalidation() const;
155 
159  int64 CacheExpirationTimeMs() const;
160 
165  void SetDateAndCaching(int64 date_ms, int64 ttl_ms,
166  const StringPiece& cache_control_suffix);
167  void SetDateAndCaching(int64 date_ms, int64 ttl_ms) {
168  SetDateAndCaching(date_ms, ttl_ms, "");
169  }
174 
176  void SetTimeHeader(const StringPiece& header, int64 time_ms);
177  void SetDate(int64 date_ms) { SetTimeHeader(HttpAttributes::kDate, date_ms); }
178  void SetLastModified(int64 last_modified_ms) {
179  SetTimeHeader(HttpAttributes::kLastModified, last_modified_ms);
180  }
181 
185  void SetCacheControlMaxAge(int64 ttl_ms);
186 
189  void SetCacheControlPublic();
190 
193  void SetOriginalContentLength(int64 content_length);
194 
197  void SetContentLength(int64 content_length);
198 
201  bool Sanitize();
202 
205  void GetSanitizedProto(HttpResponseHeaders* proto) const;
206 
208  bool headers_complete() const { return has_status_code(); }
209 
210  int status_code() const;
211  bool has_status_code() const;
212  void set_status_code(const int code);
213  const char* reason_phrase() const;
214  void set_reason_phrase(const StringPiece& reason_phrase);
215 
216  const HttpOptions& http_options() const { return http_options_; }
217 
220  int64 implicit_cache_ttl_ms() const {
221  return http_options_.implicit_cache_ttl_ms;
222  }
223  void set_implicit_cache_ttl_ms(const int64 ttl) {
224  http_options_.implicit_cache_ttl_ms = ttl;
225  }
226 
227  bool has_last_modified_time_ms() const;
228  int64 last_modified_time_ms() const;
229  int64 date_ms() const;
230  bool has_date_ms() const;
231  int64 cache_ttl_ms() const;
232  bool is_implicitly_cacheable() const;
233 
234  GoogleString ToString() const;
235 
237  void SetStatusAndReason(HttpStatus::Code code);
238 
239  void DebugPrint() const;
240 
242  static bool ParseTime(const char* time_str, int64* time_ms);
243 
245  inline bool IsErrorStatus() const {
246  int status = status_code();
247  return status >= 400 && status <= 599;
248  }
249 
251  inline bool IsServerErrorStatus() const {
252  int status = status_code();
253  return status >= 500 && status <= 599;
254  }
255 
257  bool IsRedirectStatus() const {
258  int status = status_code();
259  return status >= 300 && status <= 399 && status != 304;
260  }
261 
263  bool IsGzipped() const;
264  bool WasGzippedLast() const;
265 
268  const ContentType* DetermineContentType() const;
269 
271  bool IsHtmlLike() const {
272  const ContentType* type = DetermineContentType();
273  return (type != NULL && type->IsHtmlLike());
274  }
275 
278 
283  void DetermineContentTypeAndCharset(const ContentType** content_type_out,
284  GoogleString* charset_out) const;
285 
286 
290  bool ParseDateHeader(const StringPiece& attr, int64* date_ms) const;
291 
293  void ParseFirstLine(const StringPiece& first_line);
294 
296  void ParseFirstLineHelper(const StringPiece& first_line);
297 
299  void set_first_line(int major_version, int minor_version, int status_code,
300  const StringPiece& reason_phrase) {
301  set_major_version(major_version);
302  set_minor_version(minor_version);
303  set_status_code(status_code);
304  set_reason_phrase(reason_phrase);
305  }
306 
309  bool FindContentLength(int64* content_length) const;
310 
319  void ForceCaching(int64 ttl_ms);
320 
323 
328  int64 SizeEstimate() const;
329 
333  bool GetCookieString(GoogleString* cookie_str) const;
334 
350  bool HasCookie(StringPiece name, StringPieceVector* values,
351  StringPieceVector* attributes) const;
352 
356  bool HasAnyCookiesWithAttribute(StringPiece attribute_name,
357  StringPiece* attribute_value);
358 
364  bool SetQueryParamsAsCookies(const GoogleUrl& gurl, StringPiece query_params,
365  const StringPieceVector& to_exclude,
366  int64 expiration_time);
367  bool ClearOptionCookies(const GoogleUrl& gurl, StringPiece option_cookies,
368  const StringPieceVector& to_exclude);
369 
372  bool HasLinkRelCanonical() const;
373 
375  static GoogleString RelCanonicalHeaderValue(StringPiece url);
376 
385  void SetSMaxAge(int s_maxage_sec);
388  static bool ApplySMaxAge(int s_maxage_sec,
389  StringPiece existing_cache_control,
390  GoogleString* updated_cache_control);
391 
394  static bool IsHopByHopIndication(StringPiece val);
395 
396  protected:
397  virtual void UpdateHook();
398 
399  private:
400  void Init(const HttpOptions& options);
401 
407  bool CombineContentTypes(const StringPiece& orig, const StringPiece& fresh);
408 
409  friend class ResponseHeadersTest;
410  bool cache_fields_dirty_;
411 
413  HttpOptions http_options_;
414 
417  int64 force_cache_ttl_ms_;
419  bool force_cached_;
420 
422 };
423 
424 }
425 
426 #endif
void SetContentLength(int64 content_length)
class GoogleUrl
Definition: google_url.h:58
virtual bool ReadFromBinary(const StringPiece &buf, MessageHandler *handler)
bool MergeContentType(const StringPiece &content_type)
bool GetCookieString(GoogleString *cookie_str) const
void UpdateFromProto(const HttpResponseHeaders &proto)
void ParseFirstLine(const StringPiece &first_line)
Parses the first line of an HTTP response, including the "HTTP/".
void ForceCaching(int64 ttl_ms)
bool ParseDateHeader(const StringPiece &attr, int64 *date_ms) const
const HttpResponseHeaders * proto() const
Definition: headers.h:217
Definition: request_headers.h:43
void SetSMaxAge(int s_maxage_sec)
bool HasAnyCookiesWithAttribute(StringPiece attribute_name, StringPiece *attribute_value)
bool FindContentLength(int64 *content_length) const
void set_first_line(int major_version, int minor_version, int status_code, const StringPiece &reason_phrase)
Set whole first line.
Definition: response_headers.h:299
ResponseHeaders()
Definition: response_headers.h:50
bool IsGzipped() const
Determines whether a response header is marked as gzipped.
void SetOriginalContentLength(int64 content_length)
void SetStatusAndReason(HttpStatus::Code code)
Sets the status code and reason_phrase based on an internal table.
Read/write API for HTTP response headers.
Definition: response_headers.h:37
static bool ApplySMaxAge(int s_maxage_sec, StringPiece existing_cache_control, GoogleString *updated_cache_control)
void SetCacheControlMaxAge(int64 ttl_ms)
bool IsServerErrorStatus() const
Returns true if our status denotes a server side error.
Definition: response_headers.h:251
virtual void UpdateFrom(const Headers< HttpResponseHeaders > &other)
int64 CacheExpirationTimeMs() const
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
bool RequiresBrowserRevalidation() const
bool RequiresProxyRevalidation() const
GoogleString DetermineCharset() const
Get the charset. Empty string if none set in a Content-Type header.
bool UpdateCacheHeadersIfForceCached()
Update the caching headers if the response has force cached.
static GoogleString RelCanonicalHeaderValue(StringPiece url)
Constructs a <url>; rel="canonical" value for use with a Link header.
Interface for writing bytes to an output stream.
Definition: writer.h:29
static bool IsImminentlyExpiring(int64 start_date_ms, int64 expire_ms, int64 now_ms, const HttpOptions &options)
void SetTimeHeader(const StringPiece &header, int64 time_ms)
Set a time-based header, converting ms since epoch to a string.
Definition: content_type.h:31
bool IsRedirectStatus() const
Returns true if our status denotes a redirect.
Definition: response_headers.h:257
bool headers_complete() const
Definition: response_headers.h:208
int64 date_ms() const
Timestamp from Date header.
bool HasCookie(StringPiece name, StringPieceVector *values, StringPieceVector *attributes) const
void ParseFirstLineHelper(const StringPiece &first_line)
Parses the first line of an HTTP response, skipping the "HTTP/".
int64 implicit_cache_ttl_ms() const
Definition: response_headers.h:220
Definition: message_handler.h:39
const ContentType * DetermineContentType() const
static bool IsHopByHopIndication(StringPiece val)
bool IsHtmlLike() const
Does this header have an HTML-like Content-Type (HTML, XHTML, ...).
Definition: response_headers.h:271
Read/write API for HTTP headers (shared base class)
Definition: headers.h:37
void GetSanitizedProto(HttpResponseHeaders *proto) const
bool IsErrorStatus() const
Returns true if our status denotes the request failing.
Definition: response_headers.h:245
int64 implicit_cache_ttl_ms
TTL assigned to resources with no explicit caching headers.
Definition: http_options.h:30
Any options which need to be accessed in http/ should be in here.
Definition: http_options.h:25
const HttpOptions kDeprecatedDefaultHttpOptions
GoogleString CacheControlValuesToPreserve()
static bool ParseTime(const char *time_str, int64 *time_ms)
Parses an arbitrary string into milliseconds since 1970.
bool SetQueryParamsAsCookies(const GoogleUrl &gurl, StringPiece query_params, const StringPieceVector &to_exclude, int64 expiration_time)
void FixDateHeaders(int64 now_ms)
void DetermineContentTypeAndCharset(const ContentType **content_type_out, GoogleString *charset_out) const
bool IsProxyCacheable() const
Definition: response_headers.h:135
virtual bool WriteAsHttp(Writer *writer, MessageHandler *handler) const
Serialize HTTP response header in HTTP format so it can be re-parsed.
void SetDateAndCaching(int64 date_ms, int64 ttl_ms, const StringPiece &cache_control_suffix)
virtual bool WriteAsBinary(Writer *writer, MessageHandler *message_handler)
Serialize HTTP response header to a binary stream.
bool cache_fields_dirty() const
Definition: response_headers.h:70
ResponseHeaders(const HttpOptions &options)
This constructor with options explicitly set should be used by all callers.
Definition: response_headers.h:43