1999-06-04 21:41:47 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-06-04 21:41:47 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-06-04 21:41:47 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-06-04 21:41:47 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:43:54 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-06-04 21:41:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "msgCore.h"
|
|
|
|
#include "nsMsgProtocol.h"
|
1999-06-05 20:21:27 +00:00
|
|
|
#include "nsIMsgMailNewsUrl.h"
|
1999-07-29 00:42:25 +00:00
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsXPIDLString.h"
|
1999-09-01 04:09:07 +00:00
|
|
|
#include "nsSpecialSystemDirectory.h"
|
1999-09-12 05:05:28 +00:00
|
|
|
#include "nsILoadGroup.h"
|
1999-07-29 00:42:25 +00:00
|
|
|
#include "nsIIOService.h"
|
1999-11-22 14:41:20 +00:00
|
|
|
#include "nsFileStream.h"
|
2000-02-26 01:04:50 +00:00
|
|
|
#include "nsINetSupportDialogService.h"
|
|
|
|
#include "nsIDNSService.h"
|
1999-06-04 21:41:47 +00:00
|
|
|
|
2000-02-26 01:04:50 +00:00
|
|
|
static NS_DEFINE_CID(kNetSupportDialogCID, NS_NETSUPPORTDIALOG_CID);
|
1999-07-29 00:42:25 +00:00
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
1999-10-12 00:13:45 +00:00
|
|
|
NS_IMPL_ISUPPORTS3(nsMsgProtocol, nsIStreamListener, nsIStreamObserver, nsIChannel)
|
1999-06-04 21:41:47 +00:00
|
|
|
|
1999-10-26 09:16:24 +00:00
|
|
|
nsMsgProtocol::nsMsgProtocol(nsIURI * aURL, nsIURI* originalURI)
|
1999-06-04 21:41:47 +00:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
m_flags = 0;
|
1999-07-29 00:42:25 +00:00
|
|
|
m_startPosition = 0;
|
|
|
|
m_readCount = 0;
|
1999-06-04 21:41:47 +00:00
|
|
|
m_socketIsOpen = PR_FALSE;
|
1999-09-01 04:09:07 +00:00
|
|
|
|
|
|
|
m_tempMsgFileSpec = nsSpecialSystemDirectory(nsSpecialSystemDirectory::OS_TemporaryDirectory);
|
|
|
|
m_tempMsgFileSpec += "tempMessage.eml";
|
1999-10-26 09:16:24 +00:00
|
|
|
m_url = aURL;
|
|
|
|
m_originalUrl = originalURI ? originalURI : aURL;
|
1999-06-04 21:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMsgProtocol::~nsMsgProtocol()
|
|
|
|
{}
|
|
|
|
|
2000-03-12 08:58:02 +00:00
|
|
|
nsresult nsMsgProtocol::OpenNetworkSocketWithInfo(const char * aHostName, PRInt32 aGetPort, const char *connectionType)
|
1999-06-04 21:41:47 +00:00
|
|
|
{
|
2000-02-26 23:11:45 +00:00
|
|
|
NS_ENSURE_ARG(aHostName);
|
1999-06-04 21:41:47 +00:00
|
|
|
|
2000-02-26 23:11:45 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsISocketTransportService> socketService (do_GetService(kSocketTransportServiceCID));
|
|
|
|
NS_ENSURE_TRUE(socketService, NS_ERROR_FAILURE);
|
|
|
|
|
1999-07-29 00:42:25 +00:00
|
|
|
m_readCount = -1; // with socket connections we want to read as much data as arrives
|
|
|
|
m_startPosition = 0;
|
|
|
|
|
2000-03-12 08:58:02 +00:00
|
|
|
rv = socketService->CreateTransportOfType(connectionType, aHostName, aGetPort, nsnull, 0, 0, getter_AddRefs(m_channel));
|
2000-02-26 23:11:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-07-29 00:42:25 +00:00
|
|
|
|
2000-02-26 23:11:45 +00:00
|
|
|
m_socketIsOpen = PR_FALSE;
|
|
|
|
return SetupTransportState();
|
|
|
|
}
|
1999-07-29 00:42:25 +00:00
|
|
|
|
2000-03-12 08:58:02 +00:00
|
|
|
nsresult nsMsgProtocol::OpenNetworkSocket(nsIURI * aURL, const char *connectionType) // open a connection on this url
|
2000-02-26 23:11:45 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aURL);
|
1999-06-04 21:41:47 +00:00
|
|
|
|
2000-02-26 23:11:45 +00:00
|
|
|
nsXPIDLCString hostName;
|
|
|
|
PRInt32 port = 0;
|
|
|
|
|
|
|
|
aURL->GetPort(&port);
|
|
|
|
aURL->GetHost(getter_Copies(hostName));
|
|
|
|
|
2000-03-12 08:58:02 +00:00
|
|
|
return OpenNetworkSocketWithInfo(hostName, port, connectionType);
|
1999-06-04 21:41:47 +00:00
|
|
|
}
|
|
|
|
|
1999-07-29 00:42:25 +00:00
|
|
|
nsresult nsMsgProtocol::OpenFileSocket(nsIURI * aURL, const nsFileSpec * aFileSpec, PRUint32 aStartPosition, PRInt32 aReadCount)
|
1999-06-04 21:41:47 +00:00
|
|
|
{
|
1999-07-29 00:42:25 +00:00
|
|
|
// mscott - file needs to be encoded directly into aURL. I should be able to get
|
|
|
|
// rid of this method completely.
|
|
|
|
|
1999-06-04 21:41:47 +00:00
|
|
|
nsresult rv = NS_OK;
|
1999-07-29 00:42:25 +00:00
|
|
|
m_startPosition = aStartPosition;
|
|
|
|
m_readCount = aReadCount;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIIOService, netService, kIOServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && aURL)
|
1999-06-04 21:41:47 +00:00
|
|
|
{
|
1999-07-29 00:42:25 +00:00
|
|
|
// extract the file path from the uri...
|
|
|
|
nsXPIDLCString filePath;
|
|
|
|
aURL->GetPath(getter_Copies(filePath));
|
|
|
|
char * urlSpec = PR_smprintf("file://%s", (const char *) filePath);
|
|
|
|
|
2000-03-21 06:26:03 +00:00
|
|
|
rv = netService->NewChannel("Load", urlSpec,
|
1999-08-26 22:45:55 +00:00
|
|
|
nsnull, // null base URI
|
2000-03-21 06:26:03 +00:00
|
|
|
m_loadGroup, // loadGroup
|
1999-11-17 08:14:52 +00:00
|
|
|
nsnull, // notificationCallbacks
|
|
|
|
nsIChannel::LOAD_NORMAL,
|
1999-10-26 09:16:24 +00:00
|
|
|
nsnull, // originalURI
|
1999-12-04 10:01:32 +00:00
|
|
|
0, 0,
|
1999-08-26 22:45:55 +00:00
|
|
|
getter_AddRefs(m_channel));
|
1999-07-29 00:42:25 +00:00
|
|
|
PR_FREEIF(urlSpec);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && m_channel)
|
1999-06-04 21:41:47 +00:00
|
|
|
{
|
1999-07-29 00:42:25 +00:00
|
|
|
m_socketIsOpen = PR_FALSE;
|
|
|
|
// rv = SetupTransportState();
|
1999-06-04 21:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsMsgProtocol::SetupTransportState()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
1999-07-29 00:42:25 +00:00
|
|
|
if (!m_socketIsOpen && m_channel)
|
1999-06-04 21:41:47 +00:00
|
|
|
{
|
1999-07-29 00:42:25 +00:00
|
|
|
rv = m_channel->OpenOutputStream(0 /* start position */, getter_AddRefs(m_outputStream));
|
1999-06-04 21:41:47 +00:00
|
|
|
|
1999-07-29 00:42:25 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create an output stream");
|
|
|
|
// we want to open the stream
|
1999-06-04 21:41:47 +00:00
|
|
|
} // if m_transport
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsMsgProtocol::CloseSocket()
|
|
|
|
{
|
2000-03-12 06:40:25 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
1999-06-04 21:41:47 +00:00
|
|
|
// release all of our socket state
|
|
|
|
m_socketIsOpen = PR_FALSE;
|
|
|
|
m_outputStream = null_nsCOMPtr();
|
2000-03-12 06:40:25 +00:00
|
|
|
|
|
|
|
// we need to call Cancel so that we remove the socket transport from the mActiveTransportList. see bug #30648
|
|
|
|
if (m_channel) {
|
|
|
|
rv = m_channel->Cancel();
|
|
|
|
}
|
1999-07-29 00:42:25 +00:00
|
|
|
m_channel = null_nsCOMPtr();
|
1999-06-04 21:41:47 +00:00
|
|
|
|
2000-03-12 06:40:25 +00:00
|
|
|
return rv;
|
1999-06-04 21:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Writes the data contained in dataBuffer into the current output stream. It also informs
|
|
|
|
* the transport layer that this data is now available for transmission.
|
|
|
|
* Returns a positive number for success, 0 for failure (not all the bytes were written to the
|
|
|
|
* stream, etc). We need to make another pass through this file to install an error system (mscott)
|
|
|
|
*/
|
|
|
|
|
1999-06-23 03:29:44 +00:00
|
|
|
PRInt32 nsMsgProtocol::SendData(nsIURI * aURL, const char * dataBuffer)
|
1999-06-04 21:41:47 +00:00
|
|
|
{
|
|
|
|
PRUint32 writeCount = 0;
|
|
|
|
PRInt32 status = 0;
|
|
|
|
|
1999-07-29 00:42:25 +00:00
|
|
|
// NS_PRECONDITION(m_outputStream, "oops....we don't have an output stream...how did that happen?");
|
1999-06-04 21:41:47 +00:00
|
|
|
if (dataBuffer && m_outputStream)
|
|
|
|
{
|
1999-08-04 20:35:58 +00:00
|
|
|
status = m_outputStream->Write(dataBuffer, PL_strlen(dataBuffer), &writeCount);
|
1999-06-04 21:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
1999-06-05 20:21:27 +00:00
|
|
|
|
|
|
|
// Whenever data arrives from the connection, core netlib notifices the protocol by calling
|
|
|
|
// OnDataAvailable. We then read and process the incoming data from the input stream.
|
1999-07-29 00:42:25 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::OnDataAvailable(nsIChannel * /* aChannel */, nsISupports *ctxt, nsIInputStream *inStr, PRUint32 sourceOffset, PRUint32 count)
|
1999-06-05 20:21:27 +00:00
|
|
|
{
|
|
|
|
// right now, this really just means turn around and churn through the state machine
|
1999-07-29 00:42:25 +00:00
|
|
|
nsCOMPtr<nsIURI> uri = do_QueryInterface(ctxt);
|
|
|
|
return ProcessProtocolState(uri, inStr, sourceOffset, count);
|
1999-06-05 20:21:27 +00:00
|
|
|
}
|
|
|
|
|
1999-07-29 00:42:25 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::OnStartRequest(nsIChannel * aChannel, nsISupports *ctxt)
|
1999-06-05 20:21:27 +00:00
|
|
|
{
|
1999-07-29 00:42:25 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr <nsIMsgMailNewsUrl> aMsgUrl = do_QueryInterface(ctxt, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && aMsgUrl)
|
2000-01-09 23:38:52 +00:00
|
|
|
{
|
1999-09-12 05:05:28 +00:00
|
|
|
rv = aMsgUrl->SetUrlState(PR_TRUE, NS_OK);
|
2000-03-21 06:26:03 +00:00
|
|
|
if (m_loadGroup)
|
|
|
|
m_loadGroup->AddChannel(NS_STATIC_CAST(nsIChannel *, this), nsnull /* context isupports */);
|
2000-01-09 23:38:52 +00:00
|
|
|
}
|
1999-09-12 05:05:28 +00:00
|
|
|
|
|
|
|
// if we are set up as a channel, we should notify our channel listener that we are starting...
|
|
|
|
// so pass in ourself as the channel and not the underlying socket or file channel the protocol
|
|
|
|
// happens to be using
|
|
|
|
if (m_channelListener)
|
|
|
|
rv = m_channelListener->OnStartRequest(this, m_channelContext);
|
|
|
|
|
|
|
|
return rv;
|
1999-06-05 20:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop binding is a "notification" informing us that the stream associated with aURL is going away.
|
1999-07-29 00:42:25 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::OnStopRequest(nsIChannel * aChannel, nsISupports *ctxt, nsresult aStatus, const PRUnichar* aMsg)
|
1999-06-05 20:21:27 +00:00
|
|
|
{
|
1999-07-29 00:42:25 +00:00
|
|
|
nsresult rv = NS_OK;
|
2000-03-21 06:26:03 +00:00
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
// if we are set up as a channel, we should notify our channel listener that we are starting...
|
|
|
|
// so pass in ourself as the channel and not the underlying socket or file channel the protocol
|
|
|
|
// happens to be using
|
|
|
|
if (m_channelListener)
|
|
|
|
rv = m_channelListener->OnStopRequest(this, m_channelContext, aStatus, aMsg);
|
2000-03-12 06:40:25 +00:00
|
|
|
|
2000-03-21 06:26:03 +00:00
|
|
|
nsCOMPtr <nsIMsgMailNewsUrl> aMsgUrl = do_QueryInterface(ctxt, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && aMsgUrl)
|
|
|
|
{
|
|
|
|
rv = aMsgUrl->SetUrlState(PR_FALSE, aStatus);
|
|
|
|
if (m_loadGroup)
|
|
|
|
m_loadGroup->RemoveChannel(NS_STATIC_CAST(nsIChannel *, this), nsnull, aStatus, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-12 06:40:25 +00:00
|
|
|
// !NS_BINDING_ABORTED because we don't want to see an alert if the user
|
|
|
|
// cancelled the operation. also, we'll get here because we call Cancel()
|
|
|
|
// to force removal of the nsSocketTransport. see CloseSocket()
|
|
|
|
// bugs #30775 and #30648 relate to this
|
|
|
|
if (NS_FAILED(aStatus) && (aStatus != NS_BINDING_ABORTED)) {
|
2000-02-26 01:04:50 +00:00
|
|
|
NS_WITH_SERVICE(nsIPrompt, dialog, kNetSupportDialogCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && dialog) {
|
|
|
|
// todo, put this into a string bundle
|
|
|
|
nsAutoString alertMsg("unknown error.");
|
|
|
|
switch (aStatus) {
|
|
|
|
case NS_ERROR_UNKNOWN_HOST:
|
|
|
|
// todo, put this into a string bundle
|
|
|
|
alertMsg = "Failed to connect to the server.";
|
|
|
|
break;
|
|
|
|
case NS_ERROR_CONNECTION_REFUSED:
|
|
|
|
// todo, put this into a string bundle
|
|
|
|
alertMsg = "Connection refused to the server.";
|
|
|
|
break;
|
|
|
|
case NS_ERROR_NET_TIMEOUT:
|
|
|
|
// todo, put this into a string bundle
|
|
|
|
alertMsg = "Connection to the server timed out.";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dialog->Alert(alertMsg.GetUnicode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
return rv;
|
1999-06-05 20:21:27 +00:00
|
|
|
}
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
nsresult nsMsgProtocol::LoadUrl(nsIURI * aURL, nsISupports * aConsumer)
|
1999-06-05 20:21:27 +00:00
|
|
|
{
|
|
|
|
// okay now kick us off to the next state...
|
|
|
|
// our first state is a process state so drive the state machine...
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr <nsIMsgMailNewsUrl> aMsgUrl = do_QueryInterface(aURL);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
rv = aMsgUrl->SetUrlState(PR_TRUE, NS_OK); // set the url as a url currently being run...
|
1999-07-29 00:42:25 +00:00
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
// if the url is given a stream consumer then we should use it to forward calls to...
|
|
|
|
if (!m_channelListener && aConsumer) // if we don't have a registered listener already
|
|
|
|
{
|
|
|
|
m_channelListener = do_QueryInterface(aConsumer);
|
|
|
|
if (!m_channelContext)
|
|
|
|
m_channelContext = do_QueryInterface(aURL);
|
|
|
|
}
|
|
|
|
|
1999-07-29 00:42:25 +00:00
|
|
|
if (!m_socketIsOpen)
|
|
|
|
{
|
1999-09-17 20:13:59 +00:00
|
|
|
nsCOMPtr<nsISupports> urlSupports = do_QueryInterface(aURL);
|
|
|
|
|
2000-02-26 23:11:45 +00:00
|
|
|
if (m_channel)
|
|
|
|
{
|
|
|
|
// put us in a state where we are always notified of incoming data
|
|
|
|
m_channel->AsyncRead(m_startPosition, m_readCount, urlSupports ,this /* stream observer */);
|
|
|
|
m_socketIsOpen = PR_TRUE; // mark the channel as open
|
|
|
|
}
|
1999-07-29 00:42:25 +00:00
|
|
|
} // if we got an event queue service
|
1999-06-05 20:21:27 +00:00
|
|
|
else // the connection is already open so we should begin processing our new url...
|
1999-07-29 00:42:25 +00:00
|
|
|
rv = ProcessProtocolState(aURL, nsnull, 0, 0);
|
1999-06-05 20:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// The rest of this file is mostly nsIChannel mumbo jumbo stuff
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult nsMsgProtocol::SetUrl(nsIURI * aURL)
|
|
|
|
{
|
|
|
|
m_url = dont_QueryInterface(aURL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-17 08:14:52 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::SetLoadGroup(nsILoadGroup * aLoadGroup)
|
1999-09-12 05:05:28 +00:00
|
|
|
{
|
|
|
|
m_loadGroup = dont_QueryInterface(aLoadGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-10-26 09:16:24 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::GetOriginalURI(nsIURI * *aURI)
|
|
|
|
{
|
|
|
|
*aURI = m_originalUrl;
|
|
|
|
NS_IF_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::GetURI(nsIURI * *aURI)
|
|
|
|
{
|
1999-10-26 09:16:24 +00:00
|
|
|
*aURI = m_url;
|
|
|
|
NS_IF_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
1999-09-12 05:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::OpenInputStream(PRUint32 startPosition, PRInt32 readCount, nsIInputStream **_retval)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::OpenOutputStream(PRUint32 startPosition, nsIOutputStream **_retval)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-10-06 08:26:01 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::AsyncOpen(nsIStreamObserver *observer, nsISupports* ctxt)
|
|
|
|
{
|
1999-12-01 06:17:49 +00:00
|
|
|
// we already know the content type...
|
|
|
|
observer->OnStartRequest(this, ctxt);
|
|
|
|
return NS_OK;
|
1999-10-06 08:26:01 +00:00
|
|
|
}
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::AsyncRead(PRUint32 startPosition, PRInt32 readCount, nsISupports *ctxt, nsIStreamListener *listener)
|
|
|
|
{
|
|
|
|
// set the stream listener and then load the url
|
|
|
|
m_channelContext = ctxt;
|
|
|
|
m_channelListener = listener;
|
|
|
|
|
|
|
|
// the following load group code is completely bogus....
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (m_loadGroup)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadGroupListenerFactory> factory;
|
|
|
|
//
|
|
|
|
// Create a load group "proxy" listener...
|
|
|
|
//
|
|
|
|
rv = m_loadGroup->GetGroupListenerFactory(getter_AddRefs(factory));
|
|
|
|
if (factory)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStreamListener> newListener;
|
|
|
|
rv = factory->CreateLoadGroupListener(m_channelListener, getter_AddRefs(newListener));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
m_channelListener = newListener;
|
|
|
|
}
|
|
|
|
} // if aLoadGroup
|
|
|
|
|
|
|
|
return LoadUrl(m_url, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::AsyncWrite(nsIInputStream *fromStream, PRUint32 startPosition, PRInt32 writeCount, nsISupports *ctxt, nsIStreamObserver *observer)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::GetLoadAttributes(nsLoadFlags *aLoadAttributes)
|
|
|
|
{
|
2000-03-21 06:26:03 +00:00
|
|
|
*aLoadAttributes = mLoadAttributes;
|
|
|
|
return NS_OK;
|
1999-09-12 05:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::SetLoadAttributes(nsLoadFlags aLoadAttributes)
|
|
|
|
{
|
2000-03-21 06:26:03 +00:00
|
|
|
mLoadAttributes = aLoadAttributes;
|
|
|
|
return NS_OK; // don't fail when trying to set this
|
1999-09-12 05:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::GetContentType(char * *aContentType)
|
|
|
|
{
|
1999-10-27 06:48:19 +00:00
|
|
|
// as url dispatching matures, we'll be intelligent and actually start
|
|
|
|
// opening the url before specifying the content type. This will allow
|
|
|
|
// us to optimize the case where the message url actual refers to
|
|
|
|
// a part in the message that has a content type that is not message/rfc822
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
*aContentType = nsCRT::strdup("message/rfc822");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-08 06:26:04 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::SetContentType(const char *aContentType)
|
|
|
|
{
|
|
|
|
// XXX: Do not allow the content type to be changed (yet)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::GetContentLength(PRInt32 * aContentLength)
|
|
|
|
{
|
|
|
|
*aContentLength = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-09-12 06:28:20 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::GetOwner(nsISupports * *aPrincipal)
|
1999-09-12 05:05:28 +00:00
|
|
|
{
|
2000-01-05 22:55:04 +00:00
|
|
|
*aPrincipal = nsnull;
|
|
|
|
return NS_OK;
|
1999-09-12 05:05:28 +00:00
|
|
|
}
|
|
|
|
|
1999-09-12 06:28:20 +00:00
|
|
|
NS_IMETHODIMP nsMsgProtocol::SetOwner(nsISupports * aPrincipal)
|
1999-09-12 05:05:28 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::GetLoadGroup(nsILoadGroup * *aLoadGroup)
|
1999-11-17 08:14:52 +00:00
|
|
|
{
|
|
|
|
*aLoadGroup = m_loadGroup;
|
|
|
|
NS_IF_ADDREF(*aLoadGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-11-18 07:36:41 +00:00
|
|
|
nsMsgProtocol::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
|
1999-09-12 05:05:28 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-11-17 08:14:52 +00:00
|
|
|
NS_IMETHODIMP
|
1999-11-18 07:36:41 +00:00
|
|
|
nsMsgProtocol::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
|
1999-11-17 08:14:52 +00:00
|
|
|
{
|
|
|
|
return NS_OK; // don't fail when trying to set this
|
|
|
|
}
|
|
|
|
|
2000-03-17 22:06:32 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMsgProtocol::GetSecurityInfo(nsISupports * *aSecurityInfo)
|
|
|
|
{
|
|
|
|
*aSecurityInfo = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-09-12 05:05:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// From nsIRequest
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::IsPending(PRBool *result)
|
|
|
|
{
|
|
|
|
*result = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::Cancel()
|
|
|
|
{
|
1999-11-03 03:16:26 +00:00
|
|
|
return m_channel->Cancel();
|
1999-09-12 05:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::Suspend()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsMsgProtocol::Resume()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
1999-11-22 14:41:20 +00:00
|
|
|
|
|
|
|
nsresult nsMsgProtocol::PostMessage(nsIURI* url, nsIFileSpec *fileSpec)
|
|
|
|
{
|
|
|
|
if (!url || !fileSpec) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
#define POST_DATA_BUFFER_SIZE 2048
|
|
|
|
|
|
|
|
// mscott -- this function should be re-written to use the file url code
|
|
|
|
// so it can be asynch
|
|
|
|
nsFileSpec afileSpec;
|
|
|
|
fileSpec->GetFileSpec(&afileSpec);
|
|
|
|
nsInputFileStream * fileStream = new nsInputFileStream(afileSpec,
|
|
|
|
PR_RDONLY, 00700);
|
|
|
|
if (fileStream && fileStream->is_open())
|
|
|
|
{
|
|
|
|
PRInt32 amtInBuffer = 0;
|
|
|
|
PRBool lastLineWasComplete = PR_TRUE;
|
|
|
|
|
|
|
|
PRBool quoteLines = PR_TRUE; // it is always true but I'd like to
|
|
|
|
// generalize this function and then it
|
|
|
|
// might not be
|
|
|
|
char buffer[POST_DATA_BUFFER_SIZE];
|
|
|
|
|
|
|
|
if (quoteLines /* || add_crlf_to_line_endings */)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
char * b = buffer;
|
|
|
|
PRInt32 bsize = POST_DATA_BUFFER_SIZE;
|
|
|
|
amtInBuffer = 0;
|
|
|
|
do {
|
|
|
|
lastLineWasComplete = PR_TRUE;
|
|
|
|
PRInt32 L = 0;
|
|
|
|
if (fileStream->eof())
|
|
|
|
{
|
|
|
|
line = nsnull;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileStream->readline(b, bsize-5))
|
|
|
|
lastLineWasComplete = PR_FALSE;
|
|
|
|
line = b;
|
|
|
|
|
|
|
|
L = PL_strlen(line);
|
|
|
|
|
|
|
|
/* escape periods only if quote_lines_p is set
|
|
|
|
*/
|
|
|
|
if (quoteLines && lastLineWasComplete && line[0] == '.')
|
|
|
|
{
|
|
|
|
/* This line begins with "." so we need to quote it
|
|
|
|
by adding another "." to the beginning of the line.
|
|
|
|
*/
|
|
|
|
PRInt32 i;
|
|
|
|
line[L+1] = 0;
|
|
|
|
for (i = L; i > 0; i--)
|
|
|
|
line[i] = line[i-1];
|
|
|
|
L++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastLineWasComplete || (L > 1 && line[L-2] == CR &&
|
|
|
|
line[L-1] == LF))
|
|
|
|
{
|
|
|
|
/* already ok */
|
|
|
|
}
|
|
|
|
else if(L > 0 /* && (line[L-1] == LF || line[L-1] == CR) */)
|
|
|
|
{
|
|
|
|
/* only add the crlf if required
|
|
|
|
* we still need to do all the
|
|
|
|
* if comparisons here to know
|
|
|
|
* if the line was complete
|
|
|
|
*/
|
|
|
|
if(/* add_crlf_to_line_endings */ PR_TRUE)
|
|
|
|
{
|
|
|
|
/* Change newline to CRLF. */
|
|
|
|
line[L++] = CR;
|
|
|
|
line[L++] = LF;
|
|
|
|
line[L] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (L == 0 && !fileStream->eof()
|
|
|
|
/* && add_crlf_to_line_endings */)
|
|
|
|
{
|
|
|
|
// jt ** empty line; output CRLF
|
|
|
|
line[L++] = CR;
|
|
|
|
line[L++] = LF;
|
|
|
|
line[L] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bsize -= L;
|
|
|
|
b += L;
|
|
|
|
amtInBuffer += L;
|
|
|
|
// test hack by mscott. If our buffer is almost full, then
|
|
|
|
// send it off & reset ourselves
|
|
|
|
// to make more room.
|
|
|
|
if (bsize < 100) // i chose 100 arbitrarily.
|
|
|
|
{
|
|
|
|
if (*buffer)
|
|
|
|
SendData(url, buffer);
|
|
|
|
buffer[0] = '\0';
|
|
|
|
b = buffer; // reset buffer
|
|
|
|
bsize = POST_DATA_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (line /* && bsize > 100 */);
|
|
|
|
}
|
|
|
|
|
|
|
|
SendData(url, buffer);
|
|
|
|
delete fileStream;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|