summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/NullHttpTransaction.cpp
blob: bb32a8098729837dbf871299f77d0fa94ad3f66a (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et 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/. */

// HttpLog.h should generally be included first
#include "HttpLog.h"

#include "nsHttp.h"
#include "NullHttpTransaction.h"
#include "nsHttpHandler.h"
#include "nsHttpRequestHead.h"
#include "nsIHttpActivityObserver.h"
#include "NullHttpChannel.h"
#include "nsQueryObject.h"
#include "nsNetUtil.h"

namespace mozilla {
namespace net {

class CallObserveActivity final : public nsIRunnable
{
  ~CallObserveActivity()
  {
  }
public:
  NS_DECL_THREADSAFE_ISUPPORTS
  CallObserveActivity(nsIHttpActivityObserver *aActivityDistributor,
                      const nsCString &aHost,
                      int32_t aPort,
                      bool aEndToEndSSL,
                      uint32_t aActivityType,
                      uint32_t aActivitySubtype,
                      PRTime aTimestamp,
                      uint64_t aExtraSizeData,
                      const nsACString &aExtraStringData)
    : mActivityDistributor(aActivityDistributor)
    , mHost(aHost)
    , mPort(aPort)
    , mEndToEndSSL(aEndToEndSSL)
    , mActivityType(aActivityType)
    , mActivitySubtype(aActivitySubtype)
    , mTimestamp(aTimestamp)
    , mExtraSizeData(aExtraSizeData)
    , mExtraStringData(aExtraStringData)
  {
  }
  NS_IMETHOD Run() override
  {
    MOZ_ASSERT(NS_IsMainThread());
    nsCOMPtr<nsIURI> uri;
    nsAutoCString port(NS_LITERAL_CSTRING(""));
    if (mPort != -1 && ((mEndToEndSSL && mPort != 443) ||
        (!mEndToEndSSL && mPort != 80))) {
      port.AppendInt(mPort);
    }

    nsresult rv = NS_NewURI(getter_AddRefs(uri),
                            (mEndToEndSSL ? NS_LITERAL_CSTRING("https://")
                            : NS_LITERAL_CSTRING("http://") ) + mHost + port);
    if (NS_FAILED(rv)) {
      return NS_OK;
    }

    RefPtr<NullHttpChannel> channel = new NullHttpChannel();
    channel->Init(uri, 0, nullptr, 0, nullptr);
    mActivityDistributor->ObserveActivity(
      nsCOMPtr<nsISupports>(do_QueryObject(channel)),
      mActivityType,
      mActivitySubtype,
      mTimestamp,
      mExtraSizeData,
      mExtraStringData);

    return NS_OK;
  }
private:
  nsCOMPtr<nsIHttpActivityObserver> mActivityDistributor;
  nsCString mHost;
  int32_t mPort;
  bool mEndToEndSSL;
  uint32_t mActivityType;
  uint32_t mActivitySubtype;
  PRTime mTimestamp;
  uint64_t mExtraSizeData;
  nsCString mExtraStringData;
};

NS_IMPL_ISUPPORTS(CallObserveActivity, nsIRunnable)

NS_IMPL_ISUPPORTS(NullHttpTransaction, NullHttpTransaction, nsISupportsWeakReference)

NullHttpTransaction::NullHttpTransaction(nsHttpConnectionInfo *ci,
                                         nsIInterfaceRequestor *callbacks,
                                         uint32_t caps)
  : mStatus(NS_OK)
  , mCaps(caps | NS_HTTP_ALLOW_KEEPALIVE)
  , mRequestHead(nullptr)
  , mCapsToClear(0)
  , mIsDone(false)
  , mClaimed(false)
  , mCallbacks(callbacks)
  , mConnectionInfo(ci)
{
  nsresult rv;
  mActivityDistributor = do_GetService(NS_HTTPACTIVITYDISTRIBUTOR_CONTRACTID,
                                       &rv);
  if (NS_FAILED(rv)) {
    return;
  }

  bool activityDistributorActive;
  rv = mActivityDistributor->GetIsActive(&activityDistributorActive);
  if (NS_SUCCEEDED(rv) && activityDistributorActive) {
    // There are some observers registered at activity distributor.
    LOG(("NulHttpTransaction::NullHttpTransaction() "
         "mActivityDistributor is active "
         "[this=%p, %s]", this, ci->GetOrigin().get()));
  } else {
    // There is no observer, so don't use it.
    mActivityDistributor = nullptr;
  }
}

NullHttpTransaction::~NullHttpTransaction()
{
  mCallbacks = nullptr;
  delete mRequestHead;
}

bool
NullHttpTransaction::Claim()
{
  if (mClaimed) {
    return false;
  }
  mClaimed = true;
  return true;
}

void
NullHttpTransaction::SetConnection(nsAHttpConnection *conn)
{
  mConnection = conn;
}

nsAHttpConnection *
NullHttpTransaction::Connection()
{
  return mConnection.get();
}

void
NullHttpTransaction::GetSecurityCallbacks(nsIInterfaceRequestor **outCB)
{
  nsCOMPtr<nsIInterfaceRequestor> copyCB(mCallbacks);
  *outCB = copyCB.forget().take();
}

void
NullHttpTransaction::OnTransportStatus(nsITransport* transport,
                                       nsresult status, int64_t progress)
{
  if (status == NS_NET_STATUS_RESOLVING_HOST) {
    if (mTimings.domainLookupStart.IsNull()) {
      mTimings.domainLookupStart = TimeStamp::Now();
    }
  } else if (status == NS_NET_STATUS_RESOLVED_HOST) {
    if (mTimings.domainLookupEnd.IsNull()) {
      mTimings.domainLookupEnd = TimeStamp::Now();
    }
  } else if (status == NS_NET_STATUS_CONNECTING_TO) {
    if (mTimings.connectStart.IsNull()) {
      mTimings.connectStart = TimeStamp::Now();
    }
  } else if (status == NS_NET_STATUS_CONNECTED_TO) {
    if (mTimings.connectEnd.IsNull()) {
      mTimings.connectEnd = TimeStamp::Now();
    }
  } else if (status == NS_NET_STATUS_TLS_HANDSHAKE_ENDED) {
    if (mTimings.secureConnectionStart.IsNull() &&
        !mTimings.connectEnd.IsNull()) {
      mTimings.secureConnectionStart = mTimings.connectEnd;
    }
    mTimings.connectEnd = TimeStamp::Now();;
  }

  if (mActivityDistributor) {
    NS_DispatchToMainThread(new CallObserveActivity(mActivityDistributor,
                                  mConnectionInfo->GetOrigin(),
                                  mConnectionInfo->OriginPort(),
                                  mConnectionInfo->EndToEndSSL(),
                                  NS_HTTP_ACTIVITY_TYPE_SOCKET_TRANSPORT,
                                  static_cast<uint32_t>(status),
                                  PR_Now(),
                                  progress,
                                  EmptyCString()));
  }
}

bool
NullHttpTransaction::IsDone()
{
  return mIsDone;
}

nsresult
NullHttpTransaction::Status()
{
  return mStatus;
}

uint32_t
NullHttpTransaction::Caps()
{
  return mCaps & ~mCapsToClear;
}

void
NullHttpTransaction::SetDNSWasRefreshed()
{
  MOZ_ASSERT(NS_IsMainThread(), "SetDNSWasRefreshed on main thread only!");
  mCapsToClear |= NS_HTTP_REFRESH_DNS;
}

uint64_t
NullHttpTransaction::Available()
{
  return 0;
}

nsresult
NullHttpTransaction::ReadSegments(nsAHttpSegmentReader *reader,
                                  uint32_t count, uint32_t *countRead)
{
  *countRead = 0;
  mIsDone = true;
  return NS_BASE_STREAM_CLOSED;
}

nsresult
NullHttpTransaction::WriteSegments(nsAHttpSegmentWriter *writer,
                                   uint32_t count, uint32_t *countWritten)
{
  *countWritten = 0;
  return NS_BASE_STREAM_CLOSED;
}

uint32_t
NullHttpTransaction::Http1xTransactionCount()
{
  return 0;
}

nsHttpRequestHead *
NullHttpTransaction::RequestHead()
{
  // We suport a requesthead at all so that a CONNECT tunnel transaction
  // can obtain a Host header from it, but we lazy-popualate that header.

  if (!mRequestHead) {
    mRequestHead = new nsHttpRequestHead();

    nsAutoCString hostHeader;
    nsCString host(mConnectionInfo->GetOrigin());
    nsresult rv = nsHttpHandler::GenerateHostPort(host,
                                                  mConnectionInfo->OriginPort(),
                                                  hostHeader);
    if (NS_SUCCEEDED(rv)) {
      mRequestHead->SetHeader(nsHttp::Host, hostHeader);
      if (mActivityDistributor) {
        // Report request headers.
        nsCString reqHeaderBuf;
        mRequestHead->Flatten(reqHeaderBuf, false);
        NS_DispatchToMainThread(new CallObserveActivity(mActivityDistributor,
                                  mConnectionInfo->GetOrigin(),
                                  mConnectionInfo->OriginPort(),
                                  mConnectionInfo->EndToEndSSL(),
                                  NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
                                  NS_HTTP_ACTIVITY_SUBTYPE_REQUEST_HEADER,
                                  PR_Now(), 0, reqHeaderBuf));
    }
  }

    // CONNECT tunnels may also want Proxy-Authorization but that is a lot
    // harder to determine, so for now we will let those connections fail in
    // the NullHttpTransaction and let them be retried from the pending queue
    // with a bound transaction
  }

  return mRequestHead;
}

nsresult
NullHttpTransaction::TakeSubTransactions(
  nsTArray<RefPtr<nsAHttpTransaction> > &outTransactions)
{
  return NS_ERROR_NOT_IMPLEMENTED;
}

void
NullHttpTransaction::SetProxyConnectFailed()
{
}

void
NullHttpTransaction::Close(nsresult reason)
{
  mStatus = reason;
  mConnection = nullptr;
  mIsDone = true;
  if (mActivityDistributor) {
    // Report that this transaction is closing.
    NS_DispatchToMainThread(new CallObserveActivity(mActivityDistributor,
                                  mConnectionInfo->GetOrigin(),
                                  mConnectionInfo->OriginPort(),
                                  mConnectionInfo->EndToEndSSL(),
                                  NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
                                  NS_HTTP_ACTIVITY_SUBTYPE_TRANSACTION_CLOSE,
                                  PR_Now(), 0, EmptyCString()));
  }
}

nsHttpConnectionInfo *
NullHttpTransaction::ConnectionInfo()
{
  return mConnectionInfo;
}

nsresult
NullHttpTransaction::AddTransaction(nsAHttpTransaction *trans)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

uint32_t
NullHttpTransaction::PipelineDepth()
{
  return 0;
}

nsresult
NullHttpTransaction::SetPipelinePosition(int32_t position)
{
    return NS_OK;
}

int32_t
NullHttpTransaction::PipelinePosition()
{
  return 1;
}

} // namespace net
} // namespace mozilla