michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsHttpResponseHead_h__ michael@0: #define nsHttpResponseHead_h__ michael@0: michael@0: #include "nsHttpHeaderArray.h" michael@0: #include "nsHttp.h" michael@0: #include "nsString.h" michael@0: michael@0: namespace mozilla { namespace net { michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpResponseHead represents the status line and headers from an HTTP michael@0: // response. michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class nsHttpResponseHead michael@0: { michael@0: public: michael@0: nsHttpResponseHead() : mVersion(NS_HTTP_VERSION_1_1) michael@0: , mStatus(200) michael@0: , mContentLength(UINT64_MAX) michael@0: , mCacheControlNoStore(false) michael@0: , mCacheControlNoCache(false) michael@0: , mPragmaNoCache(false) {} michael@0: michael@0: const nsHttpHeaderArray & Headers() const { return mHeaders; } michael@0: nsHttpHeaderArray &Headers() { return mHeaders; } michael@0: nsHttpVersion Version() const { return mVersion; } michael@0: // X11's Xlib.h #defines 'Status' to 'int' on some systems! michael@0: #undef Status michael@0: uint16_t Status() const { return mStatus; } michael@0: const nsAFlatCString &StatusText() const { return mStatusText; } michael@0: int64_t ContentLength() const { return mContentLength; } michael@0: const nsAFlatCString &ContentType() const { return mContentType; } michael@0: const nsAFlatCString &ContentCharset() const { return mContentCharset; } michael@0: bool NoStore() const { return mCacheControlNoStore; } michael@0: bool NoCache() const { return (mCacheControlNoCache || mPragmaNoCache); } michael@0: /** michael@0: * Full length of the entity. For byte-range requests, this may be larger michael@0: * than ContentLength(), which will only represent the requested part of the michael@0: * entity. michael@0: */ michael@0: int64_t TotalEntitySize() const; michael@0: michael@0: const char *PeekHeader(nsHttpAtom h) const { return mHeaders.PeekHeader(h); } michael@0: nsresult SetHeader(nsHttpAtom h, const nsACString &v, bool m=false); michael@0: nsresult GetHeader(nsHttpAtom h, nsACString &v) const { return mHeaders.GetHeader(h, v); } michael@0: void ClearHeader(nsHttpAtom h) { mHeaders.ClearHeader(h); } michael@0: void ClearHeaders() { mHeaders.Clear(); } michael@0: michael@0: const char *FindHeaderValue(nsHttpAtom h, const char *v) const michael@0: { michael@0: return mHeaders.FindHeaderValue(h, v); michael@0: } michael@0: bool HasHeaderValue(nsHttpAtom h, const char *v) const michael@0: { michael@0: return mHeaders.HasHeaderValue(h, v); michael@0: } michael@0: michael@0: void SetContentType(const nsACString &s) { mContentType = s; } michael@0: void SetContentCharset(const nsACString &s) { mContentCharset = s; } michael@0: void SetContentLength(int64_t); michael@0: michael@0: // write out the response status line and headers as a single text block, michael@0: // optionally pruning out transient headers (ie. headers that only make michael@0: // sense the first time the response is handled). michael@0: void Flatten(nsACString &, bool pruneTransients); michael@0: michael@0: // parse flattened response head. block must be null terminated. parsing is michael@0: // destructive. michael@0: nsresult Parse(char *block); michael@0: michael@0: // parse the status line. line must be null terminated. michael@0: void ParseStatusLine(const char *line); michael@0: michael@0: // parse a header line. line must be null terminated. parsing is destructive. michael@0: nsresult ParseHeaderLine(const char *line); michael@0: michael@0: // cache validation support methods michael@0: nsresult ComputeFreshnessLifetime(uint32_t *) const; michael@0: nsresult ComputeCurrentAge(uint32_t now, uint32_t requestTime, uint32_t *result) const; michael@0: bool MustValidate() const; michael@0: bool MustValidateIfExpired() const; michael@0: michael@0: // returns true if the server appears to support byte range requests. michael@0: bool IsResumable() const; michael@0: michael@0: // returns true if the Expires header has a value in the past relative to the michael@0: // value of the Date header. michael@0: bool ExpiresInPast() const; michael@0: michael@0: // update headers... michael@0: nsresult UpdateHeaders(const nsHttpHeaderArray &headers); michael@0: michael@0: // reset the response head to it's initial state michael@0: void Reset(); michael@0: michael@0: // these return failure if the header does not exist. michael@0: nsresult ParseDateHeader(nsHttpAtom header, uint32_t *result) const; michael@0: nsresult GetAgeValue(uint32_t *result) const; michael@0: nsresult GetMaxAgeValue(uint32_t *result) const; michael@0: nsresult GetDateValue(uint32_t *result) const michael@0: { michael@0: return ParseDateHeader(nsHttp::Date, result); michael@0: } michael@0: nsresult GetExpiresValue(uint32_t *result) const ; michael@0: nsresult GetLastModifiedValue(uint32_t *result) const michael@0: { michael@0: return ParseDateHeader(nsHttp::Last_Modified, result); michael@0: } michael@0: michael@0: private: michael@0: void AssignDefaultStatusText(); michael@0: void ParseVersion(const char *); michael@0: void ParseCacheControl(const char *); michael@0: void ParsePragma(const char *); michael@0: michael@0: private: michael@0: // All members must be copy-constructable and assignable michael@0: nsHttpHeaderArray mHeaders; michael@0: nsHttpVersion mVersion; michael@0: uint16_t mStatus; michael@0: nsCString mStatusText; michael@0: int64_t mContentLength; michael@0: nsCString mContentType; michael@0: nsCString mContentCharset; michael@0: bool mCacheControlNoStore; michael@0: bool mCacheControlNoCache; michael@0: bool mPragmaNoCache; michael@0: michael@0: friend struct IPC::ParamTraits; michael@0: }; michael@0: }} // namespace mozilla::net michael@0: michael@0: #endif // nsHttpResponseHead_h__