1999-12-20 21:48:15 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*/
|
|
|
|
|
|
|
|
// datetime implementation
|
|
|
|
|
|
|
|
#include "nsDateTimeChannel.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsISocketTransportService.h"
|
2001-02-21 20:38:08 +00:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
1999-12-20 21:48:15 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
|
|
|
|
// nsDateTimeChannel methods
|
|
|
|
nsDateTimeChannel::nsDateTimeChannel() {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mContentLength = -1;
|
|
|
|
mPort = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDateTimeChannel::~nsDateTimeChannel() {
|
|
|
|
}
|
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
NS_IMPL_ISUPPORTS4(nsDateTimeChannel,
|
|
|
|
nsIChannel,
|
|
|
|
nsIRequest,
|
|
|
|
nsIStreamListener,
|
2001-04-10 06:01:08 +00:00
|
|
|
nsIRequestObserver)
|
1999-12-20 21:48:15 +00:00
|
|
|
|
|
|
|
nsresult
|
2000-03-29 03:58:50 +00:00
|
|
|
nsDateTimeChannel::Init(nsIURI* uri)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2000-01-11 22:16:19 +00:00
|
|
|
NS_ASSERTION(uri, "no uri");
|
|
|
|
|
1999-12-21 19:40:05 +00:00
|
|
|
mUrl = uri;
|
|
|
|
|
2000-01-11 22:16:19 +00:00
|
|
|
rv = mUrl->GetPort(&mPort);
|
|
|
|
if (NS_FAILED(rv) || mPort < 1)
|
|
|
|
mPort = DATETIME_PORT;
|
|
|
|
|
|
|
|
rv = mUrl->GetPath(getter_Copies(mHost));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (!*(const char *)mHost) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsDateTimeChannel::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult)
|
|
|
|
{
|
|
|
|
nsDateTimeChannel* dc = new nsDateTimeChannel();
|
|
|
|
if (dc == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(dc);
|
|
|
|
nsresult rv = dc->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(dc);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRequest methods:
|
|
|
|
|
2000-08-21 08:23:54 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetName(PRUnichar* *result)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-12-20 21:48:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::IsPending(PRBool *result)
|
|
|
|
{
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_NOTREACHED("nsDateTimeChannel::IsPending");
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-03-29 03:58:50 +00:00
|
|
|
nsDateTimeChannel::GetStatus(nsresult *status)
|
|
|
|
{
|
|
|
|
*status = NS_OK;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::Cancel(nsresult status)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
2000-04-12 07:58:24 +00:00
|
|
|
NS_ASSERTION(NS_FAILED(status), "shouldn't cancel with a success code");
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_NOTREACHED("nsDateTimeChannel::Cancel");
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::Suspend(void)
|
|
|
|
{
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_NOTREACHED("nsDateTimeChannel::Suspend");
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::Resume(void)
|
|
|
|
{
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_NOTREACHED("nsDateTimeChannel::Resume");
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIChannel methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-03-29 03:58:50 +00:00
|
|
|
nsDateTimeChannel::GetOriginalURI(nsIURI* *aURI)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
2000-03-29 03:58:50 +00:00
|
|
|
*aURI = mOriginalURI ? mOriginalURI : mUrl;
|
1999-12-20 21:48:15 +00:00
|
|
|
NS_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-03-29 03:58:50 +00:00
|
|
|
nsDateTimeChannel::SetOriginalURI(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
mOriginalURI = aURI;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetURI(nsIURI* *aURI)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
|
|
|
*aURI = mUrl;
|
|
|
|
NS_IF_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsDateTimeChannel::Open(nsIInputStream **_retval)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsISocketTransportService, socketService, kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
nsCOMPtr<nsITransport> transport;
|
|
|
|
rv = socketService->CreateTransport(mHost, mPort, nsnull, -1, 32, 32, getter_AddRefs(transport));
|
1999-12-20 21:48:15 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-03-06 05:10:33 +00:00
|
|
|
transport->SetNotificationCallbacks(mCallbacks,
|
2001-04-10 06:01:08 +00:00
|
|
|
(mLoadFlags & LOAD_BACKGROUND));
|
1999-12-21 19:40:05 +00:00
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
return transport->OpenInputStream(0, -1, 0, _retval);
|
1999-12-20 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsDateTimeChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *ctxt)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsISocketTransportService, socketService, kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
nsCOMPtr<nsITransport> transport;
|
|
|
|
rv = socketService->CreateTransport(mHost, mPort, nsnull, -1, 32, 32, getter_AddRefs(transport));
|
1999-12-20 21:48:15 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-03-06 05:10:33 +00:00
|
|
|
transport->SetNotificationCallbacks(mCallbacks,
|
2001-04-10 06:01:08 +00:00
|
|
|
(mLoadFlags & LOAD_BACKGROUND));
|
1999-12-21 19:40:05 +00:00
|
|
|
|
1999-12-20 21:48:15 +00:00
|
|
|
mListener = aListener;
|
2001-02-21 20:38:08 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRequest> request;
|
|
|
|
return transport->AsyncRead(this, ctxt, 0, -1, 0, getter_AddRefs(request));
|
1999-12-20 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-04-10 06:01:08 +00:00
|
|
|
nsDateTimeChannel::GetLoadFlags(PRUint32 *aLoadFlags)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
2001-04-10 06:01:08 +00:00
|
|
|
*aLoadFlags = mLoadFlags;
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-04-10 06:01:08 +00:00
|
|
|
nsDateTimeChannel::SetLoadFlags(PRUint32 aLoadFlags)
|
1999-12-20 21:48:15 +00:00
|
|
|
{
|
2001-04-10 06:01:08 +00:00
|
|
|
mLoadFlags = aLoadFlags;
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DATETIME_TYPE "text/plain"
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetContentType(char* *aContentType) {
|
|
|
|
if (!aContentType) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aContentType = nsCRT::strdup(DATETIME_TYPE);
|
|
|
|
if (!*aContentType) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-08 06:26:04 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::SetContentType(const char *aContentType)
|
|
|
|
{
|
|
|
|
//It doesn't make sense to set the content-type on this type
|
|
|
|
// of channel...
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-12-20 21:48:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetContentLength(PRInt32 *aContentLength)
|
|
|
|
{
|
|
|
|
*aContentLength = mContentLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::SetContentLength(PRInt32 aContentLength)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsDateTimeChannel::SetContentLength");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-12-20 21:48:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
|
|
|
|
{
|
|
|
|
*aLoadGroup = mLoadGroup;
|
|
|
|
NS_IF_ADDREF(*aLoadGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
|
|
|
|
{
|
1999-12-21 19:40:05 +00:00
|
|
|
if (mLoadGroup) // if we already had a load group remove ourselves...
|
2001-04-10 06:01:08 +00:00
|
|
|
(void)mLoadGroup->RemoveRequest(this, nsnull, NS_OK);
|
1999-12-21 19:40:05 +00:00
|
|
|
|
1999-12-20 21:48:15 +00:00
|
|
|
mLoadGroup = aLoadGroup;
|
1999-12-21 19:40:05 +00:00
|
|
|
if (mLoadGroup) {
|
2001-02-21 20:38:08 +00:00
|
|
|
return mLoadGroup->AddRequest(this, nsnull);
|
1999-12-21 19:40:05 +00:00
|
|
|
}
|
1999-12-20 21:48:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetOwner(nsISupports* *aOwner)
|
|
|
|
{
|
|
|
|
*aOwner = mOwner.get();
|
|
|
|
NS_IF_ADDREF(*aOwner);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::SetOwner(nsISupports* aOwner)
|
|
|
|
{
|
|
|
|
mOwner = aOwner;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
|
|
|
|
{
|
|
|
|
*aNotificationCallbacks = mCallbacks.get();
|
|
|
|
NS_IF_ADDREF(*aNotificationCallbacks);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
|
|
|
|
{
|
|
|
|
mCallbacks = aNotificationCallbacks;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDateTimeChannel::GetSecurityInfo(nsISupports **sec)
|
2000-03-17 22:06:32 +00:00
|
|
|
{
|
2001-02-21 20:38:08 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(sec);
|
|
|
|
*sec = nsnull;
|
2000-03-17 22:06:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-12-20 21:48:15 +00:00
|
|
|
|
2001-04-10 06:01:08 +00:00
|
|
|
// nsIRequestObserver methods
|
1999-12-20 21:48:15 +00:00
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsDateTimeChannel::OnStartRequest(nsIRequest *request, nsISupports *aContext) {
|
1999-12-20 21:48:15 +00:00
|
|
|
return mListener->OnStartRequest(this, aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsDateTimeChannel::OnStopRequest(nsIRequest *request, nsISupports* aContext,
|
2001-04-10 06:01:08 +00:00
|
|
|
nsresult aStatus) {
|
1999-12-21 19:40:05 +00:00
|
|
|
if (mLoadGroup) {
|
2001-04-10 06:01:08 +00:00
|
|
|
nsresult rv = mLoadGroup->RemoveRequest(this, nsnull, aStatus);
|
1999-12-21 19:40:05 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2001-04-10 06:01:08 +00:00
|
|
|
return mListener->OnStopRequest(this, aContext, aStatus);
|
1999-12-20 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsIStreamListener method
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsDateTimeChannel::OnDataAvailable(nsIRequest *request, nsISupports* aContext,
|
1999-12-20 21:48:15 +00:00
|
|
|
nsIInputStream *aInputStream, PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength) {
|
|
|
|
mContentLength = aLength;
|
|
|
|
return mListener->OnDataAvailable(this, aContext, aInputStream, aSourceOffset, aLength);
|
|
|
|
}
|
|
|
|
|