Class: Behavior

Inherits:
Object
  • Object
show all
Defined in:
lib/Behavior.rb

Direct Known Subclasses

AdditionBehavior, DefaultBehavior

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_behavior) ⇒ Behavior

Returns a new instance of Behavior.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#output_array_for_backend_respObject (readonly)

Returns the value of attribute output_array_for_backend_resp.



2
3
4
# File 'lib/Behavior.rb', line 2

def output_array_for_backend_resp
  @output_array_for_backend_resp
end

#output_array_for_hashObject (readonly)

Returns the value of attribute output_array_for_hash.



2
3
4
# File 'lib/Behavior.rb', line 2

def output_array_for_hash
  @output_array_for_hash
end

#output_array_for_recvObject (readonly)

Returns the value of attribute output_array_for_recv.



2
3
4
# File 'lib/Behavior.rb', line 2

def output_array_for_recv
  @output_array_for_recv
end

Instance Method Details

#delete_symbol(str) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# 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_respObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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_hashObject



95
96
97
98
99
100
101
102
103
104
105
# 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_recvObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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