summaryrefslogtreecommitdiffstats
path: root/dom/fetch/InternalResponse.h
blob: f256d3ec5bcde0edd6237a5323a9331d9134497d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_InternalResponse_h
#define mozilla_dom_InternalResponse_h

#include "nsIInputStream.h"
#include "nsICacheInfoChannel.h"
#include "nsISupportsImpl.h"
#include "nsProxyRelease.h"

#include "mozilla/dom/InternalHeaders.h"
#include "mozilla/dom/RequestBinding.h"
#include "mozilla/dom/ResponseBinding.h"
#include "mozilla/dom/ChannelInfo.h"
#include "mozilla/UniquePtr.h"

namespace mozilla {
namespace ipc {
class AutoIPCStream;
class PBackgroundChild;
class PrincipalInfo;
}  // namespace ipc

namespace dom {

class IPCInternalResponse;
class InternalHeaders;

class InternalResponse final {
  friend class FetchDriver;

 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InternalResponse)

  InternalResponse(
      uint16_t aStatus, const nsACString& aStatusText,
      RequestCredentials aCredentialsMode = RequestCredentials::Omit);

  static RefPtr<InternalResponse> FromIPC(
      const IPCInternalResponse& aIPCResponse);

  // Note: the AutoIPCStreams must outlive the IPCInternalResponse.
  void ToIPC(
      IPCInternalResponse* aIPCResponse,
      mozilla::ipc::PBackgroundChild* aManager,
      UniquePtr<mozilla::ipc::AutoIPCStream>& aAutoBodyStream,
      UniquePtr<mozilla::ipc::AutoIPCStream>& aAutoAlternativeBodyStream);

  enum CloneType {
    eCloneInputStream,
    eDontCloneInputStream,
  };

  already_AddRefed<InternalResponse> Clone(CloneType eCloneType);

  static already_AddRefed<InternalResponse> NetworkError(nsresult aRv) {
    MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(aRv));
    RefPtr<InternalResponse> response = new InternalResponse(0, ""_ns);
    ErrorResult result;
    response->Headers()->SetGuard(HeadersGuardEnum::Immutable, result);
    MOZ_ASSERT(!result.Failed());
    response->mType = ResponseType::Error;
    response->mErrorCode = aRv;
    return response.forget();
  }

  already_AddRefed<InternalResponse> OpaqueResponse();

  already_AddRefed<InternalResponse> OpaqueRedirectResponse();

  already_AddRefed<InternalResponse> BasicResponse();

  already_AddRefed<InternalResponse> CORSResponse();

  ResponseType Type() const {
    MOZ_ASSERT_IF(mType == ResponseType::Error, !mWrappedResponse);
    MOZ_ASSERT_IF(mType == ResponseType::Default, !mWrappedResponse);
    MOZ_ASSERT_IF(mType == ResponseType::Basic, mWrappedResponse);
    MOZ_ASSERT_IF(mType == ResponseType::Cors, mWrappedResponse);
    MOZ_ASSERT_IF(mType == ResponseType::Opaque, mWrappedResponse);
    MOZ_ASSERT_IF(mType == ResponseType::Opaqueredirect, mWrappedResponse);
    return mType;
  }

  bool IsError() const { return Type() == ResponseType::Error; }
  // GetUrl should return last fetch URL in response's url list and null if
  // response's url list is the empty list.
  const nsCString& GetURL() const {
    // Empty urlList when response is a synthetic response.
    if (mURLList.IsEmpty()) {
      return EmptyCString();
    }
    return mURLList.LastElement();
  }
  void GetURLList(nsTArray<nsCString>& aURLList) const {
    aURLList.Assign(mURLList);
  }
  const nsCString& GetUnfilteredURL() const {
    if (mWrappedResponse) {
      return mWrappedResponse->GetURL();
    }
    return GetURL();
  }
  void GetUnfilteredURLList(nsTArray<nsCString>& aURLList) const {
    if (mWrappedResponse) {
      return mWrappedResponse->GetURLList(aURLList);
    }

    return GetURLList(aURLList);
  }

  nsTArray<nsCString> GetUnfilteredURLList() const {
    nsTArray<nsCString> list;
    GetUnfilteredURLList(list);
    return list;
  }

  void SetURLList(const nsTArray<nsCString>& aURLList) {
    mURLList.Assign(aURLList);

#ifdef DEBUG
    for (uint32_t i = 0; i < mURLList.Length(); ++i) {
      MOZ_ASSERT(mURLList[i].Find("#"_ns) == kNotFound);
    }
#endif
  }

  uint16_t GetStatus() const { return mStatus; }

  uint16_t GetUnfilteredStatus() const {
    if (mWrappedResponse) {
      return mWrappedResponse->GetStatus();
    }

    return GetStatus();
  }

  const nsCString& GetStatusText() const { return mStatusText; }

  const nsCString& GetUnfilteredStatusText() const {
    if (mWrappedResponse) {
      return mWrappedResponse->GetStatusText();
    }

    return GetStatusText();
  }

  InternalHeaders* Headers() { return mHeaders; }

  InternalHeaders* UnfilteredHeaders() {
    if (mWrappedResponse) {
      return mWrappedResponse->Headers();
    };

    return Headers();
  }

  void GetUnfilteredBody(nsIInputStream** aStream,
                         int64_t* aBodySize = nullptr) {
    if (mWrappedResponse) {
      MOZ_ASSERT(!mBody);
      return mWrappedResponse->GetBody(aStream, aBodySize);
    }
    nsCOMPtr<nsIInputStream> stream = mBody;
    stream.forget(aStream);
    if (aBodySize) {
      *aBodySize = mBodySize;
    }
  }

  void GetBody(nsIInputStream** aStream, int64_t* aBodySize = nullptr) {
    if (Type() == ResponseType::Opaque ||
        Type() == ResponseType::Opaqueredirect) {
      *aStream = nullptr;
      if (aBodySize) {
        *aBodySize = UNKNOWN_BODY_SIZE;
      }
      return;
    }

    GetUnfilteredBody(aStream, aBodySize);
  }

  void SetBodyBlobURISpec(nsACString& aBlobURISpec) {
    mBodyBlobURISpec = aBlobURISpec;
  }

  const nsACString& BodyBlobURISpec() const {
    if (mWrappedResponse) {
      return mWrappedResponse->BodyBlobURISpec();
    }
    return mBodyBlobURISpec;
  }

  void SetBodyLocalPath(nsAString& aLocalPath) { mBodyLocalPath = aLocalPath; }

  const nsAString& BodyLocalPath() const {
    if (mWrappedResponse) {
      return mWrappedResponse->BodyLocalPath();
    }
    return mBodyLocalPath;
  }

  void SetBody(nsIInputStream* aBody, int64_t aBodySize) {
    if (mWrappedResponse) {
      return mWrappedResponse->SetBody(aBody, aBodySize);
    }
    // A request's body may not be reset once set.
    MOZ_ASSERT(!mBody);
    MOZ_ASSERT(mBodySize == UNKNOWN_BODY_SIZE);
    // Check arguments.
    MOZ_ASSERT(aBodySize == UNKNOWN_BODY_SIZE || aBodySize >= 0);
    // If body is not given, then size must be unknown.
    MOZ_ASSERT_IF(!aBody, aBodySize == UNKNOWN_BODY_SIZE);

    mBody = aBody;
    mBodySize = aBodySize;
  }

  uint32_t GetPaddingInfo();

  nsresult GeneratePaddingInfo();

  int64_t GetPaddingSize();

  void SetPaddingSize(int64_t aPaddingSize);

  void SetAlternativeDataType(const nsACString& aAltDataType) {
    if (mWrappedResponse) {
      return mWrappedResponse->SetAlternativeDataType(aAltDataType);
    }

    MOZ_DIAGNOSTIC_ASSERT(mAlternativeDataType.IsEmpty());

    mAlternativeDataType.Assign(aAltDataType);
  }

  const nsCString& GetAlternativeDataType() {
    if (mWrappedResponse) {
      return mWrappedResponse->GetAlternativeDataType();
    }

    return mAlternativeDataType;
  }

  void SetAlternativeBody(nsIInputStream* aAlternativeBody) {
    if (mWrappedResponse) {
      return mWrappedResponse->SetAlternativeBody(aAlternativeBody);
    }
    // A request's body may not be reset once set.
    MOZ_DIAGNOSTIC_ASSERT(!mAlternativeBody);

    mAlternativeBody = aAlternativeBody;
  }

  already_AddRefed<nsIInputStream> TakeAlternativeBody() {
    if (mWrappedResponse) {
      return mWrappedResponse->TakeAlternativeBody();
    }

    if (!mAlternativeBody) {
      return nullptr;
    }

    // cleanup the non-alternative body here.
    // Once alternative data is used, the real body is no need anymore.
    mBody = nullptr;
    mBodySize = UNKNOWN_BODY_SIZE;
    return mAlternativeBody.forget();
  }

  void SetCacheInfoChannel(
      const nsMainThreadPtrHandle<nsICacheInfoChannel>& aCacheInfoChannel) {
    if (mWrappedResponse) {
      return mWrappedResponse->SetCacheInfoChannel(aCacheInfoChannel);
    }
    MOZ_ASSERT(!mCacheInfoChannel);
    mCacheInfoChannel = aCacheInfoChannel;
  }

  nsMainThreadPtrHandle<nsICacheInfoChannel> TakeCacheInfoChannel() {
    if (mWrappedResponse) {
      return mWrappedResponse->TakeCacheInfoChannel();
    }
    nsMainThreadPtrHandle<nsICacheInfoChannel> rtn = mCacheInfoChannel;
    mCacheInfoChannel = nullptr;
    return rtn;
  }

  bool HasCacheInfoChannel() const {
    if (mWrappedResponse) {
      return !!mWrappedResponse->HasCacheInfoChannel();
    }
    return !!mCacheInfoChannel;
  }

  void InitChannelInfo(nsIChannel* aChannel) {
    mChannelInfo.InitFromChannel(aChannel);
  }

  void InitChannelInfo(const mozilla::ipc::IPCChannelInfo& aChannelInfo) {
    mChannelInfo.InitFromIPCChannelInfo(aChannelInfo);
  }

  void InitChannelInfo(const ChannelInfo& aChannelInfo) {
    mChannelInfo = aChannelInfo;
  }

  const ChannelInfo& GetChannelInfo() const { return mChannelInfo; }

  const UniquePtr<mozilla::ipc::PrincipalInfo>& GetPrincipalInfo() const {
    return mPrincipalInfo;
  }

  bool IsRedirected() const { return mURLList.Length() > 1; }

  nsresult GetErrorCode() const { return mErrorCode; }

  // Takes ownership of the principal info.
  void SetPrincipalInfo(UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo);

  LoadTainting GetTainting() const;

  already_AddRefed<InternalResponse> Unfiltered();

 private:
  ~InternalResponse();

  explicit InternalResponse(const InternalResponse& aOther) = delete;
  InternalResponse& operator=(const InternalResponse&) = delete;

  // Returns an instance of InternalResponse which is a copy of this
  // InternalResponse, except headers, body and wrapped response (if any) which
  // are left uninitialized. Used for cloning and filtering.
  already_AddRefed<InternalResponse> CreateIncompleteCopy();

  ResponseType mType;
  // A response has an associated url list (a list of zero or more fetch URLs).
  // Unless stated otherwise, it is the empty list. The current url is the last
  // element in mURLlist
  nsTArray<nsCString> mURLList;
  const uint16_t mStatus;
  const nsCString mStatusText;
  RefPtr<InternalHeaders> mHeaders;
  nsCOMPtr<nsIInputStream> mBody;
  nsCString mBodyBlobURISpec;
  nsString mBodyLocalPath;
  int64_t mBodySize;
  // It's used to passed to the CacheResponse to generate padding size. Once, we
  // generate the padding size for resposne, we don't need it anymore.
  Maybe<uint32_t> mPaddingInfo;
  int64_t mPaddingSize;
  nsresult mErrorCode;
  RequestCredentials mCredentialsMode;

  // For alternative data such as JS Bytecode cached in the HTTP cache.
  nsCString mAlternativeDataType;
  nsCOMPtr<nsIInputStream> mAlternativeBody;
  nsMainThreadPtrHandle<nsICacheInfoChannel> mCacheInfoChannel;

 public:
  static const int64_t UNKNOWN_BODY_SIZE = -1;
  static const int64_t UNKNOWN_PADDING_SIZE = -1;

 private:
  ChannelInfo mChannelInfo;
  UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;

  // For filtered responses.
  // Cache, and SW interception should always serialize/access the underlying
  // unfiltered headers and when deserializing, create an InternalResponse
  // with the unfiltered headers followed by wrapping it.
  RefPtr<InternalResponse> mWrappedResponse;
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_InternalResponse_h