|
24 | 24 | #include <cstring>
|
25 | 25 | #include <zlib.h>
|
26 | 26 |
|
| 27 | +#include "ts/apidefs.h" |
27 | 28 | #include "tscore/ink_config.h"
|
28 | 29 |
|
| 30 | +#include <tsutil/PostScript.h> |
| 31 | + |
29 | 32 | #if HAVE_BROTLI_ENCODE_H
|
30 | 33 | #include <brotli/encode.h>
|
31 | 34 | #endif
|
@@ -76,6 +79,59 @@ static TSMutex compress_config_mutex = nullptr;
|
76 | 79 | Configuration *cur_config = nullptr;
|
77 | 80 | Configuration *prev_config = nullptr;
|
78 | 81 |
|
| 82 | +namespace |
| 83 | +{ |
| 84 | +/** |
| 85 | + If client request has both of Range and Accept-Encoding header, follow range-request config. |
| 86 | + */ |
| 87 | +void |
| 88 | +handle_range_request(TSMBuffer req_buf, TSMLoc req_loc, HostConfiguration *hc) |
| 89 | +{ |
| 90 | + TSMLoc accept_encoding_hdr_field = |
| 91 | + TSMimeHdrFieldFind(req_buf, req_loc, TS_MIME_FIELD_ACCEPT_ENCODING, TS_MIME_LEN_ACCEPT_ENCODING); |
| 92 | + ts::PostScript accept_encoding_defer([&]() -> void { TSHandleMLocRelease(req_buf, req_loc, accept_encoding_hdr_field); }); |
| 93 | + if (accept_encoding_hdr_field == TS_NULL_MLOC) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + TSMLoc range_hdr_field = TSMimeHdrFieldFind(req_buf, req_loc, TS_MIME_FIELD_RANGE, TS_MIME_LEN_RANGE); |
| 98 | + ts::PostScript range_defer([&]() -> void { TSHandleMLocRelease(req_buf, req_loc, range_hdr_field); }); |
| 99 | + if (range_hdr_field == TS_NULL_MLOC) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + debug("Both of Accept-Encoding and Range header are found in the request"); |
| 104 | + |
| 105 | + switch (hc->range_request_ctl()) { |
| 106 | + case RangeRequestCtrl::IGNORE_RANGE: { |
| 107 | + debug("Remove the Range header by ignore-range config"); |
| 108 | + while (range_hdr_field) { |
| 109 | + TSMLoc next_dup = TSMimeHdrFieldNextDup(req_buf, req_loc, range_hdr_field); |
| 110 | + TSMimeHdrFieldDestroy(req_buf, req_loc, range_hdr_field); |
| 111 | + TSHandleMLocRelease(req_buf, req_loc, range_hdr_field); |
| 112 | + range_hdr_field = next_dup; |
| 113 | + } |
| 114 | + break; |
| 115 | + } |
| 116 | + case RangeRequestCtrl::NO_COMPRESSION: { |
| 117 | + debug("Remove the Accept-Encoding header by no-compression config"); |
| 118 | + while (accept_encoding_hdr_field) { |
| 119 | + TSMLoc next_dup = TSMimeHdrFieldNextDup(req_buf, req_loc, accept_encoding_hdr_field); |
| 120 | + TSMimeHdrFieldDestroy(req_buf, req_loc, accept_encoding_hdr_field); |
| 121 | + TSHandleMLocRelease(req_buf, req_loc, accept_encoding_hdr_field); |
| 122 | + accept_encoding_hdr_field = next_dup; |
| 123 | + } |
| 124 | + break; |
| 125 | + } |
| 126 | + case RangeRequestCtrl::NONE: |
| 127 | + [[fallthrough]]; |
| 128 | + default: |
| 129 | + debug("Do nothing by none config"); |
| 130 | + break; |
| 131 | + } |
| 132 | +} |
| 133 | +} // namespace |
| 134 | + |
79 | 135 | static Data *
|
80 | 136 | data_alloc(int compression_type, int compression_algorithms)
|
81 | 137 | {
|
@@ -635,7 +691,7 @@ transformable(TSHttpTxn txnp, bool server, HostConfiguration *host_configuration
|
635 | 691 | /* Client request header */
|
636 | 692 | TSMBuffer cbuf;
|
637 | 693 | TSMLoc chdr;
|
638 |
| - TSMLoc cfield, rfield; |
| 694 | + TSMLoc cfield; |
639 | 695 |
|
640 | 696 | const char *value;
|
641 | 697 | int len;
|
@@ -677,17 +733,6 @@ transformable(TSHttpTxn txnp, bool server, HostConfiguration *host_configuration
|
677 | 733 | return 0;
|
678 | 734 | }
|
679 | 735 |
|
680 |
| - // check if Range Requests are cacheable |
681 |
| - bool range_request = host_configuration->range_request(); |
682 |
| - rfield = TSMimeHdrFieldFind(cbuf, chdr, TS_MIME_FIELD_RANGE, TS_MIME_LEN_RANGE); |
683 |
| - if (rfield != TS_NULL_MLOC && !range_request) { |
684 |
| - debug("Range header found in the request and range_request is configured as false, not compressible"); |
685 |
| - TSHandleMLocRelease(cbuf, chdr, rfield); |
686 |
| - TSHandleMLocRelease(cbuf, TS_NULL_MLOC, chdr); |
687 |
| - TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc); |
688 |
| - return 0; |
689 |
| - } |
690 |
| - |
691 | 736 | // the only compressible method is currently GET.
|
692 | 737 | int method_length;
|
693 | 738 | const char *method = TSHttpHdrMethodGet(cbuf, chdr, &method_length);
|
@@ -922,7 +967,8 @@ transform_plugin(TSCont contp, TSEvent event, void *edata)
|
922 | 967 | * 2. For global plugin, get host configuration from global config
|
923 | 968 | * For remap plugin, get host configuration from configs populated through remap
|
924 | 969 | * 3. Check for Accept encoding
|
925 |
| - * 4. Schedules TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK and TS_HTTP_TXN_CLOSE_HOOK for |
| 970 | + * 4. Remove Range header |
| 971 | + * 5. Schedules TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK and TS_HTTP_TXN_CLOSE_HOOK for |
926 | 972 | * further processing
|
927 | 973 | */
|
928 | 974 | static void
|
@@ -957,6 +1003,7 @@ handle_request(TSHttpTxn txnp, Configuration *config)
|
957 | 1003 |
|
958 | 1004 | info("Kicking off compress plugin for request");
|
959 | 1005 | normalize_accept_encoding(txnp, req_buf, req_loc);
|
| 1006 | + handle_range_request(req_buf, req_loc, hc); |
960 | 1007 | TSHttpTxnHookAdd(txnp, TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, transform_contp);
|
961 | 1008 | TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, transform_contp); // To release the config
|
962 | 1009 | }
|
|
0 commit comments