-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_want_digest.c
740 lines (651 loc) · 27.9 KB
/
mod_want_digest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/* This code is licensed under the Apache License, Version 2.0
* You may obtain a copy of the license at
* http://www.apache.org/licenses/LICENSE-2.0
*
* The function 'atoq' is taken from Apache httpd's mod_negotiation without modification.
* The function 'get_entry' is a modified version from mod_negotiation.
* The instance digests are calculated using the functions implemented in the Apache runtime (MD5, SHA) and zlib (ADLER32).
* For an indication of additional information regarding copyright ownership, you are referred to the NOTICE file in Apache Software Foundation's httpd project.
*/
/* Include the required headers from httpd */
#include "httpd.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_log.h"
#include "apr_lib.h"
#include "apr_env.h"
#include "apr_strings.h"
#include "apr_md5.h"
#include "apr_sha1.h"
#include "apr_base64.h"
#include <apr_file_info.h>
#include <apr_file_io.h>
#include "util_filter.h"
#include "zlib.h"
#include "libgen.h"
module AP_MODULE_DECLARE_DATA want_digest_filter_module;
/* Define prototypes of our functions in this module */
//static void register_hooks(apr_pool_t *pool);
//static int want_digest_handler(request_rec *r);
//
//define filter names
static const char filter_name_put[] = "WANT_DIGEST_PUT";
static ap_filter_rec_t *filter_handle_put;
// struct for mapping the wanted digest algorithm and corresponding quality value from
// the 'Want-Digest' header token.
typedef struct digest_algorithm {
float quality;
char *name;
} digest_algorithm;
typedef struct st_md5 {
unsigned char digest[APR_MD5_DIGESTSIZE];
//char hex_digest[2*APR_MD5_DIGESTSIZE+1];
apr_md5_ctx_t md5;
} st_md5;
typedef struct st_sha {
unsigned char digest[APR_SHA1_DIGESTSIZE];
//char hex_digest[2*APR_SHA1_DIGESTSIZE+1];
apr_sha1_ctx_t sha1;
} st_sha;
// per-dir config with root path to hash storage dir
typedef struct wd_dir_config {
char *digest_root_dir;
char *error;
} wd_dir_config;
// struct for saving the filter context.
typedef struct want_digest_ctx {
int lock;
st_md5 *md5_ctx;
st_sha *sha_ctx;
size_t adler;
const char *filename;
char *digest_root_dir;
apr_off_t remaining;
int seen_eos;
const char *filename_base;
const char *filename_dir;
const char *digest_save_path;
const char *lock_filename;
} want_digest_ctx;
// parses q-values from a string
// taken from httpd/modules/mappers/mod_negotiation.c
static float atoq(const char *string)
{
if (!string || !*string) {
return 1.0f;
}
while (apr_isspace(*string)) {
++string;
}
/* be tolerant and accept qvalues without leading zero
* (also for backwards compat, where atof() was in use)
*/
if (*string != '.' && *string++ != '0') {
return 1.0f;
}
if (*string == '.') {
/* better only one division later, than dealing with fscking
* IEEE format 0.1 factors ...
*/
int i = 0;
if (*++string >= '0' && *string <= '9') {
i += (*string - '0') * 100;
if (*++string >= '0' && *string <= '9') {
i += (*string - '0') * 10;
if (*++string > '0' && *string <= '9') {
i += (*string - '0');
}
}
}
return (float)i / 1000.0f;
}
return 0.0f;
}
// adapted from httpd/modules/mappers/mod_negotiation.c
static const char *get_entry(apr_pool_t *p, digest_algorithm *result,
const char *accept_line)
{
result->quality = 1.0f;
/*
* Note that this handles what I gather is the "old format",
*
* Accept: text/html text/plain moo/zot
*
* without any compatibility kludges --- if the token after the
* MIME type begins with a semicolon, we know we're looking at parms,
* otherwise, we know we aren't. (So why all the pissing and moaning
* in the CERN server code? I must be missing something).
*/
result->name = ap_get_token(p, &accept_line, 0);
ap_str_tolower(result->name); /* You want case insensitive,
* you'll *get* case insensitive.
*/
/* KLUDGE!!! Default HTML to level 2.0 unless the browser
* *explicitly* says something else.
*/
while (*accept_line == ';') {
/* Parameters ... */
char *parm;
char *cp;
char *end;
++accept_line;
parm = ap_get_token(p, &accept_line, 1);
/* Look for 'var = value' --- and make sure the var is in lcase. */
for (cp = parm; (*cp && !apr_isspace(*cp) && *cp != '='); ++cp) {
*cp = apr_tolower(*cp);
}
if (!*cp) {
continue; /* No '='; just ignore it. */
}
*cp++ = '\0'; /* Delimit var */
while (apr_isspace(*cp) || *cp == '=') {
++cp;
}
if (*cp == '"') {
++cp;
for (end = cp;
(*end && *end != '\n' && *end != '\r' && *end != '\"');
end++);
}
else {
for (end = cp; (*end && !apr_isspace(*end)); end++);
}
if (*end) {
*end = '\0'; /* strip ending quote or return */
}
ap_str_tolower(cp);
if (parm[0] == 'q'
&& (parm[1] == '\0' || (parm[1] == 's' && parm[2] == '\0'))) {
result->quality = atoq(cp);
}
}
if (*accept_line == ',') {
++accept_line;
}
return accept_line;
}
// the handler function that takes care of the request.
static int want_digest_get(request_rec *r)
{
// variables
int rv, file_exists, hash_exists, len;
apr_finfo_t finfo;
apr_file_t* file;
char *filename, *hash_filename;
char buffer[512];
apr_size_t readBytes = 256;
int n, num_digests;
const char* digest_string;
// check incoming headers
if (NULL == (digest_string = apr_table_get(r->headers_in, "Want-Digest"))) return DECLINED;
// Figure out which file is being requested
filename = apr_pstrdup(r->pool, r->filename);
// Check if the file a digest is requested for exists and that it isn't a directory, otherwise don't serve the request
rv = apr_stat(&finfo, filename, APR_FINFO_NORM, r->pool);
if (rv == APR_SUCCESS)
{
file_exists = ( !(finfo.filetype & APR_NOFILE) && !(finfo.filetype & APR_DIR));
if (!file_exists) return HTTP_NOT_FOUND; // Return a 404 if not found.
}
else if (rv == 2) return HTTP_NOT_FOUND; // If apr_stat returns 2, the file does not exist. same return value as the system function stat.
else return HTTP_FORBIDDEN; // If apr_stat failed, we're probably not allowed to check this file.
// get DigestRootDir from cfg
wd_dir_config *cfg = ap_get_module_config(r->per_dir_config,
&want_digest_filter_module);
char *digest_root_dir = cfg->digest_root_dir;
// sort wanted digests into array for processing and check if chached hashes exist.
apr_array_header_t *wanted_digests;
wanted_digests = apr_array_make(r->pool, 40, sizeof(digest_algorithm));
while(*digest_string){
digest_algorithm *new = (digest_algorithm *) apr_array_push(wanted_digests);
digest_string = get_entry(r->pool, new, digest_string);
}
// Check for each algorithm if digest has been cached, if so, return from cache, otherwise calculate it
digest_algorithm *digests_to_calc = (digest_algorithm *) wanted_digests->elts;
for(int i=0; i<wanted_digests->nelts; i++)
{
// Which digest type are we looking at here?
if (!strcasecmp(digests_to_calc[i].name, "md5"))
{
hash_filename = apr_pstrcat(r->pool, digest_root_dir, filename, ".md5", NULL);
hash_exists = apr_stat(&finfo, hash_filename, APR_FINFO_NORM, r->pool);
if (hash_exists == 0)
{
rv = apr_file_open(&file, hash_filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rv == APR_SUCCESS)
{
char hash_buf[finfo.size];
char b64_digest[apr_base64_encode_len(sizeof(hash_buf))];
char final_digest[sizeof(b64_digest)+5];
rv = apr_file_read(file, &hash_buf, &finfo.size);
if (rv != APR_SUCCESS) return rv;
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, APLOGNO()
"Read MD5 digest %s from file of size %li.", hash_buf, finfo.size);
len = apr_base64_encode(b64_digest, hash_buf, sizeof(hash_buf));
snprintf(&final_digest[0], sizeof(final_digest), "MD5=%s", b64_digest);
apr_table_add(r->headers_out, "Digest", final_digest);
}
else return rv;
apr_file_close(file);
}
else
{
rv = apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rv == APR_SUCCESS)
{
unsigned char digest[APR_MD5_DIGESTSIZE];
char b64_digest[apr_base64_encode_len(sizeof(digest))];
char final_digest[sizeof(b64_digest)+5]; // length of b64_digest + "MD5=" + "\0"
int len;
apr_md5_ctx_t md5;
apr_md5_init(&md5);
while ( apr_file_read(file, buffer, &readBytes) == APR_SUCCESS ) {
apr_md5_update(&md5, buffer, readBytes);
}
apr_md5_final(digest, &md5);
len = apr_base64_encode(b64_digest, digest, sizeof(digest));
snprintf(&final_digest[0], sizeof(final_digest), "MD5=%s", b64_digest);
apr_table_add(r->headers_out, "Digest", final_digest);
}
else return rv;
apr_file_close(file);
}
}
else if (!strcasecmp(digests_to_calc[i].name, "sha"))
{
hash_filename = apr_pstrcat(r->pool, digest_root_dir, filename, ".sha", NULL);
hash_exists = apr_stat(&finfo, hash_filename, APR_FINFO_NORM, r->pool);
if (hash_exists == 0)
{
rv = apr_file_open(&file, hash_filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rv == APR_SUCCESS)
{
char hash_buf[finfo.size];
char b64_digest[apr_base64_encode_len(sizeof(hash_buf))];
char final_digest[sizeof(b64_digest)+4];
rv = apr_file_read(file, &hash_buf, &finfo.size);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, APLOGNO()
"Read SHA digest %s from file of size %li.", hash_buf, finfo.size);
len = apr_base64_encode(b64_digest, hash_buf, sizeof(hash_buf));
snprintf(&final_digest[0], sizeof(final_digest), "SHA=%s", b64_digest);
apr_table_add(r->headers_out, "Digest", final_digest);
}
else return rv;
apr_file_close(file);
}
else
{
rv = apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rv == APR_SUCCESS)
{
unsigned char digest[APR_SHA1_DIGESTSIZE];
char b64_digest[apr_base64_encode_len(sizeof(digest))];
char final_digest[sizeof(b64_digest)+5];
int len;
apr_sha1_ctx_t sha1;
apr_sha1_init(&sha1);
while ( apr_file_read(file, buffer, &readBytes) == APR_SUCCESS ) {
apr_sha1_update(&sha1, buffer, readBytes);
}
apr_sha1_final(digest, &sha1);
len = apr_base64_encode(b64_digest, digest, sizeof(digest));
snprintf(&final_digest[0], sizeof(final_digest), "SHA=%s", b64_digest);
apr_table_add(r->headers_out, "Digest", final_digest);
}
else return rv;
apr_file_close(file);
}
}
else if (!strcasecmp(digests_to_calc[i].name, "adler32"))
{
hash_filename = apr_pstrcat(r->pool, digest_root_dir, filename, ".adler32", NULL);
hash_exists = apr_stat(&finfo, hash_filename, APR_FINFO_NORM, r->pool);
if (hash_exists == 0)
{
rv = apr_file_open(&file, hash_filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rv == APR_SUCCESS)
{
char final_digest[finfo.size+9];
char hash_buf[finfo.size];
rv = apr_file_read(file, &hash_buf, &finfo.size);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, APLOGNO()
"Read ADLER32 digest %s from file %li.", hash_buf, finfo.size);
snprintf(&final_digest[0], sizeof(final_digest), "ADLER32=%s", hash_buf);
apr_table_add(r->headers_out, "Digest", final_digest);
}
else return rv;
apr_file_close(file);
}
else
{
rv = apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rv == APR_SUCCESS)
{
size_t adler = adler32_z(0L, Z_NULL, 0);
while ( apr_file_read(file, buffer, &readBytes) == APR_SUCCESS ) {
adler = adler32_z(adler, buffer, readBytes);
}
char digest[17];
snprintf(&digest[0], sizeof(digest), "ADLER32=%08lx", adler);
apr_table_add(r->headers_out, "Digest", digest);
}
else return rv;
apr_file_close(file);
}
}
else
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, APLOGNO()
"digestType: %s unknown, no header returned.", digest_string);
}
}
// Let Apache know that we responded to this request.
// Somehow, if we say OK or DONE, the request processing chain ends here and nothing else is done...
// Implementation as a dynamic filter seems to be more fitting for this kind of task...
return DECLINED;
}
// when the input filter function is added to the filter chain on PUT it calculates the hashes of the file
// on-the-fly and stores them as files in a replicated directory tree outside of the directory served by WebDAV.
static apr_status_t want_digest_put_filter(ap_filter_t *f, apr_bucket_brigade *bb,
ap_input_mode_t mode,
apr_read_type_e block,
apr_off_t readbytes)
{
apr_bucket *bucket;
apr_status_t rv;
const char *data;
char *filepath, *filename, *path, *new_path;
apr_file_t *fhandle;
apr_size_t len;
apr_finfo_t finfo;
// the context for this filter
want_digest_ctx *ctx = f->ctx;
// per-dir config
wd_dir_config *cfg = ap_get_module_config(f->r->per_dir_config,
&want_digest_filter_module);
if (mode != AP_MODE_READBYTES)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"mode was not readbytes.");
return ap_get_brigade(f->next, bb, mode, block, readbytes);
}
if (!ctx)
{
// allocate context itself
ctx = f->ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
// allocate hash ctx for md5 and sha and intialize all three
ctx->md5_ctx = apr_pcalloc(f->r->pool, sizeof(*ctx->md5_ctx));
ctx->sha_ctx = apr_pcalloc(f->r->pool, sizeof(*ctx->sha_ctx));
apr_md5_init(&(ctx->md5_ctx->md5));
apr_sha1_init(&(ctx->sha_ctx->sha1));
ctx->adler = adler32_z(0L, Z_NULL, 0);
// directory paths for ctx
ctx->filename = f->r->filename;
ctx->digest_root_dir = cfg->digest_root_dir;
ctx->filename_dir = dirname(apr_pstrdup(f->r->pool, ctx->filename));
ctx->filename_base = basename(apr_pstrdup(f->r->pool, ctx->filename));
ctx->digest_save_path = apr_pstrcat(f->r->pool, ctx->digest_root_dir, ctx->filename_dir, NULL);
rv = apr_dir_make_recursive(ctx->digest_save_path, APR_FPROT_OS_DEFAULT, f->r->pool);
// status variables in ctx
ctx->seen_eos = 0;
ctx->remaining = 0;
ctx->lock = 0;
// check for content-length, without it, we cannot proceed.
if (apr_table_get(f->r->headers_in, "Content-Length"))
{
ctx->remaining = atoi(apr_table_get(f->r->headers_in, "Content-Length"));
}
else
{
ap_remove_input_filter(f);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"No content-length given, stepping aside.");
return APR_SUCCESS;
}
// finally: check for the lockfile, if any other application is dealing with digests at the moment,
// do not interfere!
ctx->lock_filename = apr_pstrcat(f->r->pool, ctx->digest_save_path, "/", ctx->filename_base, ".lock", NULL);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"ctx->lock_filename: %s.", ctx->lock_filename);
rv = apr_stat(&finfo, ctx->lock_filename, APR_FINFO_NORM, f->r->pool);
if (rv == APR_SUCCESS && ctx->lock == 0)
{
ap_remove_input_filter(f);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"Digest lock file for %s in place, stepping aside.", ctx->filename);
return APR_SUCCESS;
}
else
{
// create lock file
rv = apr_file_open(&fhandle, ctx->lock_filename, (APR_FOPEN_WRITE|APR_FOPEN_CREATE), APR_FPROT_OS_DEFAULT, f->r->pool);
if (rv != APR_SUCCESS){
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"Unable to open lock file %s.", ctx->lock_filename);
}
rv = apr_file_close(fhandle);
ctx->lock = 1;
}
}
rv = ap_get_brigade(f->next,bb,mode,block,readbytes);
if (rv != APR_SUCCESS) return rv;
// get the buckets one by one
for (bucket = APR_BRIGADE_FIRST(bb);
bucket != APR_BRIGADE_SENTINEL(bb);
bucket = APR_BUCKET_NEXT(bucket))
{
if (APR_BUCKET_IS_EOS(bucket) || ctx->remaining == 0)
{
ctx->seen_eos = 1;
break;
}
else if (APR_BUCKET_IS_METADATA(bucket))
{
continue;
}
else if (ctx->remaining < 0)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"ctx->remaining < 0");
// delete lock file
if (ctx-> lock == 1)
{
rv = apr_file_remove(ctx->lock_filename, f->r->pool);
if (rv != APR_SUCCESS) return rv;
}
ap_remove_input_filter(f);
break;
}
else
{
rv = apr_bucket_read(bucket, &data, &len, block);
if (rv != APR_SUCCESS)
{
//error
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"Could not read from bucket...");
return rv;
}
//update all hashes here!
apr_md5_update(&(ctx->md5_ctx->md5), data, len);
apr_sha1_update(&(ctx->sha_ctx->sha1), data, len);
ctx->adler = adler32_z(ctx->adler, data, len);
ctx->remaining -= len;
}
}
if (ctx->remaining == 0 || ctx->seen_eos == 1)
{
// finalize hashes and write output files
// MD5
apr_md5_final(ctx->md5_ctx->digest, &(ctx->md5_ctx->md5));
// SHA
apr_sha1_final(ctx->sha_ctx->digest, &(ctx->sha_ctx->sha1));
//ADLER32 is already finished at this point.
// now to save the hashes!
// create directory recursively to store the file's hashes
rv = apr_dir_make_recursive(ctx->digest_save_path, APR_FPROT_OS_DEFAULT, f->r->pool);
if (rv != APR_SUCCESS) return rv;
// prepare paths
char *md5_filename = apr_pstrcat(f->r->pool, ctx->digest_save_path, "/", ctx->filename_base, ".md5", NULL);
apr_size_t md5_len = sizeof(ctx->md5_ctx->digest);
char *sha_filename = apr_pstrcat(f->r->pool, ctx->digest_save_path, "/", ctx->filename_base, ".sha", NULL);
apr_size_t sha_len = sizeof(ctx->sha_ctx->digest);
char *adler32_filename = apr_pstrcat(f->r->pool, ctx->digest_save_path, "/", ctx->filename_base, ".adler32", NULL);
char adler32[sizeof(ctx->adler)+1];
snprintf(adler32, sizeof(adler32), "%08lx", ctx->adler);
apr_size_t adler32_len = sizeof(adler32);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, f->r->server, APLOGNO()
"Saving/Caching digests for %s.", ctx->filename);
// create and write files
rv = apr_file_open(&fhandle, md5_filename, (APR_FOPEN_WRITE|APR_FOPEN_CREATE), APR_FPROT_OS_DEFAULT, f->r->pool);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_write(fhandle, &ctx->md5_ctx->digest, &md5_len);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_close(fhandle);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_open(&fhandle, sha_filename, (APR_FOPEN_WRITE|APR_FOPEN_CREATE), APR_FPROT_OS_DEFAULT, f->r->pool);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_write(fhandle, &ctx->sha_ctx->digest, &sha_len);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_close(fhandle);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_open(&fhandle, adler32_filename, (APR_FOPEN_WRITE|APR_FOPEN_CREATE), APR_FPROT_OS_DEFAULT, f->r->pool);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_write(fhandle, &adler32, &adler32_len);
if (rv != APR_SUCCESS) return rv;
rv = apr_file_close(fhandle);
if (rv != APR_SUCCESS) return rv;
// delete lock file
if (ctx-> lock == 1)
{
rv = apr_file_remove(ctx->lock_filename, f->r->pool);
if (rv != APR_SUCCESS) return rv;
}
// step aside
ap_remove_input_filter(f);
}
return APR_SUCCESS;
}
// when it is added on a DELETE, it takes care of deleting the previously hashed files and the directory if it is empty.
static apr_status_t want_digest_delete(request_rec *r)
{
int rv, empty;
apr_finfo_t finfo;
apr_dir_t *dir;
char *filename, *digest_root_dir, *path, *delete_path;
// get filename from request
filename = apr_pstrdup(r->pool, r->filename);
// get DigestRootDir from cfg
wd_dir_config *cfg = ap_get_module_config(r->per_dir_config,
&want_digest_filter_module);
digest_root_dir = cfg->digest_root_dir;
// build paths for eventually cached digests
char *md5_filename = apr_pstrcat(r->pool, digest_root_dir, filename, ".md5", NULL);
char *sha_filename = apr_pstrcat(r->pool, digest_root_dir, filename, ".sha", NULL);
char *adler_filename = apr_pstrcat(r->pool, digest_root_dir, filename, ".adler32", NULL);
// check if digests are cached for this file and delete them if they exist.
rv = apr_stat(&finfo, md5_filename, APR_FINFO_NORM, r->pool);
if (rv == APR_SUCCESS)
{
rv = apr_file_remove(md5_filename, r->pool);
}
else return rv;
rv = apr_stat(&finfo, sha_filename, APR_FINFO_NORM, r->pool);
if (rv == APR_SUCCESS)
{
rv = apr_file_remove(sha_filename, r->pool);
}
else return rv;
rv = apr_stat(&finfo, adler_filename, APR_FINFO_NORM, r->pool);
if (rv == APR_SUCCESS)
{
rv = apr_file_remove(adler_filename, r->pool);
}
else return rv;
path = dirname((char*)filename);
delete_path = apr_pstrcat(r->pool, digest_root_dir, path, NULL);
// check if directory is empty, if yes, delete it.
// ideally, we would employ a function that checks the complete directory
// tree up to digest_root_dir and deletes all empty directories on the way.
// for now it just deletes the bottom-most directory.
rv = apr_dir_open(&dir, delete_path, r->pool);
if (rv != APR_SUCCESS) return rv;
int count = 0;
empty=1;
while ((rv = apr_dir_read(&finfo,APR_FINFO_NAME, dir)) == APR_SUCCESS)
{
count++;
if (count > 2)
{
// directory contains more than just . and .., not empty!
empty=0;
break;
}
}
rv = apr_dir_close(dir);
if (rv != APR_SUCCESS) return rv;
if (empty=1)
{
// dir is empty, remove it.
rv = apr_dir_remove(delete_path, r->pool);
}
return DECLINED;
}
static void insert_filter(request_rec *r){
if (r->method_number == M_PUT ){
ap_add_input_filter_handle(filter_handle_put, NULL, r, r->connection);
}
}
static int want_digest_handler(request_rec *r)
{
if (r->method_number == M_GET) return want_digest_get(r);
else if (r->method_number == M_DELETE) return want_digest_delete(r);
else return DECLINED;
}
static const char *wd_cmd_func(cmd_parms *cmd, void *config, const char *arg1)
{
wd_dir_config *cfg = (wd_dir_config *)config;
if (arg1 != NULL)
{
cfg->digest_root_dir = (char *)arg1;
return NULL;
}
else
{
return apr_psprintf(cmd->temp_pool,
"No root directory for digest caching configured!");
}
}
/* register_hooks: Adds a hook to the httpd process */
static void register_hooks(apr_pool_t *pool)
{
// register input filter for PUT
filter_handle_put =
ap_register_input_filter(filter_name_put, want_digest_put_filter, NULL, AP_FTYPE_RESOURCE);
// hook handler for GET and DELETE, those are not handled by filters but by module functions
ap_hook_handler(want_digest_handler, NULL, NULL, APR_HOOK_FIRST);
// hook in function to add the filter to a PUT request
ap_hook_insert_filter(insert_filter, NULL, NULL, APR_HOOK_LAST);
}
static void *create_per_dir_config(apr_pool_t *p, char *s)
{
wd_dir_config *cfg = apr_pcalloc(p, sizeof(wd_dir_config));
return cfg;
}
static const command_rec wd_commands[] = {
AP_INIT_TAKE1("DigestRootDir", wd_cmd_func, NULL, ACCESS_CONF,
"Specify the root directory for storing cached digests."),
{NULL}
};
/* Define our module as an entity and assign a function for registering hooks */
module AP_MODULE_DECLARE_DATA want_digest_filter_module =
{
STANDARD20_MODULE_STUFF,
create_per_dir_config, // Per-directory configuration handler
NULL, // Merge handler for per-directory configurations
NULL, // Per-server configuration handler
NULL, // Merge handler for per-server configurations
wd_commands, // Any directives we may have for httpd
register_hooks // Our hook registering function
};