Class: RCTHTTP
- Inherits:
-
Object
- Object
- RCTHTTP
- Defined in:
- lib/rct/rct_http.rb
Overview
This class provides the HTTP connection support for rct. This serves to hide the details of the underlying HTTP connector class so that it can be changed conveniently if desired.
Currently using HTTPClient: rubydoc.info/gems/httpclient/2.1.5.2/HTTPClient
Instance Method Summary collapse
-
#handle_request ⇒ Object
—————————————————————————- Handle one HTTP request.
-
#initialize ⇒ RCTHTTP
constructor
—————————————————————————- Constructor.
-
#show_request(method, url, headers, body) ⇒ Object
—————————————————————————- Show verbose info about the request.
-
#show_response(res) ⇒ Object
—————————————————————————- Show verbose info about the response.
Constructor Details
#initialize ⇒ RCTHTTP
Constructor.
39 40 41 |
# File 'lib/rct/rct_http.rb', line 39 def initialize @http_client = HTTPClient.new() end |
Instance Method Details
#handle_request ⇒ Object
Handle one HTTP request.
Connection parameters are all obtained from the current state (see constants.rb). Destination server is specified by type (SERVER_TYPE) OR by hostname+port. If both are given, type takes precedence.
-
SERVER_TYPE - Connect to a server of this type.
-
SERVER_HOSTNAME - Connect to this server.
-
SERVER_PORT - Connect to this port on server.
-
SERVER_PROTOCOL - Protocol (http/https).
-
REQ_PATH - Request path.
-
REQ_PARAMS - If set, appended to request path.
-
REQ_METHOD - HTTP method to use (if not set, default is GET)
-
REQ_HEADERS - Optional hash of additional request headers to set.
-
REQ_BODY - Optional request body.
-
SSL_CA_FILE - Override default trusted CA file for https.
Always returns an rct Response object.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# 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) ⇒ Object
Show verbose info about the request.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# 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) ⇒ Object
Show verbose info about the response.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# 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 |