-
Notifications
You must be signed in to change notification settings - Fork 0
Class and Method
Ryo Okubo edited this page Nov 14, 2016
·
36 revisions
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
- Kernel Class
- ATS Class
- ATS::Request Class
- ATS::Connection Class
- ATS::Headers_in Class
- ATS::Headers_out Class
- ATS::Filter Class
- ATS::Upstream Class
- ATS::Records Class
- ATS::EventSystem Class
Get server class, ATS
in ts_mruby.
Server = get_server_class
Server::echo 'Hello, World.'
get server software name
if server_name == "NGINX"
Server = Nginx
elsif server_name == "Apache"
Server = Apache
elsif server_name == "ApacheTrafficServer"
Server = ATS
end
create response text
ATS.rputs "hello ts_mruby world!"
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"
return ATS status code
return ATS::HTTP_SERVICE_UNAVAILABLE
alias ATS.return
logging to error.log
ATS.errlogger ATS::LOG_ERR, "ts_mruby error!"
ATS.echo ATS.module_name #=> ts_mruby
ATS.echo ATS.module_version #=> 0.0.1
ATS.echo ATS.trafficserver_version #=> Apache Traffic Server 5.0.0
ATS.redirect "https://github.com/syucream/ts_mruby", ATS::HTTPMOVED_PERMANENTLY
# curl https://127.0.0.1/
r = ATS::Request.new
r.scheme #=> https
set string to args
r = ATS::Request.new
r.content_type = "text/plain"
r = ATS::Request.new
# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.uri #=> /hello
set string to uri
r = ATS::Request.new
# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.unparsed_uri #=> /hello?a=1
set string to unparsed_uri
r = ATS::Request.new
# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.method #=> GET
set string to method
r = ATS::Request.new
# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.protocol #=> HTTP/1.1
r = ATS::Request.new
# curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo r.args #=> a=1
set string to args
hin = ATS::Headers_in.new
ATS.rputs hin["User-Agent"] #=> curl/7.29.0
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
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: */*
hin = ATS::Headers_in.new
hin["X-Remove-Header"] = "to be deleted!"
hin.delete("X-Remove-Header")
ATS.rputs hin["X-Remove-Header"] #=> nil
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
<
...
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
...
<
...
f = ATS::Filter.new
f.output = "Modified response body\n"
$ curl -s http://localhost/
Modified response body
alias ATS::Filter#body=
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
c = ATS::Connection.new
# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 192.168.12.9
c = ATS::Connection.new
# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 54430
c = ATS::Connection.new
# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 192.168.12.9
c = ATS::Connection.new
# $ curl -v http://192.168.12.9:8001/hello?a=1
ATS.echo c.remote_ip # => 8001
upstream = ATS::Upstream.new
upstream.server = "www.google.com:80"
Getter/setter for ATS overridable configs. See also config entries.
records = ATS::Records.new
records.get ATS::Records::TS_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR #=> 0, 1 or 2
records = ATS::Records.new
# override to insert response 'via' header field.
records.set ATS::Records::TS_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR 2
It enables mruby scripts to register an event handler. Usable events are some of ATS hooks.
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