-
Notifications
You must be signed in to change notification settings - Fork 782
/
statichit.cc
738 lines (585 loc) · 21.4 KB
/
statichit.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
/** @file
Static Hit Content Serving
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <cerrno>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include <string>
#include <filesystem>
#include <getopt.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "ts/ts.h"
#include "ts/remap.h"
constexpr char PLUGIN[] = "statichit";
static DbgCtl dbg_ctl{PLUGIN};
#define VDEBUG(fmt, ...) Dbg(dbg_ctl, fmt, ##__VA_ARGS__)
#define VERROR(fmt, ...) TSError("[%s] %s: " fmt, PLUGIN, __FUNCTION__, ##__VA_ARGS__)
#define VIODEBUG(vio, fmt, ...) \
VDEBUG("vio=%p vio.cont=%p, vio.cont.data=%p, vio.vc=%p " fmt, (vio), TSVIOContGet(vio), TSContDataGet(TSVIOContGet(vio)), \
TSVIOVConnGet(vio), ##__VA_ARGS__)
static int StatCountBytes = -1;
static int StatCountResponses = -1;
static int StaticHitInterceptHook(TSCont contp, TSEvent event, void *edata);
static int StaticHitTxnHook(TSCont contp, TSEvent event, void *edata);
struct StaticHitConfig {
explicit StaticHitConfig(const std::string &fileOrDir, const std::string &mimeType, bool exact) : mimeType(mimeType)
{
std::filesystem::path base_path{fileOrDir};
if (!base_path.is_absolute()) {
base_path = std::filesystem::path(TSConfigDirGet()) / base_path;
}
base_path = std::filesystem::weakly_canonical(base_path);
if (std::filesystem::is_directory(base_path)) {
dirPath = base_path;
filePath = "";
disableExact = true;
} else {
dirPath = "";
filePath = base_path;
disableExact = exact;
}
}
~StaticHitConfig() { TSContDestroy(cont); }
std::string_view
makePath(TSHttpTxn txnp, std::string &output) const
{
std::string_view ret = {};
if (!dirPath.empty()) {
TSMBuffer reqp;
TSMLoc hdr_loc = nullptr, url_loc = nullptr;
if (TS_SUCCESS == TSHttpTxnClientReqGet(txnp, &reqp, &hdr_loc)) {
if (TS_SUCCESS == TSHttpHdrUrlGet(reqp, hdr_loc, &url_loc)) {
int path_len = 0;
auto path = TSUrlPathGet(reqp, url_loc, &path_len);
std::filesystem::path requested_file_path(
std::filesystem::weakly_canonical(dirPath / std::string_view{path, static_cast<size_t>(path_len)}));
TSHandleMLocRelease(reqp, hdr_loc, url_loc);
TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc);
if (std::equal(dirPath.begin(), dirPath.end(), requested_file_path.begin()) &&
std::filesystem::is_regular_file(requested_file_path)) {
output = requested_file_path.string();
ret = {output.c_str(), output.size()};
}
} else {
TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc);
}
}
} else {
ret = filePath;
}
return ret;
}
std::filesystem::path dirPath;
std::string filePath;
std::string mimeType = "";
int successCode = 200;
int failureCode = 404;
int maxAge = 0;
bool disableExact = false;
bool isDirectory = false;
TSCont cont = nullptr;
};
struct StaticHitRequest;
union argument_type {
void *ptr;
intptr_t ecode;
TSVConn vc;
TSVIO vio;
TSHttpTxn txn;
StaticHitRequest *trq;
argument_type(void *_p) : ptr(_p) {}
};
// This structure represents the state of a streaming I/O request. It
// is directional (ie. either a read or a write). We need two of these
// for each TSVConn; one to push data into the TSVConn and one to pull
// data out.
struct IOChannel {
TSVIO vio = nullptr;
TSIOBuffer iobuf;
TSIOBufferReader reader;
IOChannel() : iobuf(TSIOBufferSizedCreate(TS_IOBUFFER_SIZE_INDEX_32K)), reader(TSIOBufferReaderAlloc(iobuf)) {}
~IOChannel()
{
if (this->reader) {
TSIOBufferReaderFree(this->reader);
}
if (this->iobuf) {
TSIOBufferDestroy(this->iobuf);
}
}
void
read(TSVConn vc, TSCont contp)
{
this->vio = TSVConnRead(vc, contp, this->iobuf, INT64_MAX);
}
void
write(TSVConn vc, TSCont contp)
{
this->vio = TSVConnWrite(vc, contp, this->reader, INT64_MAX);
}
};
struct StaticHitHttpHeader {
TSMBuffer buffer;
TSMLoc header;
TSHttpParser parser;
StaticHitHttpHeader()
{
this->buffer = TSMBufferCreate();
this->header = TSHttpHdrCreate(this->buffer);
this->parser = TSHttpParserCreate();
}
~StaticHitHttpHeader()
{
if (this->parser) {
TSHttpParserDestroy(this->parser);
}
TSHttpHdrDestroy(this->buffer, this->header);
TSHandleMLocRelease(this->buffer, TS_NULL_MLOC, this->header);
TSMBufferDestroy(this->buffer);
}
};
struct StaticHitRequest {
StaticHitRequest() {}
off_t nbytes = 0; // Number of bytes to generate.
unsigned maxAge = 0; // Max age for cache responses.
unsigned statusCode = 200;
IOChannel readio;
IOChannel writeio;
StaticHitHttpHeader rqheader;
std::string body;
std::string mimeType;
static StaticHitRequest *
createStaticHitRequest(StaticHitConfig *tc, TSHttpTxn txn)
{
StaticHitRequest *shr = new StaticHitRequest;
std::ifstream ifstr;
std::string output;
std::string_view filePath = tc->makePath(txn, output);
VDEBUG("Requested file path: %s", filePath.data());
ifstr.open(filePath.data());
if (!ifstr) {
shr->statusCode = tc->failureCode;
return shr;
}
std::stringstream sstr;
sstr << ifstr.rdbuf();
shr->body = sstr.str();
shr->nbytes = shr->body.size();
shr->mimeType = tc->mimeType;
shr->statusCode = tc->successCode;
shr->maxAge = tc->maxAge;
return shr;
}
~StaticHitRequest() = default;
};
// Destroy a StaticHitRequest, including the per-txn continuation.
static void
StaticHitRequestDestroy(StaticHitRequest *trq, TSVIO vio, TSCont contp)
{
if (vio) {
TSVConnClose(TSVIOVConnGet(vio));
}
TSContDestroy(contp);
delete trq;
}
// NOTE: This will always append a new "field_name: value"
static void
HeaderFieldDateSet(const StaticHitHttpHeader &http, const char *field_name, int64_t field_len, time_t value)
{
TSMLoc field;
TSMimeHdrFieldCreateNamed(http.buffer, http.header, field_name, field_len, &field);
TSMimeHdrFieldValueDateSet(http.buffer, http.header, field, value);
TSMimeHdrFieldAppend(http.buffer, http.header, field);
TSHandleMLocRelease(http.buffer, http.header, field);
}
// NOTE: This will always append a new "field_name: value"
static void
HeaderFieldIntSet(const StaticHitHttpHeader &http, const char *field_name, int64_t field_len, int64_t value)
{
TSMLoc field;
TSMimeHdrFieldCreateNamed(http.buffer, http.header, field_name, field_len, &field);
TSMimeHdrFieldValueInt64Set(http.buffer, http.header, field, -1, value);
TSMimeHdrFieldAppend(http.buffer, http.header, field);
TSHandleMLocRelease(http.buffer, http.header, field);
}
// NOTE: This will always append a new "field_name: value"
static void
HeaderFieldStringSet(const StaticHitHttpHeader &http, const char *field_name, int64_t field_len, const char *value)
{
TSMLoc field;
TSMimeHdrFieldCreateNamed(http.buffer, http.header, field_name, field_len, &field);
TSMimeHdrFieldValueStringSet(http.buffer, http.header, field, -1, value, -1);
TSMimeHdrFieldAppend(http.buffer, http.header, field);
TSHandleMLocRelease(http.buffer, http.header, field);
}
static TSReturnCode
WriteResponseHeader(StaticHitRequest *trq, TSHttpStatus status)
{
StaticHitHttpHeader response;
VDEBUG("writing response header");
if (TSHttpHdrTypeSet(response.buffer, response.header, TS_HTTP_TYPE_RESPONSE) != TS_SUCCESS) {
VERROR("failed to set type");
return TS_ERROR;
}
if (TSHttpHdrVersionSet(response.buffer, response.header, TS_HTTP_VERSION(1, 1)) != TS_SUCCESS) {
VERROR("failed to set HTTP version");
return TS_ERROR;
}
if (TSHttpHdrStatusSet(response.buffer, response.header, status) != TS_SUCCESS) {
VERROR("failed to set HTTP status");
return TS_ERROR;
}
TSHttpHdrReasonSet(response.buffer, response.header, TSHttpHdrReasonLookup(status), -1);
if (status == TS_HTTP_STATUS_OK) {
// Set the Content-Length header.
HeaderFieldIntSet(response, TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH, trq->nbytes);
// Set the Cache-Control header.
if (trq->maxAge > 0) {
char buf[64];
snprintf(buf, sizeof(buf), "max-age=%u", trq->maxAge);
HeaderFieldStringSet(response, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL, buf);
HeaderFieldDateSet(response, TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED, time(nullptr));
} else {
HeaderFieldStringSet(response, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL, "no-cache");
}
HeaderFieldStringSet(response, TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE, trq->mimeType.c_str());
}
// Write the header to the IO buffer. Set the VIO bytes so that we can get a WRITE_COMPLETE
// event when this is done.
int hdrlen = TSHttpHdrLengthGet(response.buffer, response.header);
TSHttpHdrPrint(response.buffer, response.header, trq->writeio.iobuf);
TSVIONBytesSet(trq->writeio.vio, hdrlen);
TSVIOReenable(trq->writeio.vio);
TSStatIntIncrement(StatCountBytes, hdrlen);
return TS_SUCCESS;
}
static bool
StaticHitParseRequest(StaticHitRequest *trq)
{
const char *path;
int pathsz;
// Make sure this is a GET request
path = TSHttpHdrMethodGet(trq->rqheader.buffer, trq->rqheader.header, &pathsz);
if (path != TS_HTTP_METHOD_GET) {
VDEBUG("%.*s method is not supported", pathsz, path);
return false;
}
return true;
}
// Handle events from TSHttpTxnServerIntercept. The intercept
// starts with TS_EVENT_NET_ACCEPT, and then continues with
// TSVConn events.
static int
StaticHitInterceptHook(TSCont contp, TSEvent event, void *edata)
{
VDEBUG("StaticHitInterceptHook: %p ", edata);
argument_type arg(edata);
VDEBUG("contp=%p, event=%s (%d), edata=%p", contp, TSHttpEventNameLookup(event), event, arg.ptr);
switch (event) {
case TS_EVENT_NET_ACCEPT: {
// TS_EVENT_NET_ACCEPT will be delivered when the server intercept
// is set up by the core. We just need to allocate a statichit
// request state and start reading the VC.
StaticHitRequest *trq = static_cast<StaticHitRequest *>(TSContDataGet(contp));
TSStatIntIncrement(StatCountResponses, 1);
VDEBUG("allocated server intercept statichit trq=%p", trq);
// This continuation was allocated in StaticHitTxnHook. Reset the
// data to keep track of this generator request.
TSContDataSet(contp, trq);
// Start reading the request from the server intercept VC.
trq->readio.read(arg.vc, contp);
VIODEBUG(trq->readio.vio, "started reading statichit request");
return TS_EVENT_NONE;
}
case TS_EVENT_NET_ACCEPT_FAILED: {
// TS_EVENT_NET_ACCEPT_FAILED will be delivered if the
// transaction is cancelled before we start tunnelling
// through the server intercept. One way that this can happen
// is if the intercept is attached early, and then we serve
// the document out of cache.
// There's nothing to do here except nuke the continuation
// that was allocated in StaticHitTxnHook().
StaticHitRequest *trq = static_cast<StaticHitRequest *>(TSContDataGet(contp));
delete trq;
TSContDestroy(contp);
return TS_EVENT_NONE;
}
case TS_EVENT_VCONN_READ_READY: {
argument_type cdata = TSContDataGet(contp);
StaticHitHttpHeader &rqheader = cdata.trq->rqheader;
VDEBUG("reading vio=%p vc=%p, trq=%p", arg.vio, TSVIOVConnGet(arg.vio), cdata.trq);
TSIOBufferBlock blk;
TSParseResult result = TS_PARSE_CONT;
for (blk = TSIOBufferReaderStart(cdata.trq->readio.reader); blk; blk = TSIOBufferBlockNext(blk)) {
const char *ptr;
const char *end;
int64_t nbytes;
TSHttpStatus status = static_cast<TSHttpStatus>(cdata.trq->statusCode);
ptr = TSIOBufferBlockReadStart(blk, cdata.trq->readio.reader, &nbytes);
if (ptr == nullptr || nbytes == 0) {
continue;
}
end = ptr + nbytes;
result = TSHttpHdrParseReq(rqheader.parser, rqheader.buffer, rqheader.header, &ptr, end);
switch (result) {
case TS_PARSE_ERROR:
// If we got a bad request, just shut it down.
VDEBUG("bad request on trq=%p, sending an error", cdata.trq);
StaticHitRequestDestroy(cdata.trq, arg.vio, contp);
return TS_EVENT_ERROR;
case TS_PARSE_DONE:
// Check the response.
VDEBUG("parsed request on trq=%p, sending a response", cdata.trq);
if (!StaticHitParseRequest(cdata.trq)) {
status = TS_HTTP_STATUS_METHOD_NOT_ALLOWED;
}
// Start the vconn write.
cdata.trq->writeio.write(TSVIOVConnGet(arg.vio), contp);
TSVIONBytesSet(cdata.trq->writeio.vio, 0);
if (WriteResponseHeader(cdata.trq, status) != TS_SUCCESS) {
VERROR("failure writing response");
return TS_EVENT_ERROR;
}
return TS_EVENT_NONE;
case TS_PARSE_CONT:
break;
}
}
TSReleaseAssert(result == TS_PARSE_CONT);
// Reenable the read VIO to get more events.
TSVIOReenable(arg.vio);
return TS_EVENT_NONE;
}
case TS_EVENT_VCONN_WRITE_READY: {
argument_type cdata = TSContDataGet(contp);
if (cdata.trq->nbytes) {
int64_t nbytes = cdata.trq->nbytes;
VIODEBUG(arg.vio, "writing %" PRId64 " bytes for trq=%p", nbytes, cdata.trq);
nbytes = TSIOBufferWrite(cdata.trq->writeio.iobuf, cdata.trq->body.c_str(), nbytes);
cdata.trq->nbytes -= nbytes;
TSStatIntIncrement(StatCountBytes, nbytes);
// Update the number of bytes to write.
TSVIONBytesSet(arg.vio, TSVIONBytesGet(arg.vio) + nbytes);
TSVIOReenable(arg.vio);
}
return TS_EVENT_NONE;
}
case TS_EVENT_ERROR:
case TS_EVENT_VCONN_EOS: {
argument_type cdata = TSContDataGet(contp);
VIODEBUG(arg.vio, "received EOS or ERROR for trq=%p", cdata.trq);
StaticHitRequestDestroy(cdata.trq, arg.vio, contp);
return event == TS_EVENT_ERROR ? TS_EVENT_ERROR : TS_EVENT_NONE;
}
case TS_EVENT_VCONN_READ_COMPLETE:
// We read data forever, so we should never get a READ_COMPLETE.
VIODEBUG(arg.vio, "unexpected TS_EVENT_VCONN_READ_COMPLETE");
return TS_EVENT_NONE;
case TS_EVENT_VCONN_WRITE_COMPLETE: {
argument_type cdata = TSContDataGet(contp);
// If we still have bytes to write, kick off a new write operation, otherwise
// we are done and we can shut down the VC.
if (cdata.trq->nbytes) {
cdata.trq->writeio.write(TSVIOVConnGet(arg.vio), contp);
TSVIONBytesSet(cdata.trq->writeio.vio, cdata.trq->nbytes);
} else {
VIODEBUG(arg.vio, "TS_EVENT_VCONN_WRITE_COMPLETE %" PRId64 " todo", TSVIONTodoGet(arg.vio));
StaticHitRequestDestroy(cdata.trq, arg.vio, contp);
}
return TS_EVENT_NONE;
}
case TS_EVENT_TIMEOUT: {
return TS_EVENT_NONE;
}
default:
VERROR("unexpected event %s (%d) edata=%p", TSHttpEventNameLookup(event), event, arg.ptr);
return TS_EVENT_ERROR;
}
}
static void
StaticHitSetupIntercept(StaticHitConfig *cfg, TSHttpTxn txn)
{
StaticHitRequest *req = StaticHitRequest::createStaticHitRequest(cfg, txn);
if (req == nullptr) {
return;
}
TSCont cnt = TSContCreate(StaticHitInterceptHook, TSMutexCreate());
TSContDataSet(cnt, req);
TSHttpTxnServerIntercept(cnt, txn);
return;
}
// Handle events that occur on the TSHttpTxn.
static int
StaticHitTxnHook(TSCont contp, TSEvent event, void *edata)
{
argument_type arg(edata);
VDEBUG("contp=%p, event=%s (%d), edata=%p", contp, TSHttpEventNameLookup(event), event, edata);
switch (event) {
case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE: {
int method_length, status;
TSMBuffer bufp;
TSMLoc hdr_loc;
const char *method;
if (TSHttpTxnCacheLookupStatusGet(arg.txn, &status) != TS_SUCCESS) {
VERROR("failed to get client request handle");
goto done;
}
if (TSHttpTxnClientReqGet(arg.txn, &bufp, &hdr_loc) != TS_SUCCESS) {
VERROR("Couldn't retrieve client request header");
goto done;
}
method = TSHttpHdrMethodGet(bufp, hdr_loc, &method_length);
if (nullptr == method) {
VERROR("Couldn't retrieve client request method");
goto done;
}
if (status != TS_CACHE_LOOKUP_HIT_FRESH || method != TS_HTTP_METHOD_GET) {
StaticHitSetupIntercept(static_cast<StaticHitConfig *>(TSContDataGet(contp)), arg.txn);
}
break;
}
default:
VERROR("unexpected event %s (%d)", TSHttpEventNameLookup(event), event);
break;
}
done:
TSHttpTxnReenable(arg.txn, TS_EVENT_HTTP_CONTINUE);
return TS_EVENT_NONE;
}
TSReturnCode
TSRemapInit(TSRemapInterface * /* api_info */, char * /* errbuf */, int /* errbuf_size */)
{
if (TSStatFindName("statichit.response_bytes", &StatCountBytes) == TS_ERROR) {
StatCountBytes = TSStatCreate("statichit.response_bytes", TS_RECORDDATATYPE_COUNTER, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_SUM);
}
if (TSStatFindName("statichit.response_count", &StatCountResponses) == TS_ERROR) {
StatCountResponses =
TSStatCreate("statichit.response_count", TS_RECORDDATATYPE_COUNTER, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
}
return TS_SUCCESS;
}
TSRemapStatus
TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
{
const TSHttpStatus txnstat = TSHttpTxnStatusGet(rh);
if (txnstat != TS_HTTP_STATUS_NONE && txnstat != TS_HTTP_STATUS_OK) {
VDEBUG("transaction status_code=%d already set; skipping processing", static_cast<int>(txnstat));
return TSREMAP_NO_REMAP;
}
StaticHitConfig *cfg = static_cast<StaticHitConfig *>(ih);
if (!cfg) {
VERROR("No remap context available, check code / config");
TSHttpTxnStatusSet(rh, TS_HTTP_STATUS_INTERNAL_SERVER_ERROR);
return TSREMAP_NO_REMAP;
}
if (!cfg->disableExact) {
// Anchor to URL specified in remap
int pathsz;
TSUrlPathGet(rri->requestBufp, rri->requestUrl, &pathsz);
if (pathsz > 0) {
VDEBUG("Path is not an exact match. Rejecting!");
TSHttpTxnStatusSet(rh, TS_HTTP_STATUS_NOT_FOUND);
return TSREMAP_NO_REMAP;
}
}
if (!cfg->maxAge) {
TSHttpTxnConfigIntSet(rh, TS_CONFIG_HTTP_CACHE_HTTP, 0);
StaticHitSetupIntercept(static_cast<StaticHitConfig *>(ih), rh);
} else {
TSHttpTxnHookAdd(rh, TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, static_cast<StaticHitConfig *>(ih)->cont);
}
return TSREMAP_NO_REMAP; // This plugin never rewrites anything.
}
TSReturnCode
TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSED */, int /* errbuf_size ATS_UNUSED */)
{
static const struct option longopt[] = {
{"file-path", required_argument, nullptr, 'f' },
{"mime-type", required_argument, nullptr, 'm' },
{"max-age", required_argument, nullptr, 'a' },
{"failure-code", required_argument, nullptr, 'c' },
{"success-code", required_argument, nullptr, 's' },
{"disable-exact", no_argument, nullptr, 'd' },
{nullptr, no_argument, nullptr, '\0'}
};
std::string filePath;
std::string mimeType = "text/plain";
int maxAge = 0, failureCode = 0, successCode = 0;
bool disableExact = false;
// argv contains the "to" and "from" URLs. Skip the first so that the
// second one poses as the program name.
--argc;
++argv;
optind = 0;
while (true) {
int opt = getopt_long(argc, (char *const *)argv, "f:m:a:c:s:d", longopt, nullptr);
switch (opt) {
case 'f': {
filePath = std::string(optarg);
} break;
case 'm': {
mimeType = std::string(optarg);
} break;
case 'a': {
maxAge = atoi(optarg);
} break;
case 'c': {
failureCode = atoi(optarg);
} break;
case 's': {
successCode = atoi(optarg);
} break;
case 'd': {
disableExact = true;
} break;
}
if (opt == -1) {
break;
}
}
if (filePath.size() == 0) {
VERROR("Need to specify --file-path\n");
return TS_ERROR;
}
StaticHitConfig *tc = new StaticHitConfig(filePath, mimeType, disableExact);
if (maxAge > 0) {
tc->maxAge = maxAge;
}
if (failureCode > 0) {
tc->failureCode = failureCode;
}
if (successCode > 0) {
tc->successCode = successCode;
}
// Finally, create the continuation to use for this remap rule, tracking the config as cont data.
tc->cont = TSContCreate(StaticHitTxnHook, nullptr);
TSContDataSet(tc->cont, tc);
*ih = static_cast<void *>(tc);
return TS_SUCCESS;
}
void
TSRemapDeleteInstance(void *ih)
{
StaticHitConfig *tc = static_cast<StaticHitConfig *>(ih);
delete tc;
}