1999-06-10 22:08:59 +00:00
|
|
|
/* -*- Mode: IDL; 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.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "nsCookieHTTPNotify.h"
|
|
|
|
#include "nsIHTTPChannel.h"
|
|
|
|
#include "nsCookie.h"
|
1999-06-12 00:01:52 +00:00
|
|
|
#include "nsIURL.h"
|
1999-08-05 08:30:52 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAtom.h"
|
1999-06-10 22:08:59 +00:00
|
|
|
#include "nsCRT.h"
|
1999-08-27 10:16:52 +00:00
|
|
|
#include "nsXPIDLString.h"
|
1999-06-10 22:08:59 +00:00
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// nsISupports
|
|
|
|
|
1999-08-24 00:36:06 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsCookieHTTPNotify, nsIHTTPNotify, nsINetNotify);
|
1999-06-10 22:08:59 +00:00
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// nsCookieHTTPNotify Implementation
|
|
|
|
|
1999-07-31 13:22:20 +00:00
|
|
|
NS_COOKIE nsresult NS_NewCookieHTTPNotify(nsIHTTPNotify** aHTTPNotify) {
|
|
|
|
if (aHTTPNotify == NULL) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
nsCookieHTTPNotify* it = new nsCookieHTTPNotify();
|
|
|
|
if (it == 0) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return it->QueryInterface(nsIHTTPNotify::GetIID(), (void **) aHTTPNotify);
|
1999-06-10 22:08:59 +00:00
|
|
|
}
|
|
|
|
|
1999-07-31 13:22:20 +00:00
|
|
|
nsCookieHTTPNotify::nsCookieHTTPNotify() {
|
|
|
|
NS_INIT_REFCNT();
|
1999-06-10 22:08:59 +00:00
|
|
|
}
|
|
|
|
|
1999-07-31 13:22:20 +00:00
|
|
|
nsCookieHTTPNotify::~nsCookieHTTPNotify() {
|
1999-06-10 22:08:59 +00:00
|
|
|
}
|
|
|
|
|
1999-10-04 14:08:52 +00:00
|
|
|
NS_METHOD
|
|
|
|
nsCookieHTTPNotify::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
|
|
|
|
if (! aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aOuter) {
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
*aResult = nsnull;
|
|
|
|
nsresult rv;
|
|
|
|
nsIHTTPNotify* inst = nsnull;
|
|
|
|
if (NS_FAILED(rv = NS_NewCookieHTTPNotify(&inst))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (!inst) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
rv = inst->QueryInterface(aIID, aResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*aResult = NULL;
|
|
|
|
}
|
|
|
|
NS_RELEASE(inst);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-06-10 22:08:59 +00:00
|
|
|
///////////////////////////////////
|
|
|
|
// nsIHTTPNotify
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-31 13:22:20 +00:00
|
|
|
nsCookieHTTPNotify::ModifyRequest(nsISupports *aContext) {
|
|
|
|
nsresult rv;
|
|
|
|
nsIHTTPChannel* pHTTPConnection= nsnull;
|
|
|
|
if (aContext) {
|
|
|
|
rv = aContext->QueryInterface(nsIHTTPChannel::GetIID(), (void**)&pHTTPConnection);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsIURI* pURL;
|
|
|
|
rv = pHTTPConnection->GetURI(&pURL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-08-27 10:16:52 +00:00
|
|
|
nsXPIDLCString url;
|
1999-07-31 13:22:20 +00:00
|
|
|
if (pURL == nsnull) {
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-08-27 10:16:52 +00:00
|
|
|
rv = pURL->GetSpec(getter_Copies(url));
|
1999-07-31 13:22:20 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(pURL);
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (url == nsnull) {
|
|
|
|
NS_RELEASE(pURL);
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-08-27 11:11:14 +00:00
|
|
|
const char* cookie = ::COOKIE_GetCookie((char*)(const char*)url);
|
1999-07-31 13:22:20 +00:00
|
|
|
if (cookie == nsnull) {
|
|
|
|
NS_RELEASE(pURL);
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-08-05 08:30:52 +00:00
|
|
|
nsCOMPtr<nsIAtom> cookieHeader;
|
|
|
|
|
|
|
|
// XXX: Should cache this atom? HTTP atoms *msut* be lower case
|
|
|
|
cookieHeader = NS_NewAtom("cookie");
|
|
|
|
if (!cookieHeader) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
rv = pHTTPConnection->SetRequestHeader(cookieHeader, cookie);
|
|
|
|
}
|
1999-07-31 13:22:20 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(pURL);
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
NS_RELEASE(pURL);
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-06-10 22:08:59 +00:00
|
|
|
|
1999-07-31 13:22:20 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieHTTPNotify::AsyncExamineResponse(nsISupports *aContext) {
|
|
|
|
nsresult rv;
|
|
|
|
nsIHTTPChannel* pHTTPConnection= nsnull;
|
|
|
|
if (aContext) {
|
|
|
|
rv = aContext->QueryInterface(nsIHTTPChannel::GetIID(), (void**)&pHTTPConnection);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
char* cookie;
|
1999-08-05 08:30:52 +00:00
|
|
|
nsCOMPtr<nsIAtom> header;
|
|
|
|
|
|
|
|
// XXX: Should cache this atom? HTTP atoms *msut* be lower case
|
1999-08-05 20:21:46 +00:00
|
|
|
header = NS_NewAtom("set-cookie");
|
1999-08-05 08:30:52 +00:00
|
|
|
if (!header) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
rv = pHTTPConnection->GetResponseHeader(header, &cookie);
|
|
|
|
}
|
1999-07-31 13:22:20 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (cookie) {
|
1999-06-12 00:01:52 +00:00
|
|
|
nsIURI* pURL;
|
|
|
|
rv = pHTTPConnection->GetURI(&pURL);
|
|
|
|
if (NS_FAILED(rv)) {
|
1999-07-31 13:22:20 +00:00
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
nsCRT::free(cookie);
|
|
|
|
return rv;
|
1999-06-12 00:01:52 +00:00
|
|
|
}
|
1999-08-27 10:16:52 +00:00
|
|
|
nsXPIDLCString url;
|
1999-06-12 00:01:52 +00:00
|
|
|
if (pURL == nsnull) {
|
1999-07-31 13:22:20 +00:00
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
nsCRT::free(cookie);
|
|
|
|
return rv;
|
1999-06-12 00:01:52 +00:00
|
|
|
}
|
1999-08-27 10:16:52 +00:00
|
|
|
rv = pURL->GetSpec(getter_Copies(url));
|
1999-06-12 00:01:52 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
1999-07-31 13:22:20 +00:00
|
|
|
NS_RELEASE(pURL);
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
nsCRT::free(cookie);
|
|
|
|
return rv;
|
1999-06-12 00:01:52 +00:00
|
|
|
}
|
|
|
|
if (url == nsnull) {
|
1999-07-31 13:22:20 +00:00
|
|
|
NS_RELEASE(pURL);
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
nsCRT::free(cookie);
|
|
|
|
return rv;
|
1999-06-12 00:01:52 +00:00
|
|
|
}
|
1999-07-31 13:22:20 +00:00
|
|
|
char *pDate = nsnull;
|
1999-08-05 08:30:52 +00:00
|
|
|
// XXX: Should cache this atom? HTTP atoms *msut* be lower case
|
|
|
|
header = NS_NewAtom("date");
|
|
|
|
if (!header) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
rv = pHTTPConnection->GetResponseHeader(header, &pDate);
|
|
|
|
}
|
1999-07-31 13:22:20 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-09-26 09:38:07 +00:00
|
|
|
COOKIE_SetCookieStringFromHttp((char*)(const char*)url, cookie, pDate);
|
1999-07-31 13:22:20 +00:00
|
|
|
if(pDate) {
|
|
|
|
nsCRT::free(pDate);
|
|
|
|
}
|
1999-06-12 00:01:52 +00:00
|
|
|
}
|
|
|
|
NS_RELEASE(pURL);
|
1999-07-31 13:22:20 +00:00
|
|
|
nsCRT::free(cookie);
|
|
|
|
}
|
|
|
|
NS_RELEASE(pHTTPConnection);
|
|
|
|
return NS_OK;
|
1999-06-10 22:08:59 +00:00
|
|
|
}
|
|
|
|
|