From 4f49b16079b27d7ce633619e4cddfdf2bffbc952 Mon Sep 17 00:00:00 2001 From: cashmere Date: Fri, 4 Sep 2026 09:05:24 +0200 Subject: initial commit --- src/cookies.c | 311 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 src/cookies.c (limited to 'src/cookies.c') diff --git a/src/cookies.c b/src/cookies.c new file mode 100644 index 0000000..ea22054 --- /dev/null +++ b/src/cookies.c @@ -0,0 +1,311 @@ +/* +** Copyright (c) 2017 D. Richard Hipp +** +** This program is free software; you can redistribute it and/or +** modify it under the terms of the Simplified BSD License (also +** known as the "2-Clause License" or "FreeBSD License".) +** +** This program is distributed in the hope that it will be useful, +** but without any warranty; without even the implied warranty of +** merchantability or fitness for a particular purpose. +** +** Author contact information: +** drh@hwaci.com +** http://www.hwaci.com/drh/ +** +******************************************************************************* +** +** This file contains code used to manage a cookie that stores user-specific +** display preferences for the web interface. +** +** cookie_parse(void); +** +** Read and parse the display preferences cookie. +** +** cookie_read_parameter(zQP, zPName); +** +** If query parameter zQP does not exist but zPName does exist in +** the parsed cookie, then initialize zQP to hold the same value +** as the zPName element in the parsed cookie. +** +** cookie_write_parameter(zQP, zPName, zDefault); +** +** If query parameter zQP exists and if it has a different value from +** the zPName parameter in the parsed cookie, then replace the value of +** zPName with the value of zQP. If zQP exists but zPName does not +** exist, then zPName is created. If zQP does not exist or if it has +** the same value as zPName, then this routine is a no-op. +** +** cookie_link_parameter(zQP, zPName, zDefault); +** +** This does both cookie_read_parameter() and cookie_write_parameter() +** all at once. +** +** cookie_render(); +** +** If any prior calls to cookie_write_parameter() have changed the +** value of the user preferences cookie, this routine will cause the +** new cookie value to be included in the HTTP header for the current +** web page. This routine is a destructor for this module and should +** be called once. +** +** char *cookie_value(zPName, zDefault); +** +** Look up the value of a cookie parameter zPName. Return zDefault if +** there is no display preferences cookie or if zPName does not exist. +*/ +#include "config.h" +#include "cookies.h" +#include +#include + +#if INTERFACE +/* the standard name of the display settings cookie for fossil */ +# define DISPLAY_SETTINGS_COOKIE "fossil_display_settings" +#endif + + +/* +** State information private to this module +*/ +#define COOKIE_NPARAM 10 +static struct { + char *zCookieValue; /* Value of the user preferences cookie */ + int bChanged; /* True if any value has changed */ + int bIsInit; /* True after initialization */ + int nParam; /* Number of parameters in the cookie */ + struct { + const char *zPName; /* Name of a parameter */ + char *zPValue; /* Value of that parameter */ + } aParam[COOKIE_NPARAM]; +} cookies; + +/* Initialize this module by parsing the content of the cookie named +** by DISPLAY_SETTINGS_COOKIE +*/ +void cookie_parse(void){ + char *z; + if( cookies.bIsInit ) return; + z = (char*)P(DISPLAY_SETTINGS_COOKIE); + if( z==0 ) z = ""; + cookies.zCookieValue = z = fossil_strdup(z); + cookies.bIsInit = 1; + while( cookies.nParam0 ) blob_append(&new, ",", 1); + blob_appendf(&new, "%s=%T", + cookies.aParam[i].zPName, cookies.aParam[i].zPValue); + } + cgi_set_cookie(DISPLAY_SETTINGS_COOKIE, blob_str(&new), 0, 31536000); + } + cookies.bIsInit = 0; +} + +/* Return the value of a preference cookie. +*/ +const char *cookie_value(const char *zPName, const char *zDefault){ + int i; + assert( zPName!=0 ); + cookie_parse(); + for(i=0; i + @
    + for(i=0; cgi_param_info(i, &zName, &zValue, &isQP); i++){ + char *zDel; + if( isQP ) continue; + if( fossil_isupper(zName[0]) ) continue; + if( bFDSonly && strcmp(zName, "fossil_display_settings")!=0 ) continue; + zDel = mprintf("del%s",zName); + if( P(zDel)!=0 ){ + const char *zPath = fossil_strcmp(ROBOT_COOKIE,zName)==0 + ? "/" : 0; + cgi_set_cookie(zName, "", zPath, -1); + cgi_redirect(g.zPath); + } + nCookie++; + @
  1. %h(zName): %h(zValue) + @ + if( fossil_strcmp(zName, DISPLAY_SETTINGS_COOKIE)==0 && cookies.nParam>0 ){ + int j; + @

    This cookie remembers your Fossil display preferences. + @

      + for(j=0; j%h(cookies.aParam[j].zPName): "%h(cookies.aParam[j].zPValue)" + } + @
    + }else + if( fossil_strcmp(zName, zLoginCookie)==0 ){ + @

    This is your login cookie. If you delete this cookie, you will + @ be logged out. + }else + if( fossil_strncmp(zName, "fossil-", 7)==0 + && strlen(zName)==23 + && hex_prefix_length(&zName[7])==16 + && hex_prefix_length(zValue)>24 + ){ + @

    This appears to be a login cookie for another Fossil repository + @ in the same website. + }else + if( fossil_strcmp(zName, ROBOT_COOKIE)==0 ){ + @

    This cookie shows that your web-browser has been tested is + @ believed to be operated by a human, not a robot. + } + else { + @

    This cookie was not generated by Fossil. It might be something + @ from another program on the same website. + } + fossil_free(zDel); + } + @

+ @ + if( nCookie==0 ){ + if( bFDSonly ){ + @

Your browser is not holding a "fossil_display_setting" cookie + @ for this website

+ }else{ + @

Your browser is not holding any cookies for this website

+ } + } + style_finish_page(); +} -- cgit v1.3.1