Skip to content
Ryo Okubo edited this page Nov 14, 2016 · 36 revisions

Note

Some items have below labels:

  • [ap] means this class/method works similar behavior to mod_mruby
  • [ngx] means this class/method works similar behavior to ngx_mruby
  • [ex] means this class/method is experimental yet

Class for ts_mruby

Kernel Class

Method

get_server_class [ap][ngx]

Get server class, ATS in ts_mruby.

Server = get_server_class
Server::echo 'Hello, World.'

server_name [ap][ngx]

get server software name

if server_name == "NGINX"
  Server = Nginx
elsif server_name == "Apache"
  Server = Apache
elsif server_name == "ApacheTrafficServer"
  Server = ATS
end

ATS Class

Method

ATS.rputs [ap][ngx]

create response text

ATS.rputs "hello ts_mruby world!"

ATS.echo [ap][ngx]

create response text which is terminated with a newline

ATS.echo "hello ts_mruby world!"

is equal to

ATS.rputs "hello ts_mruby world!¥n"

ATS.return [ap][ngx]

return ATS status code

return ATS::HTTP_SERVICE_UNAVAILABLE

ATS.send_header

alias ATS.return

ATS.errlogger

logging to error.log

ATS.errlogger ATS::LOG_ERR, "ts_mruby error!"

ATS.module_name

ATS.echo ATS.module_name #=> ts_mruby

ATS.module_version

ATS.echo ATS.module_version #=> 0.0.1

ATS.trafficserver_version

ATS.echo ATS.trafficserver_version #=> Apache Traffic Server 5.0.0

ATS.redirect

ATS.redirect "https://github.com/syucream/ts_mruby", ATS::HTTPMOVED_PERMANENTLY

ATS::Request Class

Method

ATS::Request#scheme [ngx]

# curl https://127.0.0.1/
r = ATS::Request.new
r.scheme #=> https

ATS::Request#content_type= [ngx]

set string to args

ATS::Request#content_type [ngx]

r = ATS::Request.new
r.content_type = "text/plain"

ATS::Request#uri

r = ATS::Request.new

# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.uri #=> /hello

ATS::Request#uri=

set string to uri

ATS::Request#unparsed_uri

r = ATS::Request.new

# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.unparsed_uri #=> /hello?a=1

ATS::Request#unparsed_uri=

set string to unparsed_uri

ATS::Request#method

r = ATS::Request.new

# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.method #=> GET

ATS::Request#method=

set string to method

ATS::Request#protocol

r = ATS::Request.new

# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.protocol #=> HTTP/1.1

ATS::Request#args

r = ATS::Request.new

# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.args #=> a=1

ATS::Request#args=

set string to args

ATS::Headers_in Class

Method

ATS::Headers_in#[]

hin = ATS::Headers_in.new
ATS.rputs hin["User-Agent"] #=> curl/7.29.0

ATS::Headers_in#[]=

hin = ATS::Headers_in#new

ATS.rputs hin["User-Agent"] #=> curl/7.29.0
hin["User-Agent"] = "test-agent"
ATS.rputs hin["User-Agent"] #=> test-agent

ATS::Headers_in#all

hin = ATS::Headers_in.new

hin.all.keys.each do |k|
  Server.echo "#{k}: #{hin[k]}"
end
# => $ curl -v http://192.168.12.9:8001/hello?a=1
# => Host: 192.168.12.9:8001
# => User-Agent: curl/7.29.0
# => Accept: */*

ATS::Headers_in#delete

hin = ATS::Headers_in.new

hin["X-Remove-Header"] = "to be deleted!"
hin.delete("X-Remove-Header")
ATS.rputs hin["X-Remove-Header"] #=> nil

ATS::Headers_out Class

Method

ATS::Headers_out#[]=

hout = ATS::Headers_out.new
hout["X-ATS-Plugin"] = "modified header by ts_mruby"
hout["Server"] = "ATS with ts_mruby"
curl -v http://localhost/
...
< HTTP/1.1 200 OK
< Server: ATS with ts_mruby
...
< Via: http/1.1 Macintosh.local (ApacheTrafficServer/6.0.0 [c sSf ])
< X-ATS-Plugin: modified header by ts_mruby
<
...

ATS::Headers_out#delete

original response:

curl -v http://localhost/
...
< HTTP/1.1 200 OK
< Server: ATS
...
< Via: http/1.1 Macintosh.local (ApacheTrafficServer/6.0.0 [c sSf ])
<
...
hout = ATS::Headers_out.new
hout.delete("Via")

customized response:

curl -v http://localhost/
...
< HTTP/1.1 200 OK
< Server: ATS
...
<
...

ATS::Filter Class

Method

ATS::Filter#body= [ngx]

f = ATS::Filter.new
f.output = "Modified response body\n"
$ curl -s http://localhost/
Modified response body

ATS::Filter#output= [ngx]

alias ATS::Filter#body=

ATS::Filter#transform! [ex]

Enable reading response body and rewriting it by using block or Proc object.

f = ATS::Filter.new
f.transform! do |body|
  # append text to the end of response body
  body + "rewritted by ts_mruby :D"
end

NOTE: the block may execute another mruby VM, so you must not access outer scope.

txt = "rewritted by ts_mruby :D"
f = ATS::Filter.new
f.transform! do |body|
  # You most avoid it! this block can't use txt, declared in outer scope.
  body + txt
end

ATS::Connection Class

Method

ATS::Connection#remote_ip

c = ATS::Connection.new

# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 192.168.12.9

ATS::Connection#remote_port

c = ATS::Connection.new

# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 54430

ATS::Connection#local_ip

c = ATS::Connection.new

# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 192.168.12.9

ATS::Connection#local_port

c = ATS::Connection.new

# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 8001

ATS::Upstream Class

Method

ATS::Upstream#server= [ngx]

upstream = ATS::Upstream.new
upstream.server = "www.google.com:80"

ATS::Records Class

Getter/setter for ATS overridable configs. See also config entries.

Method

ATS::Records#get [ex]

records = ATS::Records.new
records.get ATS::Records::TS_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR #=> 0, 1 or 2

ATS::Records#set [ex]

records = ATS::Records.new
# override to insert response 'via' header field.
records.set ATS::Records::TS_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR 2

ATS::EventSystem Class

It enables mruby scripts to register an event handler. Usable events are some of ATS hooks.

Method

ATS::EventSystem#register [ex]

class MyHandler 
  def on_send_request_hdr
    # A handler for SEND_REQUEST_HDR_HOOK.
  end

  def on_read_response_hdr
    # A handler for READ_RESPONSE_HDR_HOOK.
  end

  def on_send_response_hdr
    # A handler for SEND_RESPONSE_HDR_HOOK.
  end
end

es = ATS::EventSystem.new
es.register MyHandler