17. URI Processing

The CDN RulesEngine allows for custom processing of a client’s URI.

UC1: URI Processing for Auth

Example:

A request Lua script to change authentication URI for token by adding /auth at the end of the path:

local leapfrog = leapfrog
local fphttp = fphttp

local req = leapfrog.get_request(txn)
local _, _, path, query, _ = fphttp.parse_uri(fphttp.get_uri(req))
leapfrog.set_prop(txn, 'uri.auth=' .. path .. "/auth?" .. query)
Token generation for an example path:

Path to be signed: /test_auth_uri_change/auth
Result token: 081cbfadbbf15aff5723a
Complete URI:
/test_auth_uri_change/auth?hash=081cbfadbbf15aff5723a
Actual request:

/test_auth_uri_change?hash=081cbfadbbf15aff5723a

UC2: URI Processing for Cache Key

Example:

A request Lua script to modify cache key by removing the 1st level directory:

local leapfrog = leapfrog
local fphttp = fphttp

local req = leapfrog.get_request(txn)
local _, _, path, query, _ = fphttp.parse_uri(fphttp.get_uri(req))

local first, rest = path:match("(/[^/]+)(/.*)")

if first ~= nil and rest ~= nil then
  local new_path = rest
  if query ~= nil then
    new_path = new_path .. "?" .. query
  end
  leapfrog.set_prop(txn, 'uri.cache_key=' .. new_path)
end
If any token authentication is configured, it will be performed against the original path (unless another script modifies uri.auth property).

UC3: URI Processing for a combination

Example:

A request script that removes the 1st directory from the path of all 3 properties at once:

local leapfrog = leapfrog
local fphttp = fphttp

local req = leapfrog.get_request(txn)
local _, _, path, query, _ = fphttp.parse_uri(fphttp.get_uri(req))

local first, rest = path:match("(/[^/]+)(/.*)")

if first ~= nil and rest ~= nil then
  local new_path = rest
  if query ~= nil then
    new_path = new_path .. "?" .. query
  end
  leapfrog.set_prop(txn, 'uri.**=' .. new_path)
end
selective qshmode

Remove a query string parameter "clientid" from cache key (only)

local leapfrog = leapfrog
local fphttp = fphttp

local req = leapfrog.get_request(txn)
local _, _, path, query, _ = fphttp.parse_uri(fphttp.get_uri(req))

if query ~= nil then
  query = fphttp.query_delete_param(query, "clientid")
  leapfrog.set_prop(txn, 'uri.cache_key=' .. path .. "?" .. query)
end
Use only some elements of query string in the cache key - ver, size and type

local leapfrog = leapfrog
local fphttp = fphttp

local qshmode = { "ver", "size", "type" }

local req = leapfrog.get_request(txn)
local _, _, path, query, _ = fphttp.parse_uri(fphttp.get_uri(req))

if query ~= nil then
  local i, v
  local new_query = ""

  for i = 1,#qshmode do
    v = fphttp.query_find_param(query, qshmode[i])
    if v ~= nil then
      new_query = fphttp.query_add_param(new_query, qshmode[i] .. "=" .. v)
    end
  end
  leapfrog.set_prop(txn, 'uri.cache_key=' .. path .. "?" .. new_query)
end