Page Speed Optimization Libraries
1.3.25.1
|
00001 // Copyright 2011 Google Inc. 00014 00016 00017 #ifndef NET_INSTAWEB_HTTP_PUBLIC_RESPONSE_HEADERS_H_ 00018 #define NET_INSTAWEB_HTTP_PUBLIC_RESPONSE_HEADERS_H_ 00019 00020 #include "net/instaweb/http/public/content_type.h" 00021 #include "net/instaweb/http/public/headers.h" 00022 #include "net/instaweb/http/public/meta_data.h" 00023 #include "net/instaweb/util/public/basictypes.h" 00024 #include "net/instaweb/util/public/string.h" 00025 #include "net/instaweb/util/public/string_util.h" 00026 #include "net/instaweb/util/public/timer.h" 00027 00028 namespace net_instaweb { 00029 00030 class HttpResponseHeaders; 00031 class RequestHeaders; 00032 class MessageHandler; 00033 class Writer; 00034 00036 class ResponseHeaders : public Headers<HttpResponseHeaders> { 00037 public: 00041 static const int64 kImplicitCacheTtlMs = 5 * Timer::kMinuteMs; 00042 00043 ResponseHeaders(); 00044 virtual ~ResponseHeaders(); 00045 00050 void FixDateHeaders(int64 now_ms); 00051 00052 virtual void Clear(); 00053 00054 void CopyFrom(const ResponseHeaders& other); 00055 00057 virtual void Add(const StringPiece& name, const StringPiece& value); 00058 00061 bool MergeContentType(const StringPiece& content_type); 00062 00064 virtual bool Remove(const StringPiece& name, const StringPiece& value); 00065 00067 virtual bool RemoveAll(const StringPiece& name); 00068 00070 virtual bool RemoveAllFromSet(const StringSetInsensitive& names); 00071 00074 virtual void Replace(const StringPiece& name, const StringPiece& value); 00075 00081 virtual void UpdateFrom(const Headers<HttpResponseHeaders>& other); 00082 00085 void UpdateFromProto(const HttpResponseHeaders& proto); 00086 00088 virtual bool WriteAsBinary(Writer* writer, MessageHandler* message_handler); 00089 00093 virtual bool ReadFromBinary(const StringPiece& buf, MessageHandler* handler); 00094 00096 virtual bool WriteAsHttp(Writer* writer, MessageHandler* handler) const; 00097 00102 void ComputeCaching(); 00103 bool IsCacheable() const; 00104 00111 bool IsProxyCacheable() const; 00112 00115 bool IsProxyCacheableGivenRequest(const RequestHeaders& req_headers) const; 00116 00120 int64 CacheExpirationTimeMs() const; 00121 00126 void SetDateAndCaching(int64 date_ms, int64 ttl_ms, 00127 const StringPiece& cache_control_suffix); 00128 void SetDateAndCaching(int64 date_ms, int64 ttl_ms) { 00129 SetDateAndCaching(date_ms, ttl_ms, ""); 00130 } 00134 GoogleString CacheControlValuesToPreserve(); 00135 00137 void SetTimeHeader(const StringPiece& header, int64 time_ms); 00138 void SetDate(int64 date_ms) { SetTimeHeader(HttpAttributes::kDate, date_ms); } 00139 void SetLastModified(int64 last_modified_ms) { 00140 SetTimeHeader(HttpAttributes::kLastModified, last_modified_ms); 00141 } 00142 00146 void SetCacheControlMaxAge(int64 ttl_ms); 00147 00150 void SetOriginalContentLength(int64 content_length); 00151 00153 bool Sanitize(); 00154 00157 void GetSanitizedProto(HttpResponseHeaders* proto) const; 00158 00160 bool headers_complete() const { return has_status_code(); } 00161 00162 int status_code() const; 00163 bool has_status_code() const; 00164 void set_status_code(const int code); 00165 const char* reason_phrase() const; 00166 void set_reason_phrase(const StringPiece& reason_phrase); 00167 int64 implicit_cache_ttl_ms() const { return implicit_cache_ttl_ms_; } 00168 void set_implicit_cache_ttl_ms(const int64 ttl) { 00169 implicit_cache_ttl_ms_ = ttl; 00170 } 00171 00172 int64 last_modified_time_ms() const; 00173 int64 date_ms() const; 00174 bool has_date_ms() const; 00175 int64 cache_ttl_ms() const; 00176 bool is_implicitly_cacheable() const; 00177 00178 GoogleString ToString() const; 00179 00181 void SetStatusAndReason(HttpStatus::Code code); 00182 00183 void DebugPrint() const; 00184 00186 static bool ParseTime(const char* time_str, int64* time_ms); 00187 00189 inline bool IsErrorStatus() { 00190 int status = status_code(); 00191 return status >= 400 && status <= 599; 00192 } 00193 00195 inline bool IsServerErrorStatus() { 00196 int status = status_code(); 00197 return status >= 500 && status <= 599; 00198 } 00199 00201 bool IsGzipped() const; 00202 bool WasGzippedLast() const; 00203 00206 const ContentType* DetermineContentType() const; 00207 00209 bool IsHtmlLike() const { 00210 const ContentType* type = DetermineContentType(); 00211 return (type != NULL && type->IsHtmlLike()); 00212 } 00213 00215 GoogleString DetermineCharset() const; 00216 00221 void DetermineContentTypeAndCharset(const ContentType** content_type_out, 00222 GoogleString* charset_out) const; 00223 00224 00228 bool ParseDateHeader(const StringPiece& attr, int64* date_ms) const; 00229 00232 bool IsDateLaterThan(int64 time_ms) const { 00233 return date_ms() > time_ms; 00234 } 00235 00237 void ParseFirstLine(const StringPiece& first_line); 00238 00240 void ParseFirstLineHelper(const StringPiece& first_line); 00241 00243 void set_first_line(int major_version, int minor_version, int status_code, 00244 const StringPiece& reason_phrase) { 00245 set_major_version(major_version); 00246 set_minor_version(minor_version); 00247 set_status_code(status_code); 00248 set_reason_phrase(reason_phrase); 00249 } 00250 00254 bool VaryCacheable(bool request_has_cookie) const; 00255 00258 bool FindContentLength(int64* content_length) const; 00259 00268 void ForceCaching(int64 ttl_ms); 00269 00271 bool UpdateCacheHeadersIfForceCached(); 00272 00277 int64 SizeEstimate() const; 00278 00282 bool GetCookieString(GoogleString* cookie_str) const; 00283 00292 bool HasCookie(StringPiece name, StringPieceVector* values) const; 00293 00294 private: 00300 bool CombineContentTypes(const StringPiece& orig, const StringPiece& fresh); 00301 00302 friend class ResponseHeadersTest; 00303 bool cache_fields_dirty_; 00304 00307 int64 implicit_cache_ttl_ms_; 00308 00311 int64 force_cache_ttl_ms_; 00313 bool force_cached_; 00314 00315 DISALLOW_COPY_AND_ASSIGN(ResponseHeaders); 00316 }; 00317 00318 } 00319 00320 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_RESPONSE_HEADERS_H_