class RCTHTTP
Public Class Methods
new()
click to toggle source
# File lib/rct/rct_http.rb, line 39 def initialize @http_client = HTTPClient.new() end
Public Instance Methods
handle_request()
click to toggle source
# File lib/rct/rct_http.rb, line 64 def handle_request if (RCT.sget(SERVER_TYPE) != nil) die("request by server type not implemented!") end protocol = RCT.sget(SERVER_PROTOCOL) host = RCT.sget(SERVER_HOSTNAME) if (protocol == nil) die("server protocol missing!") end cafile = RCT.sget(SSL_CA_FILE) if (cafile != nil) @http_client.ssl_config.set_trust_ca(cafile) RCT.log(INFO, "CA trust file set to #{cafile}") end if (host == nil) die("server hostname missing!") end url = "#{protocol}://#{host}" port = RCT.sget(SERVER_PORT) if (port != nil) url += ":#{port}" end url += RCT.sget(REQ_PATH) params = RCT.sget(REQ_PARAMS) if (params != nil && !params.empty?) url += params end method = RCT.sget(REQ_METHOD) if (method == nil || method.empty?) method = "GET" end headers = RCT.sget(REQ_HEADERS) if (headers == nil) headers = Hash.new() end headers['User-agent'] = "rct/#{RCT_VERSION}" auth = RCT.sget(REQ_AUTH_TYPE) if (auth != nil) if (auth == REQ_AUTH_TYPE_BASIC) name = RCT.sget(REQ_AUTH_NAME) pwd = RCT.sget(REQ_AUTH_PWD) # This does not send the authorization header unless server # first responds with 401, which some REST services won't do. # So, instead, set header manually below. # @http_client.set_auth(nil, name, pwd) up = "#{name}:#{pwd}" encoded = Base64.strict_encode64(up) headers['Authorization'] = "Basic #{encoded}" else raise "Requested auth type '#{auth}' unknown" end end show_request(method, url, headers, RCT.sget(REQ_BODY)) res = nil begin if (method == "GET") res = @http_client.get(url, nil, headers) elsif (method == "POST") body = RCT.sget(REQ_BODY) res = @http_client.post(url, body, headers) elsif (method == "PUT") body = RCT.sget(REQ_BODY) res = @http_client.put(url, body, headers) elsif (method == "DELETE") res = @http_client.delete(url, headers) else raise "Method #{method} not implemented yet!" end rescue Exception => e response = Response.new(nil) response.add_error(e.to_s) show_response(response) return response end response = Response.new(res) status = response.status if (status == 401) response.add_error("Unauthorized") end show_response(response) return response end
show_request(method, url, headers, body)
click to toggle source
# File lib/rct/rct_http.rb, line 173 def show_request(method, url, headers, body) return if (RCT.log_level < INFO) RCT.log(INFO, "-----[ REQUEST ]---" + "-" * 60) RCT.log(INFO, "#{method} #{url}") headers.each { |k,v| RCT.log(INFO, "#{k}: #{v}") } RCT.log(INFO, "") if (body != nil) RCT.log(INFO, body) RCT.log(INFO, "") end end
show_response(res)
click to toggle source
# File lib/rct/rct_http.rb, line 193 def show_response(res) return if (RCT.log_level < INFO) RCT.log(INFO, "-----[ RESPONSE ]--" + "-" * 60) RCT.log(INFO, "#{res.to_s}") headers = res.headers if (headers != nil) headers.each { |k,v| RCT.log(INFO, "#{k}: #{v}") } RCT.log(INFO, "") end RCT.log(INFO, res.body) end