From ff26fb40587ac7d151acc62ce07c0001bc7f5754 Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Thu, 30 Jun 2011 13:38:04 -0700 Subject: [PATCH 1/8] unlink tempfile when done building payload --- lib/restclient/payload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/restclient/payload.rb b/lib/restclient/payload.rb index c4c8d4d..ff0fd89 100644 --- a/lib/restclient/payload.rb +++ b/lib/restclient/payload.rb @@ -91,7 +91,7 @@ module RestClient alias :length :size def close - @stream.close + @stream.close! end def inspect From 8b7927591b44ed6342c34ad497d702089fd47a87 Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Tue, 5 Jul 2011 15:56:27 -0700 Subject: [PATCH 2/8] updated to call Base#close when finished transmitting --- lib/restclient/request.rb | 2 ++ spec/request2_spec.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/restclient/request.rb b/lib/restclient/request.rb index 75983f5..05dc855 100644 --- a/lib/restclient/request.rb +++ b/lib/restclient/request.rb @@ -62,6 +62,8 @@ module RestClient def execute & block uri = parse_url_with_auth(url) transmit uri, net_http_request_class(method).new(uri.request_uri, processed_headers), payload, & block + ensure + payload.close if payload end # Extract the query parameters for get request and append them to the url diff --git a/spec/request2_spec.rb b/spec/request2_spec.rb index 7a897bd..8870657 100644 --- a/spec/request2_spec.rb +++ b/spec/request2_spec.rb @@ -23,5 +23,18 @@ describe RestClient::Request do response_value.should == "foo" end + it 'closes payload if not nil' do + test_file = File.new(File.join( File.dirname(File.expand_path(__FILE__)), 'master_shake.jpg')) + initial_count = tmp_count + + stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200) + RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file}) + + tmp_count.should == initial_count + end + end +def tmp_count + Dir.glob(Dir::tmpdir + "/*").size +end \ No newline at end of file From 40d3641f84d4a588239af304892503ce352823cc Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Tue, 5 Jul 2011 16:00:47 -0700 Subject: [PATCH 3/8] removed bad commit --- lib/restclient/payload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/restclient/payload.rb b/lib/restclient/payload.rb index ff0fd89..c4c8d4d 100644 --- a/lib/restclient/payload.rb +++ b/lib/restclient/payload.rb @@ -91,7 +91,7 @@ module RestClient alias :length :size def close - @stream.close! + @stream.close end def inspect From c2624244572611dd107252c2ecfe4c11031e9956 Mon Sep 17 00:00:00 2001 From: Garry Shutler Date: Wed, 6 Jul 2011 22:01:48 +0100 Subject: [PATCH 4/8] Added a section to the README on passing query parameters --- README.rdoc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.rdoc b/README.rdoc index 70db237..7408333 100644 --- a/README.rdoc +++ b/README.rdoc @@ -217,6 +217,15 @@ use whatever proxy the system is configured to use: RestClient.proxy = ENV['http_proxy'] +== Query parameters + +Request objects know about query parameters and will automatically add them to +the url for GET, HEAD and DELETE requests and escape the keys and values as +needed: + + RestClient.get 'http://example.com/resource', :params => {:foo => 'bar', :baz => 'qux'} + # will GET http://example.com/resource?foo=bar&baz=qux + == Cookies Request and Response objects know about HTTP cookies, and will automatically From d86dae2349236027a8b50738278f1dafdbc43f38 Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Thu, 7 Jul 2011 10:04:29 -0700 Subject: [PATCH 5/8] updated payload to only close if open --- lib/restclient/payload.rb | 2 +- spec/payload_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/restclient/payload.rb b/lib/restclient/payload.rb index c4c8d4d..59925e4 100644 --- a/lib/restclient/payload.rb +++ b/lib/restclient/payload.rb @@ -91,7 +91,7 @@ module RestClient alias :length :size def close - @stream.close + @stream.close unless @stream.closed? end def inspect diff --git a/spec/payload_spec.rb b/spec/payload_spec.rb index 887e5c1..9605346 100644 --- a/spec/payload_spec.rb +++ b/spec/payload_spec.rb @@ -54,6 +54,11 @@ describe RestClient::Payload do RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s. should == "foo[]=bar&foo[]=baz" end + + it 'should not close if stream already closed' do + p = RestClient::Payload::UrlEncoded.new({'foo ' => 'bar'}) + 3.times {p.close} + end end @@ -63,6 +68,11 @@ describe RestClient::Payload do m.stub!(:boundary).and_return(123) m.headers['Content-Type'].should == 'multipart/form-data; boundary=123' end + + it 'should not error on close if stream already closed' do + m = RestClient::Payload::Multipart.new(:file => File.new(File.join(File.dirname(File.expand_path(__FILE__)), 'master_shake.jpg'))) + 3.times {m.close} + end it "should form properly separated multipart data" do m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]]) From 14ab0a89c46e92b3f71f995bd481d78a078013d0 Mon Sep 17 00:00:00 2001 From: Syl Turner Date: Tue, 12 Jul 2011 10:36:20 -0400 Subject: [PATCH 6/8] Monkey patching the HTTP PATCH method to Net::HTTP. --- lib/restclient/net_http_ext.rb | 46 ++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/restclient/net_http_ext.rb b/lib/restclient/net_http_ext.rb index 74148cd..b40dbbc 100644 --- a/lib/restclient/net_http_ext.rb +++ b/lib/restclient/net_http_ext.rb @@ -1,12 +1,44 @@ -# -# Replace the request method in Net::HTTP to sniff the body type -# and set the stream if appropriate -# -# Taken from: -# http://www.missiondata.com/blog/ruby/29/streaming-data-to-s3-with-ruby/ - module Net class HTTP + + # Adding the patch method (rest-client issue: https://github.com/archiloque/rest-client/issues/79) + # Code taken from this commit: https://github.com/ruby/ruby/commit/ab70e53ac3b5102d4ecbe8f38d4f76afad29d37d#lib/net/http.rb + class Protocol + # Sends a PATCH request to the +path+ and gets a response, + # as an HTTPResponse object. + def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+ + send_entity(path, data, initheader, dest, Patch, &block) + end + + # Executes a request which uses a representation + # and returns its body. + def send_entity(path, data, initheader, dest, type, &block) + res = nil + request(type.new(path, initheader), data) {|r| + r.read_body dest, &block + res = r + } + unless @newimpl + res.value + return res, res.body + end + res + end + end + + class Patch < HTTPRequest + METHOD = 'PATCH' + REQUEST_HAS_BODY = true + RESPONSE_HAS_BODY = true + end + + # + # Replace the request method in Net::HTTP to sniff the body type + # and set the stream if appropriate + # + # Taken from: + # http://www.missiondata.com/blog/ruby/29/streaming-data-to-s3-with-ruby/ + alias __request__ request def request(req, body=nil, &block) From d4ac54bbd6767c5997dcca14cb365f05a7348af1 Mon Sep 17 00:00:00 2001 From: Syl Turner Date: Tue, 12 Jul 2011 11:30:35 -0400 Subject: [PATCH 7/8] Added a check to see if the Patch class already exists in Net::HTTP (then we don't need this monkey patch) --- lib/restclient/net_http_ext.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/restclient/net_http_ext.rb b/lib/restclient/net_http_ext.rb index b40dbbc..ee40e9e 100644 --- a/lib/restclient/net_http_ext.rb +++ b/lib/restclient/net_http_ext.rb @@ -1,7 +1,8 @@ module Net - class HTTP + class HTTP - # Adding the patch method (rest-client issue: https://github.com/archiloque/rest-client/issues/79) + # Adding the patch method if it doesn't exist (rest-client issue: https://github.com/archiloque/rest-client/issues/79) + if !defined?(Net::HTTP::Patch) # Code taken from this commit: https://github.com/ruby/ruby/commit/ab70e53ac3b5102d4ecbe8f38d4f76afad29d37d#lib/net/http.rb class Protocol # Sends a PATCH request to the +path+ and gets a response, @@ -31,6 +32,7 @@ module Net REQUEST_HAS_BODY = true RESPONSE_HAS_BODY = true end + end # # Replace the request method in Net::HTTP to sniff the body type From a2476327af1130c05c9b247846e86dd9e478b45c Mon Sep 17 00:00:00 2001 From: Julien Kirch Date: Tue, 12 Jul 2011 19:31:48 +0200 Subject: [PATCH 8/8] adding history --- history.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/history.md b/history.md index d180d38..b947a1e 100644 --- a/history.md +++ b/history.md @@ -1,3 +1,7 @@ +# 1.6.4 +- fix unlinking temp file (patch provided by Evan Smith) +- monkeypatching ruby for http patch method (patch provided by Syl Turner) + # 1.6.3 - 1.6.2 was yanked