Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #56 from mjijackson/master

Compare requests
  • Loading branch information...
commit 6214c2981fb4da7b368b30260d9debf0b166228e 2 parents e45457e + 5fb9cd2
@toland authored
View
17 ext/patron/session_ext.c
@@ -336,8 +336,10 @@ static void set_options_from_request(VALUE self, VALUE request) {
rb_hash_foreach(headers, each_http_header, self);
}
- action = SYM2ID(rb_iv_get(request, "@action"));
- if (action == rb_intern("get")) {
+ VALUE action_name = rb_iv_get(request, "@action");
+
+ action = rb_to_id(action_name);
+ if (action == rb_intern("GET")) {
VALUE data = rb_iv_get(request, "@upload_data");
VALUE download_file = rb_iv_get(request, "@file_name");
@@ -357,7 +359,7 @@ static void set_options_from_request(VALUE self, VALUE request) {
} else {
state->download_file = NULL;
}
- } else if (action == rb_intern("post") || action == rb_intern("put")) {
+ } else if (action == rb_intern("POST") || action == rb_intern("PUT")) {
VALUE data = rb_iv_get(request, "@upload_data");
VALUE filename = rb_iv_get(request, "@file_name");
VALUE multipart = rb_iv_get(request, "@multipart");
@@ -367,7 +369,7 @@ static void set_options_from_request(VALUE self, VALUE request) {
state->upload_buf = StringValuePtr(data);
- if (action == rb_intern("post")) {
+ if (action == rb_intern("POST")) {
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, state->upload_buf);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
@@ -382,14 +384,14 @@ static void set_options_from_request(VALUE self, VALUE request) {
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
- if (action == rb_intern("post")) {
+ if (action == rb_intern("POST")) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
}
state->upload_file = open_file(filename, "r");
curl_easy_setopt(curl, CURLOPT_READDATA, state->upload_file);
} else if (!NIL_P(multipart)) {
- if (action == rb_intern("post")) {
+ if (action == rb_intern("POST")) {
if(!NIL_P(data) && !NIL_P(filename)) {
if (rb_type(data) == T_HASH && rb_type(filename) == T_HASH) {
rb_hash_foreach(data, formadd_values, self);
@@ -405,10 +407,9 @@ static void set_options_from_request(VALUE self, VALUE request) {
} else {
rb_raise(rb_eArgError, "Must provide either data or a filename when doing a PUT or POST");
}
- } else if (action == rb_intern("head")) {
+ } else if (action == rb_intern("HEAD")) {
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
} else {
- VALUE action_name = rb_funcall(request, rb_intern("action_name"), 0);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, StringValuePtr(action_name));
}
View
58 lib/patron/request.rb
@@ -32,19 +32,29 @@ module Patron
# used in every request.
class Request
- VALID_ACTIONS = [:get, :put, :post, :delete, :head, :copy]
+ VALID_ACTIONS = %w[GET PUT POST DELETE HEAD COPY]
def initialize
- @action = :get
+ @action = 'GET'
@headers = {}
@timeout = 0
@connect_timeout = 0
@max_redirects = -1
end
- attr_accessor :url, :username, :password, :file_name, :proxy, :proxy_type, :auth_type, :insecure, :ignore_content_length, :multipart
- attr_reader :action, :timeout, :connect_timeout, :max_redirects, :headers, :buffer_size
- attr_reader :auth_type
+ READER_VARS = [
+ :url, :username, :password, :file_name, :proxy, :proxy_type, :insecure,
+ :ignore_content_length, :multipart, :action, :timeout, :connect_timeout,
+ :max_redirects, :headers, :auth_type, :upload_data, :buffer_size
+ ]
+
+ WRITER_VARS = [
+ :url, :username, :password, :file_name, :proxy, :proxy_type, :insecure,
+ :ignore_content_length, :multipart
+ ]
+
+ attr_reader *READER_VARS
+ attr_writer *WRITER_VARS
# Set the type of authentication to use for this request.
#
@@ -71,22 +81,20 @@ def auth_type=(type=:basic)
def upload_data=(data)
@upload_data = case data
when Hash
- self.multipart ? data : Util.build_query_string_from_hash(data, @action == :post)
+ self.multipart ? data : Util.build_query_string_from_hash(data, action == 'POST')
else
data
end
end
- def upload_data
- @upload_data
- end
-
def action=(new_action)
- if !VALID_ACTIONS.include?(new_action)
+ action = new_action.to_s.upcase
+
+ if !VALID_ACTIONS.include?(action)
raise ArgumentError, "Action must be one of #{VALID_ACTIONS.join(', ')}"
end
- @action = new_action
+ @action = action
end
def timeout=(new_timeout)
@@ -129,14 +137,32 @@ def buffer_size=(buffer_size)
@buffer_size = buffer_size != nil ? buffer_size.to_i : nil
end
- def action_name
- @action.to_s.upcase
- end
-
def credentials
return nil if username.nil? || password.nil?
"#{username}:#{password}"
end
+ def eql?(request)
+ return false unless Request === request
+
+ READER_VARS.inject(true) do |memo, name|
+ memo && (self.send(name) == request.send(name))
+ end
+ end
+
+ alias_method :==, :eql?
+
+ def marshal_dump
+ [ @url, @username, @password, @file_name, @proxy, @proxy_type, @insecure,
+ @ignore_content_length, @multipart, @action, @timeout, @connect_timeout,
+ @max_redirects, @headers, @auth_type, @upload_data, @buffer_size ]
+ end
+
+ def marshal_load(data)
+ @url, @username, @password, @file_name, @proxy, @proxy_type, @insecure,
+ @ignore_content_length, @multipart, @action, @timeout, @connect_timeout,
+ @max_redirects, @headers, @auth_type, @upload_data, @buffer_size = data
+ end
+
end
end
View
18 lib/patron/response.rb
@@ -35,13 +35,13 @@ def initialize(url, status, redirect_count, header_data, body, default_charset =
@status = status
@redirect_count = redirect_count
@body = body
-
+
@charset = determine_charset(header_data, body) || default_charset
-
+
[url, header_data].each do |attr|
convert_to_default_encoding!(attr)
end
-
+
parse_headers(header_data)
if @headers["Content-Type"] && @headers["Content-Type"][0, 5] == "text/"
convert_to_default_encoding!(@body)
@@ -55,14 +55,22 @@ def inspect
"#<Patron::Response @status_line='#{@status_line}'>"
end
+ def marshal_dump
+ [@url, @status, @status_line, @redirect_count, @body, @headers, @charset]
+ end
+
+ def marshal_load(data)
+ @url, @status, @status_line, @redirect_count, @body, @headers, @charset = data
+ end
+
private
def determine_charset(header_data, body)
header_data.match(charset_regex) || (body && body.match(charset_regex))
-
+
$1
end
-
+
def charset_regex
/(?:charset|encoding)="?([a-z0-9-]+)"?/i
end
View
18 spec/request_spec.rb
@@ -83,4 +83,22 @@
end
end
+
+ describe :eql? do
+
+ it "should return true when two requests are equal" do
+ @request.should eql(Patron::Request.new)
+ end
+
+ it "should return false when two requests are not equal" do
+ req = Patron::Request.new
+ req.action = :post
+ @request.should_not eql(req)
+ end
+
+ end
+
+ it "should be able to serialize and deserialize itself" do
+ Marshal.load(Marshal.dump(@request)).should eql(@request)
+ end
end
View
6 spec/response_spec.rb
@@ -48,11 +48,15 @@
response.headers['Content-Type'].should == 'image/png'
response.body.encoding.should == Encoding::ASCII_8BIT
end
-
+
it "should not allow a default charset to be nil" do
Encoding.stub(:default_internal).and_return("UTF-8")
expect {
Patron::Response.new("url", "status", 0, "", "", nil)
}.to_not raise_error
end
+
+ it "should be able to serialize and deserialize itself" do
+ Marshal.load(Marshal.dump(@request)).should eql(@request)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.