From 2b8b8f1ffd23d41204537e23d5bfe4386773596c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 6 Oct 2015 00:26:45 +0900 Subject: [PATCH] Update mruby doc --- doc/nghttpx.h2r | 52 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/doc/nghttpx.h2r b/doc/nghttpx.h2r index f5cf38c8..4cacbad8 100644 --- a/doc/nghttpx.h2r +++ b/doc/nghttpx.h2r @@ -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 response without forwarding request to backend servers. -To set request phase hook, use :option:`--request-phase-file` option. -To set response phase hook, use :option:`--response-phase-file` -option. - -For request and response phase hook, user calls :rb:meth:`Nghttpx.run` -with block. The :rb:class:`Nghttpx::Env` is passed to the block. -User can can access :rb:class:`Nghttpx::Request` and -:rb:class:`Nghttpx::Response` objects via :rb:attr:`Nghttpx::Env#req` -and :rb:attr:`Nghttpx::Env#resp` respectively. +To specify mruby script file, use :option:`--mruby-file` option. The +script will be evaluated once per thread on startup, and it must +instantiate object and evaluate it as the return value (e.g., +``App.new``). This object is called app object. If app object +defines ``on_req`` method, it is called with :rb:class:`Nghttpx::Env` +object on request hook. Similarly, if app object defines ``on_resp`` +method, it is called with :rb:class:`Nghttpx::Env` object on response +hook. For each method invocation, user can can access +: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: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 Constant to represent request phase. @@ -379,28 +376,35 @@ Modify requet path: .. code-block:: ruby - Nghttpx.run do |env| - env.req.path = "/apps#{env.req.path}" + class App + def on_req(env) + env.req.path = "/apps#{env.req.path}" + end end -Note that the file containing the above script must be set with -:option:`--request-phase-file` option since we modify request path. + App.new + +Don't forget to instantiate and evaluate object at the last line. Restrict permission of viewing a content to a specific client addresses: .. code-block:: ruby - Nghttpx.run do |env| - allowed_clients = ["127.0.0.1", "::1"] + class App + def on_req(env) + allowed_clients = ["127.0.0.1", "::1"] - if env.req.path.start_with?("/log/") && - !allowed_clients.include?(env.remote_addr) then - env.resp.status = 404 - env.resp.return "permission denied" + if env.req.path.start_with?("/log/") && + !allowed_clients.include?(env.remote_addr) then + env.resp.status = 404 + env.resp.return "permission denied" + end end end + App.new + SEE ALSO --------