summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/PendingTransactionInfo.cpp
blob: 448d8757e4113f2e3db565068f65e07feeeee207 (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
/* vim:t ts=4 sw=2 sts=2 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 "PendingTransactionInfo.h"
#include "HalfOpenSocket.h"

// Log on level :5, instead of default :4.
#undef LOG
#define LOG(args) LOG5(args)
#undef LOG_ENABLED
#define LOG_ENABLED() LOG5_ENABLED()

namespace mozilla {
namespace net {

PendingTransactionInfo::~PendingTransactionInfo() {
  if (mHalfOpen) {
    RefPtr<HalfOpenSocket> halfOpen = do_QueryReferent(mHalfOpen);
    LOG(
        ("PendingTransactionInfo::PendingTransactionInfo "
         "[trans=%p halfOpen=%p]",
         mTransaction.get(), halfOpen.get()));
    if (halfOpen) {
      halfOpen->Unclaim();
    }
    mHalfOpen = nullptr;
  } else if (mActiveConn) {
    RefPtr<HttpConnectionBase> activeConn = do_QueryReferent(mActiveConn);
    if (activeConn && activeConn->Transaction() &&
        activeConn->Transaction()->IsNullTransaction()) {
      NullHttpTransaction* nullTrans =
          activeConn->Transaction()->QueryNullTransaction();
      nullTrans->Unclaim();
      LOG((
          "PendingTransactionInfo::PendingTransactionInfo - mark %p unclaimed.",
          activeConn.get()));
    }
  }
}

bool PendingTransactionInfo::IsAlreadyClaimedInitializingConn() {
  LOG(
      ("PendingTransactionInfo::IsAlreadyClaimedInitializingConn "
       "[trans=%p, halfOpen=%p, activeConn=%p]\n",
       mTransaction.get(), mHalfOpen.get(), mActiveConn.get()));

  // When this transaction has already established a half-open
  // connection, we want to prevent any duplicate half-open
  // connections from being established and bound to this
  // transaction. Allow only use of an idle persistent connection
  // (if found) for transactions referred by a half-open connection.
  bool alreadyHalfOpenOrWaitingForTLS = false;
  if (mHalfOpen) {
    MOZ_ASSERT(!mActiveConn);
    RefPtr<HalfOpenSocket> halfOpen = do_QueryReferent(mHalfOpen);
    LOG(
        ("PendingTransactionInfo::IsAlreadyClaimedInitializingConn "
         "[trans=%p, halfOpen=%p]\n",
         mTransaction.get(), halfOpen.get()));
    if (halfOpen) {
      alreadyHalfOpenOrWaitingForTLS = true;
    } else {
      // If we have not found the halfOpen socket, remove the pointer.
      mHalfOpen = nullptr;
    }
  } else if (mActiveConn) {
    MOZ_ASSERT(!mHalfOpen);
    RefPtr<HttpConnectionBase> activeConn = do_QueryReferent(mActiveConn);
    LOG(
        ("PendingTransactionInfo::IsAlreadyClaimedInitializingConn "
         "[trans=%p, activeConn=%p]\n",
         mTransaction.get(), activeConn.get()));
    // Check if this transaction claimed a connection that is still
    // performing tls handshake with a NullHttpTransaction or it is between
    // finishing tls and reclaiming (When nullTrans finishes tls handshake,
    // httpConnection does not have a transaction any more and a
    // ReclaimConnection is dispatched). But if an error occurred the
    // connection will be closed, it will exist but CanReused will be
    // false.
    if (activeConn &&
        ((activeConn->Transaction() &&
          activeConn->Transaction()->IsNullTransaction()) ||
         (!activeConn->Transaction() && activeConn->CanReuse()))) {
      alreadyHalfOpenOrWaitingForTLS = true;
    } else {
      // If we have not found the connection, remove the pointer.
      mActiveConn = nullptr;
    }
  }

  return alreadyHalfOpenOrWaitingForTLS;
}

void PendingTransactionInfo::AbandonHalfOpenAndForgetActiveConn() {
  // Abandon all half-open sockets belonging to the given transaction.
  RefPtr<HalfOpenSocket> half = do_QueryReferent(mHalfOpen);
  if (half) {
    half->Abandon();
  }
  mHalfOpen = nullptr;
  mActiveConn = nullptr;
}

bool PendingTransactionInfo::TryClaimingHalfOpen(HalfOpenSocket* sock) {
  if (sock->Claim()) {
    mHalfOpen =
        do_GetWeakReference(static_cast<nsISupportsWeakReference*>(sock));
    return true;
  }
  return false;
}

bool PendingTransactionInfo::TryClaimingActiveConn(HttpConnectionBase* conn) {
  nsAHttpTransaction* activeTrans = conn->Transaction();
  NullHttpTransaction* nullTrans =
      activeTrans ? activeTrans->QueryNullTransaction() : nullptr;
  if (nullTrans && nullTrans->Claim()) {
    mActiveConn =
        do_GetWeakReference(static_cast<nsISupportsWeakReference*>(conn));
    return true;
  }
  return false;
}

void PendingTransactionInfo::AddHalfOpen(HalfOpenSocket* sock) {
  mHalfOpen = do_GetWeakReference(static_cast<nsISupportsWeakReference*>(sock));
}

}  // namespace net
}  // namespace mozilla