Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsHttpResponseHead_h__
7 #define nsHttpResponseHead_h__
9 #include "nsHttpHeaderArray.h"
10 #include "nsHttp.h"
11 #include "nsString.h"
13 namespace mozilla { namespace net {
15 //-----------------------------------------------------------------------------
16 // nsHttpResponseHead represents the status line and headers from an HTTP
17 // response.
18 //-----------------------------------------------------------------------------
20 class nsHttpResponseHead
21 {
22 public:
23 nsHttpResponseHead() : mVersion(NS_HTTP_VERSION_1_1)
24 , mStatus(200)
25 , mContentLength(UINT64_MAX)
26 , mCacheControlNoStore(false)
27 , mCacheControlNoCache(false)
28 , mPragmaNoCache(false) {}
30 const nsHttpHeaderArray & Headers() const { return mHeaders; }
31 nsHttpHeaderArray &Headers() { return mHeaders; }
32 nsHttpVersion Version() const { return mVersion; }
33 // X11's Xlib.h #defines 'Status' to 'int' on some systems!
34 #undef Status
35 uint16_t Status() const { return mStatus; }
36 const nsAFlatCString &StatusText() const { return mStatusText; }
37 int64_t ContentLength() const { return mContentLength; }
38 const nsAFlatCString &ContentType() const { return mContentType; }
39 const nsAFlatCString &ContentCharset() const { return mContentCharset; }
40 bool NoStore() const { return mCacheControlNoStore; }
41 bool NoCache() const { return (mCacheControlNoCache || mPragmaNoCache); }
42 /**
43 * Full length of the entity. For byte-range requests, this may be larger
44 * than ContentLength(), which will only represent the requested part of the
45 * entity.
46 */
47 int64_t TotalEntitySize() const;
49 const char *PeekHeader(nsHttpAtom h) const { return mHeaders.PeekHeader(h); }
50 nsresult SetHeader(nsHttpAtom h, const nsACString &v, bool m=false);
51 nsresult GetHeader(nsHttpAtom h, nsACString &v) const { return mHeaders.GetHeader(h, v); }
52 void ClearHeader(nsHttpAtom h) { mHeaders.ClearHeader(h); }
53 void ClearHeaders() { mHeaders.Clear(); }
55 const char *FindHeaderValue(nsHttpAtom h, const char *v) const
56 {
57 return mHeaders.FindHeaderValue(h, v);
58 }
59 bool HasHeaderValue(nsHttpAtom h, const char *v) const
60 {
61 return mHeaders.HasHeaderValue(h, v);
62 }
64 void SetContentType(const nsACString &s) { mContentType = s; }
65 void SetContentCharset(const nsACString &s) { mContentCharset = s; }
66 void SetContentLength(int64_t);
68 // write out the response status line and headers as a single text block,
69 // optionally pruning out transient headers (ie. headers that only make
70 // sense the first time the response is handled).
71 void Flatten(nsACString &, bool pruneTransients);
73 // parse flattened response head. block must be null terminated. parsing is
74 // destructive.
75 nsresult Parse(char *block);
77 // parse the status line. line must be null terminated.
78 void ParseStatusLine(const char *line);
80 // parse a header line. line must be null terminated. parsing is destructive.
81 nsresult ParseHeaderLine(const char *line);
83 // cache validation support methods
84 nsresult ComputeFreshnessLifetime(uint32_t *) const;
85 nsresult ComputeCurrentAge(uint32_t now, uint32_t requestTime, uint32_t *result) const;
86 bool MustValidate() const;
87 bool MustValidateIfExpired() const;
89 // returns true if the server appears to support byte range requests.
90 bool IsResumable() const;
92 // returns true if the Expires header has a value in the past relative to the
93 // value of the Date header.
94 bool ExpiresInPast() const;
96 // update headers...
97 nsresult UpdateHeaders(const nsHttpHeaderArray &headers);
99 // reset the response head to it's initial state
100 void Reset();
102 // these return failure if the header does not exist.
103 nsresult ParseDateHeader(nsHttpAtom header, uint32_t *result) const;
104 nsresult GetAgeValue(uint32_t *result) const;
105 nsresult GetMaxAgeValue(uint32_t *result) const;
106 nsresult GetDateValue(uint32_t *result) const
107 {
108 return ParseDateHeader(nsHttp::Date, result);
109 }
110 nsresult GetExpiresValue(uint32_t *result) const ;
111 nsresult GetLastModifiedValue(uint32_t *result) const
112 {
113 return ParseDateHeader(nsHttp::Last_Modified, result);
114 }
116 private:
117 void AssignDefaultStatusText();
118 void ParseVersion(const char *);
119 void ParseCacheControl(const char *);
120 void ParsePragma(const char *);
122 private:
123 // All members must be copy-constructable and assignable
124 nsHttpHeaderArray mHeaders;
125 nsHttpVersion mVersion;
126 uint16_t mStatus;
127 nsCString mStatusText;
128 int64_t mContentLength;
129 nsCString mContentType;
130 nsCString mContentCharset;
131 bool mCacheControlNoStore;
132 bool mCacheControlNoCache;
133 bool mPragmaNoCache;
135 friend struct IPC::ParamTraits<nsHttpResponseHead>;
136 };
137 }} // namespace mozilla::net
139 #endif // nsHttpResponseHead_h__