1999-11-23 10:54:25 +03:00
|
|
|
/* -*- Mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
1999-06-11 02:08:59 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03: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-11 02:08:59 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03: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-11 02:08:59 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-06-11 02:08:59 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-06-11 02:08:59 +04:00
|
|
|
*/
|
|
|
|
|
2000-08-03 11:00:14 +04:00
|
|
|
#ifdef DEBUG_dp
|
1999-06-11 02:08:59 +04:00
|
|
|
#include <stdio.h>
|
2000-08-03 11:00:14 +04:00
|
|
|
#endif
|
|
|
|
|
2001-03-29 06:11:48 +04:00
|
|
|
#include "nsCookieService.h" /* don't remove -- needed for mac build */
|
1999-06-11 02:08:59 +04:00
|
|
|
#include "nsCookieHTTPNotify.h"
|
1999-11-24 00:57:05 +03:00
|
|
|
#include "nsIGenericFactory.h"
|
2001-05-12 01:05:08 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
1999-06-11 02:08:59 +04:00
|
|
|
#include "nsCookie.h"
|
1999-06-12 04:01:52 +04:00
|
|
|
#include "nsIURL.h"
|
1999-06-11 02:08:59 +04:00
|
|
|
#include "nsCRT.h"
|
1999-08-27 14:16:52 +04:00
|
|
|
#include "nsXPIDLString.h"
|
1999-11-30 01:02:20 +03:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsINetModuleMgr.h"
|
1999-12-18 08:48:26 +03:00
|
|
|
#include "nsILoadGroup.h"
|
2000-01-23 01:46:13 +03:00
|
|
|
#include "nsICategoryManager.h"
|
2001-05-12 01:05:08 +04:00
|
|
|
#include "nsIHttpProtocolHandler.h" // for NS_HTTP_STARTUP_CATEGORY
|
2000-06-30 05:23:16 +04:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2000-06-16 18:56:47 +04:00
|
|
|
#include "nsIPrompt.h"
|
1999-11-30 01:02:20 +03:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kINetModuleMgrCID, NS_NETMODULEMGR_CID);
|
1999-06-11 02:08:59 +04:00
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// nsISupports
|
|
|
|
|
2001-05-12 01:05:08 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(nsCookieHTTPNotify, nsIHttpNotify, nsINetNotify);
|
1999-06-11 02:08:59 +04:00
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// nsCookieHTTPNotify Implementation
|
|
|
|
|
1999-11-24 00:57:05 +03:00
|
|
|
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsCookieHTTPNotify, Init)
|
|
|
|
|
2000-01-23 01:46:13 +03:00
|
|
|
nsresult nsCookieHTTPNotify::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
1999-11-23 10:54:25 +03:00
|
|
|
{
|
2000-01-23 01:46:13 +03:00
|
|
|
return nsCookieHTTPNotifyConstructor(aOuter, aIID, aResult);
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
2000-02-01 01:57:28 +03:00
|
|
|
NS_METHOD nsCookieHTTPNotify::RegisterProc(nsIComponentManager *aCompMgr,
|
|
|
|
nsIFile *aPath,
|
|
|
|
const char *registryLocation,
|
2001-03-12 23:43:02 +03:00
|
|
|
const char *componentType,
|
|
|
|
const nsModuleComponentInfo *info)
|
1999-11-23 10:54:25 +03:00
|
|
|
{
|
2000-01-23 01:46:13 +03:00
|
|
|
// Register ourselves into the NS_CATEGORY_HTTP_STARTUP
|
|
|
|
nsresult rv;
|
2001-03-29 06:11:48 +04:00
|
|
|
nsCOMPtr<nsICategoryManager> catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
2000-01-23 01:46:13 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsXPIDLCString prevEntry;
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = catman->AddCategoryEntry(NS_HTTP_STARTUP_CATEGORY, "Http Cookie Notify", NS_COOKIEHTTPNOTIFY_CONTRACTID,
|
2000-01-23 01:46:13 +03:00
|
|
|
PR_TRUE, PR_TRUE, getter_Copies(prevEntry));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-02-01 01:57:28 +03:00
|
|
|
NS_METHOD nsCookieHTTPNotify::UnregisterProc(nsIComponentManager *aCompMgr,
|
|
|
|
nsIFile *aPath,
|
2001-03-12 23:43:02 +03:00
|
|
|
const char *registryLocation,
|
|
|
|
const nsModuleComponentInfo *info)
|
2000-01-23 01:46:13 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2001-03-29 06:11:48 +04:00
|
|
|
nsCOMPtr<nsICategoryManager> catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
2000-01-23 01:46:13 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsXPIDLCString prevEntry;
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = catman->DeleteCategoryEntry(NS_HTTP_STARTUP_CATEGORY, NS_COOKIEHTTPNOTIFY_CONTRACTID, PR_TRUE,
|
2000-01-23 01:46:13 +03:00
|
|
|
getter_Copies(prevEntry));
|
|
|
|
|
|
|
|
// Return value is not used from this function.
|
|
|
|
return NS_OK;
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-24 00:57:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieHTTPNotify::Init()
|
1999-11-23 10:54:25 +03:00
|
|
|
{
|
1999-11-30 01:02:20 +03:00
|
|
|
// Register to handing http requests and responses
|
2000-01-23 01:46:13 +03:00
|
|
|
nsresult rv = NS_OK;
|
1999-11-30 01:02:20 +03:00
|
|
|
nsCOMPtr<nsINetModuleMgr> pNetModuleMgr = do_GetService(kINetModuleMgrCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = pNetModuleMgr->RegisterModule(NS_NETWORK_MODULE_MANAGER_HTTP_REQUEST_CONTRACTID,
|
2001-05-12 01:05:08 +04:00
|
|
|
(nsIHttpNotify *)this);
|
1999-11-30 01:02:20 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = pNetModuleMgr->RegisterModule(NS_NETWORK_MODULE_MANAGER_HTTP_RESPONSE_CONTRACTID,
|
2001-05-12 01:05:08 +04:00
|
|
|
(nsIHttpNotify *)this);
|
2000-01-23 01:46:13 +03:00
|
|
|
return rv;
|
|
|
|
}
|
1999-11-30 01:02:20 +03:00
|
|
|
|
2000-01-23 01:46:13 +03:00
|
|
|
nsCookieHTTPNotify::nsCookieHTTPNotify()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mCookieService = nsnull;
|
2000-10-29 02:17:53 +04:00
|
|
|
#ifdef DEBUG_dp
|
|
|
|
printf("CookieHTTPNotify Created.\n");
|
|
|
|
#endif /* DEBUG_dp */
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
1999-11-24 00:57:05 +03:00
|
|
|
nsCookieHTTPNotify::~nsCookieHTTPNotify()
|
1999-11-23 10:54:25 +03:00
|
|
|
{
|
1999-10-04 18:08:52 +04:00
|
|
|
}
|
|
|
|
|
1999-11-30 01:02:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieHTTPNotify::SetupCookieService()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mCookieService)
|
1999-12-17 03:42:33 +03:00
|
|
|
{
|
2000-09-14 03:57:52 +04:00
|
|
|
mCookieService = do_GetService(NS_COOKIESERVICE_CONTRACTID, &rv);
|
1999-12-17 03:42:33 +03:00
|
|
|
}
|
1999-11-30 01:02:20 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-06-11 02:08:59 +04:00
|
|
|
///////////////////////////////////
|
2001-05-12 01:05:08 +04:00
|
|
|
// nsIHttpNotify
|
1999-06-11 02:08:59 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-05-12 01:05:08 +04:00
|
|
|
nsCookieHTTPNotify::OnModifyRequest(nsIHttpChannel *aHttpChannel)
|
1999-11-23 10:54:25 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-11-30 01:02:20 +03:00
|
|
|
// Preconditions
|
2001-05-12 01:05:08 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aHttpChannel);
|
1999-11-24 00:57:05 +03:00
|
|
|
|
1999-12-18 08:48:26 +03:00
|
|
|
// Get the url
|
1999-11-24 00:57:05 +03:00
|
|
|
nsCOMPtr<nsIURI> pURL;
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->GetURI(getter_AddRefs(pURL));
|
1999-11-24 00:57:05 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-12-18 08:48:26 +03:00
|
|
|
// Get the original url that the user either typed in or clicked on
|
|
|
|
nsCOMPtr<nsILoadGroup> pLoadGroup;
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->GetLoadGroup(getter_AddRefs(pLoadGroup));
|
1999-12-18 08:48:26 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> pChannel;
|
1999-12-19 06:44:34 +03:00
|
|
|
if (pLoadGroup) {
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIRequest> pRequest;
|
|
|
|
rv = pLoadGroup->GetDefaultLoadRequest(getter_AddRefs(pRequest));
|
|
|
|
if (pRequest)
|
|
|
|
pChannel = do_QueryInterface(pRequest);
|
1999-12-19 06:44:34 +03:00
|
|
|
}
|
1999-12-18 08:48:26 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> pFirstURL;
|
|
|
|
if (pChannel) {
|
|
|
|
rv = pChannel->GetURI(getter_AddRefs(pFirstURL));
|
|
|
|
} else {
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->GetURI(getter_AddRefs(pFirstURL));
|
1999-12-18 08:48:26 +03:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-11-30 01:02:20 +03:00
|
|
|
// Ensure that the cookie service exists
|
|
|
|
rv = SetupCookieService();
|
1999-11-24 00:57:05 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-03-29 06:11:48 +04:00
|
|
|
char * cookie;
|
|
|
|
rv = mCookieService->GetCookieStringFromHttp(pURL, pFirstURL, &cookie);
|
1999-11-24 00:57:05 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-11-30 01:02:20 +03:00
|
|
|
// Set the cookie into the request headers
|
2001-03-29 06:11:48 +04:00
|
|
|
if (cookie && *cookie)
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->SetRequestHeader("Cookie", cookie);
|
2001-03-29 06:11:48 +04:00
|
|
|
nsMemory::Free((void *)cookie);
|
1999-11-30 01:02:20 +03:00
|
|
|
|
|
|
|
return rv;
|
1999-11-23 10:54:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-05-12 01:05:08 +04:00
|
|
|
nsCookieHTTPNotify::OnExamineResponse(nsIHttpChannel *aHttpChannel)
|
1999-11-23 10:54:25 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-11-30 01:02:20 +03:00
|
|
|
// Preconditions
|
2001-05-12 01:05:08 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aHttpChannel);
|
1999-11-24 00:57:05 +03:00
|
|
|
|
|
|
|
// Get the Cookie header
|
1999-11-30 01:02:20 +03:00
|
|
|
nsXPIDLCString cookieHeader;
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->GetResponseHeader("Set-Cookie", getter_Copies(cookieHeader));
|
1999-11-24 00:57:05 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-08-03 11:00:14 +04:00
|
|
|
if (!cookieHeader) return NS_OK; // not an error, there's just no header.
|
1999-11-24 00:57:05 +03:00
|
|
|
|
1999-11-30 01:02:20 +03:00
|
|
|
// Get the url
|
1999-11-24 00:57:05 +03:00
|
|
|
nsCOMPtr<nsIURI> pURL;
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->GetURI(getter_AddRefs(pURL));
|
1999-11-24 00:57:05 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-30 01:02:20 +03:00
|
|
|
|
1999-12-18 08:48:26 +03:00
|
|
|
// Get the original url that the user either typed in or clicked on
|
|
|
|
nsCOMPtr<nsILoadGroup> pLoadGroup;
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->GetLoadGroup(getter_AddRefs(pLoadGroup));
|
1999-12-18 08:48:26 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
nsCOMPtr<nsIChannel> pChannel;
|
1999-12-19 06:44:34 +03:00
|
|
|
if (pLoadGroup) {
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIRequest> pRequest;
|
|
|
|
rv = pLoadGroup->GetDefaultLoadRequest(getter_AddRefs(pRequest));
|
1999-12-19 06:44:34 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-02-21 23:38:08 +03:00
|
|
|
pChannel = do_QueryInterface(pRequest);
|
1999-12-19 06:44:34 +03:00
|
|
|
}
|
1999-12-18 08:48:26 +03:00
|
|
|
nsCOMPtr<nsIURI> pFirstURL;
|
|
|
|
if (pChannel) {
|
|
|
|
rv = pChannel->GetURI(getter_AddRefs(pFirstURL));
|
|
|
|
} else {
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = aHttpChannel->GetURI(getter_AddRefs(pFirstURL));
|
1999-12-18 08:48:26 +03:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-09-14 18:49:30 +04:00
|
|
|
// Get the prompter
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> pInterfaces;
|
|
|
|
nsCOMPtr<nsIPrompt> pPrompter;
|
|
|
|
if (pChannel) {
|
|
|
|
pChannel->GetNotificationCallbacks(getter_AddRefs(pInterfaces));
|
|
|
|
} else {
|
2001-05-12 01:05:08 +04:00
|
|
|
aHttpChannel->GetNotificationCallbacks(getter_AddRefs(pInterfaces));
|
2000-09-14 18:49:30 +04:00
|
|
|
}
|
|
|
|
if (pInterfaces)
|
|
|
|
pInterfaces->GetInterface(NS_GET_IID(nsIPrompt), getter_AddRefs(pPrompter));
|
|
|
|
|
1999-11-24 00:57:05 +03:00
|
|
|
// Get the expires
|
2001-05-12 01:05:08 +04:00
|
|
|
nsXPIDLCString dateHeader;
|
|
|
|
rv = aHttpChannel->GetResponseHeader("Date", getter_Copies(dateHeader));
|
1999-11-30 01:02:20 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Ensure that we have the cookie service
|
|
|
|
rv = SetupCookieService();
|
1999-11-24 00:57:05 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Save the cookie
|
2001-05-12 01:05:08 +04:00
|
|
|
rv = mCookieService->SetCookieStringFromHttp(pURL, pFirstURL, pPrompter, cookieHeader, dateHeader);
|
1999-11-24 00:57:05 +03:00
|
|
|
|
1999-11-30 01:02:20 +03:00
|
|
|
return rv;
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|