-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetalink.cc
907 lines (674 loc) · 26.5 KB
/
metalink.cc
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#include <strings.h>
#include <openssl/sha.h>
#include <ts/ts.h>
/* Implement TS_HTTP_READ_RESPONSE_HDR_HOOK to implement a null
* transformation. Compute the SHA-256 digest of the content, write
* it to the cache and store the request URL at that key.
*
* Implement TS_HTTP_SEND_RESPONSE_HDR_HOOK to check the Location and
* Digest headers. Use TSCacheRead() to check if the URL in the
* Location header is already cached. If not, potentially rewrite
* that header. Do this after responses are cached because the cache
* will change.
*
* More details are on the [wiki page] in the Traffic Server wiki.
*
* [wiki page] https://cwiki.apache.org/confluence/display/TS/Metalink */
/* TSCacheWrite() and TSVConnWrite() data: Write the digest to the
* cache and store the request URL at that key */
typedef struct {
TSHttpTxn txnp;
TSCacheKey key;
TSVConn connp;
TSIOBuffer cache_bufp;
} WriteData;
/* TSTransformCreate() data: Compute the SHA-256 digest of the content */
typedef struct {
TSHttpTxn txnp;
/* Null transformation */
TSIOBuffer output_bufp;
TSVIO output_viop;
/* Message digest handle */
SHA256_CTX c;
} TransformData;
/* TSCacheRead() and TSVConnRead() data: Check the Location and Digest
* headers */
typedef struct {
TSHttpTxn txnp;
TSMBuffer resp_bufp;
TSMLoc hdr_loc;
/* Location header */
TSMLoc location_loc;
/* Cache key */
TSMLoc url_loc;
TSCacheKey key;
/* Digest header */
TSMLoc digest_loc;
/* Digest header field value index */
int idx;
TSVConn connp;
TSIOBuffer cache_bufp;
const char *value;
int64_t length;
} SendData;
/* Implement TS_HTTP_READ_RESPONSE_HDR_HOOK to implement a null
* transformation */
/* Write the digest to the cache and store the request URL at that key */
static int
cache_open_write(TSCont contp, void *edata)
{
TSMBuffer req_bufp;
TSMLoc hdr_loc;
TSMLoc url_loc;
char *value;
int length;
WriteData *data = (WriteData *) TSContDataGet(contp);
data->connp = (TSVConn) edata;
TSCacheKeyDestroy(data->key);
if (TSHttpTxnClientReqGet(data->txnp, &req_bufp, &hdr_loc) != TS_SUCCESS) {
TSError("Couldn't retrieve client request header");
TSContDestroy(contp);
TSfree(data);
return 0;
}
if (TSHttpHdrUrlGet(req_bufp, hdr_loc, &url_loc) != TS_SUCCESS) {
TSContDestroy(contp);
TSfree(data);
TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, hdr_loc);
return 0;
}
/* Allocation! Must free! */
value = TSUrlStringGet(req_bufp, url_loc, &length);
if (!value) {
TSContDestroy(contp);
TSfree(data);
TSHandleMLocRelease(req_bufp, hdr_loc, url_loc);
TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, hdr_loc);
return 0;
}
TSHandleMLocRelease(req_bufp, hdr_loc, url_loc);
TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, hdr_loc);
/* Store the request URL */
data->cache_bufp = TSIOBufferCreate();
TSIOBufferReader readerp = TSIOBufferReaderAlloc(data->cache_bufp);
int nbytes = TSIOBufferWrite(data->cache_bufp, value, length);
TSfree(value);
/* Reentrant! Reuse the TSCacheWrite() continuation. */
TSVConnWrite(data->connp, contp, readerp, nbytes);
return 0;
}
/* Do nothing */
static int
cache_open_write_failed(TSCont contp, void */* edata ATS_UNUSED */)
{
WriteData *data = (WriteData *) TSContDataGet(contp);
TSContDestroy(contp);
TSCacheKeyDestroy(data->key);
TSfree(data);
return 0;
}
static int
write_vconn_write_complete(TSCont contp, void */* edata ATS_UNUSED */)
{
WriteData *data = (WriteData *) TSContDataGet(contp);
TSContDestroy(contp);
/* The object is not committed to the cache until the VConnection is
* closed. When all the data has been transferred, the user (contp)
* must do a TSVConnClose() */
TSVConnClose(data->connp);
TSIOBufferDestroy(data->cache_bufp);
TSfree(data);
return 0;
}
/* TSCacheWrite() and TSVConnWrite() handler: Write the digest to the
* cache and store the request URL at that key */
static int
write_handler(TSCont contp, TSEvent event, void *edata)
{
switch (event) {
case TS_EVENT_CACHE_OPEN_WRITE:
return cache_open_write(contp, edata);
case TS_EVENT_CACHE_OPEN_WRITE_FAILED:
return cache_open_write_failed(contp, edata);
case TS_EVENT_VCONN_WRITE_COMPLETE:
return write_vconn_write_complete(contp, edata);
default:
TSAssert(!"Unexpected event");
}
return 0;
}
/* Copy content from the input buffer to the output buffer without
* modification and feed it through the message digest at the same
* time.
*
* 1. Check if we are "closed" before doing anything else to avoid
* errors.
*
* 2. Then deal with any input that's available now.
*
* 3. Check if the input is complete after dealing with any
* available input in case it was the last of it. If it is
* complete, tell downstream, thank upstream, and finish
* computing the digest. Otherwise either wait for more input
* or abort if upstream is "closed".
*
* The handler is guaranteed to get called at least once, even if the
* response is 304 Not Modified, so we are guaranteed an opportunity
* to clean up e.g. data that we allocated when we called
* TSTransformCreate().
*
* TS_EVENT_VCONN_WRITE_READY and TS_EVENT_VCONN_WRITE_COMPLETE events
* are sent from downstream, e.g. by
* TransformTerminus::handle_event(). TS_EVENT_IMMEDIATE events are
* sent by INKVConnInternal::do_io_write(),
* INKVConnInternal::do_io_close(), and INKVConnInternal::reenable()
* which are called from upstream, e.g. by
* TransformVConnection::do_io_write(),
* TransformVConnection::do_io_close(), and
* HttpTunnel::producer_handler().
*
* Clean up the output buffer on TS_EVENT_VCONN_WRITE_COMPLETE and not
* before. We are guaranteed a TS_EVENT_VCONN_WRITE_COMPLETE event
* *unless* we are "closed". In that case we instead get a
* TS_EVENT_IMMEDIATE event where TSVConnClosedGet() is one. We'll
* only ever get one event where TSVConnClosedGet() is one and it will
* be our last, so we *must* check for this case and clean up then
* too. Because we'll only ever get one such event and it will be our
* last, there's no risk of double freeing.
*
* The response headers get sent when TSVConnWrite() gets called and
* not before. (We could potentially edit them until then.)
*
* The events say nothing about the state of the input. Gather this
* instead from TSVConnClosedGet(), TSVIOReaderGet(), and
* TSVIONTodoGet() and handle the end of the input independently from
* the TS_EVENT_VCONN_WRITE_COMPLETE event from downstream.
*
* When TSVConnClosedGet() is one, *we* are "closed". We *must* check
* for this case, if only to clean up allocated data. Some state is
* already inconsistent. (This happens when the response is 304 Not
* Modified or when the client or origin disconnect before the message
* is complete.)
*
* When TSVIOReaderGet() is NULL, upstream is "closed". In that case
* it's clearly an error to call TSIOBufferReaderAvail(), it's also an
* error at that point to send any events upstream with TSContCall().
* (This happens when the content length is zero or when we get the
* final chunk of a chunked response.)
*
* The input is complete only when TSVIONTodoGet() is zero. (Don't
* update the downstream nbytes otherwise!) Update the downstream
* nbytes when the input is complete in case the response is chunked
* (in which case nbytes is unknown until then). Downstream will
* (normally) send the TS_EVENT_VCONN_WRITE_COMPLETE event (and the
* final chunk if the response is chunked) when ndone equals nbytes
* and not before.
*
* Send our own TS_EVENT_VCONN_WRITE_COMPLETE event upstream when the
* input is complete otherwise HttpSM::update_stats() won't get called
* and the transaction won't get logged. (If there are upstream
* transformations they won't get a chance to clean up otherwise!)
*
* Summary of the cases each event can fall into:
*
* Closed *We* are "closed". Clean up allocated data.
* │
* ├ Start First (and last) time the handler was called.
* │ (This happens when the response is 304 Not
* │ Modified.)
* │
* └ Not start (This happens when the client or origin disconnect
* before the message is complete.)
*
* Start First time the handler was called. Initialize
* │ data here because we can't call TSVConnWrite()
* │ before TS_HTTP_RESPONSE_TRANSFORM_HOOK.
* │
* ├ Content length
* │
* └ Chunked response
*
* Upstream closed
* (This happens when the content length is zero or
* when we get the final chunk of a chunked
* response.)
*
* Available input
*
* Input complete
* │
* ├ Deja vu There might be multiple TS_EVENT_IMMEDIATE events
* │ between the end of the input and the
* │ TS_EVENT_VCONN_WRITE_COMPLETE event from
* │ downstream.
* │
* └ Not deja vu
* Tell downstream and thank upstream.
*
* Downstream complete
* Clean up the output buffer. */
static int
vconn_write_ready(TSCont contp, void */* edata ATS_UNUSED */)
{
const char *value;
int64_t length;
char digest[32]; /* SHA-256 */
TransformData *transform_data = (TransformData *) TSContDataGet(contp);
/* Check if we are "closed" before doing anything else to avoid
* errors. We *must* check for this case, if only to clean up
* allocated data. Some state is already inconsistent. (This
* happens if the response is 304 Not Modified or if the client or
* origin disconnect before the message is complete.) */
int closed = TSVConnClosedGet(contp);
if (closed) {
TSContDestroy(contp);
/* Avoid failed assert "sdk_sanity_check_iocore_structure(bufp) ==
* TS_SUCCESS" in TSIOBufferDestroy() if the response is 304 Not
* Modified */
if (transform_data->output_bufp) {
TSIOBufferDestroy(transform_data->output_bufp);
}
TSfree(transform_data);
return 0;
}
TSVIO input_viop = TSVConnWriteVIOGet(contp);
/* Initialize data here because we can't call TSVConnWrite() before
* TS_HTTP_RESPONSE_TRANSFORM_HOOK */
if (!transform_data->output_bufp) {
TSVConn output_connp = TSTransformOutputVConnGet(contp);
transform_data->output_bufp = TSIOBufferCreate();
TSIOBufferReader readerp = TSIOBufferReaderAlloc(transform_data->output_bufp);
/* Determines the Content-Length header (or a chunked response) */
/* Reentrant! Avoid failed assert "nbytes >= 0" if the response
* is chunked. */
int nbytes = TSVIONBytesGet(input_viop);
transform_data->output_viop = TSVConnWrite(output_connp, contp, readerp, nbytes < 0 ? INT64_MAX : nbytes);
SHA256_Init(&transform_data->c);
}
/* Then deal with any input that's available now. Avoid failed
* assert "sdk_sanity_check_iocore_structure(readerp) == TS_SUCCESS"
* in TSIOBufferReaderAvail() if the content length is zero or when
* we get the final chunk of a chunked response. */
TSIOBufferReader readerp = TSVIOReaderGet(input_viop);
if (readerp) {
int avail = TSIOBufferReaderAvail(readerp);
if (avail) {
TSIOBufferCopy(transform_data->output_bufp, readerp, avail, 0);
/* Feed content to the message digest */
TSIOBufferBlock blockp = TSIOBufferReaderStart(readerp);
while (blockp) {
/* No allocation? */
value = TSIOBufferBlockReadStart(blockp, readerp, &length);
SHA256_Update(&transform_data->c, value, length);
blockp = TSIOBufferBlockNext(blockp);
}
TSIOBufferReaderConsume(readerp, avail);
/* Call TSVIONDoneSet() for TSVIONTodoGet() condition */
int ndone = TSVIONDoneGet(input_viop);
TSVIONDoneSet(input_viop, ndone + avail);
}
}
/* Check if the input is complete after dealing with any available
* input in case it was the last of it */
int ntodo = TSVIONTodoGet(input_viop);
if (ntodo) {
TSVIOReenable(transform_data->output_viop);
TSContCall(TSVIOContGet(input_viop), TS_EVENT_VCONN_WRITE_READY, input_viop);
/* Don't finish computing the digest (and tell downstream and thank
* upstream) more than once! There might be multiple
* TS_EVENT_IMMEDIATE events between the end of the input and the
* TS_EVENT_VCONN_WRITE_COMPLETE event from downstream, e.g.
* INKVConnInternal::reenable() is called by
* HttpTunnel::producer_handler() when more input is available and
* TransformVConnection::do_io_shutdown() is called by
* HttpSM::tunnel_handler_transform_write() when we send our own
* TS_EVENT_VCONN_WRITE_COMPLETE event upstream. */
} else if (transform_data->txnp) {
int ndone = TSVIONDoneGet(input_viop);
TSVIONBytesSet(transform_data->output_viop, ndone);
TSVIOReenable(transform_data->output_viop);
/* Avoid failed assert "c->alive == true" in TSContCall() if the
* content length is zero or when we get the final chunk of a
* chunked response */
if (readerp) {
TSContCall(TSVIOContGet(input_viop), TS_EVENT_VCONN_WRITE_COMPLETE, input_viop);
}
/* Write the digest to the cache */
SHA256_Final((unsigned char *) digest, &transform_data->c);
WriteData *write_data = (WriteData *) TSmalloc(sizeof(WriteData));
write_data->txnp = transform_data->txnp;
/* Don't finish computing the digest more than once! */
transform_data->txnp = NULL;
write_data->key = TSCacheKeyCreate();
if (TSCacheKeyDigestSet(write_data->key, digest, sizeof(digest)) != TS_SUCCESS) {
TSCacheKeyDestroy(write_data->key);
TSfree(write_data);
return 0;
}
/* Can't reuse the TSTransformCreate() continuation because we
* don't know whether to destroy it in
* cache_open_write()/cache_open_write_failed() or
* transform_vconn_write_complete() */
contp = TSContCreate(write_handler, NULL);
TSContDataSet(contp, write_data);
/* Reentrant! */
TSCacheWrite(contp, write_data->key);
}
return 0;
}
static int
transform_vconn_write_complete(TSCont contp, void */* edata ATS_UNUSED */)
{
TransformData *data = (TransformData *) TSContDataGet(contp);
TSContDestroy(contp);
TSIOBufferDestroy(data->output_bufp);
TSfree(data);
return 0;
}
/* TSTransformCreate() handler: Compute the SHA-256 digest of the
* content */
static int
transform_handler(TSCont contp, TSEvent event, void *edata)
{
switch (event) {
case TS_EVENT_IMMEDIATE:
case TS_EVENT_VCONN_WRITE_READY:
return vconn_write_ready(contp, edata);
case TS_EVENT_VCONN_WRITE_COMPLETE:
return transform_vconn_write_complete(contp, edata);
default:
TSAssert(!"Unexpected event");
}
return 0;
}
/* Compute the SHA-256 digest of the content, write it to the cache
* and store the request URL at that key */
static int
http_read_response_hdr(TSCont /* contp ATS_UNUSED */, void *edata)
{
TransformData *data = (TransformData *) TSmalloc(sizeof(TransformData));
data->txnp = (TSHttpTxn) edata;
/* Can't initialize data here because we can't call TSVConnWrite()
* before TS_HTTP_RESPONSE_TRANSFORM_HOOK */
data->output_bufp = NULL;
TSVConn connp = TSTransformCreate(transform_handler, data->txnp);
TSContDataSet(connp, data);
TSHttpTxnHookAdd(data->txnp, TS_HTTP_RESPONSE_TRANSFORM_HOOK, connp);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
return 0;
}
/* Implement TS_HTTP_SEND_RESPONSE_HDR_HOOK to check the Location and
* Digest headers */
/* Read the URL stored at the digest */
static int
cache_open_read(TSCont contp, void *edata)
{
SendData *data = (SendData *) TSContDataGet(contp);
data->connp = (TSVConn) edata;
data->cache_bufp = TSIOBufferCreate();
/* Reentrant! Reuse the TSCacheRead() continuation. */
TSVConnRead(data->connp, contp, data->cache_bufp, INT64_MAX);
return 0;
}
/* Do nothing, just reenable the response */
static int
cache_open_read_failed(TSCont contp, void */* edata ATS_UNUSED */)
{
SendData *data = (SendData *) TSContDataGet(contp);
TSContDestroy(contp);
TSCacheKeyDestroy(data->key);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
/* TSCacheRead() handler: Check if the URL stored at the digest is
* cached */
static int
rewrite_handler(TSCont contp, TSEvent event, void */* edata ATS_UNUSED */)
{
SendData *data = (SendData *) TSContDataGet(contp);
TSContDestroy(contp);
TSCacheKeyDestroy(data->key);
switch (event) {
/* Yes: Rewrite the Location header and reenable the response */
case TS_EVENT_CACHE_OPEN_READ:
TSMimeHdrFieldValuesClear(data->resp_bufp, data->hdr_loc, data->location_loc);
TSMimeHdrFieldValueStringInsert(data->resp_bufp, data->hdr_loc, data->location_loc, -1, data->value, data->length);
break;
/* No: Do nothing, just reenable the response */
case TS_EVENT_CACHE_OPEN_READ_FAILED:
break;
default:
TSAssert(!"Unexpected event");
}
TSIOBufferDestroy(data->cache_bufp);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
/* Read the URL stored at the digest */
static int
vconn_read_ready(TSCont contp, void */* edata ATS_UNUSED */)
{
SendData *data = (SendData *) TSContDataGet(contp);
TSContDestroy(contp);
TSVConnClose(data->connp);
TSIOBufferReader readerp = TSIOBufferReaderAlloc(data->cache_bufp);
TSIOBufferBlock blockp = TSIOBufferReaderStart(readerp);
/* No allocation, freed with data->cache_bufp? */
const char *value = data->value = TSIOBufferBlockReadStart(blockp, readerp, &data->length);
/* The start pointer is both an input and an output parameter.
* After a successful parse the start pointer equals the end
* pointer. */
if (TSUrlParse(data->resp_bufp, data->url_loc, &value, value + data->length) != TS_PARSE_DONE) {
TSIOBufferDestroy(data->cache_bufp);
TSCacheKeyDestroy(data->key);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
if (TSCacheKeyDigestFromUrlSet(data->key, data->url_loc) != TS_SUCCESS) {
TSIOBufferDestroy(data->cache_bufp);
TSCacheKeyDestroy(data->key);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
/* Check if the URL stored at the digest is cached */
contp = TSContCreate(rewrite_handler, NULL);
TSContDataSet(contp, data);
/* Reentrant! (Particularly in case of a cache miss.)
* rewrite_handler() will clean up the TSVConnRead() buffer so be
* sure to close this virtual connection or CacheVC::openReadMain()
* will continue operating on it! */
TSCacheRead(contp, data->key);
return 0;
}
/* TSCacheRead() and TSVConnRead() handler: Check if the digest
* already exists in the cache */
static int
digest_handler(TSCont contp, TSEvent event, void *edata)
{
switch (event) {
/* Yes: Read the URL stored at that key */
case TS_EVENT_CACHE_OPEN_READ:
return cache_open_read(contp, edata);
/* No: Do nothing, just reenable the response */
case TS_EVENT_CACHE_OPEN_READ_FAILED:
return cache_open_read_failed(contp, edata);
case TS_EVENT_VCONN_READ_READY:
return vconn_read_ready(contp, edata);
default:
TSAssert(!"Unexpected event");
}
return 0;
}
/* TSCacheRead() handler: Check if the Location URL is already cached */
static int
location_handler(TSCont contp, TSEvent event, void */* edata ATS_UNUSED */)
{
const char *value;
int length;
char digest[33]; /* ATS_BASE64_DECODE_DSTLEN() */
SendData *data = (SendData *) TSContDataGet(contp);
TSContDestroy(contp);
switch (event) {
/* Yes: Do nothing, just reenable the response */
case TS_EVENT_CACHE_OPEN_READ:
break;
/* No: Check if the digest already exists in the cache */
case TS_EVENT_CACHE_OPEN_READ_FAILED:
/* No allocation, freed with data->resp_bufp? */
value = TSMimeHdrFieldValueStringGet(data->resp_bufp, data->hdr_loc, data->digest_loc, data->idx, &length);
if (TSBase64Decode(value + 8, length - 8, (unsigned char *) digest, sizeof(digest), NULL) != TS_SUCCESS
|| TSCacheKeyDigestSet(data->key, digest, 32 /* SHA-256 */ ) != TS_SUCCESS) {
break;
}
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->digest_loc);
/* Check if the digest already exists in the cache */
contp = TSContCreate(digest_handler, NULL);
TSContDataSet(contp, data);
/* Reentrant! */
TSCacheRead(contp, data->key);
return 0;
default:
TSAssert(!"Unexpected event");
}
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->digest_loc);
TSCacheKeyDestroy(data->key);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
/* Use TSCacheRead() to check if the URL in the Location header is
* already cached. If not, potentially rewrite that header. Do this
* after responses are cached because the cache will change. */
static int
http_send_response_hdr(TSCont contp, void *edata)
{
const char *value;
int length;
SendData *data = (SendData *) TSmalloc(sizeof(SendData));
data->txnp = (TSHttpTxn) edata;
if (TSHttpTxnClientRespGet(data->txnp, &data->resp_bufp, &data->hdr_loc) != TS_SUCCESS) {
TSError("Couldn't retrieve client response header");
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
/* If Instance Digests are not provided by the Metalink servers, the
* Link header fields pertaining to this specification MUST be
* ignored */
/* Metalinks contain whole file hashes as described in Section 6,
* and MUST include SHA-256, as specified in [FIPS-180-3] */
/* Assumption: We want to minimize cache reads, so check first that
*
* 1. the response has a Location header and
*
* 2. the response has a Digest header.
*
* Then scan if the URL or digest already exist in the cache. */
/* If the response has a Location header */
data->location_loc = TSMimeHdrFieldFind(data->resp_bufp, data->hdr_loc, TS_MIME_FIELD_LOCATION, TS_MIME_LEN_LOCATION);
if (!data->location_loc) {
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
TSUrlCreate(data->resp_bufp, &data->url_loc);
/* If we can't parse or lookup the Location URL, should we still
* check if the response has a Digest header? No: Can't parse or
* lookup the URL in the Location header is an error. */
/* No allocation, freed with data->resp_bufp? */
value = TSMimeHdrFieldValueStringGet(data->resp_bufp, data->hdr_loc, data->location_loc, -1, &length);
if (TSUrlParse(data->resp_bufp, data->url_loc, &value, value + length) != TS_PARSE_DONE) {
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
data->key = TSCacheKeyCreate();
if (TSCacheKeyDigestFromUrlSet(data->key, data->url_loc) != TS_SUCCESS) {
TSCacheKeyDestroy(data->key);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
/* ... and a Digest header */
data->digest_loc = TSMimeHdrFieldFind(data->resp_bufp, data->hdr_loc, "Digest", 6);
while (data->digest_loc) {
int count = TSMimeHdrFieldValuesCount(data->resp_bufp, data->hdr_loc, data->digest_loc);
for (data->idx = 0; data->idx < count; data->idx += 1) {
/* No allocation, freed with data->resp_bufp? */
value = TSMimeHdrFieldValueStringGet(data->resp_bufp, data->hdr_loc, data->digest_loc, data->idx, &length);
if (length < 8 + 44 /* 32 bytes, Base64 */ || strncasecmp(value, "SHA-256=", 8)) {
continue;
}
/* Check if the Location URL is already cached */
contp = TSContCreate(location_handler, NULL);
TSContDataSet(contp, data);
/* Reentrant! */
TSCacheRead(contp, data->key);
return 0;
}
TSMLoc next_loc = TSMimeHdrFieldNextDup(data->resp_bufp, data->hdr_loc, data->digest_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->digest_loc);
data->digest_loc = next_loc;
}
/* Didn't find a Digest header, just reenable the response */
TSCacheKeyDestroy(data->key);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->url_loc);
TSHandleMLocRelease(data->resp_bufp, data->hdr_loc, data->location_loc);
TSHandleMLocRelease(data->resp_bufp, TS_NULL_MLOC, data->hdr_loc);
TSHttpTxnReenable(data->txnp, TS_EVENT_HTTP_CONTINUE);
TSfree(data);
return 0;
}
static int
handler(TSCont contp, TSEvent event, void *edata)
{
switch (event) {
case TS_EVENT_HTTP_READ_RESPONSE_HDR:
return http_read_response_hdr(contp, edata);
case TS_EVENT_HTTP_SEND_RESPONSE_HDR:
return http_send_response_hdr(contp, edata);
default:
TSAssert(!"Unexpected event");
}
return 0;
}
void
TSPluginInit(int /* argc ATS_UNUSED */, const char */* argv ATS_UNUSED */[])
{
TSPluginRegistrationInfo info;
info.plugin_name = (char *) "metalink";
info.vendor_name = (char *) "Jack Bates";
info.support_email = (char *) "[email protected]";
if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) {
TSError("Plugin registration failed");
}
TSCont contp = TSContCreate(handler, NULL);
TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, contp);
TSHttpHookAdd(TS_HTTP_SEND_RESPONSE_HDR_HOOK, contp);
}