class Behavior

Attributes

output_array_for_backend_resp[R]
output_array_for_hash[R]
output_array_for_recv[R]

Public Class Methods

new(cache_behavior) click to toggle source
# File lib/Behavior.rb, line 3
def initialize(cache_behavior)
  @backend_name = delete_symbol(cache_behavior.target_origin_id)
  @allowed_methods = cache_behavior.allowed_methods.items
  @cached_methods = cache_behavior.allowed_methods.cached_methods.items
  @forward_headers = cache_behavior.forwarded_values.headers
  @forward_cookies = cache_behavior.forwarded_values.cookies
  @forward_query_string = cache_behavior.forwarded_values.query_string
  @min_ttl = cache_behavior.min_ttl
  @max_ttl = cache_behavior.max_ttl
  @default_ttl = cache_behavior.default_ttl
  @output_array_for_recv = Array.new
  @output_array_for_backend_resp = Array.new
  @output_array_for_hash = Array.new
end

Public Instance Methods

delete_symbol(str) click to toggle source
# File lib/Behavior.rb, line 107
def delete_symbol(str)
  result_str = ""
  for i in 1..str.size
    if str[i-1] == "." || str[i-1] == "-"
    else
      result_str = result_str + str[i-1]
    end
  end
  return result_str
end
store_code_for_backend_resp() click to toggle source
# File lib/Behavior.rb, line 73
def store_code_for_backend_resp
  ## cacheがprivateではないか
  ## cacheがprivateであれば,min_ttlをセットする
  @output_array_for_backend_resp << "  if(beresp.http.cache-control ~ \"no-cache\" || beresp.http.cache-control ~ \"no-store\" || beresp.http.cache-control ~ \"private\"){"
  @output_array_for_backend_resp << "    set beresp.ttl = #{@min_ttl}s;"
  @output_array_for_backend_resp << "    return (deliver);"
  @output_array_for_backend_resp << "  }"
  @output_array_for_backend_resp << "  if((beresp.http.cache-control ~ \"max-age\") || (beresp.http.cache-control ~ \"s-maxage\") || (beresp.http.expires)){"
  ## beresp.ttlに,beresp.ttl(max-age?=>s-maxage?=>expires?=>default_ttl)がmin_ttlより小さければmin_ttlを,max_ttlより大きければmax_ttlを設定
  @output_array_for_backend_resp << "    if(beresp.ttl <= #{@min_ttl}s){"
  @output_array_for_backend_resp << "      set beresp.ttl = #{@min_ttl}s;"
  @output_array_for_backend_resp << "    }else if(beresp.ttl >= #{@max_ttl}s){"
  @output_array_for_backend_resp << "      set beresp.ttl = #{@max_ttl}s;"
  @output_array_for_backend_resp << "    }"
  @output_array_for_backend_resp << "    return (deliver);"
  ## HTTPヘッダのcache-controlにmax-ageが設定されていない場合,min_ttlとdefault_ttlの大きい方をセットする
  @output_array_for_backend_resp << "  }else{"
  @output_array_for_backend_resp << "    set beresp.ttl = #{(@min_ttl >= @default_ttl)? @min_ttl : @default_ttl}s;"
  @output_array_for_backend_resp << "    return (deliver);"
  @output_array_for_backend_resp << "  }"
end
store_code_for_hash() click to toggle source
# File lib/Behavior.rb, line 95
def store_code_for_hash
  # FORWARD COOKIES
  @output_array_for_hash << "  hash_data(req.http.Cookie);"
  # FORWARD HEADERS
  if @forward_headers.items[0] != "*"
    @forward_headers.items.each_with_index do |header, index|
      @output_array_for_hash << "  hash_data(req.http.#{header});"
    end
    # @output_array_for_hash << "  return(lookup);"
  end
end
store_code_for_recv() click to toggle source
# File lib/Behavior.rb, line 19
def store_code_for_recv
  ## CFのForwardQueryStringが NO だった場合,QueryStrginを削除
  @forward_query_string? "" : "#{@output_array_for_recv << "  set req.url = regsub(req.url, \"\\?.*$\", \"\");"}"
  # ALLOWED METHODS
  @output_array_for_recv << "  if("
  @allowed_methods.each_with_index do |method, index|
    @output_array_for_recv[@output_array_for_recv.length-1] += "req.method == \"#{method}\""
    # 最後の要素で無ければOR演算子を付加する
    @output_array_for_recv[@output_array_for_recv.length-1] += (index != (@allowed_methods.size-1))? " || " : ""
  end
  @output_array_for_recv[@output_array_for_recv.length-1] += "){"
  # CACHED METHODS
  @output_array_for_recv << "    if("
  @cached_methods.each_with_index do |method, index|
    @output_array_for_recv[@output_array_for_recv.length-1] += "req.method == \"#{method}\""
    # 最後の要素で無ければOR演算子を付加する
    @output_array_for_recv[@output_array_for_recv.length-1] += (index != (@cached_methods.size-1))? " || " : ""
  end
  # FORWARD HEADERS
  @output_array_for_recv[@output_array_for_recv.length-1] += "){"
  if @forward_headers.items[0] == "*"
    @output_array_for_recv << "        return (pipe);"
  end
  # FORWARD COOKIES
  case @forward_cookies.forward
  when "none"
    @output_array_for_recv << "      unset req.http.Cookie;"

  when "whitelist"
    @output_array_for_recv << "      set req.http.Cookie = \";\" + req.http.Cookie;"
    @output_array_for_recv << "      set req.http.Cookie = regsuball(req.http.Cookie, \"; +\", \";\");"
    @output_array_for_recv << "      set req.http.Cookie = regsuball(req.http.Cookie, \";("
    @forward_cookies.whitelisted_names.items.each_with_index do |cookie, index|
      @output_array_for_recv[@output_array_for_recv.length-1] += "#{cookie}"
      @output_array_for_recv[@output_array_for_recv.length-1] += (index != (@forward_cookies.whitelisted_names.quantity-1))? "|" : ""
    end
    @output_array_for_recv[@output_array_for_recv.length-1] += ")=\", \"; \\1=\");"
    @output_array_for_recv << "      set req.http.Cookie = regsuball(req.http.Cookie, \";[^ ][^;]*\", \"\");"
    @output_array_for_recv << "      set req.http.Cookie = regsuball(req.http.Cookie, \"^[; ]+|[; ]+$\", \"\");"

  when "all"
  end

  # backendをセットする
  @output_array_for_recv << "      set req.backend_hint = #{@backend_name};"
  @output_array_for_recv << "      return (hash);"
  @output_array_for_recv << "    }else{"
  @output_array_for_recv << "      return (pipe);"
  @output_array_for_recv << "    }"
  @output_array_for_recv << "  }else{"
  @output_array_for_recv << "    return (synth(405, \"Method Not Allowed\"));"
  @output_array_for_recv << "  }"
end