DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set ts=4 sw=4 sts=4 et cin: */
/* 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 "HttpTransactionChild.h"

#include "mozilla/ipc/IPCStreamUtils.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/net/BackgroundDataBridgeParent.h"
#include "mozilla/net/InputChannelThrottleQueueChild.h"
#include "mozilla/net/SocketProcessChild.h"
#include "mozilla/StaticPrefs_network.h"
#include "nsInputStreamPump.h"
#include "nsHttpHandler.h"
#include "nsProxyInfo.h"
#include "nsProxyRelease.h"
#include "nsQueryObject.h"
#include "nsSerializationHelper.h"

using mozilla::ipc::BackgroundParent;

namespace mozilla {
namespace net {

NS_IMPL_ISUPPORTS(HttpTransactionChild, nsIRequestObserver, nsIStreamListener,
                  nsITransportEventSink, nsIThrottledInputChannel,
                  nsIThreadRetargetableStreamListener);

//-----------------------------------------------------------------------------
// HttpTransactionChild <public>
//-----------------------------------------------------------------------------

HttpTransactionChild::HttpTransactionChild()
    : mCanceled(false), mStatus(NS_OK), mChannelId(0), mIsDocumentLoad(false) {
  LOG(("Creating HttpTransactionChild @%p\n", this));
}

HttpTransactionChild::~HttpTransactionChild() {
  LOG(("Destroying HttpTransactionChild @%p\n", this));
}

static already_AddRefed<nsIRequestContext> CreateRequestContext(
    uint64_t aRequestContextID) {
  if (!aRequestContextID) {
    return nullptr;
  }

  nsIRequestContextService* rcsvc = gHttpHandler->GetRequestContextService();
  if (!rcsvc) {
    return nullptr;
  }

  nsCOMPtr<nsIRequestContext> requestContext;
  rcsvc->GetRequestContext(aRequestContextID, getter_AddRefs(requestContext));

  return requestContext.forget();
}

nsresult HttpTransactionChild::InitInternal(
    uint32_t caps, const HttpConnectionInfoCloneArgs& infoArgs,
    nsHttpRequestHead* requestHead, nsIInputStream* requestBody,
    uint64_t requestContentLength, bool requestBodyHasHeaders,
    uint64_t topLevelOuterContentWindowId, uint8_t httpTrafficCategory,
    uint64_t requestContextID, uint32_t classOfService, uint32_t initialRwin,
    bool responseTimeoutEnabled, uint64_t channelId,
    bool aHasTransactionObserver,
    const Maybe<H2PushedStreamArg>& aPushedStreamArg) {
  LOG(("HttpTransactionChild::InitInternal [this=%p caps=%x]\n", this, caps));

  RefPtr<nsHttpConnectionInfo> cinfo =
      nsHttpConnectionInfo::DeserializeHttpConnectionInfoCloneArgs(infoArgs);
  nsCOMPtr<nsIRequestContext> rc = CreateRequestContext(requestContextID);

  HttpTransactionShell::OnPushCallback pushCallback = nullptr;
  if (caps & NS_HTTP_ONPUSH_LISTENER) {
    RefPtr<HttpTransactionChild> self = this;
    pushCallback = [self](uint32_t aPushedStreamId, const nsACString& aUrl,
                          const nsACString& aRequestString,
                          HttpTransactionShell* aTransaction) {
      bool res = false;
      if (self->CanSend()) {
        res =
            self->SendOnH2PushStream(aPushedStreamId, PromiseFlatCString(aUrl),
                                     PromiseFlatCString(aRequestString));
      }
      return res ? NS_OK : NS_ERROR_FAILURE;
    };
  }

  std::function<void(TransactionObserverResult &&)> observer;
  if (aHasTransactionObserver) {
    nsMainThreadPtrHandle<HttpTransactionChild> handle(
        new nsMainThreadPtrHolder<HttpTransactionChild>(
            "HttpTransactionChildProxy", this, false));
    observer = [handle](TransactionObserverResult&& aResult) {
      handle->mTransactionObserverResult.emplace(std::move(aResult));
    };
  }

  RefPtr<nsHttpTransaction> transWithPushedStream;
  uint32_t pushedStreamId = 0;
  if (aPushedStreamArg) {
    HttpTransactionChild* transChild = static_cast<HttpTransactionChild*>(
        aPushedStreamArg.ref().transWithPushedStreamChild());
    transWithPushedStream = transChild->GetHttpTransaction();
    pushedStreamId = aPushedStreamArg.ref().pushedStreamId();
  }

  nsresult rv = mTransaction->Init(
      caps, cinfo, requestHead, requestBody, requestContentLength,
      requestBodyHasHeaders, GetCurrentThreadEventTarget(),
      nullptr,  // TODO: security callback, fix in bug 1512479.
      this, topLevelOuterContentWindowId,
      static_cast<HttpTrafficCategory>(httpTrafficCategory), rc, classOfService,
      initialRwin, responseTimeoutEnabled, channelId, std::move(observer),
      std::move(pushCallback), transWithPushedStream, pushedStreamId);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    mTransaction = nullptr;
    return rv;
  }

  Unused << mTransaction->AsyncRead(this, getter_AddRefs(mTransactionPump));
  return rv;
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvCancelPump(
    const nsresult& aStatus) {
  LOG(("HttpTransactionChild::RecvCancelPump start [this=%p]\n", this));
  CancelInternal(aStatus);
  return IPC_OK();
}

void HttpTransactionChild::CancelInternal(nsresult aStatus) {
  MOZ_ASSERT(NS_FAILED(aStatus));

  mCanceled = true;
  mStatus = aStatus;
  if (mTransactionPump) {
    mTransactionPump->Cancel(mStatus);
  }
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvSuspendPump() {
  LOG(("HttpTransactionChild::RecvSuspendPump start [this=%p]\n", this));

  if (mTransactionPump) {
    mTransactionPump->Suspend();
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvResumePump() {
  LOG(("HttpTransactionChild::RecvResumePump start [this=%p]\n", this));

  if (mTransactionPump) {
    mTransactionPump->Resume();
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvInit(
    const uint32_t& aCaps, const HttpConnectionInfoCloneArgs& aArgs,
    const nsHttpRequestHead& aReqHeaders, const Maybe<IPCStream>& aRequestBody,
    const uint64_t& aReqContentLength, const bool& aReqBodyIncludesHeaders,
    const uint64_t& aTopLevelOuterContentWindowId,
    const uint8_t& aHttpTrafficCategory, const uint64_t& aRequestContextID,
    const uint32_t& aClassOfService, const uint32_t& aInitialRwin,
    const bool& aResponseTimeoutEnabled, const uint64_t& aChannelId,
    const bool& aHasTransactionObserver,
    const Maybe<H2PushedStreamArg>& aPushedStreamArg,
    const mozilla::Maybe<PInputChannelThrottleQueueChild*>& aThrottleQueue,
    const bool& aIsDocumentLoad) {
  mRequestHead = aReqHeaders;
  if (aRequestBody) {
    mUploadStream = mozilla::ipc::DeserializeIPCStream(aRequestBody);
  }

  mTransaction = new nsHttpTransaction();
  mChannelId = aChannelId;
  mIsDocumentLoad = aIsDocumentLoad;

  if (aThrottleQueue.isSome()) {
    mThrottleQueue =
        static_cast<InputChannelThrottleQueueChild*>(aThrottleQueue.ref());
  }

  nsresult rv = InitInternal(
      aCaps, aArgs, &mRequestHead, mUploadStream, aReqContentLength,
      aReqBodyIncludesHeaders, aTopLevelOuterContentWindowId,
      aHttpTrafficCategory, aRequestContextID, aClassOfService, aInitialRwin,
      aResponseTimeoutEnabled, aChannelId, aHasTransactionObserver,
      aPushedStreamArg);
  if (NS_FAILED(rv)) {
    LOG(("HttpTransactionChild::RecvInit: [this=%p] InitInternal failed!\n",
         this));
    mTransaction = nullptr;
    SendOnInitFailed(rv);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvUpdateClassOfService(
    const uint32_t& classOfService) {
  LOG(("HttpTransactionChild::RecvUpdateClassOfService start [this=%p]\n",
       this));
  if (mTransaction) {
    mTransaction->SetClassOfService(classOfService);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvSetDNSWasRefreshed() {
  LOG(("HttpTransactionChild::SetDNSWasRefreshed [this=%p]\n", this));
  if (mTransaction) {
    mTransaction->SetDNSWasRefreshed();
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvDontReuseConnection() {
  LOG(("HttpTransactionChild::RecvDontReuseConnection [this=%p]\n", this));
  if (mTransaction) {
    mTransaction->DontReuseConnection();
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult HttpTransactionChild::RecvSetH2WSConnRefTaken() {
  LOG(("HttpTransactionChild::RecvSetH2WSConnRefTaken [this=%p]\n", this));
  if (mTransaction) {
    mTransaction->SetH2WSConnRefTaken();
  }
  return IPC_OK();
}

void HttpTransactionChild::ActorDestroy(ActorDestroyReason aWhy) {
  LOG(("HttpTransactionChild::ActorDestroy [this=%p]\n", this));
  mTransaction = nullptr;
  mTransactionPump = nullptr;
}

nsHttpTransaction* HttpTransactionChild::GetHttpTransaction() {
  return mTransaction.get();
}

//-----------------------------------------------------------------------------
// HttpTransactionChild <nsIStreamListener>
//-----------------------------------------------------------------------------

NS_IMETHODIMP
HttpTransactionChild::OnDataAvailable(nsIRequest* aRequest,
                                      nsIInputStream* aInputStream,
                                      uint64_t aOffset, uint32_t aCount) {
  LOG(("HttpTransactionChild::OnDataAvailable [this=%p, aOffset= %" PRIu64
       " aCount=%" PRIu32 "]\n",
       this, aOffset, aCount));

  // Don't bother sending IPC if already canceled.
  if (mCanceled) {
    return mStatus;
  }

  // TODO: send string data in chunks and handle errors. Bug 1600129.
  nsCString data;
  nsresult rv = NS_ReadInputStreamToString(aInputStream, data, aCount);
  if (NS_FAILED(rv)) {
    return rv;
  }

  if (NS_IsMainThread()) {
    if (!CanSend()) {
      return NS_ERROR_FAILURE;
    }

    LOG(("  ODA to parent process"));
    Unused << SendOnDataAvailable(data, aOffset, aCount, false);
    return NS_OK;
  }

  ipc::AssertIsOnBackgroundThread();
  MOZ_ASSERT(mDataBridgeParent);

  if (!mDataBridgeParent->CanSend()) {
    return NS_ERROR_FAILURE;
  }

  bool dataSentToContentProcess =
      mDataBridgeParent->SendOnTransportAndData(aOffset, aCount, data);
  LOG(("  ODA to content process, dataSentToContentProcess=%d",
       dataSentToContentProcess));
  if (!dataSentToContentProcess) {
    MOZ_ASSERT(false, "Send ODA to content process failed");
    return NS_ERROR_FAILURE;
  }

  // We still need to send ODA to parent process, because the data needs to be
  // saved in cache. Note that we set dataSentToChildProcess to true, to this
  // ODA will not be sent to child process.
  RefPtr<HttpTransactionChild> self = this;
  rv = NS_DispatchToMainThread(
      NS_NewRunnableFunction(
          "HttpTransactionChild::OnDataAvailable",
          [self, offset(aOffset), count(aCount), data(data)]() {
            if (!self->SendOnDataAvailable(data, offset, count, true)) {
              self->CancelInternal(NS_ERROR_FAILURE);
            }
          }),
      NS_DISPATCH_NORMAL);
  MOZ_ASSERT(NS_SUCCEEDED(rv));

  return NS_OK;
}

static TimingStructArgs ToTimingStructArgs(TimingStruct aTiming) {
  TimingStructArgs args;
  args.domainLookupStart() = aTiming.domainLookupStart;
  args.domainLookupEnd() = aTiming.domainLookupEnd;
  args.connectStart() = aTiming.connectStart;
  args.tcpConnectEnd() = aTiming.tcpConnectEnd;
  args.secureConnectionStart() = aTiming.secureConnectionStart;
  args.connectEnd() = aTiming.connectEnd;
  args.requestStart() = aTiming.requestStart;
  args.responseStart() = aTiming.responseStart;
  args.responseEnd() = aTiming.responseEnd;
  return args;
}

// The maximum number of bytes to consider when attempting to sniff.
// See https://mimesniff.spec.whatwg.org/#reading-the-resource-header.
static const uint32_t MAX_BYTES_SNIFFED = 1445;

static void GetDataForSniffer(void* aClosure, const uint8_t* aData,
                              uint32_t aCount) {
  nsTArray<uint8_t>* outData = static_cast<nsTArray<uint8_t>*>(aClosure);
  outData->AppendElements(aData, std::min(aCount, MAX_BYTES_SNIFFED));
}

bool HttpTransactionChild::CanSendODAToContentProcessDirectly(
    const Maybe<nsHttpResponseHead>& aHead) {
  if (!StaticPrefs::network_send_ODA_to_content_directly()) {
    return false;
  }

  // If this is a document load, the content process that receives ODA is not
  // decided yet, so don't bother to do the rest check.
  if (mIsDocumentLoad) {
    return false;
  }

  if (!aHead) {
    return false;
  }

  // We only need to deliver ODA when the response is succeed.
  if (aHead->Status() != 200) {
    return false;
  }

  // UnknownDecoder could be used in parent process, so we can't send ODA to
  // content process.
  if (!aHead->HasContentType()) {
    return false;
  }

  return true;
}

NS_IMETHODIMP
HttpTransactionChild::OnStartRequest(nsIRequest* aRequest) {
  LOG(("HttpTransactionChild::OnStartRequest start [this=%p] mTransaction=%p\n",
       this, mTransaction.get()));

  // Don't bother sending IPC to parent process if already canceled.
  if (mCanceled) {
    return mStatus;
  }

  if (!CanSend()) {
    return NS_ERROR_FAILURE;
  }

  MOZ_ASSERT(mTransaction);

  nsresult status;
  aRequest->GetStatus(&status);

  nsCString serializedSecurityInfoOut;
  nsCOMPtr<nsISupports> secInfoSupp = mTransaction->SecurityInfo();
  if (secInfoSupp) {
    nsCOMPtr<nsISerializable> secInfoSer = do_QueryInterface(secInfoSupp);
    if (secInfoSer) {
      NS_SerializeToString(secInfoSer, serializedSecurityInfoOut);
    }
  }

  UniquePtr<nsHttpResponseHead> head(mTransaction->TakeResponseHead());
  Maybe<nsHttpResponseHead> optionalHead;
  nsTArray<uint8_t> dataForSniffer;
  if (head) {
    optionalHead = Some(*head);
    if (mTransaction->Caps() & NS_HTTP_CALL_CONTENT_SNIFFER) {
      nsAutoCString contentTypeOptionsHeader;
      if (!(head->GetContentTypeOptionsHeader(contentTypeOptionsHeader) &&
            contentTypeOptionsHeader.EqualsIgnoreCase("nosniff"))) {
        RefPtr<nsInputStreamPump> pump = do_QueryObject(mTransactionPump);
        pump->PeekStream(GetDataForSniffer, &dataForSniffer);
      }
    }
  }

  if (CanSendODAToContentProcessDirectly(optionalHead)) {
    Maybe<RefPtr<BackgroundDataBridgeParent>> dataBridgeParent =
        SocketProcessChild::GetSingleton()->GetAndRemoveDataBridge(mChannelId);
    // Check if there is a registered BackgroundDataBridgeParent.
    if (dataBridgeParent) {
      mDataBridgeParent = std::move(dataBridgeParent.ref());

      nsCOMPtr<nsIThread> backgroundThread =
          mDataBridgeParent->GetBackgroundThread();
      nsCOMPtr<nsIThreadRetargetableRequest> retargetableTransactionPump;
      retargetableTransactionPump = do_QueryObject(mTransactionPump);
      // nsInputStreamPump should implement this interface.
      MOZ_ASSERT(retargetableTransactionPump);

      nsresult rv =
          retargetableTransactionPump->RetargetDeliveryTo(backgroundThread);
      LOG((" Retarget to background thread [this=%p rv=%08x]\n", this,
           static_cast<uint32_t>(rv)));
      if (NS_FAILED(rv)) {
        mDataBridgeParent->Destroy();
        mDataBridgeParent = nullptr;
      }
    }
  }

  int32_t proxyConnectResponseCode =
      mTransaction->GetProxyConnectResponseCode();

  Unused << SendOnStartRequest(status, optionalHead, serializedSecurityInfoOut,
                               mTransaction->ProxyConnectFailed(),
                               ToTimingStructArgs(mTransaction->Timings()),
                               proxyConnectResponseCode, dataForSniffer);
  return NS_OK;
}

NS_IMETHODIMP
HttpTransactionChild::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
  LOG(("HttpTransactionChild::OnStopRequest [this=%p]\n", this));

  if (mDataBridgeParent) {
    mDataBridgeParent->Destroy();
    mDataBridgeParent = nullptr;
  }

  // Don't bother sending IPC to parent process if already canceled.
  if (mCanceled) {
    return mStatus;
  }

  if (!CanSend()) {
    return NS_ERROR_FAILURE;
  }

  MOZ_ASSERT(mTransaction);

  UniquePtr<nsHttpHeaderArray> headerArray(
      mTransaction->TakeResponseTrailers());
  Maybe<nsHttpHeaderArray> responseTrailers;
  if (headerArray) {
    responseTrailers.emplace(*headerArray);
  }

  int64_t requestSize = mTransaction->GetRequestSize();

  Unused << SendOnStopRequest(aStatus, mTransaction->ResponseIsComplete(),
                              mTransaction->GetTransferSize(),
                              ToTimingStructArgs(mTransaction->Timings()),
                              responseTrailers,
                              mTransaction->HasStickyConnection(),
                              mTransactionObserverResult, requestSize);

  return NS_OK;
}

//-----------------------------------------------------------------------------
// HttpTransactionChild <nsITransportEventSink>
//-----------------------------------------------------------------------------

NS_IMETHODIMP
HttpTransactionChild::OnTransportStatus(nsITransport* aTransport,
                                        nsresult aStatus, int64_t aProgress,
                                        int64_t aProgressMax) {
  LOG(("HttpTransactionChild::OnTransportStatus [this=%p status=%" PRIx32
       " progress=%" PRId64 "]\n",
       this, static_cast<uint32_t>(aStatus), aProgress));

  if (!CanSend()) {
    return NS_OK;
  }

  if (aStatus == NS_NET_STATUS_CONNECTED_TO ||
      aStatus == NS_NET_STATUS_WAITING_FOR) {
    NetAddr selfAddr;
    NetAddr peerAddr;
    bool isTrr = false;
    if (mTransaction) {
      mTransaction->GetNetworkAddresses(selfAddr, peerAddr, isTrr);
    } else {
      nsCOMPtr<nsISocketTransport> socketTransport =
          do_QueryInterface(aTransport);
      if (socketTransport) {
        socketTransport->GetSelfAddr(&selfAddr);
        socketTransport->GetPeerAddr(&peerAddr);
        socketTransport->ResolvedByTRR(&isTrr);
      }
    }
    Unused << SendOnNetAddrUpdate(selfAddr, peerAddr, isTrr);
  }

  Unused << SendOnTransportStatus(aStatus, aProgress, aProgressMax);
  return NS_OK;
}

//-----------------------------------------------------------------------------
// HttpBaseChannel::nsIThrottledInputChannel
//-----------------------------------------------------------------------------

NS_IMETHODIMP
HttpTransactionChild::SetThrottleQueue(nsIInputChannelThrottleQueue* aQueue) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
HttpTransactionChild::GetThrottleQueue(nsIInputChannelThrottleQueue** aQueue) {
  nsCOMPtr<nsIInputChannelThrottleQueue> queue =
      static_cast<nsIInputChannelThrottleQueue*>(mThrottleQueue.get());
  queue.forget(aQueue);
  return NS_OK;
}

//-----------------------------------------------------------------------------
// EventSourceImpl::nsIThreadRetargetableStreamListener
//-----------------------------------------------------------------------------
NS_IMETHODIMP
HttpTransactionChild::CheckListenerChain() {
  MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread!");
  return NS_OK;
}

}  // namespace net
}  // namespace mozilla