Mercurial > prosody-modules
comparison mod_http_logging/mod_http_logging.lua @ 2966:678be8ea4d38
mod_http_logging: Factor out body length calculation
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 30 Mar 2018 13:35:43 +0200 |
| parents | 557c976735e1 |
| children | 135ca695fcbf |
comparison
equal
deleted
inserted
replaced
| 2965:557c976735e1 | 2966:678be8ea4d38 |
|---|---|
| 12 | 12 |
| 13 module:set_global(); | 13 module:set_global(); |
| 14 | 14 |
| 15 local server = require "net.http.server"; | 15 local server = require "net.http.server"; |
| 16 | 16 |
| 17 local function get_content_len(response, body) | |
| 18 local len = response.headers.content_length; | |
| 19 if len then return len; end | |
| 20 if not body then body = request.body; end | |
| 21 if body then return #tostring(body); end | |
| 22 end | |
| 23 | |
| 17 local function log_response(response, body) | 24 local function log_response(response, body) |
| 18 body = body or response.body; | 25 local len = tostring(get_content_len(response, body) or "-"); |
| 19 local len = response.headers.content_length or (body and #body) or "-"; | |
| 20 local request = response.request; | 26 local request = response.request; |
| 21 local ip = request.conn:ip(); | 27 local ip = request.conn:ip(); |
| 22 local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion); | 28 local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion); |
| 23 local date = os.date("%d/%m/%Y:%H:%M:%S %z"); | 29 local date = os.date("%d/%m/%Y:%H:%M:%S %z"); |
| 24 module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, tostring(len)); | 30 module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, len); |
| 25 end | 31 end |
| 26 | 32 |
| 27 local send_response = server.send_response; | 33 local send_response = server.send_response; |
| 28 local function log_and_send_response(response, body) | 34 local function log_and_send_response(response, body) |
| 29 if not response.finished then | 35 if not response.finished then |
