Fossil SCM

fossil-scm / src / etag.c
Blame History Raw 289 lines
1
/*
2
** Copyright (c) 2018 D. Richard Hipp
3
**
4
** This program is free software; you can redistribute it and/or
5
** modify it under the terms of the Simplified BSD License (also
6
** known as the "2-Clause License" or "FreeBSD License".)
7
**
8
** This program is distributed in the hope that it will be useful,
9
** but without any warranty; without even the implied warranty of
10
** merchantability or fitness for a particular purpose.
11
**
12
** Author contact information:
13
** [email protected]
14
** http://www.hwaci.com/drh/
15
**
16
*******************************************************************************
17
**
18
** This file implements ETags: cache control for Fossil
19
**
20
** An ETag is a hash that encodes attributes which must be the same for
21
** the page to continue to be valid. Attributes that might be contained
22
** in the ETag include:
23
**
24
** (1) The mtime on the Fossil executable
25
** (2) The last change to the CONFIG table
26
** (3) The last change to the EVENT table
27
** (4) The value of the display cookie
28
** (5) A hash value supplied by the page generator
29
** (6) The details of the request URI
30
** (7) The name user as determined by the login cookie
31
**
32
** Item (1) is always included in the ETag. The other elements are
33
** optional. Because (1) is always included as part of the ETag, all
34
** outstanding ETags can be invalidated by touching the fossil executable.
35
**
36
** A page generator routine invokes etag_check() exactly once, with
37
** arguments that indicates which of the above elements to include in the
38
** hash. If the request contained an If-None-Match header which matches
39
** the generated ETag, then a 304 Not Modified reply is generated and
40
** the process exits. In other words, etag_check() never returns. But
41
** if there is no If-None_Match header or if the ETag does not match,
42
** then etag_check() returns normally. Later, during reply generation,
43
** the cgi.c module will invoke etag_tag() to recover the generated tag
44
** and include it in the reply header.
45
**
46
** 2018-02-25:
47
**
48
** Also support Last-Modified: and If-Modified-Since:. The
49
** etag_last_modified(mtime) API records a timestamp for the page in
50
** seconds since 1970. This causes a Last-Modified: header to be
51
** issued in the reply. Or, if the request contained If-Modified-Since:
52
** and the new mtime is not greater than the mtime associated with
53
** If-Modified-Since, then a 304 Not Modified reply is generated and
54
** the etag_last_modified() API never returns.
55
*/
56
#include "config.h"
57
#include "etag.h"
58
59
#if INTERFACE
60
/*
61
** Things to monitor
62
*/
63
#define ETAG_CONFIG 0x01 /* Output depends on the CONFIG table */
64
#define ETAG_DATA 0x02 /* Output depends on the EVENT table */
65
#define ETAG_COOKIE 0x04 /* Output depends on a display cookie value */
66
#define ETAG_HASH 0x08 /* Output depends on a hash */
67
#define ETAG_QUERY 0x10 /* Output depends on PATH_INFO and QUERY_STRING */
68
/* and the g.zLogin value */
69
#endif
70
71
static char zETag[33]; /* The generated ETag */
72
static int iMaxAge = 0; /* The max-age parameter in the reply */
73
static sqlite3_int64 iEtagMtime = 0; /* Last-Modified time */
74
static int etagCancelled = 0; /* Never send an etag */
75
76
/*
77
** Return a hash that changes every time the Fossil source code is
78
** rebuilt.
79
**
80
** The FOSSIL_BUILD_HASH string that is returned here gets computed by
81
** the mkversion utility program. The result is a hash of MANIFEST_UUID
82
** and the unix timestamp for when the mkversion utility program is run.
83
**
84
** During development rebuilds, if you need the source code id to change
85
** in order to invalidate caches, simply "touch" the "manifest" file in
86
** the top of the source directory prior to running "make" and a new
87
** FOSSIL_BUILD_HASH will be generated automatically.
88
*/
89
const char *fossil_exe_id(void){
90
return FOSSIL_BUILD_HASH;
91
}
92
93
/*
94
** Generate an ETag
95
*/
96
void etag_check(unsigned eFlags, const char *zHash){
97
const char *zIfNoneMatch;
98
char zBuf[50];
99
const int cchETag = 32; /* Not including NULL terminator. */
100
int cch; /* Length of zIfNoneMatch header. */
101
assert( zETag[0]==0 ); /* Only call this routine once! */
102
103
if( etagCancelled ) return;
104
105
/* By default, ETagged URLs never expire since the ETag will change
106
* when the content changes. Approximate this policy as 10 years. */
107
iMaxAge = 10 * 365 * 24 * 60 * 60;
108
md5sum_init();
109
110
/* Always include the executable ID as part of the hash */
111
md5sum_step_text("exe-id: ", -1);
112
md5sum_step_text(fossil_exe_id(), -1);
113
md5sum_step_text("\n", 1);
114
115
if( (eFlags & ETAG_HASH)!=0 && zHash ){
116
md5sum_step_text("hash: ", -1);
117
md5sum_step_text(zHash, -1);
118
md5sum_step_text("\n", 1);
119
iMaxAge = 0;
120
}
121
if( eFlags & ETAG_DATA ){
122
int iKey = db_int(0, "SELECT max(rcvid) FROM rcvfrom");
123
sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",iKey);
124
md5sum_step_text("data: ", -1);
125
md5sum_step_text(zBuf, -1);
126
md5sum_step_text("\n", 1);
127
iMaxAge = 60;
128
}
129
if( eFlags & ETAG_CONFIG ){
130
int iKey = db_int(0, "SELECT value FROM config WHERE name='cfgcnt'");
131
sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",iKey);
132
md5sum_step_text("config: ", -1);
133
md5sum_step_text(zBuf, -1);
134
md5sum_step_text("\n", 1);
135
iMaxAge = 3600;
136
}
137
138
/* Include the display cookie */
139
if( eFlags & ETAG_COOKIE ){
140
md5sum_step_text("display-cookie: ", -1);
141
md5sum_step_text(PD(DISPLAY_SETTINGS_COOKIE,""), -1);
142
md5sum_step_text("\n", 1);
143
iMaxAge = 0;
144
}
145
146
/* Output depends on PATH_INFO and QUERY_STRING */
147
if( eFlags & ETAG_QUERY ){
148
const char *zQS = P("QUERY_STRING");
149
md5sum_step_text("query: ", -1);
150
md5sum_step_text(PD("PATH_INFO",""), -1);
151
if( zQS ){
152
md5sum_step_text("?", 1);
153
md5sum_step_text(zQS, -1);
154
}
155
md5sum_step_text("\n",1);
156
if( g.zLogin ){
157
md5sum_step_text("login: ", -1);
158
md5sum_step_text(g.zLogin, -1);
159
md5sum_step_text("\n", 1);
160
}
161
}
162
163
/* Generate the ETag */
164
memcpy(zETag, md5sum_finish(0), cchETag+1);
165
166
/* Check to see if the generated ETag matches If-None-Match and
167
** generate a 304 reply if it does. Test both with and without
168
** double quotes. */
169
zIfNoneMatch = P("HTTP_IF_NONE_MATCH");
170
if( zIfNoneMatch==0 ) return;
171
cch = strlen(zIfNoneMatch);
172
if( cch==cchETag+2 && zIfNoneMatch[0]=='"' && zIfNoneMatch[cch-1]=='"' ){
173
if( memcmp(&zIfNoneMatch[1],zETag,cchETag)!=0 ) return;
174
}else{
175
if( strcmp(zIfNoneMatch,zETag)!=0 ) return;
176
}
177
178
/* If we get this far, it means that the content has
179
** not changed and we can do a 304 reply */
180
cgi_reset_content();
181
cgi_set_status(304, "Not Modified");
182
cgi_reply();
183
db_close(0);
184
fossil_exit(0);
185
}
186
187
/*
188
** If the output is determined purely by hash parameter and the hash
189
** is long enough to be invariant, then set the g.isConst flag, indicating
190
** that the output will never change.
191
*/
192
void etag_check_for_invariant_name(const char *zHash){
193
size_t nHash = strlen(zHash);
194
if( nHash<HNAME_MIN ){
195
return; /* Name is too short */
196
}
197
if( !validate16(zHash, (int)nHash) ){
198
return; /* Name is not pure hex */
199
}
200
g.isConst = 1; /* A long hex identifier must be a unique hash */
201
}
202
203
/*
204
** Accept a new Last-Modified time. This routine should be called by
205
** page generators that know a valid last-modified time. This routine
206
** might generate a 304 Not Modified reply and exit(), never returning.
207
** Or, if not, it will cause a Last-Modified: header to be included in the
208
** reply.
209
*/
210
void etag_last_modified(sqlite3_int64 mtime){
211
const char *zIfModifiedSince;
212
sqlite3_int64 x;
213
assert( iEtagMtime==0 ); /* Only call this routine once */
214
assert( mtime>0 ); /* Only call with a valid mtime */
215
iEtagMtime = mtime;
216
217
/* Check to see the If-Modified-Since constraint is satisfied */
218
zIfModifiedSince = P("HTTP_IF_MODIFIED_SINCE");
219
if( zIfModifiedSince==0 ) return;
220
x = cgi_rfc822_parsedate(zIfModifiedSince);
221
if( x<mtime ) return;
222
223
#if 0
224
/* If the Fossil executable is more recent than If-Modified-Since,
225
** go ahead and regenerate the resource. */
226
if( file_mtime(g.nameOfExe, ExtFILE)>x ) return;
227
#endif
228
229
/* If we reach this point, it means that the resource has not changed
230
** and that we should generate a 304 Not Modified reply */
231
cgi_reset_content();
232
cgi_set_status(304, "Not Modified");
233
cgi_reply();
234
db_close(0);
235
fossil_exit(0);
236
}
237
238
/* Return the ETag, if there is one.
239
*/
240
const char *etag_tag(void){
241
return g.isConst ? "" : zETag;
242
}
243
244
/* Return the recommended max-age
245
*/
246
int etag_maxage(void){
247
return iMaxAge;
248
}
249
250
/* Return the last-modified time in seconds since 1970. Or return 0 if
251
** there is no last-modified time.
252
*/
253
sqlite3_int64 etag_mtime(void){
254
return iEtagMtime;
255
}
256
257
/*
258
** COMMAND: test-etag
259
**
260
** Usage: fossil test-etag -key KEY-NUMBER -hash HASH
261
**
262
** Generate an etag given a KEY-NUMBER and/or a HASH.
263
**
264
** KEY-NUMBER is some combination of:
265
**
266
** 1 ETAG_CONFIG The config table version number
267
** 2 ETAG_DATA The event table version number
268
** 4 ETAG_COOKIE The display cookie
269
*/
270
void test_etag_cmd(void){
271
const char *zHash = 0;
272
const char *zKey;
273
int iKey = 0;
274
db_find_and_open_repository(0, 0);
275
zKey = find_option("key",0,1);
276
zHash = find_option("hash",0,1);
277
if( zKey ) iKey = atoi(zKey);
278
etag_check(iKey, zHash);
279
fossil_print("%s\n", etag_tag());
280
}
281
282
/*
283
** Cancel the ETag.
284
*/
285
void etag_cancel(void){
286
etagCancelled = 1;
287
zETag[0] = 0;
288
}
289

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button