netwerk/protocol/ftp/nsFTPChannel.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/protocol/ftp/nsFTPChannel.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim:set ts=4 sts=4 sw=4 et cin: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsFTPChannel.h"
    1.11 +#include "nsFtpConnectionThread.h"  // defines nsFtpState
    1.12 +
    1.13 +#include "nsThreadUtils.h"
    1.14 +#include "mozilla/Attributes.h"
    1.15 +
    1.16 +#if defined(PR_LOGGING)
    1.17 +extern PRLogModuleInfo* gFTPLog;
    1.18 +#endif /* PR_LOGGING */
    1.19 +
    1.20 +// There are two transport connections established for an 
    1.21 +// ftp connection. One is used for the command channel , and
    1.22 +// the other for the data channel. The command channel is the first
    1.23 +// connection made and is used to negotiate the second, data, channel.
    1.24 +// The data channel is driven by the command channel and is either
    1.25 +// initiated by the server (PORT command) or by the client (PASV command).
    1.26 +// Client initiation is the most common case and is attempted first.
    1.27 +
    1.28 +//-----------------------------------------------------------------------------
    1.29 +
    1.30 +NS_IMPL_ISUPPORTS_INHERITED(nsFtpChannel,
    1.31 +                            nsBaseChannel,
    1.32 +                            nsIUploadChannel,
    1.33 +                            nsIResumableChannel,
    1.34 +                            nsIFTPChannel,
    1.35 +                            nsIProxiedChannel)
    1.36 +
    1.37 +//-----------------------------------------------------------------------------
    1.38 +
    1.39 +NS_IMETHODIMP
    1.40 +nsFtpChannel::SetUploadStream(nsIInputStream *stream,
    1.41 +                              const nsACString &contentType,
    1.42 +                              int64_t contentLength)
    1.43 +{
    1.44 +    NS_ENSURE_TRUE(!Pending(), NS_ERROR_IN_PROGRESS);
    1.45 +
    1.46 +    mUploadStream = stream;
    1.47 +
    1.48 +    // NOTE: contentLength is intentionally ignored here.
    1.49 + 
    1.50 +    return NS_OK;
    1.51 +}
    1.52 +
    1.53 +NS_IMETHODIMP
    1.54 +nsFtpChannel::GetUploadStream(nsIInputStream **stream)
    1.55 +{
    1.56 +    NS_ENSURE_ARG_POINTER(stream);
    1.57 +    *stream = mUploadStream;
    1.58 +    NS_IF_ADDREF(*stream);
    1.59 +    return NS_OK;
    1.60 +}
    1.61 +
    1.62 +//-----------------------------------------------------------------------------
    1.63 +
    1.64 +NS_IMETHODIMP
    1.65 +nsFtpChannel::ResumeAt(uint64_t aStartPos, const nsACString& aEntityID)
    1.66 +{
    1.67 +    NS_ENSURE_TRUE(!Pending(), NS_ERROR_IN_PROGRESS);
    1.68 +    mEntityID = aEntityID;
    1.69 +    mStartPos = aStartPos;
    1.70 +    mResumeRequested = (mStartPos || !mEntityID.IsEmpty());
    1.71 +    return NS_OK;
    1.72 +}
    1.73 +
    1.74 +NS_IMETHODIMP
    1.75 +nsFtpChannel::GetEntityID(nsACString& entityID)
    1.76 +{
    1.77 +    if (mEntityID.IsEmpty())
    1.78 +      return NS_ERROR_NOT_RESUMABLE;
    1.79 +
    1.80 +    entityID = mEntityID;
    1.81 +    return NS_OK;
    1.82 +}
    1.83 +
    1.84 +//-----------------------------------------------------------------------------
    1.85 +NS_IMETHODIMP
    1.86 +nsFtpChannel::GetProxyInfo(nsIProxyInfo** aProxyInfo)
    1.87 +{
    1.88 +    *aProxyInfo = ProxyInfo();
    1.89 +    NS_IF_ADDREF(*aProxyInfo);
    1.90 +    return NS_OK;
    1.91 +}
    1.92 +
    1.93 +//-----------------------------------------------------------------------------
    1.94 +
    1.95 +nsresult
    1.96 +nsFtpChannel::OpenContentStream(bool async, nsIInputStream **result,
    1.97 +                                nsIChannel** channel)
    1.98 +{
    1.99 +    if (!async)
   1.100 +        return NS_ERROR_NOT_IMPLEMENTED;
   1.101 +
   1.102 +    nsFtpState *state = new nsFtpState();
   1.103 +    if (!state)
   1.104 +        return NS_ERROR_OUT_OF_MEMORY;
   1.105 +    NS_ADDREF(state);
   1.106 +
   1.107 +    nsresult rv = state->Init(this);
   1.108 +    if (NS_FAILED(rv)) {
   1.109 +        NS_RELEASE(state);
   1.110 +        return rv;
   1.111 +    }
   1.112 +
   1.113 +    *result = state;
   1.114 +    return NS_OK;
   1.115 +}
   1.116 +
   1.117 +bool
   1.118 +nsFtpChannel::GetStatusArg(nsresult status, nsString &statusArg)
   1.119 +{
   1.120 +    nsAutoCString host;
   1.121 +    URI()->GetHost(host);
   1.122 +    CopyUTF8toUTF16(host, statusArg);
   1.123 +    return true;
   1.124 +}
   1.125 +
   1.126 +void
   1.127 +nsFtpChannel::OnCallbacksChanged()
   1.128 +{
   1.129 +    mFTPEventSink = nullptr;
   1.130 +}
   1.131 +
   1.132 +//-----------------------------------------------------------------------------
   1.133 +
   1.134 +namespace {
   1.135 +
   1.136 +class FTPEventSinkProxy MOZ_FINAL : public nsIFTPEventSink
   1.137 +{
   1.138 +public:
   1.139 +    FTPEventSinkProxy(nsIFTPEventSink* aTarget)
   1.140 +        : mTarget(aTarget)
   1.141 +        , mTargetThread(do_GetCurrentThread())
   1.142 +    { }
   1.143 +        
   1.144 +    NS_DECL_THREADSAFE_ISUPPORTS
   1.145 +    NS_DECL_NSIFTPEVENTSINK
   1.146 +
   1.147 +    class OnFTPControlLogRunnable : public nsRunnable
   1.148 +    {
   1.149 +    public:
   1.150 +        OnFTPControlLogRunnable(nsIFTPEventSink* aTarget,
   1.151 +                                bool aServer,
   1.152 +                                const char* aMessage)
   1.153 +            : mTarget(aTarget)
   1.154 +            , mServer(aServer)
   1.155 +            , mMessage(aMessage)
   1.156 +        { }
   1.157 +
   1.158 +        NS_DECL_NSIRUNNABLE
   1.159 +
   1.160 +    private:
   1.161 +        nsCOMPtr<nsIFTPEventSink> mTarget;
   1.162 +        bool mServer;
   1.163 +        nsCString mMessage;
   1.164 +    };
   1.165 +
   1.166 +private:
   1.167 +    nsCOMPtr<nsIFTPEventSink> mTarget;
   1.168 +    nsCOMPtr<nsIThread> mTargetThread;
   1.169 +};
   1.170 +
   1.171 +NS_IMPL_ISUPPORTS(FTPEventSinkProxy, nsIFTPEventSink)
   1.172 +
   1.173 +NS_IMETHODIMP
   1.174 +FTPEventSinkProxy::OnFTPControlLog(bool aServer, const char* aMsg)
   1.175 +{
   1.176 +    nsRefPtr<OnFTPControlLogRunnable> r =
   1.177 +        new OnFTPControlLogRunnable(mTarget, aServer, aMsg);
   1.178 +    return mTargetThread->Dispatch(r, NS_DISPATCH_NORMAL);
   1.179 +}
   1.180 +
   1.181 +NS_IMETHODIMP
   1.182 +FTPEventSinkProxy::OnFTPControlLogRunnable::Run()
   1.183 +{
   1.184 +    mTarget->OnFTPControlLog(mServer, mMessage.get());
   1.185 +    return NS_OK;
   1.186 +}
   1.187 +
   1.188 +} // anonymous namespace
   1.189 +
   1.190 +void
   1.191 +nsFtpChannel::GetFTPEventSink(nsCOMPtr<nsIFTPEventSink> &aResult)
   1.192 +{
   1.193 +    if (!mFTPEventSink) {
   1.194 +        nsCOMPtr<nsIFTPEventSink> ftpSink;
   1.195 +        GetCallback(ftpSink);
   1.196 +        if (ftpSink) {
   1.197 +            mFTPEventSink = new FTPEventSinkProxy(ftpSink);
   1.198 +        }
   1.199 +    }
   1.200 +    aResult = mFTPEventSink;
   1.201 +}
   1.202 +
   1.203 +void
   1.204 +nsFtpChannel::ForcePending(bool aForcePending)
   1.205 +{
   1.206 +    // Set true here so IsPending will return true.
   1.207 +    // Required for callback diversion from child back to parent. In such cases
   1.208 +    // OnStopRequest can be called in the parent before callbacks are diverted
   1.209 +    // back from the child to the listener in the parent.
   1.210 +    mForcePending = aForcePending;
   1.211 +}
   1.212 +
   1.213 +NS_IMETHODIMP
   1.214 +nsFtpChannel::IsPending(bool *result)
   1.215 +{
   1.216 +  *result = Pending();
   1.217 +  return NS_OK;
   1.218 +}
   1.219 +
   1.220 +bool
   1.221 +nsFtpChannel::Pending() const
   1.222 +{
   1.223 +  return nsBaseChannel::Pending() || mForcePending;
   1.224 +}

mercurial