Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsIOService.h" |
michael@0 | 7 | #include "nsFtpControlConnection.h" |
michael@0 | 8 | #include "nsFtpProtocolHandler.h" |
michael@0 | 9 | #include "prlog.h" |
michael@0 | 10 | #include "nsIInputStream.h" |
michael@0 | 11 | #include "nsISocketTransportService.h" |
michael@0 | 12 | #include "nsISocketTransport.h" |
michael@0 | 13 | #include "nsThreadUtils.h" |
michael@0 | 14 | #include "nsIOutputStream.h" |
michael@0 | 15 | #include "nsNetCID.h" |
michael@0 | 16 | #include <algorithm> |
michael@0 | 17 | |
michael@0 | 18 | #if defined(PR_LOGGING) |
michael@0 | 19 | extern PRLogModuleInfo* gFTPLog; |
michael@0 | 20 | #endif |
michael@0 | 21 | #define LOG(args) PR_LOG(gFTPLog, PR_LOG_DEBUG, args) |
michael@0 | 22 | #define LOG_ALWAYS(args) PR_LOG(gFTPLog, PR_LOG_ALWAYS, args) |
michael@0 | 23 | |
michael@0 | 24 | // |
michael@0 | 25 | // nsFtpControlConnection implementation ... |
michael@0 | 26 | // |
michael@0 | 27 | |
michael@0 | 28 | NS_IMPL_ISUPPORTS(nsFtpControlConnection, nsIInputStreamCallback) |
michael@0 | 29 | |
michael@0 | 30 | NS_IMETHODIMP |
michael@0 | 31 | nsFtpControlConnection::OnInputStreamReady(nsIAsyncInputStream *stream) |
michael@0 | 32 | { |
michael@0 | 33 | char data[4096]; |
michael@0 | 34 | |
michael@0 | 35 | // Consume data whether we have a listener or not. |
michael@0 | 36 | uint64_t avail64; |
michael@0 | 37 | uint32_t avail = 0; |
michael@0 | 38 | nsresult rv = stream->Available(&avail64); |
michael@0 | 39 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 40 | avail = (uint32_t)std::min(avail64, (uint64_t)sizeof(data)); |
michael@0 | 41 | |
michael@0 | 42 | uint32_t n; |
michael@0 | 43 | rv = stream->Read(data, avail, &n); |
michael@0 | 44 | if (NS_SUCCEEDED(rv)) |
michael@0 | 45 | avail = n; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | // It's important that we null out mListener before calling one of its |
michael@0 | 49 | // methods as it may call WaitData, which would queue up another read. |
michael@0 | 50 | |
michael@0 | 51 | nsRefPtr<nsFtpControlConnectionListener> listener; |
michael@0 | 52 | listener.swap(mListener); |
michael@0 | 53 | |
michael@0 | 54 | if (!listener) |
michael@0 | 55 | return NS_OK; |
michael@0 | 56 | |
michael@0 | 57 | if (NS_FAILED(rv)) { |
michael@0 | 58 | listener->OnControlError(rv); |
michael@0 | 59 | } else { |
michael@0 | 60 | listener->OnControlDataAvailable(data, avail); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | return NS_OK; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | nsFtpControlConnection::nsFtpControlConnection(const nsCSubstring& host, |
michael@0 | 67 | uint32_t port) |
michael@0 | 68 | : mServerType(0), mSessionId(gFtpHandler->GetSessionId()) |
michael@0 | 69 | , mUseUTF8(false), mHost(host), mPort(port) |
michael@0 | 70 | { |
michael@0 | 71 | LOG_ALWAYS(("FTP:CC created @%p", this)); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | nsFtpControlConnection::~nsFtpControlConnection() |
michael@0 | 75 | { |
michael@0 | 76 | LOG_ALWAYS(("FTP:CC destroyed @%p", this)); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | bool |
michael@0 | 80 | nsFtpControlConnection::IsAlive() |
michael@0 | 81 | { |
michael@0 | 82 | if (!mSocket) |
michael@0 | 83 | return false; |
michael@0 | 84 | |
michael@0 | 85 | bool isAlive = false; |
michael@0 | 86 | mSocket->IsAlive(&isAlive); |
michael@0 | 87 | return isAlive; |
michael@0 | 88 | } |
michael@0 | 89 | nsresult |
michael@0 | 90 | nsFtpControlConnection::Connect(nsIProxyInfo* proxyInfo, |
michael@0 | 91 | nsITransportEventSink* eventSink) |
michael@0 | 92 | { |
michael@0 | 93 | if (mSocket) |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | |
michael@0 | 96 | // build our own |
michael@0 | 97 | nsresult rv; |
michael@0 | 98 | nsCOMPtr<nsISocketTransportService> sts = |
michael@0 | 99 | do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv); |
michael@0 | 100 | if (NS_FAILED(rv)) |
michael@0 | 101 | return rv; |
michael@0 | 102 | |
michael@0 | 103 | rv = sts->CreateTransport(nullptr, 0, mHost, mPort, proxyInfo, |
michael@0 | 104 | getter_AddRefs(mSocket)); // the command transport |
michael@0 | 105 | if (NS_FAILED(rv)) |
michael@0 | 106 | return rv; |
michael@0 | 107 | |
michael@0 | 108 | mSocket->SetQoSBits(gFtpHandler->GetControlQoSBits()); |
michael@0 | 109 | |
michael@0 | 110 | // proxy transport events back to current thread |
michael@0 | 111 | if (eventSink) |
michael@0 | 112 | mSocket->SetEventSink(eventSink, NS_GetCurrentThread()); |
michael@0 | 113 | |
michael@0 | 114 | // open buffered, blocking output stream to socket. so long as commands |
michael@0 | 115 | // do not exceed 1024 bytes in length, the writing thread (the main thread) |
michael@0 | 116 | // will not block. this should be OK. |
michael@0 | 117 | rv = mSocket->OpenOutputStream(nsITransport::OPEN_BLOCKING, 1024, 1, |
michael@0 | 118 | getter_AddRefs(mSocketOutput)); |
michael@0 | 119 | if (NS_FAILED(rv)) |
michael@0 | 120 | return rv; |
michael@0 | 121 | |
michael@0 | 122 | // open buffered, non-blocking/asynchronous input stream to socket. |
michael@0 | 123 | nsCOMPtr<nsIInputStream> inStream; |
michael@0 | 124 | rv = mSocket->OpenInputStream(0, |
michael@0 | 125 | nsIOService::gDefaultSegmentSize, |
michael@0 | 126 | nsIOService::gDefaultSegmentCount, |
michael@0 | 127 | getter_AddRefs(inStream)); |
michael@0 | 128 | if (NS_SUCCEEDED(rv)) |
michael@0 | 129 | mSocketInput = do_QueryInterface(inStream); |
michael@0 | 130 | |
michael@0 | 131 | return rv; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | nsresult |
michael@0 | 135 | nsFtpControlConnection::WaitData(nsFtpControlConnectionListener *listener) |
michael@0 | 136 | { |
michael@0 | 137 | LOG(("FTP:(%p) wait data [listener=%p]\n", this, listener)); |
michael@0 | 138 | |
michael@0 | 139 | // If listener is null, then simply disconnect the listener. Otherwise, |
michael@0 | 140 | // ensure that we are listening. |
michael@0 | 141 | if (!listener) { |
michael@0 | 142 | mListener = nullptr; |
michael@0 | 143 | return NS_OK; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | NS_ENSURE_STATE(mSocketInput); |
michael@0 | 147 | |
michael@0 | 148 | mListener = listener; |
michael@0 | 149 | return mSocketInput->AsyncWait(this, 0, 0, NS_GetCurrentThread()); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | nsresult |
michael@0 | 153 | nsFtpControlConnection::Disconnect(nsresult status) |
michael@0 | 154 | { |
michael@0 | 155 | if (!mSocket) |
michael@0 | 156 | return NS_OK; // already disconnected |
michael@0 | 157 | |
michael@0 | 158 | LOG_ALWAYS(("FTP:(%p) CC disconnecting (%x)", this, status)); |
michael@0 | 159 | |
michael@0 | 160 | if (NS_FAILED(status)) { |
michael@0 | 161 | // break cyclic reference! |
michael@0 | 162 | mSocket->Close(status); |
michael@0 | 163 | mSocket = 0; |
michael@0 | 164 | mSocketInput->AsyncWait(nullptr, 0, 0, nullptr); // clear any observer |
michael@0 | 165 | mSocketInput = nullptr; |
michael@0 | 166 | mSocketOutput = nullptr; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | return NS_OK; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | nsresult |
michael@0 | 173 | nsFtpControlConnection::Write(const nsCSubstring& command) |
michael@0 | 174 | { |
michael@0 | 175 | NS_ENSURE_STATE(mSocketOutput); |
michael@0 | 176 | |
michael@0 | 177 | uint32_t len = command.Length(); |
michael@0 | 178 | uint32_t cnt; |
michael@0 | 179 | nsresult rv = mSocketOutput->Write(command.Data(), len, &cnt); |
michael@0 | 180 | |
michael@0 | 181 | if (NS_FAILED(rv)) |
michael@0 | 182 | return rv; |
michael@0 | 183 | |
michael@0 | 184 | if (len != cnt) |
michael@0 | 185 | return NS_ERROR_FAILURE; |
michael@0 | 186 | |
michael@0 | 187 | return NS_OK; |
michael@0 | 188 | } |