Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/http/nsHttpResponseHead.h
Line
Count
Source (jump to first uncovered line)
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/. */
5
6
#ifndef nsHttpResponseHead_h__
7
#define nsHttpResponseHead_h__
8
9
#include "nsHttpHeaderArray.h"
10
#include "nsHttp.h"
11
#include "nsString.h"
12
#include "mozilla/RecursiveMutex.h"
13
14
class nsIHttpHeaderVisitor;
15
16
// This needs to be forward declared here so we can include only this header
17
// without also including PHttpChannelParams.h
18
namespace IPC {
19
    template <typename> struct ParamTraits;
20
} // namespace IPC
21
22
namespace mozilla { namespace net {
23
24
//-----------------------------------------------------------------------------
25
// nsHttpResponseHead represents the status line and headers from an HTTP
26
// response.
27
//-----------------------------------------------------------------------------
28
29
class nsHttpResponseHead
30
{
31
public:
32
    nsHttpResponseHead() : mVersion(HttpVersion::v1_1)
33
                         , mStatus(200)
34
                         , mContentLength(-1)
35
                         , mCacheControlPrivate(false)
36
                         , mCacheControlNoStore(false)
37
                         , mCacheControlNoCache(false)
38
                         , mCacheControlImmutable(false)
39
                         , mPragmaNoCache(false)
40
                         , mRecursiveMutex("nsHttpResponseHead.mRecursiveMutex")
41
0
                         , mInVisitHeaders(false) {}
42
43
    nsHttpResponseHead(const nsHttpResponseHead &aOther);
44
    nsHttpResponseHead &operator=(const nsHttpResponseHead &aOther);
45
46
    void Enter() { mRecursiveMutex.Lock(); }
47
    void Exit() { mRecursiveMutex.Unlock(); }
48
49
    HttpVersion Version();
50
// X11's Xlib.h #defines 'Status' to 'int' on some systems!
51
#undef Status
52
    uint16_t Status();
53
    void StatusText(nsACString &aStatusText);
54
    int64_t ContentLength();
55
    void ContentType(nsACString &aContentType);
56
    void ContentCharset(nsACString &aContentCharset);
57
    bool Private();
58
    bool NoStore();
59
    bool NoCache();
60
    bool Immutable();
61
    /**
62
     * Full length of the entity. For byte-range requests, this may be larger
63
     * than ContentLength(), which will only represent the requested part of the
64
     * entity.
65
     */
66
    int64_t TotalEntitySize();
67
68
    MOZ_MUST_USE nsresult SetHeader(const nsACString &h, const nsACString &v,
69
                                    bool m=false);
70
    MOZ_MUST_USE nsresult SetHeader(nsHttpAtom h, const nsACString &v,
71
                                    bool m=false);
72
    MOZ_MUST_USE nsresult GetHeader(nsHttpAtom h, nsACString &v);
73
    void ClearHeader(nsHttpAtom h);
74
    void ClearHeaders();
75
    bool HasHeaderValue(nsHttpAtom h, const char *v);
76
    bool HasHeader(nsHttpAtom h);
77
78
    void SetContentType(const nsACString &s);
79
    void SetContentCharset(const nsACString &s);
80
    void SetContentLength(int64_t);
81
82
    // write out the response status line and headers as a single text block,
83
    // optionally pruning out transient headers (ie. headers that only make
84
    // sense the first time the response is handled).
85
    // Both functions append to the string supplied string.
86
    void Flatten(nsACString &, bool pruneTransients);
87
    void FlattenNetworkOriginalHeaders(nsACString &buf);
88
89
    // The next 2 functions parse flattened response head and original net headers.
90
    // They are used when we are reading an entry from the cache.
91
    //
92
    // To keep proper order of the original headers we MUST call
93
    // ParseCachedOriginalHeaders FIRST and then ParseCachedHead.
94
    //
95
    // block must be null terminated.
96
    MOZ_MUST_USE nsresult ParseCachedHead(const char *block);
97
    MOZ_MUST_USE nsresult ParseCachedOriginalHeaders(char *block);
98
99
    // parse the status line.
100
    void ParseStatusLine(const nsACString &line);
101
102
    // parse a header line.
103
    MOZ_MUST_USE nsresult ParseHeaderLine(const nsACString &line);
104
105
    // cache validation support methods
106
    MOZ_MUST_USE nsresult ComputeFreshnessLifetime(uint32_t *);
107
    MOZ_MUST_USE nsresult ComputeCurrentAge(uint32_t now, uint32_t requestTime,
108
                                            uint32_t *result);
109
    bool MustValidate();
110
    bool MustValidateIfExpired();
111
112
    // returns true if the server appears to support byte range requests.
113
    bool IsResumable();
114
115
    // returns true if the Expires header has a value in the past relative to the
116
    // value of the Date header.
117
    bool ExpiresInPast();
118
119
    // update headers...
120
    MOZ_MUST_USE nsresult UpdateHeaders(nsHttpResponseHead *headers);
121
122
    // reset the response head to it's initial state
123
    void Reset();
124
125
    MOZ_MUST_USE nsresult GetAgeValue(uint32_t *result);
126
    MOZ_MUST_USE nsresult GetMaxAgeValue(uint32_t *result);
127
    MOZ_MUST_USE nsresult GetDateValue(uint32_t *result);
128
    MOZ_MUST_USE nsresult GetExpiresValue(uint32_t *result);
129
    MOZ_MUST_USE nsresult GetLastModifiedValue(uint32_t *result);
130
131
    bool operator==(const nsHttpResponseHead& aOther) const;
132
133
    // Using this function it is possible to itereate through all headers
134
    // automatically under one lock.
135
    MOZ_MUST_USE nsresult VisitHeaders(nsIHttpHeaderVisitor *visitor,
136
                                       nsHttpHeaderArray::VisitorFilter filter);
137
    MOZ_MUST_USE nsresult GetOriginalHeader(nsHttpAtom aHeader,
138
                                            nsIHttpHeaderVisitor *aVisitor);
139
140
    bool HasContentType();
141
    bool HasContentCharset();
142
private:
143
    MOZ_MUST_USE nsresult SetHeader_locked(nsHttpAtom atom, const nsACString &h,
144
                                           const nsACString &v, bool m=false);
145
    void AssignDefaultStatusText();
146
    void ParseVersion(const char *);
147
    void ParseCacheControl(const char *);
148
    void ParsePragma(const char *);
149
150
    void ParseStatusLine_locked(const nsACString &line);
151
    MOZ_MUST_USE nsresult ParseHeaderLine_locked(const nsACString &line,
152
                                                 bool originalFromNetHeaders);
153
154
    // these return failure if the header does not exist.
155
    MOZ_MUST_USE nsresult ParseDateHeader(nsHttpAtom header,
156
                                          uint32_t *result) const;
157
158
    bool ExpiresInPast_locked() const;
159
    MOZ_MUST_USE nsresult GetAgeValue_locked(uint32_t *result) const;
160
    MOZ_MUST_USE nsresult GetExpiresValue_locked(uint32_t *result) const;
161
    MOZ_MUST_USE nsresult GetMaxAgeValue_locked(uint32_t *result) const;
162
163
    MOZ_MUST_USE nsresult GetDateValue_locked(uint32_t *result) const
164
0
    {
165
0
        return ParseDateHeader(nsHttp::Date, result);
166
0
    }
167
168
    MOZ_MUST_USE nsresult GetLastModifiedValue_locked(uint32_t *result) const
169
0
    {
170
0
        return ParseDateHeader(nsHttp::Last_Modified, result);
171
0
    }
172
173
private:
174
    // All members must be copy-constructable and assignable
175
    nsHttpHeaderArray mHeaders;
176
    HttpVersion       mVersion;
177
    uint16_t          mStatus;
178
    nsCString         mStatusText;
179
    int64_t           mContentLength;
180
    nsCString         mContentType;
181
    nsCString         mContentCharset;
182
    bool              mCacheControlPrivate;
183
    bool              mCacheControlNoStore;
184
    bool              mCacheControlNoCache;
185
    bool              mCacheControlImmutable;
186
    bool              mPragmaNoCache;
187
188
    // We are using RecursiveMutex instead of a Mutex because VisitHeader
189
    // function calls nsIHttpHeaderVisitor::VisitHeader while under lock.
190
    RecursiveMutex  mRecursiveMutex;
191
    // During VisitHeader we sould not allow cal to SetHeader.
192
    bool              mInVisitHeaders;
193
194
    friend struct IPC::ParamTraits<nsHttpResponseHead>;
195
};
196
197
} // namespace net
198
} // namespace mozilla
199
200
#endif // nsHttpResponseHead_h__