typhoeus_curly 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,157 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ #### Start of system configuration section. ####
5
+
6
+ srcdir = .
7
+ topdir = /usr/local/lib/ruby/1.8/i686-darwin9.6.0
8
+ hdrdir = $(topdir)
9
+ VPATH = $(srcdir):$(topdir):$(hdrdir)
10
+ exec_prefix = $(prefix)
11
+ prefix = $(DESTDIR)/usr/local
12
+ sharedstatedir = $(prefix)/com
13
+ mandir = $(datarootdir)/man
14
+ psdir = $(docdir)
15
+ oldincludedir = $(DESTDIR)/usr/include
16
+ localedir = $(datarootdir)/locale
17
+ bindir = $(exec_prefix)/bin
18
+ libexecdir = $(exec_prefix)/libexec
19
+ sitedir = $(libdir)/ruby/site_ruby
20
+ htmldir = $(docdir)
21
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
22
+ includedir = $(prefix)/include
23
+ infodir = $(datarootdir)/info
24
+ vendorlibdir = $(vendordir)/$(ruby_version)
25
+ sysconfdir = $(prefix)/etc
26
+ libdir = $(exec_prefix)/lib
27
+ sbindir = $(exec_prefix)/sbin
28
+ rubylibdir = $(libdir)/ruby/$(ruby_version)
29
+ docdir = $(datarootdir)/doc/$(PACKAGE)
30
+ dvidir = $(docdir)
31
+ vendordir = $(libdir)/ruby/vendor_ruby
32
+ datarootdir = $(prefix)/share
33
+ pdfdir = $(docdir)
34
+ archdir = $(rubylibdir)/$(arch)
35
+ sitearchdir = $(sitelibdir)/$(sitearch)
36
+ datadir = $(datarootdir)
37
+ localstatedir = $(prefix)/var
38
+ sitelibdir = $(sitedir)/$(ruby_version)
39
+
40
+ CC = gcc
41
+ LIBRUBY = $(LIBRUBY_A)
42
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
43
+ LIBRUBYARG_SHARED =
44
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
45
+
46
+ RUBY_EXTCONF_H =
47
+ CFLAGS = -fno-common -g -O2 -pipe -fno-common $(cflags) -g -DXP_UNIX -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline
48
+ INCFLAGS = -I. -I. -I/usr/local/lib/ruby/1.8/i686-darwin9.6.0 -I.
49
+ DEFS =
50
+ CPPFLAGS = -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE
51
+ CXXFLAGS = $(CFLAGS)
52
+ ldflags = -L.
53
+ dldflags =
54
+ archflag =
55
+ DLDFLAGS = $(ldflags) $(dldflags) $(archflag)
56
+ LDSHARED = cc -dynamic -bundle -undefined suppress -flat_namespace
57
+ AR = ar
58
+ EXEEXT =
59
+
60
+ RUBY_INSTALL_NAME = ruby
61
+ RUBY_SO_NAME = ruby
62
+ arch = i686-darwin9.6.0
63
+ sitearch = i686-darwin9.6.0
64
+ ruby_version = 1.8
65
+ ruby = /usr/local/bin/ruby
66
+ RUBY = $(ruby)
67
+ RM = rm -f
68
+ MAKEDIRS = mkdir -p
69
+ INSTALL = /usr/bin/install -c
70
+ INSTALL_PROG = $(INSTALL) -m 0755
71
+ INSTALL_DATA = $(INSTALL) -m 644
72
+ COPY = cp
73
+
74
+ #### End of system configuration section. ####
75
+
76
+ preload =
77
+
78
+ libpath = . $(libdir) /opt/local/lib
79
+ LIBPATH = -L. -L$(libdir) -L/opt/local/lib
80
+ DEFFILE =
81
+
82
+ CLEANFILES = mkmf.log
83
+ DISTCLEANFILES =
84
+
85
+ extout =
86
+ extout_prefix =
87
+ target_prefix = /typhoeus
88
+ LOCAL_LIBS =
89
+ LIBS = -lcurl -ldl -lobjc
90
+ SRCS = native.c typhoeus_easy.c typhoeus_multi.c
91
+ OBJS = native.o typhoeus_easy.o typhoeus_multi.o
92
+ TARGET = native
93
+ DLLIB = $(TARGET).bundle
94
+ EXTSTATIC =
95
+ STATIC_LIB =
96
+
97
+ BINDIR = $(bindir)
98
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
99
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
100
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
101
+
102
+ TARGET_SO = $(DLLIB)
103
+ CLEANLIBS = $(TARGET).bundle $(TARGET).il? $(TARGET).tds $(TARGET).map
104
+ CLEANOBJS = *.o *.a *.s[ol] *.pdb *.exp *.bak
105
+
106
+ all: $(DLLIB)
107
+ static: $(STATIC_LIB)
108
+
109
+ clean:
110
+ @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
111
+
112
+ distclean: clean
113
+ @-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
114
+ @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
115
+
116
+ realclean: distclean
117
+ install: install-so install-rb
118
+
119
+ install-so: $(RUBYARCHDIR)
120
+ install-so: $(RUBYARCHDIR)/$(DLLIB)
121
+ $(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
122
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
123
+ install-rb: pre-install-rb install-rb-default
124
+ install-rb-default: pre-install-rb-default
125
+ pre-install-rb: Makefile
126
+ pre-install-rb-default: Makefile
127
+ $(RUBYARCHDIR):
128
+ $(MAKEDIRS) $@
129
+
130
+ site-install: site-install-so site-install-rb
131
+ site-install-so: install-so
132
+ site-install-rb: install-rb
133
+
134
+ .SUFFIXES: .c .m .cc .cxx .cpp .C .o
135
+
136
+ .cc.o:
137
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
138
+
139
+ .cxx.o:
140
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
141
+
142
+ .cpp.o:
143
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
144
+
145
+ .C.o:
146
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
147
+
148
+ .c.o:
149
+ $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) -c $<
150
+
151
+ $(DLLIB): $(OBJS)
152
+ @-$(RM) $@
153
+ $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
154
+
155
+
156
+
157
+ $(OBJS): ruby.h defines.h
@@ -0,0 +1,65 @@
1
+ ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
2
+
3
+ # :stopdoc:
4
+
5
+ require 'mkmf'
6
+
7
+ ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
8
+ specified_curl = ARGV[0] =~ /^--with-curl/ ? ARGV[0].split("=")[1] : nil
9
+ LIBDIR = specified_curl ? "#{specified_curl}/lib": Config::CONFIG['libdir']
10
+ INCLUDEDIR = specified_curl ? "#{specified_curl}/include" : Config::CONFIG['includedir']
11
+
12
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'macruby'
13
+ $LIBRUBYARG_STATIC.gsub!(/-static/, '')
14
+ end
15
+
16
+ $CFLAGS << " #{ENV["CFLAGS"]}"
17
+ if Config::CONFIG['target_os'] == 'mingw32'
18
+ $CFLAGS << " -DXP_WIN -DXP_WIN32 -DUSE_INCLUDED_VASPRINTF"
19
+ elsif Config::CONFIG['target_os'] == 'solaris2'
20
+ $CFLAGS << " -DUSE_INCLUDED_VASPRINTF"
21
+ else
22
+ $CFLAGS << " -g -DXP_UNIX"
23
+ end
24
+
25
+ use_macports = !(defined?(RUBY_ENGINE) && RUBY_ENGINE != 'ruby')
26
+ $LIBPATH << "/opt/local/lib" if use_macports
27
+
28
+ $CFLAGS << " -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline"
29
+
30
+ if Config::CONFIG['target_os'] == 'mingw32'
31
+ header = File.join(ROOT, 'cross', 'curl-7.19.4.win32', 'include')
32
+ unless find_header('curl/curl.h', header)
33
+ abort "need libcurl"
34
+ end
35
+ else
36
+ HEADER_DIRS = [
37
+ File.join(INCLUDEDIR, "curl"),
38
+ INCLUDEDIR,
39
+ '/usr/include/curl',
40
+ '/usr/local/include/curl'
41
+ ]
42
+
43
+ [
44
+ '/opt/local/include/curl',
45
+ '/opt/local/include',
46
+ ].each { |x| HEADER_DIRS.unshift(x) } if use_macports
47
+
48
+ unless find_header('curl/curl.h', *HEADER_DIRS)
49
+ abort "need libcurl"
50
+ end
51
+ end
52
+
53
+ if Config::CONFIG['target_os'] == 'mingw32'
54
+ find_library('curl', 'curl_easy_init',
55
+ File.join(ROOT, 'cross', 'curl-7.19.4.win32', 'bin'))
56
+ else
57
+ find_library('curl', 'curl_easy_init',
58
+ LIBDIR,
59
+ '/opt/local/lib',
60
+ '/usr/local/lib',
61
+ '/usr/lib'
62
+ )
63
+ end
64
+
65
+ create_makefile("typhoeus/native")
@@ -0,0 +1,11 @@
1
+ #include <native.h>
2
+
3
+ VALUE mTyphoeus;
4
+
5
+ void Init_native()
6
+ {
7
+ mTyphoeus = rb_const_get(rb_cObject, rb_intern("Typhoeus"));
8
+
9
+ init_typhoeus_easy();
10
+ init_typhoeus_multi();
11
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef TYPHOEUS_NATIVE
2
+ #define TYPHOEUS_NATIVE
3
+
4
+ #include <ruby.h>
5
+ #include <curl/curl.h>
6
+ #include <curl/easy.h>
7
+ #include <curl/multi.h>
8
+
9
+ void Init_native();
10
+ extern VALUE mTyphoeus;
11
+ extern void init_typhoeus_easy();
12
+ extern void init_typhoeus_multi();
13
+
14
+ #endif
15
+
16
+ #ifndef RSTRING_PTR
17
+
18
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
19
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
20
+
21
+ #endif
@@ -0,0 +1,207 @@
1
+ #include <typhoeus_easy.h>
2
+
3
+ static VALUE idAppend;
4
+ VALUE cTyphoeusEasy;
5
+
6
+ static void dealloc(CurlEasy *curl_easy) {
7
+ if (curl_easy->request_chunk != NULL) {
8
+ free(curl_easy->request_chunk);
9
+ }
10
+
11
+ if (curl_easy->headers != NULL) {
12
+ curl_slist_free_all(curl_easy->headers);
13
+ }
14
+
15
+ curl_easy_cleanup(curl_easy->curl);
16
+
17
+ free(curl_easy);
18
+ }
19
+
20
+ static VALUE easy_setopt_string(VALUE self, VALUE opt_name, VALUE parameter) {
21
+ CurlEasy *curl_easy;
22
+ Data_Get_Struct(self, CurlEasy, curl_easy);
23
+
24
+ long opt = NUM2LONG(opt_name);
25
+ curl_easy_setopt(curl_easy->curl, opt, StringValuePtr(parameter));
26
+ return opt_name;
27
+ }
28
+
29
+ static VALUE easy_setopt_long(VALUE self, VALUE opt_name, VALUE parameter) {
30
+ CurlEasy *curl_easy;
31
+ Data_Get_Struct(self, CurlEasy, curl_easy);
32
+
33
+ long opt = NUM2LONG(opt_name);
34
+ curl_easy_setopt(curl_easy->curl, opt, NUM2LONG(parameter));
35
+ return opt_name;
36
+ }
37
+
38
+ static VALUE easy_getinfo_string(VALUE self, VALUE info) {
39
+ char *info_string;
40
+ CurlEasy *curl_easy;
41
+ Data_Get_Struct(self, CurlEasy, curl_easy);
42
+
43
+ long opt = NUM2LONG(info);
44
+ curl_easy_getinfo(curl_easy->curl, opt, &info_string);
45
+
46
+ return rb_str_new2(info_string);
47
+ }
48
+
49
+ static VALUE easy_getinfo_long(VALUE self, VALUE info) {
50
+ long info_long;
51
+ CurlEasy *curl_easy;
52
+ Data_Get_Struct(self, CurlEasy, curl_easy);
53
+
54
+ long opt = NUM2LONG(info);
55
+ curl_easy_getinfo(curl_easy->curl, opt, &info_long);
56
+
57
+ return LONG2NUM(info_long);
58
+ }
59
+
60
+ static VALUE easy_getinfo_double(VALUE self, VALUE info) {
61
+ double info_double = 0;
62
+ CurlEasy *curl_easy;
63
+ Data_Get_Struct(self, CurlEasy, curl_easy);
64
+
65
+ long opt = NUM2LONG(info);
66
+ curl_easy_getinfo(curl_easy->curl, opt, &info_double);
67
+
68
+ return rb_float_new(info_double);
69
+ }
70
+
71
+ static VALUE easy_perform(VALUE self) {
72
+ CurlEasy *curl_easy;
73
+ Data_Get_Struct(self, CurlEasy, curl_easy);
74
+ curl_easy_perform(curl_easy->curl);
75
+
76
+ return Qnil;
77
+ }
78
+
79
+ static size_t write_data_handler(char *stream, size_t size, size_t nmemb, VALUE val) {
80
+ long stream_size = (long)(size * nmemb);
81
+ rb_funcall(val, idAppend, 1, rb_str_new(stream, stream_size));
82
+ return size * nmemb;
83
+ }
84
+
85
+ static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *data) {
86
+ size_t realsize = size * nmemb;
87
+ RequestChunk *mem = (RequestChunk *)data;
88
+
89
+ if (realsize > mem->size - mem->read) {
90
+ realsize = mem->size - mem->read;
91
+ }
92
+
93
+ if (realsize != 0) {
94
+ memcpy(ptr, &(mem->memory[mem->read]), realsize);
95
+ mem->read += realsize;
96
+ }
97
+
98
+ return realsize;
99
+ }
100
+
101
+ static void set_response_handlers(VALUE easy, CURL *curl) {
102
+ rb_iv_set(easy, "@response_body", rb_str_new2(""));
103
+ rb_iv_set(easy, "@response_header", rb_str_new2(""));
104
+
105
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)&write_data_handler);
106
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, rb_iv_get(easy, "@response_body"));
107
+ curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)&write_data_handler);
108
+ curl_easy_setopt(curl, CURLOPT_HEADERDATA, rb_iv_get(easy, "@response_header"));
109
+ }
110
+
111
+ static VALUE easy_reset(VALUE self) {
112
+ CurlEasy *curl_easy;
113
+ Data_Get_Struct(self, CurlEasy, curl_easy);
114
+
115
+ if (curl_easy->request_chunk != NULL) {
116
+ free(curl_easy->request_chunk);
117
+ curl_easy->request_chunk = NULL;
118
+ }
119
+
120
+ if (curl_easy->headers != NULL) {
121
+ curl_slist_free_all(curl_easy->headers);
122
+ curl_easy->headers = NULL;
123
+ }
124
+
125
+ curl_easy_reset(curl_easy->curl);
126
+
127
+ set_response_handlers(self, curl_easy->curl);
128
+
129
+ return Qnil;
130
+ }
131
+
132
+ static VALUE easy_add_header(VALUE self, VALUE header) {
133
+ CurlEasy *curl_easy;
134
+ Data_Get_Struct(self, CurlEasy, curl_easy);
135
+
136
+ curl_easy->headers = curl_slist_append(curl_easy->headers, RSTRING_PTR(header));
137
+ return header;
138
+ }
139
+
140
+ static VALUE easy_set_headers(VALUE self) {
141
+ CurlEasy *curl_easy;
142
+ Data_Get_Struct(self, CurlEasy, curl_easy);
143
+
144
+ curl_easy_setopt(curl_easy->curl, CURLOPT_HTTPHEADER, curl_easy->headers);
145
+
146
+ return Qnil;
147
+ }
148
+
149
+ static VALUE easy_set_request_body(VALUE self, VALUE data, VALUE content_length_header) {
150
+ CurlEasy *curl_easy;
151
+ Data_Get_Struct(self, CurlEasy, curl_easy);
152
+
153
+ curl_easy->request_chunk = ALLOC(RequestChunk);
154
+ curl_easy->request_chunk->size = RSTRING_LEN(data);
155
+ curl_easy->request_chunk->memory = StringValuePtr(data);
156
+ curl_easy->request_chunk->read = 0;
157
+
158
+ curl_easy_setopt(curl_easy->curl, CURLOPT_READFUNCTION, (curl_read_callback)read_callback);
159
+ curl_easy_setopt(curl_easy->curl, CURLOPT_READDATA, curl_easy->request_chunk);
160
+ curl_easy_setopt(curl_easy->curl, CURLOPT_INFILESIZE, RSTRING_LEN(data));
161
+
162
+ return Qnil;
163
+ }
164
+
165
+ static VALUE easy_escape(VALUE self, VALUE data, VALUE length) {
166
+ CurlEasy *curl_easy;
167
+ Data_Get_Struct(self, CurlEasy, curl_easy);
168
+
169
+ return rb_str_new2(curl_easy_escape(curl_easy->curl, StringValuePtr(data), NUM2INT(length)));
170
+ }
171
+
172
+ static VALUE version(VALUE self) {
173
+ return rb_str_new2(curl_version());
174
+ }
175
+
176
+ static VALUE new(int argc, VALUE *argv, VALUE klass) {
177
+ CURL *curl = curl_easy_init();
178
+ CurlEasy *curl_easy = ALLOC(CurlEasy);
179
+ curl_easy->curl = curl;
180
+ curl_easy->headers = NULL;
181
+ curl_easy->request_chunk = NULL;
182
+ VALUE easy = Data_Wrap_Struct(cTyphoeusEasy, 0, dealloc, curl_easy);
183
+
184
+ set_response_handlers(easy, curl);
185
+
186
+ rb_obj_call_init(easy, argc, argv);
187
+
188
+ return easy;
189
+ }
190
+
191
+ void init_typhoeus_easy() {
192
+ VALUE klass = cTyphoeusEasy = rb_define_class_under(mTyphoeus, "Easy", rb_cObject);
193
+ idAppend = rb_intern("<<");
194
+ rb_define_singleton_method(klass, "new", new, -1);
195
+ rb_define_private_method(klass, "easy_setopt_string", easy_setopt_string, 2);
196
+ rb_define_private_method(klass, "easy_setopt_long", easy_setopt_long, 2);
197
+ rb_define_private_method(klass, "easy_getinfo_string", easy_getinfo_string, 1);
198
+ rb_define_private_method(klass, "easy_getinfo_long", easy_getinfo_long, 1);
199
+ rb_define_private_method(klass, "easy_getinfo_double", easy_getinfo_double, 1);
200
+ rb_define_private_method(klass, "easy_perform", easy_perform, 0);
201
+ rb_define_private_method(klass, "easy_reset", easy_reset, 0);
202
+ rb_define_private_method(klass, "easy_set_request_body", easy_set_request_body, 1);
203
+ rb_define_private_method(klass, "easy_set_headers", easy_set_headers, 0);
204
+ rb_define_private_method(klass, "easy_add_header", easy_add_header, 1);
205
+ rb_define_private_method(klass, "easy_escape", easy_escape, 2);
206
+ rb_define_private_method(klass, "version", version, 0);
207
+ }