Last active
August 28, 2019 14:55
-
-
Save selcukusta/3bf42557980d765710e42d84b90a2c64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apiVersion: networking.istio.io/v1alpha3 | |
| kind: EnvoyFilter | |
| metadata: | |
| name: cache-control-header-envoy-filter | |
| spec: | |
| workloadLabels: | |
| app: app-name | |
| filters: | |
| - listenerMatch: | |
| portNumber: 80 | |
| listenerType: SIDECAR_INBOUND # will match with the inbound listener for app-name:80 | |
| listenerProtocol: HTTP | |
| filterName: envoy.lua | |
| filterType: HTTP | |
| filterConfig: | |
| inlineCode: | | |
| function envoy_on_request(request_handle) | |
| dynamic_metadata = request_handle:streamInfo():dynamicMetadata() | |
| request_path = request_handle:headers():get(":path") | |
| if request_path ~= nil then | |
| dynamic_metadata:set("envoy.lua", "request_path", request_path) | |
| else | |
| dynamic_metadata:set("envoy.lua", "request_path", '') | |
| end | |
| end | |
| function envoy_on_response(response_handle) | |
| dynamic_metadata = response_handle:streamInfo():dynamicMetadata() | |
| local function has_value (arr, val) | |
| for index, value in ipairs(arr) do | |
| if value == val then | |
| return true | |
| end | |
| end | |
| return false | |
| end | |
| local function set_cache_control_header(arr, value) | |
| if has_value(arr, value) | |
| then | |
| response_handle:headers():add("Cache-Control", "max-age=604800, public") | |
| end | |
| end | |
| local extensions = {'css', 'scss', 'js', 'cur', 'jpg', 'jpeg', 'gif', 'htc', 'ico', 'png', 'otf', 'ttf', 'eot', 'woff', 'woff2', 'svg'} | |
| local request_path = dynamic_metadata:get("envoy.lua")["request_path"] | |
| for last_part in string.gmatch(request_path, "[^.]+$") do | |
| if last_part:match("(.-)?") then | |
| for normalized in last_part:gmatch("(.-)?") do | |
| set_cache_control_header(extensions, normalized) | |
| end | |
| else | |
| set_cache_control_header(extensions, last_part) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment