Mercurial > prosody-modules
comparison mod_log_http/mod_log_http.lua @ 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 | |
| children | 3f44abfe7264 |
comparison
equal
deleted
inserted
replaced
| 2699:dae655657a92 | 2700:7a5dae85f26f |
|---|---|
| 1 module:set_global(); | |
| 2 | |
| 3 local http = require "net.http"; | |
| 4 local codes = require "net.http.codes"; | |
| 5 local json = require "util.json"; | |
| 6 | |
| 7 local log = assert(io.open(assert(module:get_option_string("log_http_file"), "Please supply log_http_file in the config"), "a+")); | |
| 8 | |
| 9 local function append_request(id, req) | |
| 10 local headers = {}; | |
| 11 for k, v in pairs(req.headers) do | |
| 12 table.insert(headers, { name = k, value = v }); | |
| 13 end | |
| 14 local queryString = {}; | |
| 15 if req.query then | |
| 16 for _, pair in ipairs(http.formdecode(req.query)) do | |
| 17 table.insert(queryString, pair); | |
| 18 end | |
| 19 end | |
| 20 log:write("<<<", json.encode({ | |
| 21 id = id; | |
| 22 type = "request"; | |
| 23 method = req.method; | |
| 24 url = req.url; | |
| 25 httpVersion = "HTTP/1.1"; | |
| 26 cookies = {}; | |
| 27 headers = headers; | |
| 28 queryString = queryString; | |
| 29 postData = req.body and { | |
| 30 mimeType = req.headers["Content-Type"]; | |
| 31 text = req.body; | |
| 32 } or nil; | |
| 33 headersSize = -1; | |
| 34 bodySize = -1; | |
| 35 }), "\n"); | |
| 36 end | |
| 37 | |
| 38 local function append_response(id, resp) | |
| 39 local headers = {}; | |
| 40 for k, v in pairs(resp.headers) do | |
| 41 table.insert(headers, { name = k, value = v }); | |
| 42 end | |
| 43 log:write(">>>", json.encode({ | |
| 44 id = id; | |
| 45 type = "response"; | |
| 46 status = resp.code; | |
| 47 statusText = codes[resp.code]; | |
| 48 httpVersion = resp.httpversion; | |
| 49 cookies = {}; | |
| 50 headers = headers; | |
| 51 content = resp.body and { | |
| 52 size = #resp.body; | |
| 53 mimeType = resp.headers.content_type; | |
| 54 text = resp.body; | |
| 55 } or nil; | |
| 56 headersSize = -1; | |
| 57 bodySize = -1; | |
| 58 }), "\n"); | |
| 59 end | |
| 60 | |
| 61 module:hook_object_event(http.events, "request", function (event) | |
| 62 module:log("warn", "Request to %s!", event.url); | |
| 63 append_request(event.request.id, event.request); | |
| 64 end); | |
| 65 | |
| 66 module:hook_object_event(http.events, "request-connection-error", function (event) | |
| 67 module:log("warn", "Failed to make request to %s!", event.url); | |
| 68 end); | |
| 69 | |
| 70 module:hook_object_event(http.events, "response", function (event) | |
| 71 module:log("warn", "Received response %d from %s!", event.code, event.url); | |
| 72 for k,v in pairs(event.response) do print("=====", k, v) end | |
| 73 append_response(event.request.id, event.response); | |
| 74 end); | |
| 75 | |
| 76 function module.unload() | |
| 77 log:close(); | |
| 78 end |
