| | @@ -26,10 +26,35 @@ |
| 26 | 26 | ** Output "304 Not Modified" message and abort; |
| 27 | 27 | ** |
| 28 | 28 | ** In other words, if all conditions specified by the ETag are met, then |
| 29 | 29 | ** Fossil will return a 304 and avoid doing all the work, and all of the |
| 30 | 30 | ** bandwidth, associating with regenerating the whole page. |
| 31 | +** |
| 32 | +** To make use of this feature, page generators can invoke the |
| 33 | +** etag_require() interface with mask of ETAG_CONST, ETAG_CONFIG, |
| 34 | +** ETAG_DATA, and/or ETAG_COOKIE. Or it can invoke etag_require_hash() |
| 35 | +** with some kind of text hash. |
| 36 | +** |
| 37 | +** Or, in the WEBPAGE: line for the page generator, extra arguments |
| 38 | +** can be added. "const", "config", "data", and/or "cookie" |
| 39 | +** |
| 40 | +** ETAG_CONST const The reply is always the same for the same |
| 41 | +** build of the fossil binary. The content |
| 42 | +** is independent of the repository. |
| 43 | +** |
| 44 | +** ETAG_CONFIG config The reply is the same as long as the repository |
| 45 | +** config is constant. |
| 46 | +** |
| 47 | +** ETAG_DATA data The reply is the same as long as no new artifacts |
| 48 | +** are added to the repository |
| 49 | +** |
| 50 | +** ETAG_COOKIE cookie The reply is the same as long as the display |
| 51 | +** cookie is unchanged. |
| 52 | +** |
| 53 | +** Page generator routines can also invoke etag_require_hash(HASH) where |
| 54 | +** HASH is some string. In that case, the reply is the same as long as |
| 55 | +** the hash is the same. |
| 31 | 56 | */ |
| 32 | 57 | #include "config.h" |
| 33 | 58 | #include "etag.h" |
| 34 | 59 | |
| 35 | 60 | #if INTERFACE |
| | @@ -38,38 +63,133 @@ |
| 38 | 63 | */ |
| 39 | 64 | #define ETAG_CONST 0x00 /* Output is independent of database or parameters */ |
| 40 | 65 | #define ETAG_CONFIG 0x01 /* Output depends on the configuration */ |
| 41 | 66 | #define ETAG_DATA 0x02 /* Output depends on 'event' table */ |
| 42 | 67 | #define ETAG_COOKIE 0x04 /* Output depends on a display cookie value */ |
| 43 | | -#define ETAG_DYNAMIC 0x08 /* Output is always different */ |
| 68 | +#define ETAG_HASH 0x08 /* Output depends on a hash */ |
| 69 | +#define ETAG_DYNAMIC 0x10 /* Output is always different */ |
| 44 | 70 | #endif |
| 45 | 71 | |
| 46 | 72 | /* Set of all etag requirements */ |
| 47 | | -static int mEtag = 0; |
| 73 | +static int mEtag = 0; /* Mask of requirements */ |
| 74 | +static const char *zEHash = 0; /* Hash value if ETAG_HASH is set */ |
| 75 | + |
| 76 | + |
| 77 | +/* Check an ETag to see if all conditions are valid. If all conditions are |
| 78 | +** valid, then return true. If any condition is false, return false. |
| 79 | +*/ |
| 80 | +static int etag_valid(const char *zTag){ |
| 81 | + int iKey; |
| 82 | + char *zCk; |
| 83 | + int rc; |
| 84 | + int nTag; |
| 85 | + if( zTag==0 || zTag[0]<=0 ) return 0; |
| 86 | + nTag = (int)strlen(zTag); |
| 87 | + if( zTag[0]=='"' && zTag[nTag-1]=='"' ){ |
| 88 | + zTag++; |
| 89 | + nTag -= 2; |
| 90 | + } |
| 91 | + iKey = zTag[0] - '0'; |
| 92 | + zCk = etag_generate(iKey); |
| 93 | + rc = nTag==(int)strlen(zCk) && strncmp(zCk, zTag, nTag)==0; |
| 94 | + fossil_free(zCk); |
| 95 | + if( rc ) mEtag = iKey; |
| 96 | + return rc; |
| 97 | +} |
| 98 | + |
| 99 | +/* |
| 100 | +** Check to see if there is an If-None-Match: header that |
| 101 | +** matches the current etag settings. If there is, then |
| 102 | +** generate a 304 Not Modified reply. |
| 103 | +** |
| 104 | +** This routine exits and does not return if the 304 Not Modified |
| 105 | +** reply is generated. |
| 106 | +** |
| 107 | +** If the etag does not match, the routine returns normally. |
| 108 | +*/ |
| 109 | +static void etag_check(void){ |
| 110 | + const char *zETag = P("HTTP_IF_NONE_MATCH"); |
| 111 | + if( zETag==0 ) return; |
| 112 | + if( !etag_valid(zETag) ) return; |
| 113 | + |
| 114 | + /* If we get this far, it means that the content has |
| 115 | + ** not changed and we can do a 304 reply */ |
| 116 | + cgi_reset_content(); |
| 117 | + cgi_set_status(304, "Not Modified"); |
| 118 | + cgi_reply(); |
| 119 | + fossil_exit(0); |
| 120 | +} |
| 121 | + |
| 48 | 122 | |
| 49 | | -/* Add one or more new etag requirements */ |
| 123 | +/* Add one or more new etag requirements. |
| 124 | +** |
| 125 | +** Page generator logic invokes one or both of these methods to signal |
| 126 | +** under what conditions page generation can be skipped |
| 127 | +** |
| 128 | +** After each call to these routines, the HTTP_IF_NONE_MATCH cookie |
| 129 | +** is checked, and if it contains a compatible ETag, then a |
| 130 | +** 304 Not Modified return is generated and execution aborts. This |
| 131 | +** routine does not return if the 304 is generated. |
| 132 | +*/ |
| 50 | 133 | void etag_require(int code){ |
| 51 | 134 | mEtag |= code; |
| 135 | + etag_check(); |
| 136 | +} |
| 137 | +void etag_require_hash(const char *zHash){ |
| 138 | + if( zHash ){ |
| 139 | + zEHash = zHash; |
| 140 | + mEtag = ETAG_HASH; |
| 141 | + etag_check(); |
| 142 | + } |
| 52 | 143 | } |
| 53 | 144 | |
| 54 | | -/* Return an appropriate max-age */ |
| 145 | +/* Return an appropriate max-age. |
| 146 | +*/ |
| 55 | 147 | int etag_maxage(void){ |
| 56 | 148 | if( mEtag ) return 1; |
| 57 | 149 | return 3600*24; |
| 58 | 150 | } |
| 59 | 151 | |
| 60 | 152 | /* Generate an appropriate ETags value that captures all requirements. |
| 61 | 153 | ** Space is obtained from fossil_malloc(). |
| 154 | +** |
| 155 | +** The argument is the mask of attributes to include in the ETag. |
| 156 | +** If the argument is -1 then whatever mask is found from prior |
| 157 | +** calls to etag_require() and etag_require_hash() is used. |
| 158 | +** |
| 159 | +** Format: |
| 160 | +** |
| 161 | +** <mask><exec-mtime>/<data-or-config-key>/<cookie>/<hash> |
| 162 | +** |
| 163 | +** The <mask> is a single-character decimal number that is the mask of |
| 164 | +** all required attributes: |
| 165 | +** |
| 166 | +** ETAG_CONFIG: 1 |
| 167 | +** ETAG_DATA: 2 |
| 168 | +** ETAG_COOKIE: 4 |
| 169 | +** ETAG_HASH: 8 |
| 170 | +** |
| 171 | +** If ETAG_HASH is present, the others are omitted, so the number is |
| 172 | +** never greater than 8. |
| 173 | +** |
| 174 | +** The <exec-mtime> is the mtime of the Fossil executable. Since this |
| 175 | +** is part of the ETag, it means that recompiling or just "touch"-ing the |
| 176 | +** fossil binary is sufficient to invalidate all prior caches. |
| 177 | +** |
| 178 | +** The other elements are only present if the appropriate mask bits |
| 179 | +** appear in the first character. |
| 62 | 180 | */ |
| 63 | 181 | char *etag_generate(int m){ |
| 64 | 182 | Blob x = BLOB_INITIALIZER; |
| 65 | 183 | int mtime; |
| 66 | 184 | if( m<0 ) m = mEtag; |
| 67 | 185 | if( m & ETAG_DYNAMIC ) return 0; |
| 68 | 186 | mtime = file_mtime(g.nameOfExe, ExtFILE); |
| 69 | 187 | blob_appendf(&x,"%d%x", m, mtime); |
| 70 | | - if( m & ETAG_DATA ){ |
| 188 | + if( m & ETAG_HASH ){ |
| 189 | + blob_appendf(&x, "/%s", zEHash); |
| 190 | + }else if( m & ETAG_DATA ){ |
| 71 | 191 | int iKey = db_int(0, "SELECT max(rcvid) FROM rcvfrom"); |
| 72 | 192 | blob_appendf(&x, "/%x", iKey); |
| 73 | 193 | }else if( m & ETAG_CONFIG ){ |
| 74 | 194 | int iKey = db_int(0, "SELECT value FROM config WHERE name='cfgcnt'"); |
| 75 | 195 | blob_appendf(&x, "/%x", iKey); |
| | @@ -80,37 +200,29 @@ |
| 80 | 200 | return blob_str(&x); |
| 81 | 201 | } |
| 82 | 202 | |
| 83 | 203 | /* |
| 84 | 204 | ** COMMAND: test-etag |
| 205 | +** |
| 206 | +** Usage: fossil test-etag -key KEY-NUMBER -hash HASH |
| 207 | +** |
| 208 | +** Generate an etag given a KEY-NUMBER and/or a HASH. |
| 209 | +** |
| 210 | +** KEY-NUMBER is some combination of: |
| 211 | +** |
| 212 | +** 1 ETAG_CONFIG The config table version number |
| 213 | +** 2 ETAG_DATA The event table version number |
| 214 | +** 4 ETAG_COOKIE The display cookie |
| 85 | 215 | */ |
| 86 | 216 | void test_etag_cmd(void){ |
| 87 | | - int iKey; |
| 88 | 217 | char *zTag; |
| 218 | + const char *zHash; |
| 219 | + const char *zKey; |
| 89 | 220 | db_find_and_open_repository(0, 0); |
| 90 | | - iKey = g.argc>2 ? atoi(g.argv[2]) : 0; |
| 91 | | - zTag = etag_generate(iKey); |
| 221 | + zKey = find_option("key",0,1); |
| 222 | + zHash = find_option("hash",0,1); |
| 223 | + if( zKey ) etag_require(atoi(zKey)); |
| 224 | + if( zHash ) etag_require_hash(zHash); |
| 225 | + zTag = etag_generate(mEtag); |
| 92 | 226 | fossil_print("%s\n", zTag); |
| 93 | 227 | fossil_free(zTag); |
| 94 | 228 | } |
| 95 | | - |
| 96 | | -/* Check an ETag to see if all conditions are valid. If all conditions are |
| 97 | | -** valid, then return true. |
| 98 | | -*/ |
| 99 | | -int etag_valid(const char *zTag){ |
| 100 | | - int iKey; |
| 101 | | - char *zCk; |
| 102 | | - int rc; |
| 103 | | - int nTag; |
| 104 | | - if( zTag==0 || zTag[0]<=0 || zTag[0]>=5 ) return 0; |
| 105 | | - nTag = (int)strlen(zTag); |
| 106 | | - if( zTag[0]=='"' && zTag[nTag-1]=='"' ){ |
| 107 | | - zTag++; |
| 108 | | - nTag -= 2; |
| 109 | | - } |
| 110 | | - iKey = zTag[0] - '0'; |
| 111 | | - zCk = etag_generate(iKey); |
| 112 | | - rc = nTag==(int)strlen(zCk) && strncmp(zCk, zTag, nTag)==0; |
| 113 | | - fossil_free(zCk); |
| 114 | | - if( rc ) mEtag = iKey; |
| 115 | | - return rc; |
| 116 | | -} |
| 117 | 229 | |