[go: nahoru, domu]

Skip to content

Logging All Headers

Yannick Koechlin edited this page Jun 30, 2017 · 3 revisions

Below sample shows a work around to be able to log all headers through sending them from mruby. You can use the 399 status code if you want to only add the headers in mruby and delegate the request.

h2o.conf:

    listen: 8070
    error-log: h2o-error.log
    limit-request-body: 524288
    max-connections: 4096
    http2-idle-timeout: 20 #Timeout for idle connections in seconds.
    send-server-name: OFF
    pid-file: h2o.pid
    access-log:
      escape: json
      format: '{"POST": %{LOG}e, "headers": %{HEADERS}e, "mrubyRackVars": %{RACKVARS}e }'
      path: /dev/stdout
    hosts:
      default:
        paths:
          "/send_me_data":
            mruby.handler-file: myapi.rb

myapi.rb:

    class MyAPI
      def call(env)
                postcontent = env["rack.input"] ? env["rack.input"].read : ""
                jsn = JSON.parse(postcontent) rescue {"error" => "parsing error"}
                headers = Hash[env.find_all{|k,v| k.start_with?("HTTP") }]
                rack_vars = Hash[env.reject{|k,v| k.start_with?("HTTP") }]
                [201,
                 {
                #   "content-type" => "application/json",
                   "x-fallthru-set-LOG" => jsn.to_json, 
                   "x-fallthru-set-HEADERS" => headers.to_json, 
                   "x-fallthru-set-RACKVARS" => rack_vars.to_json
                 },
                 []
                ]

              end
            end

    MyAPI.new