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
/* -*- 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
 * 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/. */

#include "mozilla/net/NeckoChild.h"
#include "mozilla/net/FTPChannelChild.h"
#include "mozilla/net/FTPChannelChild.h"
using namespace mozilla;
using namespace mozilla::net;

#include "nsFtpProtocolHandler.h"
#include "nsFTPChannel.h"
#include "nsFTPChannel.h"
#include "mozilla/Logging.h"
#include "nsIPrefBranch.h"
#include "nsIObserverService.h"
#include "nsEscape.h"
#include "nsAlgorithm.h"
#include "nsAlgorithm.h"

//-----------------------------------------------------------------------------

//
// Log module for FTP Protocol logging...
//
// To enable logging (see prlog.h for full details):
// To enable logging (see prlog.h for full details):
//
//    set MOZ_LOG=nsFtp:5
//    set MOZ_LOG_FILE=ftp.log
//
// This enables LogLevel::Debug level information and places all output in
// the file ftp.log.
// the file ftp.log.
//
LazyLogModule gFTPLog("nsFtp");
#undef LOG
#define LOG(args) MOZ_LOG(gFTPLog, mozilla::LogLevel::Debug, args)


//-----------------------------------------------------------------------------

#define IDLE_TIMEOUT_PREF "network.ftp.idleConnectionTimeout"
#define IDLE_CONNECTION_LIMIT 8 /* TODO pref me */


#define ENABLED_PREF "network.ftp.enabled"
#define QOS_DATA_PREF "network.ftp.data.qos"
#define QOS_CONTROL_PREF "network.ftp.control.qos"

nsFtpProtocolHandler* gFtpHandler = nullptr;

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

nsFtpProtocolHandler::nsFtpProtocolHandler()
    : mIdleTimeout(-1),
      mEnabled(true),
      mSessionId(0),
      mSessionId(0),
      mControlQoSBits(0x00),
      mDataQoSBits(0x00) {
  LOG(("FTP:creating handler @%p\n", this));

  gFtpHandler = this;
  gFtpHandler = this;
}

nsFtpProtocolHandler::~nsFtpProtocolHandler() {
  LOG(("FTP:destroying handler @%p\n", this));

  NS_ASSERTION(mRootConnectionList.Length() == 0, "why wasn't Observe called?");
  NS_ASSERTION(mRootConnectionList.Length() == 0, "why wasn't Observe called?");

  gFtpHandler = nullptr;
}

NS_IMPL_ISUPPORTS(nsFtpProtocolHandler, nsIProtocolHandler,
                  nsIProxiedProtocolHandler, nsIObserver,
                  nsIProxiedProtocolHandler, nsIObserver,
                  nsISupportsWeakReference)

nsresult nsFtpProtocolHandler::Init() {
  if (IsNeckoChild()) NeckoChild::InitNeckoChild();


  if (mIdleTimeout == -1) {
    nsresult rv;
    nsCOMPtr<nsIPrefBranch> branch =
        do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
    if (NS_FAILED(rv)) return rv;
    if (NS_FAILED(rv)) return rv;

    rv = branch->GetIntPref(IDLE_TIMEOUT_PREF, &mIdleTimeout);
    if (NS_FAILED(rv)) mIdleTimeout = 5 * 60;  // 5 minute default

    rv = branch->AddObserver(IDLE_TIMEOUT_PREF, this, true);
    rv = branch->AddObserver(IDLE_TIMEOUT_PREF, this, true);
    if (NS_FAILED(rv)) return rv;

    rv = branch->GetBoolPref(ENABLED_PREF, &mEnabled);
    if (NS_FAILED(rv)) mEnabled = true;


    rv = branch->AddObserver(ENABLED_PREF, this, true);
    if (NS_FAILED(rv)) return rv;

    int32_t val;
    int32_t val;
    rv = branch->GetIntPref(QOS_DATA_PREF, &val);
    if (NS_SUCCEEDED(rv)) mDataQoSBits = (uint8_t)clamped(val, 0, 0xff);

    rv = branch->AddObserver(QOS_DATA_PREF, this, true);
    if (NS_FAILED(rv)) return rv;
    if (NS_FAILED(rv)) return rv;

    rv = branch->GetIntPref(QOS_CONTROL_PREF, &val);
    if (NS_SUCCEEDED(rv)) mControlQoSBits = (uint8_t)clamped(val, 0, 0xff);
    if (NS_SUCCEEDED(rv)) mControlQoSBits = (uint8_t)clamped(val, 0, 0xff);

    rv = branch->AddObserver(QOS_CONTROL_PREF, this, true);
    if (NS_FAILED(rv)) return rv;
  }


  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  if (observerService) {
    observerService->AddObserver(this, "network:offline-about-to-go-offline",
    observerService->AddObserver(this, "network:offline-about-to-go-offline",
                                 true);

    observerService->AddObserver(this, "net:clear-active-logins", true);
  }


  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsIProtocolHandler methods:
// nsIProtocolHandler methods:

NS_IMETHODIMP
nsFtpProtocolHandler::GetScheme(nsACString& result) {
  result.AssignLiteral("ftp");
  return NS_OK;
  return NS_OK;
}

NS_IMETHODIMP
nsFtpProtocolHandler::GetDefaultPort(int32_t* result) {
  *result = 21;
  *result = 21;
  return NS_OK;
}

NS_IMETHODIMP
nsFtpProtocolHandler::GetProtocolFlags(uint32_t* result) {
nsFtpProtocolHandler::GetProtocolFlags(uint32_t* result) {
  *result = URI_STD | ALLOWS_PROXY | ALLOWS_PROXY_HTTP | URI_LOADABLE_BY_ANYONE;
  return NS_OK;
  return NS_OK;
}

NS_IMETHODIMP
nsFtpProtocolHandler::NewChannel(nsIURI* url, nsILoadInfo* aLoadInfo,
                                 nsIChannel** result) {
                                 nsIChannel** result) {
  return NewProxiedChannel(url, nullptr, 0, nullptr, aLoadInfo, result);
}

NS_IMETHODIMP
nsFtpProtocolHandler::NewProxiedChannel(nsIURI* uri, nsIProxyInfo* proxyInfo,
nsFtpProtocolHandler::NewProxiedChannel(nsIURI* uri, nsIProxyInfo* proxyInfo,
                                        uint32_t proxyResolveFlags,
                                        nsIURI* proxyURI,
                                        nsILoadInfo* aLoadInfo,
                                        nsIChannel** result) {
  if (!mEnabled) {
  if (!mEnabled) {
    return NS_ERROR_UNKNOWN_PROTOCOL;
  }

  NS_ENSURE_ARG_POINTER(uri);
  RefPtr<nsBaseChannel> channel;
  RefPtr<nsBaseChannel> channel;
  if (IsNeckoChild())
    channel = new FTPChannelChild(uri);
  else
    channel = new nsFtpChannel(uri, proxyInfo);


  // set the loadInfo on the new channel
  nsresult rv = channel->SetLoadInfo(aLoadInfo);
  NS_ENSURE_SUCCESS(rv, rv);

  channel.forget(result);
  return rv;
}
}

NS_IMETHODIMP
nsFtpProtocolHandler::AllowPort(int32_t port, const char* scheme,
                                bool* _retval) {
  *_retval = (port == 21 || port == 22);
  *_retval = (port == 21 || port == 22);
  return NS_OK;
}

// connection cache methods


void nsFtpProtocolHandler::Timeout(nsITimer* aTimer, void* aClosure) {
  LOG(("FTP:timeout reached for %p\n", aClosure));

  bool found = gFtpHandler->mRootConnectionList.RemoveElement(aClosure);
  if (!found) {
  if (!found) {
    NS_ERROR("timerStruct not found");
    return;
  }

  timerStruct* s = (timerStruct*)aClosure;
  timerStruct* s = (timerStruct*)aClosure;
  delete s;
}

nsresult nsFtpProtocolHandler::RemoveConnection(
nsresult nsFtpProtocolHandler::RemoveConnection(
    nsIURI* aKey, nsFtpControlConnection** _retval) {
  NS_ASSERTION(_retval, "null pointer");
  NS_ASSERTION(aKey, "null pointer");

  *_retval = nullptr;
  *_retval = nullptr;

  nsAutoCString spec;
  aKey->GetPrePath(spec);

  LOG(("FTP:removing connection for %s\n", spec.get()));
  LOG(("FTP:removing connection for %s\n", spec.get()));

  timerStruct* ts = nullptr;
  uint32_t i;
  bool found = false;


  for (i = 0; i < mRootConnectionList.Length(); ++i) {
    ts = mRootConnectionList[i];
    if (strcmp(spec.get(), ts->key) == 0) {
    if (strcmp(spec.get(), ts->key) == 0) {
      found = true;
      mRootConnectionList.RemoveElementAt(i);
      break;
    }
  }
  }

  if (!found) return NS_ERROR_FAILURE;

  // swap connection ownership
  // swap connection ownership
  ts->conn.forget(_retval);
  delete ts;

  return NS_OK;
}
}

nsresult nsFtpProtocolHandler::InsertConnection(nsIURI* aKey,
                                                nsFtpControlConnection* aConn) {
  NS_ASSERTION(aConn, "null pointer");
  NS_ASSERTION(aKey, "null pointer");
  NS_ASSERTION(aKey, "null pointer");

  if (aConn->mSessionId != mSessionId) return NS_ERROR_FAILURE;

  nsAutoCString spec;
  aKey->GetPrePath(spec);
  aKey->GetPrePath(spec);

  LOG(("FTP:inserting connection for %s\n", spec.get()));

  timerStruct* ts = new timerStruct();
  if (!ts) return NS_ERROR_OUT_OF_MEMORY;
  if (!ts) return NS_ERROR_OUT_OF_MEMORY;

  nsCOMPtr<nsITimer> timer;
  nsresult rv = NS_NewTimerWithFuncCallback(
      getter_AddRefs(timer), nsFtpProtocolHandler::Timeout, ts,
      mIdleTimeout * 1000, nsITimer::TYPE_REPEATING_SLACK,
      mIdleTimeout * 1000, nsITimer::TYPE_REPEATING_SLACK,
      "nsFtpProtocolHandler::InsertConnection");
  if (NS_FAILED(rv)) {
  if (NS_FAILED(rv)) {
    delete ts;
    return rv;
  }

  ts->key = ToNewCString(spec);
  ts->key = ToNewCString(spec, mozilla::fallible);
  if (!ts->key) {
    delete ts;
    return NS_ERROR_OUT_OF_MEMORY;
  }
  }

  // ts->conn is a RefPtr
  ts->conn = aConn;
  ts->timer = timer;


  //
  // limit number of idle connections.  if limit is reached, then prune
  // eldest connection with matching key.  if none matching, then prune
  // eldest connection.
  //
  //
  if (mRootConnectionList.Length() == IDLE_CONNECTION_LIMIT) {
    uint32_t i;
    for (i = 0; i < mRootConnectionList.Length(); ++i) {
      timerStruct* candidate = mRootConnectionList[i];
      if (strcmp(candidate->key, ts->key) == 0) {
      if (strcmp(candidate->key, ts->key) == 0) {
        mRootConnectionList.RemoveElementAt(i);
        delete candidate;
        break;
      }
    }
    }
    if (mRootConnectionList.Length() == IDLE_CONNECTION_LIMIT) {
      timerStruct* eldest = mRootConnectionList[0];
      mRootConnectionList.RemoveElementAt(0);
      delete eldest;
    }
    }
  }

  mRootConnectionList.AppendElement(ts);
  return NS_OK;
}
}

//-----------------------------------------------------------------------------
// nsIObserver

NS_IMETHODIMP
nsFtpProtocolHandler::Observe(nsISupports* aSubject, const char* aTopic,
                              const char16_t* aData) {
                              const char16_t* aData) {
  LOG(("FTP:observing [%s]\n", aTopic));

  if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
    nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(aSubject);
    if (!branch) {
    if (!branch) {
      NS_ERROR("no prefbranch");
      return NS_ERROR_UNEXPECTED;
    }
    int32_t val;
    nsresult rv = branch->GetIntPref(IDLE_TIMEOUT_PREF, &val);
    nsresult rv = branch->GetIntPref(IDLE_TIMEOUT_PREF, &val);
    if (NS_SUCCEEDED(rv)) mIdleTimeout = val;
    bool enabled;
    rv = branch->GetBoolPref(ENABLED_PREF, &enabled);
    if (NS_SUCCEEDED(rv)) mEnabled = enabled;


    rv = branch->GetIntPref(QOS_DATA_PREF, &val);
    if (NS_SUCCEEDED(rv)) mDataQoSBits = (uint8_t)clamped(val, 0, 0xff);

    rv = branch->GetIntPref(QOS_CONTROL_PREF, &val);
    if (NS_SUCCEEDED(rv)) mControlQoSBits = (uint8_t)clamped(val, 0, 0xff);
    if (NS_SUCCEEDED(rv)) mControlQoSBits = (uint8_t)clamped(val, 0, 0xff);
  } else if (!strcmp(aTopic, "network:offline-about-to-go-offline")) {
    ClearAllConnections();
  } else if (!strcmp(aTopic, "net:clear-active-logins")) {
    ClearAllConnections();
    ClearAllConnections();
    mSessionId++;
  } else {
    MOZ_ASSERT_UNREACHABLE("unexpected topic");
  }
  }

  return NS_OK;
}

void nsFtpProtocolHandler::ClearAllConnections() {
void nsFtpProtocolHandler::ClearAllConnections() {
  uint32_t i;
  for (i = 0; i < mRootConnectionList.Length(); ++i)
    delete mRootConnectionList[i];
  mRootConnectionList.Clear();
}
}