Software /
code /
prosody-modules
Changeset
2700:7a5dae85f26f
mod_log_http: Add new module for logging outgoing HTTP request
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 21 Apr 2017 16:46:09 +0100 |
parents | 2699:dae655657a92 |
children | 2702:caabb980d1d8 2706:3e97dae28215 |
files | mod_log_http/README.markdown mod_log_http/mod_log_http.lua |
diffstat | 2 files changed, 100 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_log_http/README.markdown Fri Apr 21 16:46:09 2017 +0100 @@ -0,0 +1,22 @@ +--- +summary: HTTP request logging +... + +Introduction +============ + +This module logs *outgoing* requests that go via the internal net.http API. + +Output format liable to change. + +Configuration +============= + +One option is required, set `log_http_file` to the file path you would like to log to. + +Compatibility +============= + + ----- ------- + 0.10 Works (requires 375cf924fce1 or later) + ----- -------
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_log_http/mod_log_http.lua Fri Apr 21 16:46:09 2017 +0100 @@ -0,0 +1,78 @@ +module:set_global(); + +local http = require "net.http"; +local codes = require "net.http.codes"; +local json = require "util.json"; + +local log = assert(io.open(assert(module:get_option_string("log_http_file"), "Please supply log_http_file in the config"), "a+")); + +local function append_request(id, req) + local headers = {}; + for k, v in pairs(req.headers) do + table.insert(headers, { name = k, value = v }); + end + local queryString = {}; + if req.query then + for _, pair in ipairs(http.formdecode(req.query)) do + table.insert(queryString, pair); + end + end + log:write("<<<", json.encode({ + id = id; + type = "request"; + method = req.method; + url = req.url; + httpVersion = "HTTP/1.1"; + cookies = {}; + headers = headers; + queryString = queryString; + postData = req.body and { + mimeType = req.headers["Content-Type"]; + text = req.body; + } or nil; + headersSize = -1; + bodySize = -1; + }), "\n"); +end + +local function append_response(id, resp) + local headers = {}; + for k, v in pairs(resp.headers) do + table.insert(headers, { name = k, value = v }); + end + log:write(">>>", json.encode({ + id = id; + type = "response"; + status = resp.code; + statusText = codes[resp.code]; + httpVersion = resp.httpversion; + cookies = {}; + headers = headers; + content = resp.body and { + size = #resp.body; + mimeType = resp.headers.content_type; + text = resp.body; + } or nil; + headersSize = -1; + bodySize = -1; + }), "\n"); +end + +module:hook_object_event(http.events, "request", function (event) + module:log("warn", "Request to %s!", event.url); + append_request(event.request.id, event.request); +end); + +module:hook_object_event(http.events, "request-connection-error", function (event) + module:log("warn", "Failed to make request to %s!", event.url); +end); + +module:hook_object_event(http.events, "response", function (event) + module:log("warn", "Received response %d from %s!", event.code, event.url); + for k,v in pairs(event.response) do print("=====", k, v) end + append_response(event.request.id, event.response); +end); + +function module.unload() + log:close(); +end