Update mruby doc

This commit is contained in:
Tatsuhiro Tsujikawa 2015-10-06 00:26:45 +09:00
parent f1eb7638d1
commit 2b8b8f1ffd
1 changed files with 28 additions and 24 deletions

View File

@ -185,23 +185,20 @@ server. These hooks allows users to modify header fields, or common
HTTP variables, like authority or request path, and even return custom HTTP variables, like authority or request path, and even return custom
response without forwarding request to backend servers. response without forwarding request to backend servers.
To set request phase hook, use :option:`--request-phase-file` option. To specify mruby script file, use :option:`--mruby-file` option. The
To set response phase hook, use :option:`--response-phase-file` script will be evaluated once per thread on startup, and it must
option. instantiate object and evaluate it as the return value (e.g.,
``App.new``). This object is called app object. If app object
For request and response phase hook, user calls :rb:meth:`Nghttpx.run` defines ``on_req`` method, it is called with :rb:class:`Nghttpx::Env`
with block. The :rb:class:`Nghttpx::Env` is passed to the block. object on request hook. Similarly, if app object defines ``on_resp``
User can can access :rb:class:`Nghttpx::Request` and method, it is called with :rb:class:`Nghttpx::Env` object on response
:rb:class:`Nghttpx::Response` objects via :rb:attr:`Nghttpx::Env#req` hook. For each method invocation, user can can access
and :rb:attr:`Nghttpx::Env#resp` respectively. :rb:class:`Nghttpx::Request` and :rb:class:`Nghttpx::Response` objects
via :rb:attr:`Nghttpx::Env#req` and :rb:attr:`Nghttpx::Env#resp`
respectively.
.. rb:module:: Nghttpx .. rb:module:: Nghttpx
.. rb:classmethod:: run(&block)
Run request or response phase hook with given *block*.
:rb:class:`Nghttpx::Env` object is passed to the given block.
.. rb:const:: REQUEST_PHASE .. rb:const:: REQUEST_PHASE
Constant to represent request phase. Constant to represent request phase.
@ -379,28 +376,35 @@ Modify requet path:
.. code-block:: ruby .. code-block:: ruby
Nghttpx.run do |env| class App
env.req.path = "/apps#{env.req.path}" def on_req(env)
env.req.path = "/apps#{env.req.path}"
end
end end
Note that the file containing the above script must be set with App.new
:option:`--request-phase-file` option since we modify request path.
Don't forget to instantiate and evaluate object at the last line.
Restrict permission of viewing a content to a specific client Restrict permission of viewing a content to a specific client
addresses: addresses:
.. code-block:: ruby .. code-block:: ruby
Nghttpx.run do |env| class App
allowed_clients = ["127.0.0.1", "::1"] def on_req(env)
allowed_clients = ["127.0.0.1", "::1"]
if env.req.path.start_with?("/log/") && if env.req.path.start_with?("/log/") &&
!allowed_clients.include?(env.remote_addr) then !allowed_clients.include?(env.remote_addr) then
env.resp.status = 404 env.resp.status = 404
env.resp.return "permission denied" env.resp.return "permission denied"
end
end end
end end
App.new
SEE ALSO SEE ALSO
-------- --------