Advertisement
Guest User

Untitled

a guest
May 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. minime (11:58) 260/0 $ cat etc/trafficserver/yum_cache.lua
  2. --  Licensed to the Apache Software Foundation (ASF) under one
  3. --  or more contributor license agreements.  See the NOTICE file
  4. --  distributed with this work for additional information
  5. --  regarding copyright ownership.  The ASF licenses this file
  6. --  to you under the Apache License, Version 2.0 (the
  7. --  "License"); you may not use this file except in compliance
  8. --  with the License.  You may obtain a copy of the License at
  9. --
  10. --  http://www.apache.org/licenses/LICENSE-2.0
  11. --
  12. --  Unless required by applicable law or agreed to in writing, software
  13. --  distributed under the License is distributed on an "AS IS" BASIS,
  14. --  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. --  See the License for the specific language governing permissions and
  16. --  limitations under the License.
  17.  
  18. -- Some helper functions, this might be useful to move over to a Lua library, that
  19. -- we can load from several of these ATS scripts
  20. local function ends_with(str, ending)
  21.     return ending == "" or str:sub(-(#ending)) == ending
  22. end
  23.  
  24. local function ends_with_which(str, suffixes)
  25.     for _, suf in ipairs(suffixes) do
  26.         if ends_with(str, suf) then
  27.             return suf
  28.         end
  29.     end
  30.  
  31.     return nil
  32. end
  33.  
  34. -- This is the list of RPM suffixes that we should cache aggressively
  35. local YUM_SUFFIXES = {".rpm", ".srpm", ".drpm"}
  36. local YUM_DEBUG_TAG = "yum_cache"
  37.  
  38. -- The Fedora repo is like (always?) to return a 302 redirect to one of the available
  39. -- mirror. In order for this to work for us, we have to follow such redirects. 3 is an
  40. -- arbitrarily picked number, 1 ought to work in most (all?) cases. It's important that
  41. -- we cache this object with the original URL as the cache key though!
  42. function do_remap()
  43.     local uri_path = ts.client_request.get_uri()
  44.     local yum_suffix = ends_with_which(uri_path, YUM_SUFFIXES)
  45.  
  46.     ts.debug(YUM_DEBUG_TAG, "Turning on follow-redirect for this request")
  47.     ts.http.config_int_set(TS_LUA_CONFIG_HTTP_NUMBER_OF_REDIRECTIONS, 3)
  48.     ts.http.config_int_set(TS_LUA_CONFIG_HTTP_REDIRECT_USE_ORIG_CACHE_KEY, 1)
  49.  
  50.     -- This is useful to turn off the HTTP cache when testing things
  51.     -- ts.http.config_int_set(TS_LUA_CONFIG_HTTP_CACHE_HTTP, 0)
  52.  
  53.     if yum_suffix ~= nil then
  54.         ts.debug(YUM_DEBUG_TAG, "Adding read-response-header hook, YUM suffix is " .. yum_suffix)
  55.         ts.hook(TS_LUA_HOOK_READ_RESPONSE_HDR, on_read_response)
  56.     end
  57.  
  58.     return 0
  59. end
  60.  
  61. -- On origin responses, we might want to tweak the Cache-Control: max-age= header, if
  62. -- one is not already present.
  63. function on_read_response()
  64.     local response = ts.server_response.get_status()
  65.  
  66.     if response == 200 then
  67.         local cc_header = ts.server_response.header["Cache-Control"]
  68.  
  69.         if cc_header == nil then
  70.             -- Add a cache max-age of 30 days
  71.             ts.server_response.header["Cache-Control"] = "max-age=2592000"
  72.         end
  73.     end
  74.  
  75.     return 0
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement