Skip to content

Commit 5fd6d70

Browse files
committed
MRI version of jruby-httpclient
1 parent bbec201 commit 5fd6d70

15 files changed

+375
-198
lines changed

jruby-httpclient.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
3838
"test/http_client/test_client_headers.rb",
3939
"test/http_client/test_cookies.rb",
4040
"test/http_client/test_redirect.rb",
41-
"test/http_client/test_request_entity.rb",
41+
"test/http_client/test_request_body.rb",
4242
"test/http_client/test_response.rb",
4343
"test/http_client/test_server_headers.rb",
4444
"test/http_test_server.rb",

lib/http_client.rb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
HTTP_CLIENT_DIR = File.join(File.dirname(__FILE__), 'http_client')
2-
VENDOR_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor'))
3-
42
$LOAD_PATH.unshift(HTTP_CLIENT_DIR) unless $LOAD_PATH.include?(HTTP_CLIENT_DIR)
5-
$LOAD_PATH.unshift(VENDOR_DIR) unless $LOAD_PATH.include?(VENDOR_DIR)
63

7-
require 'java'
8-
require 'commons-logging-1.1.1'
9-
require 'commons-codec-1.4'
10-
require 'httpcore-4.1'
11-
require 'httpmime-4.1.1'
12-
require 'httpclient-4.1.1'
13-
require 'httpclient-cache-4.1.1'
4+
require 'status'
5+
6+
if defined?(JRUBY_VERSION)
7+
JRUBY_HTTP_CLIENT_DIR = File.join(HTTP_CLIENT_DIR, 'jruby')
8+
VENDOR_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor'))
9+
10+
$LOAD_PATH.unshift(JRUBY_HTTP_CLIENT_DIR) unless $LOAD_PATH.include?(JRUBY_HTTP_CLIENT_DIR)
11+
$LOAD_PATH.unshift(VENDOR_DIR) unless $LOAD_PATH.include?(VENDOR_DIR)
12+
13+
require 'java'
14+
require 'commons-logging-1.1.1'
15+
require 'commons-codec-1.4'
16+
require 'httpcore-4.1'
17+
require 'httpmime-4.1.1'
18+
require 'httpclient-4.1.1'
19+
require 'httpclient-cache-4.1.1'
20+
21+
require 'client_configuration'
22+
else
23+
MRI_HTTP_CLIENT_DIR = File.join(HTTP_CLIENT_DIR, 'mri')
24+
$LOAD_PATH.unshift(MRI_HTTP_CLIENT_DIR) unless $LOAD_PATH.include?(MRI_HTTP_CLIENT_DIR)
25+
26+
require 'net/http'
27+
require 'cgi'
28+
end
1429

15-
require 'client_configuration'
16-
require 'response'
1730
require 'methods'
31+
require 'response'
1832
require 'client'
File renamed without changes.
File renamed without changes.

lib/http_client/jruby/response.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module HTTP
2+
class Response
3+
attr_reader :headers, :cookies
4+
5+
def initialize(native_response, cookies)
6+
@native_response = native_response
7+
@headers = Headers.new(native_response)
8+
@cookies = Cookies.new(cookies)
9+
end
10+
11+
def body
12+
@body ||= EntityUtils.to_string(@native_response.entity)
13+
end
14+
15+
def status_code
16+
@native_response.status_line.status_code
17+
end
18+
end
19+
20+
private
21+
EntityUtils = org.apache.http.util.EntityUtils
22+
23+
class Headers
24+
def initialize(native_response)
25+
@native_response = native_response
26+
end
27+
28+
def [](header_name)
29+
@native_response.get_first_header(header_name).value
30+
end
31+
end
32+
33+
class Cookies
34+
def initialize(cookies)
35+
@cookies = cookies
36+
end
37+
38+
def [](cookie_name)
39+
@cookies.find {|cookie| cookie.name == cookie_name }.value
40+
end
41+
end
42+
end

lib/http_client/mri/client.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module HTTP
2+
class Client
3+
def initialize(options)
4+
@default_host = options[:default_host]
5+
@timeout_in_seconds = options[:timeout_in_seconds]
6+
@uri = URI.parse(@default_host)
7+
end
8+
9+
def get(url, params = {})
10+
execute(HTTP::Get.new(url, params)).body
11+
end
12+
13+
def post(url, params = {})
14+
execute(HTTP::Post.new(url, params)).body
15+
end
16+
17+
def delete(url)
18+
execute(HTTP::Delete.new(url)).body
19+
end
20+
21+
def put(url)
22+
execute(HTTP::Put.new(url)).body
23+
end
24+
25+
def execute(req)
26+
req.execute_native_request(self)
27+
end
28+
29+
def execute_request(requested_host, requested_port, req)
30+
host = requested_host || @uri.host
31+
port = requested_port || @uri.port
32+
req['Cookie'] = @cookie unless @cookie.nil?
33+
34+
Net::HTTP.new(host, port).start do |http|
35+
http.read_timeout = @timeout_in_seconds || 30
36+
response = http.request(req)
37+
38+
if response['location']
39+
redirect_uri = URI.parse(response['location'])
40+
return execute_request(redirect_uri.host, redirect_uri.port, Net::HTTP::Get.new("#{redirect_uri.path}?#{redirect_uri.query}"))
41+
end
42+
43+
@cookie = response['set-cookie']
44+
45+
response
46+
end
47+
end
48+
49+
def shutdown; end
50+
end
51+
end

lib/http_client/mri/methods.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module HTTP
2+
class Request
3+
def initialize(url, params = {})
4+
@requested_uri = url
5+
@params = params
6+
@headers = {}
7+
end
8+
9+
def basic_auth(username, password)
10+
@username = username
11+
@password = password
12+
end
13+
14+
def add_headers(headers)
15+
@headers = headers
16+
end
17+
18+
def content_type=(content_type)
19+
@content_type = content_type
20+
end
21+
22+
def body=(body)
23+
@body = body
24+
end
25+
26+
def execute_native_request(client)
27+
host, port, req = create_request
28+
req.basic_auth(@username, @password)
29+
30+
@headers.each {|name, value| req[name.to_s] = value.to_s}
31+
req.content_type = @content_type unless @content_type.nil?
32+
req.body = @body unless @body.nil?
33+
34+
HTTP::Response.new(client.execute_request(host, port, req))
35+
end
36+
37+
private
38+
39+
def parse_uri
40+
request_uri = URI.parse(@requested_uri)
41+
42+
host = request_uri.host
43+
port = request_uri.port
44+
path = request_uri.path
45+
query = request_uri.query
46+
47+
[host, port, path, query]
48+
end
49+
end
50+
51+
class Get < Request
52+
def create_request
53+
host, port, path, query = parse_uri
54+
get = Net::HTTP::Get.new("#{path}?#{query || to_query_string(@params)}")
55+
[host, port, get]
56+
end
57+
58+
def to_query_string(params_as_hash)
59+
params_as_hash.map { |name, value| "#{URI.encode(name.to_s)}=#{URI.encode(value.to_s)}" }.join("&")
60+
end
61+
end
62+
63+
class Post < Request
64+
def create_request
65+
host, port, path, query = parse_uri
66+
post = Net::HTTP::Post.new(path)
67+
post.set_form_data(@params)
68+
[host, port, post]
69+
end
70+
end
71+
72+
class Put < Request
73+
def create_request
74+
host, port, path, query = parse_uri
75+
put = Net::HTTP::Put.new(path)
76+
[host, port, put]
77+
end
78+
end
79+
80+
class Delete < Request
81+
def create_request
82+
host, port, path, query = parse_uri
83+
delete = Net::HTTP::Delete.new(path)
84+
[host, port, delete]
85+
end
86+
end
87+
end

lib/http_client/mri/response.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module HTTP
2+
class Response
3+
attr_reader :status_code, :body
4+
5+
def initialize(native_response)
6+
@native_response = native_response
7+
@status_code = native_response.code.to_i
8+
@body = native_response.body
9+
end
10+
11+
def headers
12+
@native_response
13+
end
14+
15+
def cookies
16+
native_cookies = CGI::Cookie.parse(@native_response['set-cookie'])
17+
cookies = {}
18+
native_cookies.each {|name, cookie| cookies[name] = cookie.value.first}
19+
cookies
20+
end
21+
end
22+
end
Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
module HTTP
2-
class Response
3-
attr_reader :headers, :cookies
4-
5-
def initialize(native_response, cookies)
6-
@native_response = native_response
7-
@headers = Headers.new(native_response)
8-
@cookies = Cookies.new(cookies)
9-
end
10-
11-
def body
12-
@body ||= EntityUtils.to_string(@native_response.entity)
13-
end
14-
15-
def status_code
16-
@native_response.status_line.status_code
17-
end
18-
end
19-
20-
module Status
2+
module Status
213
ACCEPTED = 202
224
BAD_GATEWAY = 502
235
BAD_REQUEST = 400
@@ -67,27 +49,4 @@ module Status
6749
UNSUPPORTED_MEDIA_TYPE = 415
6850
USE_PROXY = 305
6951
end
70-
71-
private
72-
EntityUtils = org.apache.http.util.EntityUtils
73-
74-
class Headers
75-
def initialize(native_response)
76-
@native_response = native_response
77-
end
78-
79-
def [](header_name)
80-
@native_response.get_first_header(header_name).value
81-
end
82-
end
83-
84-
class Cookies
85-
def initialize(cookies)
86-
@cookies = cookies
87-
end
88-
89-
def [](cookie_name)
90-
@cookies.find {|cookie| cookie.name == cookie_name }.value
91-
end
92-
end
9352
end

0 commit comments

Comments
 (0)