pjs/extensions/cookie/nsCookieHTTPNotify.cpp

251 строка
7.8 KiB
C++
Исходник Обычный вид История

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
*
* 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
*
* 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
*
* 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
* 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
#include "nsCookieService.h" /* don't remove -- needed for mac build */
1999-06-11 02:08:59 +04:00
#include "nsCookieHTTPNotify.h"
#include "nsIGenericFactory.h"
#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"
#include "nsXPIDLString.h"
#include "nsIServiceManager.h"
#include "nsINetModuleMgr.h"
#include "nsILoadGroup.h"
#include "nsICategoryManager.h"
#include "nsIHttpProtocolHandler.h" // for NS_HTTP_STARTUP_CATEGORY
#include "nsIInterfaceRequestor.h"
#include "nsIPrompt.h"
static NS_DEFINE_CID(kINetModuleMgrCID, NS_NETMODULEMGR_CID);
1999-06-11 02:08:59 +04:00
///////////////////////////////////
// nsISupports
NS_IMPL_ISUPPORTS2(nsCookieHTTPNotify, nsIHttpNotify, nsINetNotify);
1999-06-11 02:08:59 +04:00
///////////////////////////////////
// nsCookieHTTPNotify Implementation
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsCookieHTTPNotify, Init)
nsresult nsCookieHTTPNotify::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
1999-11-23 10:54:25 +03:00
{
return nsCookieHTTPNotifyConstructor(aOuter, aIID, aResult);
1999-06-11 02:08:59 +04:00
}
NS_METHOD nsCookieHTTPNotify::RegisterProc(nsIComponentManager *aCompMgr,
nsIFile *aPath,
const char *registryLocation,
const char *componentType,
const nsModuleComponentInfo *info)
1999-11-23 10:54:25 +03:00
{
// Register ourselves into the NS_CATEGORY_HTTP_STARTUP
nsresult rv;
nsCOMPtr<nsICategoryManager> catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
nsXPIDLCString prevEntry;
rv = catman->AddCategoryEntry(NS_HTTP_STARTUP_CATEGORY, "Http Cookie Notify", NS_COOKIEHTTPNOTIFY_CONTRACTID,
PR_TRUE, PR_TRUE, getter_Copies(prevEntry));
return NS_OK;
}
NS_METHOD nsCookieHTTPNotify::UnregisterProc(nsIComponentManager *aCompMgr,
nsIFile *aPath,
const char *registryLocation,
const nsModuleComponentInfo *info)
{
nsresult rv;
nsCOMPtr<nsICategoryManager> catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
nsXPIDLCString prevEntry;
rv = catman->DeleteCategoryEntry(NS_HTTP_STARTUP_CATEGORY, NS_COOKIEHTTPNOTIFY_CONTRACTID, PR_TRUE,
getter_Copies(prevEntry));
// Return value is not used from this function.
return NS_OK;
1999-06-11 02:08:59 +04:00
}
NS_IMETHODIMP
nsCookieHTTPNotify::Init()
1999-11-23 10:54:25 +03:00
{
// Register to handing http requests and responses
nsresult rv = NS_OK;
nsCOMPtr<nsINetModuleMgr> pNetModuleMgr = do_GetService(kINetModuleMgrCID, &rv);
if (NS_FAILED(rv)) return rv;
rv = pNetModuleMgr->RegisterModule(NS_NETWORK_MODULE_MANAGER_HTTP_REQUEST_CONTRACTID,
(nsIHttpNotify *)this);
if (NS_FAILED(rv)) return rv;
rv = pNetModuleMgr->RegisterModule(NS_NETWORK_MODULE_MANAGER_HTTP_RESPONSE_CONTRACTID,
(nsIHttpNotify *)this);
return rv;
}
nsCookieHTTPNotify::nsCookieHTTPNotify()
{
NS_INIT_REFCNT();
mCookieService = nsnull;
#ifdef DEBUG_dp
printf("CookieHTTPNotify Created.\n");
#endif /* DEBUG_dp */
1999-06-11 02:08:59 +04:00
}
nsCookieHTTPNotify::~nsCookieHTTPNotify()
1999-11-23 10:54:25 +03:00
{
1999-10-04 18:08:52 +04:00
}
NS_IMETHODIMP
nsCookieHTTPNotify::SetupCookieService()
{
nsresult rv = NS_OK;
if (!mCookieService)
{
mCookieService = do_GetService(NS_COOKIESERVICE_CONTRACTID, &rv);
}
return rv;
}
1999-06-11 02:08:59 +04:00
///////////////////////////////////
// nsIHttpNotify
1999-06-11 02:08:59 +04:00
NS_IMETHODIMP
nsCookieHTTPNotify::OnModifyRequest(nsIHttpChannel *aHttpChannel)
1999-11-23 10:54:25 +03:00
{
nsresult rv;
// Preconditions
NS_ENSURE_ARG_POINTER(aHttpChannel);
// Get the url
nsCOMPtr<nsIURI> pURL;
rv = aHttpChannel->GetURI(getter_AddRefs(pURL));
if (NS_FAILED(rv)) return rv;
// Get the original url that the user either typed in or clicked on
nsCOMPtr<nsILoadGroup> pLoadGroup;
rv = aHttpChannel->GetLoadGroup(getter_AddRefs(pLoadGroup));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIChannel> pChannel;
if (pLoadGroup) {
nsCOMPtr<nsIRequest> pRequest;
rv = pLoadGroup->GetDefaultLoadRequest(getter_AddRefs(pRequest));
if (pRequest)
pChannel = do_QueryInterface(pRequest);
}
nsCOMPtr<nsIURI> pFirstURL;
if (pChannel) {
rv = pChannel->GetURI(getter_AddRefs(pFirstURL));
} else {
rv = aHttpChannel->GetURI(getter_AddRefs(pFirstURL));
}
if (NS_FAILED(rv)) return rv;
// Ensure that the cookie service exists
rv = SetupCookieService();
if (NS_FAILED(rv)) return rv;
char * cookie;
rv = mCookieService->GetCookieStringFromHttp(pURL, pFirstURL, &cookie);
if (NS_FAILED(rv)) return rv;
// Set the cookie into the request headers
if (cookie && *cookie)
rv = aHttpChannel->SetRequestHeader("Cookie", cookie);
nsMemory::Free((void *)cookie);
return rv;
1999-11-23 10:54:25 +03:00
}
NS_IMETHODIMP
nsCookieHTTPNotify::OnExamineResponse(nsIHttpChannel *aHttpChannel)
1999-11-23 10:54:25 +03:00
{
nsresult rv;
// Preconditions
NS_ENSURE_ARG_POINTER(aHttpChannel);
// Get the Cookie header
nsXPIDLCString cookieHeader;
rv = aHttpChannel->GetResponseHeader("Set-Cookie", getter_Copies(cookieHeader));
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.
// Get the url
nsCOMPtr<nsIURI> pURL;
rv = aHttpChannel->GetURI(getter_AddRefs(pURL));
if (NS_FAILED(rv)) return rv;
// Get the original url that the user either typed in or clicked on
nsCOMPtr<nsILoadGroup> pLoadGroup;
rv = aHttpChannel->GetLoadGroup(getter_AddRefs(pLoadGroup));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIChannel> pChannel;
if (pLoadGroup) {
nsCOMPtr<nsIRequest> pRequest;
rv = pLoadGroup->GetDefaultLoadRequest(getter_AddRefs(pRequest));
if (NS_FAILED(rv)) return rv;
pChannel = do_QueryInterface(pRequest);
}
nsCOMPtr<nsIURI> pFirstURL;
if (pChannel) {
rv = pChannel->GetURI(getter_AddRefs(pFirstURL));
} else {
rv = aHttpChannel->GetURI(getter_AddRefs(pFirstURL));
}
if (NS_FAILED(rv)) return rv;
// Get the prompter
nsCOMPtr<nsIInterfaceRequestor> pInterfaces;
nsCOMPtr<nsIPrompt> pPrompter;
if (pChannel) {
pChannel->GetNotificationCallbacks(getter_AddRefs(pInterfaces));
} else {
aHttpChannel->GetNotificationCallbacks(getter_AddRefs(pInterfaces));
}
if (pInterfaces)
pInterfaces->GetInterface(NS_GET_IID(nsIPrompt), getter_AddRefs(pPrompter));
// Get the expires
nsXPIDLCString dateHeader;
rv = aHttpChannel->GetResponseHeader("Date", getter_Copies(dateHeader));
if (NS_FAILED(rv)) return rv;
// Ensure that we have the cookie service
rv = SetupCookieService();
if (NS_FAILED(rv)) return rv;
// Save the cookie
rv = mCookieService->SetCookieStringFromHttp(pURL, pFirstURL, pPrompter, cookieHeader, dateHeader);
return rv;
1999-06-11 02:08:59 +04:00
}