2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
module ApplicationController::LogsHttpAccess
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_action :http_log
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def http_log_config(config)
|
|
|
|
@http_log_support = config
|
|
|
|
end
|
|
|
|
|
|
|
|
# log http access
|
|
|
|
def http_log
|
|
|
|
return if !@http_log_support
|
|
|
|
|
|
|
|
# request
|
|
|
|
request_data = {
|
2018-12-19 17:31:51 +00:00
|
|
|
content: '',
|
|
|
|
content_type: request.headers['Content-Type'],
|
2017-03-09 11:44:51 +00:00
|
|
|
content_encoding: request.headers['Content-Encoding'],
|
2018-12-19 17:31:51 +00:00
|
|
|
source: request.headers['User-Agent'] || request.headers['Server'],
|
2017-03-09 11:44:51 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
request.headers.each do |key, value|
|
2017-03-09 11:44:51 +00:00
|
|
|
next if key[0, 5] != 'HTTP_'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
request_data[:content] += if key == 'HTTP_COOKIE'
|
|
|
|
"#{key}: xxxxx\n"
|
|
|
|
else
|
|
|
|
"#{key}: #{value}\n"
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
body = request.body.read
|
|
|
|
if body
|
2020-09-30 09:07:01 +00:00
|
|
|
request_data[:content] += "\n#{body}"
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
request_data[:content] = request_data[:content].slice(0, 8000)
|
|
|
|
|
|
|
|
# response
|
|
|
|
response_data = {
|
2018-12-19 17:31:51 +00:00
|
|
|
code: response.status = response.code,
|
|
|
|
content: '',
|
|
|
|
content_type: nil,
|
2017-03-09 11:44:51 +00:00
|
|
|
content_encoding: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
source: nil,
|
2017-03-09 11:44:51 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
response.headers.each do |key, value|
|
2017-03-09 11:44:51 +00:00
|
|
|
response_data[:content] += "#{key}: #{value}\n"
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
body = response.body
|
|
|
|
if body
|
2020-09-30 09:07:01 +00:00
|
|
|
response_data[:content] += "\n#{body}"
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
response_data[:content] = response_data[:content].slice(0, 8000)
|
|
|
|
record = {
|
|
|
|
direction: 'in',
|
2018-12-19 17:31:51 +00:00
|
|
|
facility: @http_log_support[:facility],
|
|
|
|
url: url_for(only_path: false, overwrite_params: {}),
|
|
|
|
status: response.status,
|
|
|
|
ip: request.remote_ip,
|
|
|
|
request: request_data,
|
|
|
|
response: response_data,
|
|
|
|
method: request.method,
|
2017-03-09 11:44:51 +00:00
|
|
|
}
|
|
|
|
HttpLog.create(record)
|
|
|
|
end
|
|
|
|
end
|