Page Speed Optimization Libraries
1.6.29.3
|
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 00049 static bool IsImminentlyExpiring( 00050 int64 start_date_ms, int64 expire_ms, int64 now_ms); 00051 00056 void FixDateHeaders(int64 now_ms); 00057 00058 virtual void Clear(); 00059 00060 void CopyFrom(const ResponseHeaders& other); 00061 00063 virtual void Add(const StringPiece& name, const StringPiece& value); 00064 00067 bool MergeContentType(const StringPiece& content_type); 00068 00070 virtual bool Remove(const StringPiece& name, const StringPiece& value); 00071 00073 virtual bool RemoveAll(const StringPiece& name); 00074 00076 virtual bool RemoveAllFromSet(const StringSetInsensitive& names); 00077 00080 virtual void Replace(const StringPiece& name, const StringPiece& value); 00081 00087 virtual void UpdateFrom(const Headers<HttpResponseHeaders>& other); 00088 00091 void UpdateFromProto(const HttpResponseHeaders& proto); 00092 00094 virtual bool WriteAsBinary(Writer* writer, MessageHandler* message_handler); 00095 00099 virtual bool ReadFromBinary(const StringPiece& buf, MessageHandler* handler); 00100 00102 virtual bool WriteAsHttp(Writer* writer, MessageHandler* handler) const; 00103 00108 void ComputeCaching(); 00109 00116 bool IsProxyCacheable() const; 00117 00120 bool IsProxyCacheableGivenRequest(const RequestHeaders& req_headers) const; 00121 00125 bool IsBrowserCacheable() const; 00126 00132 bool VaryCacheable(bool request_has_cookie) const; 00133 00137 int64 CacheExpirationTimeMs() const; 00138 00143 void SetDateAndCaching(int64 date_ms, int64 ttl_ms, 00144 const StringPiece& cache_control_suffix); 00145 void SetDateAndCaching(int64 date_ms, int64 ttl_ms) { 00146 SetDateAndCaching(date_ms, ttl_ms, ""); 00147 } 00151 GoogleString CacheControlValuesToPreserve(); 00152 00154 void SetTimeHeader(const StringPiece& header, int64 time_ms); 00155 void SetDate(int64 date_ms) { SetTimeHeader(HttpAttributes::kDate, date_ms); } 00156 void SetLastModified(int64 last_modified_ms) { 00157 SetTimeHeader(HttpAttributes::kLastModified, last_modified_ms); 00158 } 00159 00163 void SetCacheControlMaxAge(int64 ttl_ms); 00164 00167 void SetOriginalContentLength(int64 content_length); 00168 00170 bool Sanitize(); 00171 00174 void GetSanitizedProto(HttpResponseHeaders* proto) const; 00175 00177 bool headers_complete() const { return has_status_code(); } 00178 00179 int status_code() const; 00180 bool has_status_code() const; 00181 void set_status_code(const int code); 00182 const char* reason_phrase() const; 00183 void set_reason_phrase(const StringPiece& reason_phrase); 00184 int64 implicit_cache_ttl_ms() const { return implicit_cache_ttl_ms_; } 00185 void set_implicit_cache_ttl_ms(const int64 ttl) { 00186 implicit_cache_ttl_ms_ = ttl; 00187 } 00188 00189 int64 last_modified_time_ms() const; 00190 int64 date_ms() const; 00191 bool has_date_ms() const; 00192 int64 cache_ttl_ms() const; 00193 bool is_implicitly_cacheable() const; 00194 00195 GoogleString ToString() const; 00196 00198 void SetStatusAndReason(HttpStatus::Code code); 00199 00200 void DebugPrint() const; 00201 00203 static bool ParseTime(const char* time_str, int64* time_ms); 00204 00206 inline bool IsErrorStatus() { 00207 int status = status_code(); 00208 return status >= 400 && status <= 599; 00209 } 00210 00212 inline bool IsServerErrorStatus() { 00213 int status = status_code(); 00214 return status >= 500 && status <= 599; 00215 } 00216 00218 bool IsGzipped() const; 00219 bool WasGzippedLast() const; 00220 00223 const ContentType* DetermineContentType() const; 00224 00226 bool IsHtmlLike() const { 00227 const ContentType* type = DetermineContentType(); 00228 return (type != NULL && type->IsHtmlLike()); 00229 } 00230 00232 GoogleString DetermineCharset() const; 00233 00238 void DetermineContentTypeAndCharset(const ContentType** content_type_out, 00239 GoogleString* charset_out) const; 00240 00241 00245 bool ParseDateHeader(const StringPiece& attr, int64* date_ms) const; 00246 00249 bool IsDateLaterThan(int64 time_ms) const { 00250 return date_ms() > time_ms; 00251 } 00252 00254 void ParseFirstLine(const StringPiece& first_line); 00255 00257 void ParseFirstLineHelper(const StringPiece& first_line); 00258 00260 void set_first_line(int major_version, int minor_version, int status_code, 00261 const StringPiece& reason_phrase) { 00262 set_major_version(major_version); 00263 set_minor_version(minor_version); 00264 set_status_code(status_code); 00265 set_reason_phrase(reason_phrase); 00266 } 00267 00270 bool FindContentLength(int64* content_length) const; 00271 00280 void ForceCaching(int64 ttl_ms); 00281 00283 bool UpdateCacheHeadersIfForceCached(); 00284 00289 int64 SizeEstimate() const; 00290 00294 bool GetCookieString(GoogleString* cookie_str) const; 00295 00304 bool HasCookie(StringPiece name, StringPieceVector* values) const; 00305 00306 private: 00312 bool CombineContentTypes(const StringPiece& orig, const StringPiece& fresh); 00313 00314 friend class ResponseHeadersTest; 00315 bool cache_fields_dirty_; 00316 00319 int64 implicit_cache_ttl_ms_; 00320 00323 int64 force_cache_ttl_ms_; 00325 bool force_cached_; 00326 00327 DISALLOW_COPY_AND_ASSIGN(ResponseHeaders); 00328 }; 00329 00330 } 00331 00332 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_RESPONSE_HEADERS_H_