Permalink
Browse files

Added Patron::Session#enable_debug method that writes debug info to s…

…tderr or specified file
  • Loading branch information...
1 parent 2fde207 commit cfc0c5c542b9ebecc14ce4a57484a4333ca64b11 @ayanko ayanko committed with Oct 28, 2010
Showing with 42 additions and 3 deletions.
  1. +1 −0 README.txt
  2. +1 −1 VERSION.yml
  3. +33 −0 ext/patron/session_ext.c
  4. +6 −1 lib/patron/session.rb
  5. +1 −1 patron.gemspec
View
@@ -17,6 +17,7 @@ requests:
sess.timeout = 10
sess.base_url = "http://myserver.com:9900"
sess.headers['User-Agent'] = 'myapp/1.0'
+ sess.enable_debug "/tmp/patron.debug"
The Session is used to make HTTP requests.
View
@@ -1,5 +1,5 @@
---
:major: 0
:minor: 4
-:patch: 9
+:patch: 10
:build:
View
@@ -43,6 +43,7 @@ struct curl_state {
char* upload_buf;
FILE* download_file;
FILE* upload_file;
+ FILE* debug_file;
char error_buf[CURL_ERROR_SIZE];
struct curl_slist* headers;
};
@@ -78,6 +79,12 @@ static size_t session_read_handler(char* stream, size_t size, size_t nmemb, char
// Cleans up the Curl handle when the Session object is garbage collected.
void session_free(struct curl_state *curl) {
curl_easy_cleanup(curl->handle);
+
+ if (curl->debug_file) {
+ fclose(curl->debug_file);
+ curl->debug_file = NULL;
+ }
+
free(curl);
}
@@ -282,6 +289,12 @@ static void set_options_from_request(VALUE self, VALUE request) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
}
+
+ if(state->debug_file) {
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
+ curl_easy_setopt(curl, CURLOPT_STDERR, state->debug_file);
+ }
+
}
// Use the info in a Curl handle to create a new Response object.
@@ -405,6 +418,25 @@ VALUE enable_cookie_session(VALUE self, VALUE file) {
return Qnil;
}
+VALUE set_debug_file(VALUE self, VALUE file) {
+ struct curl_state *state;
+ Data_Get_Struct(self, struct curl_state, state);
+ char* file_path = RSTRING_PTR(file);
+
+ if(state->debug_file){
+ fclose(state->debug_file);
+ state->debug_file = NULL;
+ }
+
+ if(file_path != NULL && strlen(file_path) != 0) {
+ state->debug_file = open_file(file, "w");
+ } else {
+ state->debug_file = stderr;
+ }
+
+ return Qnil;
+}
+
//------------------------------------------------------------------------------
// Extension initialization
//
@@ -436,6 +468,7 @@ void Init_session_ext() {
rb_define_method(cSession, "unescape", session_unescape, 1);
rb_define_method(cSession, "handle_request", session_handle_request, 1);
rb_define_method(cSession, "enable_cookie_session", enable_cookie_session, 1);
+ rb_define_method(cSession, "set_debug_file", set_debug_file, 1);
rb_define_const(cRequest, "AuthBasic", INT2FIX(CURLAUTH_BASIC));
rb_define_const(cRequest, "AuthDigest", INT2FIX(CURLAUTH_DIGEST));
View
@@ -63,7 +63,7 @@ class Session
# Does this session stricly verify SSL certificates?
attr_accessor :insecure
- private :ext_initialize, :handle_request, :enable_cookie_session
+ private :ext_initialize, :handle_request, :enable_cookie_session, :set_debug_file
# Create a new Session object.
def initialize
@@ -92,6 +92,11 @@ def handle_cookies(file = nil)
self
end
+ # Enable debug output to stderr or to specified +file+.
+ def enable_debug(file = nil)
+ set_debug_file(file.to_s)
+ end
+
###################################################################
### Standard HTTP methods
###
View
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{patron}
- s.version = "0.4.9"
+ s.version = "0.4.10"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Phillip Toland"]

0 comments on commit cfc0c5c

Please sign in to comment.