This topic describes the application scenarios of EdgeScript, including authentication logic customization, request header and response header customization, rewrite and redirect customization, M3U8 rewrite customization, cache control customization, and throttling customization.

Customize an authentication logic

An example of custom authentication algorithms is as follows:
  • Requirements
    • Request URL format: /path/digest/?.ts? key=&t=
    • For .ts requests, the requirements for hotlinking protection customization are as follows:
      • Rule 1: If the request does not contain the t or key parameter, the CDN node returns status code 403 and adds the X-AUTH-MSG response header to indicate the failure cause.
      • Rule 2: The t parameter specifies the absolute expiration time. If the specified t parameter is earlier than the current time, the CDN node returns status code 403 and adds the X-AUTH-MSG response header to indicate the failure cause.
      • Rule 3: The CDN node matches the md5 parameter with the digest parameter. If md5 does not match digest, the CDN node returns status code 403.

        Value format of the md5 parameter: Private key + Path + File name.extension

  • Corresponding EdgeScript rules
    if eq(substr($uri, -3, -1), '.ts') {
    
       if or(not($arg_t), not($arg_key)) {
           add_rsp_header('X-AUTH-MSG', 'auth failed - missing necessary arg')
           exit(403)
       }
    
       t = tonumber($arg_t)
       if not(t) {
           add_rsp_header('X-AUTH-MSG', 'auth failed - invalid time')
           exit(403)
       }
    
       if gt(now(), t) {
           add_rsp_header('X-AUTH-MSG', 'auth failed - expired url')
           exit(403)
       }
    
        pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)/([^?] +)\?(.*)')
        sec1 = get(pcs, 1)
        sec2 = get(pcs, 2)
        sec3 = get(pcs, 3)
    
        if or(not(sec1), not(sec2), not(sec3)) {
            add_rsp_header('X-AUTH-MSG', 'auth failed - malformed url')
            exit(403)
        }
    
        key = 'b98d643a-9170-4937-8524-6c33514bbc23'
        signstr = concat(key, sec1, sec3)
        digest = md5(signstr)
        if ne(digest, sec2) {
            add_rsp_header('X-AUTH-DEBUG', concat('signstr: ', signstr))
            add_rsp_header('X-AUTH-MSG', 'auth failed - invalid digest')
            exit(403)
        }
    
    }

Customize request headers and response headers

An example of automatic file renaming is as follows:
  • Requirements

    If the filename parameter is specified, the file is automatically renamed with the value specified by the filename parameter. If no file name is specified, the default file name is used.

  • Corresponding EdgeScript rules
    if $arg_filename {
        hn = 'Content-Disposition'
        hv = concat('attachment;filename=', $arg_filename)
        add_rsp_header(hn, hv)
    }
    
    The following rule has the similar effect as the preceding rule, but the value for the filename parameter is enclosed in a pair of double quotation marks. The string "34" is the ASCII string for double quotation marks. It can be converted back to the quotation mark string ("") through the tochar function.
    add_rsp_header('Content-Disposition', concat('attachment;filename=', tochar(34), filename, tochar(34)))
    Output: Content-Disposition: attachment;filename="monitor.apk"

Customize rewrites or redirects

Examples of rewrite and redirect customization are as follows:
  • Rewrite a URI.
    • Requirements

      Configure CDN to rewrite /hello as /index.html. As a result, the URI of the origin request is changed to /index.html and the parameters remain unchanged.

    • Corresponding EdgeScript rules
      if match_re($uri, '^/hello$') {
          rewrite('/index.html', 'break')
      }
  • Rewrite a file extension.
    • Requirements

      Configure CDN to rewrite /1.txt as /1.<value of arg_type>. For example, /1.txt? type=mp4 will be rewritten as /1.mp4? type=mp4 for origin fetch and caching.

    • Corresponding EdgeScript rules
      if and(match_re($uri, '^/1.txt$'), $arg_type) {
           rewrite(concat('/1.', $arg_type), 'break')
      }
  • Convert a file extension to lowercase letters.
    • Requirements

      Convert the URI string to lowercase letters.

    • Corresponding EdgeScript rules
      pcs = capture_re($uri, '^(. +%.)([ ^.]+)')
      section = get(pcs, 1)
      postfix = get(pcs, 2)
      
      if and(section, postfix) {
          rewrite(concat(section, lower(postfix)), 'break')
      }
  • Add a URI prefix.
    • Requirements

      Configure CDN to rewrite ^/nn_live/(. *) as /3rd/nn_live/$1.

    • Corresponding EdgeScript rules
      pcs = capture_re($uri, '^/nn_live/(.*)')
      sec = get(pcs, 1)
      
      if sec {
           dst = concat('/3rd/nn_live/', sec)
           rewrite(dst, 'break')
      }
  • Set a 302 redirect.
    • Requirements

      Perform a 302 redirect to /app/movie/pages/index/index.html for the / root directory.

    • Corresponding EdgeScript rules
      if eq($uri, '/') {
          rewrite('/app/movie/pages/index/index.html', 'redirect')
      }
  • Set an HTTPS 302 redirect.
    • Requirements
      Redirect the following URIs that match the ^/$ root directory to https://rtmp.cdnpe.com/index.html. You can specify the redirected URI as needed.
      • http://rtmp.cdnpe.com
      • https://rtmp.cdnpe.com
    • Corresponding EdgeScript rules
      if eq($uri, '/') {
          rewrite('https://rtmp.cdnpe.com/index.html', 'redirect')
      }

Customize M3U8 rewrite rules

An example of rewriting the parameters of .ts requests is as follows:
  • Requirements

    Use the parameters in the .m3u8 request as the parameters in the .ts request.

  • Corresponding EdgeScript rules
    def rewrite_callback(line_info, user_data) {
        line = get(line_info, 'line')
        if eq(substr(line, -3, -1), '.ts') {
            if $args {
                return concat(line, '?', $args)
            }
        }
        return line
    }
    
    if eq(substr($uri, -5, -1), '.m3u8') {
        m3u8_rewrite(rewrite_callback, [])
    }

Customize cache control

An example of customizing the cache duration is as follows:
  • Requirements

    Customize the resource cache duration based on various conditions.

  • Corresponding EdgeScript rules
    if match_re($uri, '^/image') {
        set_cache_ttl('code', '301=10,302=5')
    }
    Note: Set the cache duration for status codes responded to requests to URIs that start with "/image", 10 seconds for status code 301 and 5 seconds for status code 302.
    
    if eq(substr($uri, -4, -1), '.mp4') {
        set_cache_ttl('path', 5)
    }
    if match_re($uri, '^/201801/mp4/') {
        set_cache_ttl('path', 50)
    }
    if match_re($uri, '^/201802/flv/') {
        set_cache_ttl('path', 10)
    }

Customize throttling policies

An example of customizing a throttling policy is as follows:
  • Requirements

    If the sp and unit parameters are specified, throttling is implemented. The sp parameter specifies the throttling threshold. The unit parameter specifies the unit. The unit can be KB or MB.

  • Corresponding EdgeScript rules
    if and($arg_sp, $arg_unit) {
        sp = tonumber($arg_sp)
        if not(sp) {
            add_rsp_header('X-LIMIT-DEBUG', 'invalid sp')
            return false
        }
    
        if and(ne($arg_unit, 'k'), ne($arg_unit, 'm')) {
            add_rsp_header('X-LIMIT-DEBUG', 'invalid unit')
            return false
        }
    
        add_rsp_header('X-LIMIT-DEBUG', concat('set on: ', sp, $arg_unit))
        limit_rate(sp, $arg_unit)
        return true
    }