summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/Http2Push.h
blob: b7e25c2028bc53fc5377f4c1b5eae6af670d78e3 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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_net_Http2Push_Internal_h
#define mozilla_net_Http2Push_Internal_h

// HTTP/2 - RFC 7540
// https://www.rfc-editor.org/rfc/rfc7540.txt

#include "Http2Session.h"
#include "Http2StreamBase.h"

#include "mozilla/Attributes.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/UniquePtr.h"
#include "nsHttpRequestHead.h"
#include "nsIRequestContext.h"
#include "nsString.h"
#include "PSpdyPush.h"

namespace mozilla {
namespace net {

class Http2PushTransactionBuffer;

class Http2PushedStream final : public Http2StreamBase {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Http2PushedStream, override)

  Http2PushedStream(Http2PushTransactionBuffer* aTransaction,
                    Http2Session* aSession, Http2StreamBase* aAssociatedStream,
                    uint32_t aID,
                    uint64_t aCurrentForegroundTabOuterContentWindowId);

  Http2PushedStream* GetHttp2PushedStream() override { return this; }
  bool GetPushComplete();

  // The consumer stream is the synthetic pull stream hooked up to this push
  Http2StreamBase* GetConsumerStream() { return mConsumerStream; };

  void SetConsumerStream(Http2StreamBase* consumer);
  [[nodiscard]] bool GetHashKey(nsCString& key);

  // override of Http2StreamBase
  [[nodiscard]] nsresult ReadSegments(nsAHttpSegmentReader*, uint32_t,
                                      uint32_t*) override;
  [[nodiscard]] nsresult WriteSegments(nsAHttpSegmentWriter*, uint32_t,
                                       uint32_t*) override;
  void AdjustInitialWindow() override;

  nsAHttpTransaction* Transaction() override { return mTransaction; }
  nsIRequestContext* RequestContext() override { return mRequestContext; };
  void ConnectPushedStream(Http2StreamBase* stream);

  [[nodiscard]] bool TryOnPush();
  [[nodiscard]] static bool TestOnPush(Http2StreamBase* stream);

  virtual bool DeferCleanup(nsresult status) override;
  void SetDeferCleanupOnSuccess(bool val) { mDeferCleanupOnSuccess = val; }

  bool IsOrphaned(TimeStamp now);
  void OnPushFailed() {
    mDeferCleanupOnPush = false;
    mOnPushFailed = true;
  }

  [[nodiscard]] nsresult GetBufferedData(char* buf, uint32_t count,
                                         uint32_t* countWritten);

  // overload of Http2StreamBase
  virtual bool HasSink() override { return !!mConsumerStream; }
  void SetPushComplete() { mPushCompleted = true; }
  virtual void CurrentBrowserIdChanged(uint64_t) override;

  nsCString& GetRequestString() { return mRequestString; }
  nsCString& GetResourceUrl() { return mResourceUrl; }

  nsresult ConvertPushHeaders(Http2Decompressor* decompressor,
                              nsACString& aHeadersIn, nsACString& aHeadersOut);

  void CloseStream(nsresult reason) override;

 protected:
  nsresult CallToReadData(uint32_t count, uint32_t* countRead) override;
  nsresult CallToWriteData(uint32_t count, uint32_t* countWritten) override;
  nsresult GenerateHeaders(nsCString& aCompressedData,
                           uint8_t& firstFrameFlags) override;

 private:
  virtual ~Http2PushedStream() = default;
  // paired request stream that consumes from real http/2 one.. null until a
  // match is made.
  Http2StreamBase* mConsumerStream{nullptr};

  nsCOMPtr<nsIRequestContext> mRequestContext;

  nsAHttpTransaction* mAssociatedTransaction;

  Http2PushTransactionBuffer* mBufferedPush;
  mozilla::TimeStamp mLastRead;

  nsCString mHashKey;
  nsresult mStatus{NS_OK};
  bool mPushCompleted{false};  // server push FIN received
  bool mDeferCleanupOnSuccess{true};

  // mDeferCleanupOnPush prevents Http2Session::CleanupStream() from
  // destroying the push stream on an error code during the period between
  // when we need to do OnPush() on another thread and the time it takes
  // for that event to create a synthetic pull stream attached to this
  // object. That synthetic pull will become mConsuemerStream.
  // Ths is essentially a delete protecting reference.
  bool mDeferCleanupOnPush{false};
  bool mOnPushFailed{false};
  nsCString mRequestString;
  nsCString mResourceUrl;

  uint32_t mDefaultPriorityDependency;

  // The underlying HTTP transaction. This pointer is used as the key
  // in the Http2Session mStreamTransactionHash so it is important to
  // keep a reference to it as long as this stream is a member of that hash.
  // (i.e. don't change it or release it after it is set in the ctor).
  RefPtr<nsAHttpTransaction> const mTransaction;
};

class Http2PushTransactionBuffer final : public nsAHttpTransaction {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSAHTTPTRANSACTION

  Http2PushTransactionBuffer();

  [[nodiscard]] nsresult GetBufferedData(char* buf, uint32_t count,
                                         uint32_t* countWritten);
  void SetPushStream(Http2PushedStream* stream) { mPushStream = stream; }

 private:
  virtual ~Http2PushTransactionBuffer();
  uint64_t Available();

  const static uint32_t kDefaultBufferSize = 4096;

  nsresult mStatus{NS_OK};
  nsHttpRequestHead* mRequestHead{nullptr};
  Http2PushedStream* mPushStream{nullptr};
  bool mIsDone{false};

  UniquePtr<char[]> mBufferedHTTP1;
  uint32_t mBufferedHTTP1Size{kDefaultBufferSize};
  uint32_t mBufferedHTTP1Used{0};
  uint32_t mBufferedHTTP1Consumed{0};
};

class Http2PushedStreamWrapper : public nsISupports {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  bool DispatchRelease();

  explicit Http2PushedStreamWrapper(Http2PushedStream* aPushStream);

  nsCString& GetRequestString() { return mRequestString; }
  nsCString& GetResourceUrl() { return mResourceUrl; }
  Http2PushedStream* GetStream();
  void OnPushFailed();
  uint32_t StreamID() { return mStreamID; }

 private:
  virtual ~Http2PushedStreamWrapper();

  nsCString mRequestString;
  nsCString mResourceUrl;
  uint32_t mStreamID;
  WeakPtr<Http2StreamBase> mStream;
};

}  // namespace net
}  // namespace mozilla

#endif  // mozilla_net_Http2Push_Internal_h