Classes
mruby_nginx_module's classes are following.
- Nginx::Request
- Nginx::Headers_in
- Nginx::Headers_out
- Nginx::Context
- Nginx::Time
- Nginx::Digest
- Nginx::Base64
- Nginx::Var
Nginx::Request
Nginx::Request
is the class for assigning a value and referrring to members of ngx_request_t
.
The members of Nginx::Request
are following.
- content_type
- request_line
- uri
- unparsed_uri
- method
- protocol
- args
- hostname
- filename
- user
var
Following is the example for assigning a value to content_type
.
r = Nginx::Request.new
r.content_type = "text/plain"
The member var
is special. This is alias for assigning a value and referring to nginx's variables.
location /print_var {
set $author "bokko";
mruby_content_handler_code '
r = Nginx::Request.new
Nginx.rputs(r.var.author + "\n")
Nginx.return Nginx::HTTP_OK
';
}
Let's access the above location.
$ curl http://localhost/print_var
bokko
$
var
makes it possible to access nginx's all variables. But assigning a value to undefined variable produces a error.
r = Nginx::Request.new
val = r.var.undefined_variable # error
var
is very useful. But var
can treat only string.
If you using various value-types(Array, String, Hash, etc), you may use Nginx::Context.
Nginx::Headers_in
Nginx::Headers_in
is the class for assigning new header and referring to headers of a request.
hin = Nginx::Headers_in.new
host = hin["Host"] # Host header
agent = hin["User-Agent"] # User-Agent header
hash = hin.all # all headers with hash table
hin["Host"] = "localhost"
Nginx::Headers_out
Nginx::Headers_out
is the class for assigning new header and referring to headers of a response.
Following is the example for setting Expires
header.
time = Nginx::Time.time()
http_time = Nginx::Time.http_time(time + 60 * 60 * 24)
hout = Nginx::Headers_out.new
hout["Expires"] = http_time.to_s
Nginx::Context
Nginx::Context
is the class for saving a context in each request.
Using nginx's variable also makes it possible to save a context in each request.
But this way permits to treat only string-values. Because nginx's variables are string-values.
Nginx::Context permits to treat various value-types(Array, String, Hash, etc).
location /ctx {
mruby_rewrite_handler_code '
ctx = Nginx::Context.new
ctx["count"] = 1
ctx["arr"] = ["rewrite"]
';
mruby_access_handler_code '
ctx = Nginx::Context.new
ctx["count"] += 2
ctx["arr"].push("access")
';
mruby_content_handler_code '
ctx = Nginx::Context.new
ctx["count"] += 3
ctx["arr"].push("content")
Nginx.rputs(ctx.table.to_s + "\n")
';
}
Let's access the above location.
$ curl http://localhost/ctx
{"arr"=>["rewrite", "access", "content"], "count"=>6}
$
Nginx::Time
Nginx::Time
is the class for using Nginx's time API.
The functions of Nginx::Time
are following.
- update
- time
- http_time
- cookie_time
- utc_time
- localtime
- parse_http_time
Nginx::Time.update
Nginx::Time.update updates Nginx's current time cache.
Nginx::Time.update # update Nginx's current time cache
Nginx::Time.time
Nginx::Time.time returns current timestamp as epoch value.
time = Nginx::Time.time # => epoch value
Nginx::Time.http_time
Nginx::Time.http_time returns a formarted string as the http header time.
time = 1377710189
Nginx::Time.http_time(time) # => Wed, 28 Aug 2013 17:16:29 GMT
Nginx::Time.cookie_time
Nginx::Time.cookie_time returns a formarted string as the cookie expiration time.
time = 1377710189
Nginx::Time.cookie_time(time) # => Wed, 28-Aug-13 17:16:29 GMT
Nginx::Time.utc_time
Nginx::Time.utc_time returns a formarted string as the UTC time by Nginx's current time cache.
Nginx::Time.utc_time # => 2013-08-28 17:20:18
Nginx::Time.localtime
Nginx::Time.localtime returns a formarted string as the local time by Nginx's current time cache.
Nginx::Time.local_time # => 2013-08-28 17:20:18
Nginx::Time.parse_http_time
Nginx::Time.parse_http_time returns a epoch value with the http header time.
time = 1377710189
http_time = Nginx::Time.http_time(time)
Nginx::Time.parse_http_time(http_time) # => 1377710189
Nginx::Digest
Nginx::Digest
is the class for using Nginx's digest API.
The functions of Nginx::Digest
are following.
- md5
- sha1
- hmac_sha1
- hexdigest
- crc32_long
- crc32_short
Nginx::Digest.md5
Nginx::Digest.md5 return a MD5 value of argument.
md5 = Nginx::Digest.md5("bokko")
Nginx::Digest.hexdigest(md5) # => fe9749f7f1d1c8ec7f6ddd1f0521cdb0
Nginx::Digest.sha1
Nginx::Digest.sha1 return a SHA1 value of argument.
sha1 = Nginx::Digest.sha1("bokko")
Nginx::Digest.hexdigest(sha1) # => cea3d1bca602a6017e9a02520e3d392ba6b7838d
Nginx::Digest.hmac_sha1
Nginx::Digest.hmac_sha1 return a HMAC_SHA1 value of argument.
hmac_sha1 = Nginx::Digest.hmac_sha1("data", "key")
Nginx::Digest.hexdigest(hmac_sha1) # => 104152c5bfdca07bc633eebd46199f0255c9f49d
This functions requires OpenSSL library. So you may add --with-http_ssl_module
to configure.
Nginx::Digest.hexdigest
Nginx::Digest.hexdigest return a hexadecimal value of argument.
Nginx::Digest.crc32_long
Nginx::Digest.crc32_long return a CRC32 digest value of argument.
Nginx::Digest.crc32_long("bokko") # => 1494474241
Nginx::Digest.crc32_short
Nginx::Digest.crc32_short return a CRC32 digest value of argument.
Nginx::Digest.crc32_short("bokko") # => 1494474241
Nginx::Base64
Nginx::Base64
is the class for using Nginx's base64encoding/decoding API.
The functions of Nginx::Base64
are following.
- encode
- decode
Nginx::Base64.encode
Nginx::Base64.encode return base64 encoded string of argument.
Nginx::Base64.encode("bokko") # => Ym9ra28=
Nginx::Base64.decode
Nginx::Base64.decode return decoded string of base64 encoded argument.
encoded = Nginx::Base64.encode("bokko")
Nginx::Base64.decode(encoded) # => bokko
Nginx::Var
Nginx::Var
is the class for assigning a value and referrring to nginx's variables.
location /set_var {
set $author "bokko";
mruby_content_handler_code '
v = Nginx::Var.new
v.set("author", "cubicdaiya")
Nginx.rputs(v.author + "\n")
Nginx.return Nginx::HTTP_OK
';
}
v.set("author", "cubicdaiya")
is able to be replaced like following.
v = Nginx::Var.new
v.author = "cubicdaiya")
Assigning a value to undefined variable produces a error as same as The member var
of Nginx::Request
.