More of the Cookie Module Stuff

This commit is contained in:
neeti%netscape.com 1999-06-12 00:01:52 +00:00
parent 4e4cb33da1
commit 9e88ac24a5
5 changed files with 253 additions and 84 deletions

View File

@ -29,19 +29,13 @@
#define alphabetize 1
#include "nsString.h"
//#include "nsINetService.h"
//#include "nsINetService.h"
#include "nsIServiceManager.h"
//#include "nsIStringBundle.h"
#include "nsFileStream.h"
#include "nsIFileLocator.h"
#include "nsFileLocations.h"
extern "C" {
#include "xp.h"
//#include "mkprefs.h"
//#include "netutils.h"
//#include "httpauth.h"
#include "prefapi.h"
#include "prmon.h"
}
@ -54,12 +48,8 @@ extern "C" {
}
extern "C" {
//#include "cookies.h"
#include "sechash.h"
#include "rosetta.h"
#include "libevent.h"
//#include "mkparse.h"
//#include "net_xp_file.h"
}
extern "C" {
@ -71,8 +61,6 @@ extern int MK_ACCESS_COOKIES_WISHES_MODIFY;
extern int MK_ACCESS_COOKIES_REMEMBER;
}
/*
static NS_DEFINE_IID(kINetServiceIID, NS_INETSERVICE_IID);
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
static NS_DEFINE_IID(kIStringBundleServiceIID, NS_ISTRINGBUNDLESERVICE_IID);
static NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);*/
@ -98,6 +86,11 @@ static NS_DEFINE_CID(kFileLocatorCID, NS_FILELOCATOR_CID);
MODULE_PRIVATE time_t
NET_ParseDate(char *date_string);
PRBool
ET_PostCheckConfirmBox(char* szMainMessage, char* szCheckMessage,
char* szOKMessage, char* szCancelMessage,
XP_Bool *bChecked);
PRIVATE Bool cookies_changed = FALSE;
PRIVATE Bool cookie_permissions_changed = FALSE;
PRIVATE Bool cookie_remember_checked = FALSE;
@ -132,7 +125,6 @@ typedef struct _net_CookiePermissionStruct {
} net_CookiePermissionStruct;
typedef struct _net_DeferCookieStruct {
MWContext * context;
char * cur_url;
char * set_cookie_header;
time_t timeToExpire;
@ -1100,12 +1092,10 @@ PRBool net_IntSetCookieStringInUse = FALSE;
PRIVATE void
net_DeferCookie(
MWContext * context,
char * cur_url,
char * set_cookie_header,
time_t timeToExpire) {
net_DeferCookieStruct * defer_cookie = PR_NEW(net_DeferCookieStruct);
// defer_cookie->context = context;
defer_cookie->cur_url = NULL;
StrAllocCopy(defer_cookie->cur_url, cur_url);
defer_cookie->set_cookie_header = NULL;
@ -1123,10 +1113,9 @@ net_DeferCookie(
XP_ListAddObject(net_defer_cookie_list, defer_cookie);
net_unlock_defer_cookie_list();
}
PRIVATE void
net_IntSetCookieString(MWContext * context,
char * cur_url,
net_IntSetCookieString(char * cur_url,
char * set_cookie_header,
time_t timeToExpire );
@ -1140,8 +1129,8 @@ net_UndeferCookies() {
}
defer_cookie = (net_DeferCookieStruct*)XP_ListRemoveEndObject(net_defer_cookie_list);
net_unlock_defer_cookie_list();
net_IntSetCookieString (
defer_cookie->context,
net_IntSetCookieString (
defer_cookie->cur_url,
defer_cookie->set_cookie_header,
defer_cookie->timeToExpire);
@ -1153,10 +1142,14 @@ net_UndeferCookies() {
/* Java script is calling NET_SetCookieString, netlib is calling
** this via NET_SetCookieStringFromHttp.
*/
/*
PRIVATE void
net_IntSetCookieString(MWContext * context,
char * cur_url,
char * set_cookie_header,
time_t timeToExpire )*/
PRIVATE void
net_IntSetCookieString(char * cur_url,
char * set_cookie_header,
time_t timeToExpire )
{
@ -1169,18 +1162,13 @@ net_IntSetCookieString(MWContext * context,
char *cur_host = NET_ParseURL(cur_url, GET_HOST_PART);
char *semi_colon, *ptr, *equal;
PRBool HG83744 is_domain=FALSE, accept=FALSE;
MWContextType type;
// MWContextType type;
Bool bCookieAdded;
if(!context) {
PR_Free(cur_path);
PR_Free(cur_host);
return;
}
/* Only allow cookies to be set in the listed contexts. We
* don't want cookies being set in html mail.
*/
/* We need to come back and work on this - Neeti
type = context->type;
if(!( (type == MWContextBrowser)
|| (type == MWContextHTMLHelp)
@ -1189,7 +1177,7 @@ net_IntSetCookieString(MWContext * context,
PR_Free(cur_host);
return;
}
*/
if(NET_GetCookieBehaviorPref() == NET_DontUse) {
PR_Free(cur_path);
PR_Free(cur_host);
@ -1203,7 +1191,7 @@ net_IntSetCookieString(MWContext * context,
if (net_IntSetCookieStringInUse) {
PR_Free(cur_path);
PR_Free(cur_host);
net_DeferCookie(context, cur_url, set_cookie_header, timeToExpire);
net_DeferCookie(cur_url, set_cookie_header, timeToExpire);
return;
}
net_IntSetCookieStringInUse = TRUE;
@ -1502,9 +1490,8 @@ net_IntSetCookieString(MWContext * context,
{
Bool old_cookie_remember_checked = cookie_remember_checked;
XP_Bool userHasAccepted = ET_PostCheckConfirmBox
(context,
new_string,
PRBool userHasAccepted = ET_PostCheckConfirmBox
(new_string,
remember_string,
0,0,
&cookie_remember_checked);
@ -1648,10 +1635,9 @@ net_IntSetCookieString(MWContext * context,
}
PUBLIC void
NET_SetCookieString(MWContext * context,
char * cur_url,
NET_SetCookieString(char * cur_url,
char * set_cookie_header) {
net_IntSetCookieString(context, cur_url, set_cookie_header, 0);
net_IntSetCookieString(cur_url, set_cookie_header, 0);
}
/* Determines whether the inlineHost is in the same domain as the currentHost. For use with rfc 2109
@ -1701,12 +1687,10 @@ NET_SameDomain(char * currentHost, char * inlineHost)
** This routine does not need to worry about the cookie lock since all of
** the work is handled by sub-routines
*/
PUBLIC void
NET_SetCookieStringFromHttp(FO_Present_Types outputFormat,
URL_Struct * URL_s,
MWContext * context,
char * cur_url,
char * set_cookie_header)
NET_SetCookieStringFromHttp(char * cur_url,
char * set_cookie_header, char * server_date)
{
/* If the outputFormat is not PRESENT (the url is not going to the screen), and not
* SAVE AS (shift-click) then
@ -1714,13 +1698,14 @@ NET_SetCookieStringFromHttp(FO_Present_Types outputFormat,
* to based on his preference to deal with foreign cookies. If it's not inline, just set
* the cookie. */
char *ptr=NULL, *date=NULL;
time_t gmtCookieExpires=0, expires=0;
time_t gmtCookieExpires=0, expires=0, sDate;
/* We need to come back and fix this. - Neeti
if(CLEAR_CACHE_BIT(outputFormat) != FO_PRESENT && CLEAR_CACHE_BIT(outputFormat) != FO_SAVE_AS)
{
if (NET_GetCookieBehaviorPref() == NET_DontAcceptForeign)
{
/* the user doesn't want foreign cookies, check to see if its foreign */
// the user doesn't want foreign cookies, check to see if its foreign
char * curSessionHistHost = 0;
char * theColon = 0;
char * curHost = NET_ParseURL(cur_url, GET_HOST_PART);
@ -1735,7 +1720,7 @@ NET_SetCookieStringFromHttp(FO_Present_Types outputFormat,
return;
}
/* strip ports */
// strip ports
theColon = PL_strchr(curHost, ':');
if(theColon)
*theColon = '\0';
@ -1743,7 +1728,7 @@ NET_SetCookieStringFromHttp(FO_Present_Types outputFormat,
if(theColon)
*theColon = '\0';
/* if it's foreign, get out of here after a little clean up */
// if it's foreign, get out of here after a little clean up
if(!NET_SameDomain(curHost, curSessionHistHost))
{
PR_FREEIF(curHost);
@ -1754,7 +1739,7 @@ NET_SetCookieStringFromHttp(FO_Present_Types outputFormat,
PR_FREEIF(curSessionHistHost);
}
}
*/
/* Determine when the cookie should expire. This is done by taking the difference between
the server time and the time the server wants the cookie to expire, and adding that
difference to the client time. This localizes the client time regardless of whether or
@ -1776,22 +1761,23 @@ NET_SetCookieStringFromHttp(FO_Present_Types outputFormat,
expires = NET_ParseDate(date);
*ptr=origLast;
}
if( URL_s->server_date && expires )
sDate = NET_ParseDate(server_date);
if( sDate && expires )
{
if( expires < URL_s->server_date )
if( expires < sDate )
{
gmtCookieExpires=1;
}
else
{
gmtCookieExpires = expires - URL_s->server_date + time(NULL);
/* if overflow */
gmtCookieExpires = expires - sDate + time(NULL);
// if overflow
if( gmtCookieExpires < time(NULL) )
gmtCookieExpires = (((unsigned) (~0) << 1) >> 1); /* max int */
gmtCookieExpires = (((unsigned) (~0) << 1) >> 1); // max int
}
}
net_IntSetCookieString(context, cur_url, set_cookie_header, gmtCookieExpires);
net_IntSetCookieString(cur_url, set_cookie_header, gmtCookieExpires);
}
#ifndef XP_MAC
@ -3385,9 +3371,9 @@ NET_ParseURL (const char *url, int parts_requested)
return rv;
}
JSBool
ET_PostCheckConfirmBox(MWContext* context,
char* szMainMessage, char* szCheckMessage,
PRBool
ET_PostCheckConfirmBox(char* szMainMessage, char* szCheckMessage,
char* szOKMessage, char* szCancelMessage,
XP_Bool *bChecked)
{
@ -3398,11 +3384,11 @@ ET_PostCheckConfirmBox(MWContext* context,
for (;;) {
c = getchar();
if (tolower(c) == 'y') {
result = JS_TRUE;
result = PR_TRUE;
break;
}
if (tolower(c) == 'n') {
result = JS_FALSE;
result = PR_FALSE;
break;
}
}
@ -3410,11 +3396,11 @@ ET_PostCheckConfirmBox(MWContext* context,
for (;;) {
c = getchar();
if (tolower(c) == 'y') {
*bChecked = TRUE;
*bChecked = PR_TRUE;
break;
}
if (tolower(c) == 'n') {
*bChecked = FALSE;
*bChecked = PR_FALSE;
break;
}
}

View File

@ -19,10 +19,9 @@
#ifndef COOKIES_H
#define COOKIES_H
#include "mkgeturl.h"
#include "nscore.h"
#include "nsError.h"
#include "nsString.h"
#ifdef _IMPL_NS_COOKIE
#define NS_COOKIE NS_EXPORT
@ -30,23 +29,16 @@
#define NS_COOKIE NS_IMPORT
#endif
PR_BEGIN_EXTERN_C
/* removes all cookies structs from the cookie list */
extern void
NET_RemoveAllCookies(void);
/*
extern char *
NET_GetCookie(MWContext * context, char * address);
*/
extern char *
NET_GetCookie(char * address);
extern void
NET_SetCookieString(MWContext * context,
char * cur_url,
NET_SetCookieString(char * cur_url,
char * set_cookie_header);
/* saves out the HTTP cookies to disk
@ -70,13 +62,22 @@ extern int NET_ReadCookies(char * filename);
/* wrapper of NET_SetCookieString for netlib use. We need outformat and url_struct to determine
* whether we're dealing with inline cookies
*/
extern void
NET_SetCookieStringFromHttp(FO_Present_Types outputFormat,
URL_Struct * URL_s,
MWContext * context,
char * cur_url,
char * set_cookie_header);
PR_END_EXTERN_C
extern void
NET_SetCookieStringFromHttp(char * cur_url,
char * set_cookie_header, char * server_date);
extern void
COOKIE_DisplayCookieInfoAsHTML();
extern void
COOKIE_GetCookieListForViewer (nsString& aCookieList);
extern void
COOKIE_GetPermissionListForViewer (nsString& aPermissionList);
extern void
COOKIE_CookieViewerReturn(nsAutoString results);
#endif /* COOKIES_H */

View File

@ -19,7 +19,9 @@
#include <stdio.h>
#include "nsCookieHTTPNotify.h"
#include "nsIHTTPChannel.h"
#include "nsIHTTPResponse.h"
#include "nsCookie.h"
#include "nsIURL.h"
#include "nsCRT.h"
@ -78,15 +80,52 @@ nsCookieHTTPNotify::ModifyRequest(nsISupports *aContext)
if (NS_FAILED(rv))
return rv;
const char* cookie = "testCookieVal";
rv = pHTTPConnection->SetRequestHeader("Cookie", cookie);
nsIURI* pURL;
rv = pHTTPConnection->GetURI(&pURL);
if (NS_FAILED(rv)) {
NS_RELEASE(pHTTPConnection);
return rv;
}
char *url;
if (pURL == nsnull) {
NS_RELEASE(pHTTPConnection);
return rv;
}
rv = pURL->GetSpec(&url);
if (NS_FAILED(rv)) {
NS_RELEASE(pURL);
NS_RELEASE(pHTTPConnection);
return rv;
}
if (url == nsnull) {
NS_RELEASE(pURL);
NS_RELEASE(pHTTPConnection);
return rv;
}
const char* cookie = ::NET_GetCookie(url);
if (cookie == nsnull) {
NS_RELEASE(pURL);
NS_RELEASE(pHTTPConnection);
nsCRT::free(url);
return rv;
}
rv = pHTTPConnection->SetRequestHeader("Cookie", cookie);
if (NS_FAILED(rv)) {
NS_RELEASE(pURL);
NS_RELEASE(pHTTPConnection);
nsCRT::free(url);
return rv;
}
NS_RELEASE(pURL);
NS_RELEASE(pHTTPConnection);
nsCRT::free(url);
return NS_OK;
}
@ -108,7 +147,7 @@ nsCookieHTTPNotify::AsyncExamineResponse(nsISupports *aContext)
return rv;
char* cookie = "testCookieVal";
char* cookie;
rv = pHTTPConnection->GetResponseHeader("Set-Cookie", &cookie);
if (NS_FAILED(rv)) {
@ -118,6 +157,49 @@ nsCookieHTTPNotify::AsyncExamineResponse(nsISupports *aContext)
if (cookie) {
printf("\nRecieving ... %s\n", cookie);
nsIURI* pURL;
rv = pHTTPConnection->GetURI(&pURL);
if (NS_FAILED(rv)) {
NS_RELEASE(pHTTPConnection);
nsCRT::free(cookie);
return rv;
}
char *url;
if (pURL == nsnull) {
NS_RELEASE(pHTTPConnection);
nsCRT::free(cookie);
return rv;
}
rv = pURL->GetSpec(&url);
if (NS_FAILED(rv)) {
NS_RELEASE(pURL);
NS_RELEASE(pHTTPConnection);
nsCRT::free(cookie);
return rv;
}
if (url == nsnull) {
NS_RELEASE(pURL);
NS_RELEASE(pHTTPConnection);
nsCRT::free(cookie);
return rv;
}
nsIHTTPResponse *pHTTPResponse;
rv = pHTTPConnection->QueryInterface(nsIHTTPResponse::GetIID(), (void **)&pHTTPResponse);
if (NS_SUCCEEDED(rv) && nsnull != pHTTPResponse) {
char *pDate;
if(pDate) {
pHTTPResponse->GetDate(&pDate);
NET_SetCookieStringFromHttp(url, cookie, pDate);
NS_RELEASE(pHTTPResponse);
nsCRT::free(pDate);
}
}
NS_RELEASE(pURL);
nsCRT::free(url);
nsCRT::free(cookie);
}

View File

@ -24,6 +24,8 @@
#include "nsCookieHTTPNotify.h"
#include "nsINetModuleMgr.h"
#include "nsIEventQueueService.h"
#include "nsCRT.h"
#include "nsCookie.h"
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
@ -44,7 +46,15 @@ public:
// nsICookieService
static nsresult GetCookieService(nsICookieService** aCookieService);
NS_IMETHOD Init();
NS_IMETHOD GetCookieString(nsIURI *aURL, nsString& aCookie);
NS_IMETHOD SetCookieString(nsIURI *aURL, const nsString& aCookie);
NS_IMETHOD Cookie_DisplayCookieInfoAsHTML();
NS_IMETHOD Cookie_CookieViewerReturn(nsAutoString results);
NS_IMETHOD Cookie_GetCookieListForViewer(nsString& aCookieList);
NS_IMETHOD Cookie_GetPermissionListForViewer(nsString& aPermissionList);
nsCookieService();
virtual ~nsCookieService(void);
@ -54,6 +64,7 @@ protected:
private:
nsIHTTPNotify *mCookieHTTPNotify;
nsresult Init();
};
@ -146,11 +157,72 @@ nsCookieService::Init()
rv = pNetModuleMgr->RegisterModule("http-response", eventQ, mCookieHTTPNotify, &kCookieHTTPNotifyCID);
if (NS_FAILED(rv))
return rv;
return rv;
}
NS_IMETHODIMP
nsCookieService::GetCookieString(nsIURI *aURL, nsString& aCookie)
{
char *spec = NULL;
nsresult result = aURL->GetSpec(&spec);
NS_ASSERTION(result == NS_OK, "deal with this");
char *cookie = NET_GetCookie((char *)spec);
if (nsnull != cookie) {
aCookie.SetString(cookie);
nsCRT::free(cookie);
}
else {
aCookie.SetString("");
}
nsCRT::free(spec);
return NS_OK;
}
NS_IMETHODIMP
nsCookieService::SetCookieString(nsIURI *aURL, const nsString& aCookie)
{
char *spec = NULL;
nsresult result = aURL->GetSpec(&spec);
NS_ASSERTION(result == NS_OK, "deal with this");
char *cookie = aCookie.ToNewCString();
NET_SetCookieString((char *)spec, cookie);
nsCRT::free(spec);
delete []cookie;
return NS_OK;
}
NS_IMETHODIMP
nsCookieService::Cookie_DisplayCookieInfoAsHTML(){
::COOKIE_DisplayCookieInfoAsHTML();
return NS_OK;
}
NS_IMETHODIMP nsCookieService::Cookie_CookieViewerReturn(nsAutoString results){
::COOKIE_CookieViewerReturn(results);
return NS_OK;
}
NS_IMETHODIMP nsCookieService::Cookie_GetCookieListForViewer(nsString& aCookieList){
::COOKIE_GetCookieListForViewer(aCookieList);
return NS_OK;
}
NS_IMETHODIMP nsCookieService::Cookie_GetPermissionListForViewer(nsString& aPermissionList){
::COOKIE_GetPermissionListForViewer(aPermissionList);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -20,6 +20,8 @@
#define nsICookieService_h__
#include "nsISupports.h"
#include "nsIURI.h"
#include "nsString.h"
// {AB397772-12D3-11d3-8AD1-00105A1B8860}
#define NS_ICOOKIESERVICE_IID \
@ -33,8 +35,34 @@
class nsICookieService : public nsISupports {
public:
NS_IMETHOD Init() = 0;
static const nsIID& GetIID() { static nsIID iid = NS_ICOOKIESERVICE_IID; return iid; }
/**
* Get the complete cookie string associated with the URL
*
* @param aURL The URL for which to get the cookie string
* @param aCookie The string object which will hold the result
* @return Returns NS_OK if successful, or NS_FALSE if an error occurred.
*/
NS_IMETHOD GetCookieString(nsIURI *aURL, nsString& aCookie)=0;
/**
* Set the cookie string associated with the URL
*
* @param aURL The URL for which to set the cookie string
* @param aCookie The string to set
* @return Returns NS_OK if successful, or NS_FALSE if an error occurred.
*/
NS_IMETHOD SetCookieString(nsIURI *aURL, const nsString& aCookie)=0;
NS_IMETHOD Cookie_DisplayCookieInfoAsHTML()=0;
NS_IMETHOD Cookie_CookieViewerReturn(nsAutoString results)=0;
NS_IMETHOD Cookie_GetCookieListForViewer(nsString& aCookieList)=0;
NS_IMETHOD Cookie_GetPermissionListForViewer(nsString& aPermissionList)=0;
};