netwerk/protocol/http/nsHttpResponseHead.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial