diff options
| author | Carlos Konstanski <ckonstanski@pippiandcarlos.com> | 2017-08-30 13:09:56 -0600 |
|---|---|---|
| committer | Carlos Konstanski <ckonstanski@pippiandcarlos.com> | 2017-08-30 13:09:56 -0600 |
| commit | 649f0dd9c5ca586d8b637400dd19c0c4059447d6 (patch) | |
| tree | 5ccc992a3aa5de1bcb510911dfdb21e6a54bd197 /bootstrap/http_client.rb | |
initial commit
Diffstat (limited to 'bootstrap/http_client.rb')
| -rw-r--r-- | bootstrap/http_client.rb | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/bootstrap/http_client.rb b/bootstrap/http_client.rb new file mode 100644 index 0000000..58eea23 --- /dev/null +++ b/bootstrap/http_client.rb @@ -0,0 +1,90 @@ +require 'json' +require 'net/http' +require 'net/https' +require 'uri' +require 'openssl' + +class HttpClient + attr_accessor :uri + attr_accessor :header + attr_accessor :cookies + attr_accessor :body + attr_accessor :response + attr_accessor :response_code + + @@req_xml = <<EOF +<?xml version="1.0"?> +<a:propfind xmlns:a="DAV:"> +<a:prop><a:resourcetype/></a:prop> +</a:propfind> +EOF + + def initialize(url: nil, username: nil, password: nil, headers: Hash.new, cookies: Hash.new, body: nil) + @uri = URI.parse(url) + @username = username + @password = password + @headers = headers + @cookies = cookies + @body = body + end + + def do_http(method: :get, do_cookies_p: false, read_timeout: 600, extra_headers: Hash.new) + resp = nil + http = Net::HTTP.new(@uri.host, @uri.port) + if uri.scheme == 'https' + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + http.read_timeout = read_timeout + req = case method + when :get + Net::HTTP::Get.new(uri.request_uri, @headers.merge(extra_headers)) + when :delete + Net::HTTP::Delete.new(uri.request_uri, @headers.merge(extra_headers)) + when :post + Net::HTTP::Post.new(uri.request_uri, @headers.merge(extra_headers)) + when :put + Net::HTTP::Put.new(uri.request_uri, @headers.merge(extra_headers)) + when :propfind + Net::HTTP::Propfind.new(uri.request_uri, {"Depth" => "1"}) + end + req.basic_auth(@username, @password) if @username and @password + if method == :propfind + req.body = @@req_xml + else + req.body = @body if @body + end + resp = http.request(req) + @response = resp.body + @response_code = resp.class + get_cookies(resp) if do_cookies_p + end + + # Below is a pair of example Set-Cookie headers after + # Net::HttpClient turns them into a concatenated string. Note the + # bad use of a comma to separate the cookies. It's bad because a + # comma already appears in the date. We deal with that by eating + # the date with gsub() before splitting. + # + # csrftoken=urvsX10TfzWIK1fgctZ2HjHuNQZDr3E7; expires=Wed, 16-Nov-2016 16:57:20 GMT; Max-Age=31449600; Path=/, sessionid=e0t7304jn44ssc7gw412abup4aasehgb; expires=Wed, 18-Nov-2015 17:57:20 GMT; httponly; Max-Age=3600; Path=/ + # + # rememberMe=deleteMe; Path=/Openbook; Max-Age=0; Expires=Wed, 16-Dec-2015 22:53:58 GMT + def get_cookies(resp) + if resp.response['Set-Cookie'] + rawstring = resp.response['Set-Cookie'].gsub(/expires.*?Path=/, '') + rawstring.chomp.split(",").each do |entry| + entryparts = entry.split(";") + if entryparts.length >= 1 + cookieparts = entryparts[0].split("=") + name = cookieparts[0].strip + value = cookieparts[1].strip + @cookies[name] = value + end + end + end + end + + def create_cookie(name, value) + "#{name}=#{value}; Path=/" + end +end |
