fast_http 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -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.8.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)
48
+ INCFLAGS = -I. -I. -I/usr/local/lib/ruby/1.8/i686-darwin9.8.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.8.0
63
+ sitearch = i686-darwin9.8.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)
79
+ LIBPATH = -L. -L$(libdir)
80
+ DEFFILE =
81
+
82
+ CLEANFILES = mkmf.log
83
+ DISTCLEANFILES =
84
+
85
+ extout =
86
+ extout_prefix =
87
+ target_prefix =
88
+ LOCAL_LIBS =
89
+ LIBS = -lc -ldl -lobjc
90
+ SRCS = http11_client.c http11_parser.c
91
+ OBJS = http11_client.o http11_parser.o
92
+ TARGET = http11_client
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) Makefile
152
+ @-$(RM) $@
153
+ $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
154
+
155
+
156
+
157
+ $(OBJS): ruby.h defines.h
@@ -0,0 +1,25 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>CFBundleDevelopmentRegion</key>
6
+ <string>English</string>
7
+ <key>CFBundleIdentifier</key>
8
+ <string>com.apple.xcode.dsym.conftest</string>
9
+ <key>CFBundleInfoDictionaryVersion</key>
10
+ <string>6.0</string>
11
+ <key>CFBundlePackageType</key>
12
+ <string>dSYM</string>
13
+ <key>CFBundleSignature</key>
14
+ <string>????</string>
15
+ <key>CFBundleShortVersionString</key>
16
+ <string>1.0</string>
17
+ <key>CFBundleVersion</key>
18
+ <string>1</string>
19
+ <key>dSYM_UUID</key>
20
+ <dict>
21
+ <key>i386</key>
22
+ <string>967BA07A-3963-B3FD-775B-94C9447BF669</string>
23
+ </dict>
24
+ </dict>
25
+ </plist>
@@ -0,0 +1,14 @@
1
+ #ifndef ext_help_h
2
+ #define ext_help_h
3
+
4
+ #define RAISE_NOT_NULL(T) if(T == NULL) rb_raise(rb_eArgError, "NULL found for " # T " when shouldn't be.");
5
+ #define DATA_GET(from,type,name) Data_Get_Struct(from,type,name); RAISE_NOT_NULL(name);
6
+ #define REQUIRE_TYPE(V, T) if(TYPE(V) != T) rb_raise(rb_eTypeError, "Wrong argument type for " # V " required " # T);
7
+
8
+ #ifdef DEBUG
9
+ #define TRACE() fprintf(stderr, "> %s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__)
10
+ #else
11
+ #define TRACE()
12
+ #endif
13
+
14
+ #endif
@@ -0,0 +1,6 @@
1
+ require 'mkmf'
2
+
3
+ dir_config("http11_client")
4
+ have_library("c", "main")
5
+
6
+ create_makefile("http11_client")
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Copyright (c) 2005 Zed A. Shaw
3
+ * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ */
5
+
6
+ #include "ruby.h"
7
+ #include "ext_help.h"
8
+ #include <assert.h>
9
+ #include <string.h>
10
+ #include "http11_parser.h"
11
+ #include <ctype.h>
12
+
13
+ static VALUE mFastHttp;
14
+ static VALUE cHttpClientParser;
15
+ static VALUE eHttpClientParserError;
16
+
17
+ #define id_reason rb_intern("@http_reason")
18
+ #define id_status rb_intern("@http_status")
19
+ #define id_version rb_intern("@http_version")
20
+ #define id_body rb_intern("@http_body")
21
+ #define id_chunk_size rb_intern("@http_chunk_size")
22
+ #define id_last_chunk rb_intern("@last_chunk")
23
+
24
+ void client_http_field(void *data, const char *field, size_t flen, const char *value, size_t vlen)
25
+ {
26
+ char *ch, *end;
27
+ VALUE req = (VALUE)data;
28
+ VALUE v = Qnil;
29
+ VALUE f = Qnil;
30
+
31
+ v = rb_str_new(value, vlen);
32
+ f = rb_str_new(field, flen);
33
+
34
+ /* Yes Children, rb_str_upcase_bang isn't even available as an intern.h function.
35
+ * how incredibly handy to not have that. Nope, I have to do it by hand.*/
36
+ for(ch = RSTRING_PTR(f), end = ch + RSTRING_LEN(f); ch < end; ch++) {
37
+ if(*ch == '-') {
38
+ *ch = '_';
39
+ } else {
40
+ *ch = toupper(*ch);
41
+ }
42
+ }
43
+
44
+ rb_hash_aset(req, f, v);
45
+ }
46
+
47
+ void client_reason_phrase(void *data, const char *at, size_t length)
48
+ {
49
+ VALUE req = (VALUE)data;
50
+ VALUE v = Qnil;
51
+
52
+ v = rb_str_new(at, length);
53
+
54
+ rb_ivar_set(req, id_reason, v);
55
+ }
56
+
57
+ void client_status_code(void *data, const char *at, size_t length)
58
+ {
59
+ VALUE req = (VALUE)data;
60
+ VALUE v = Qnil;
61
+
62
+ v = rb_str_new(at, length);
63
+
64
+ rb_ivar_set(req, id_status, v);
65
+ }
66
+
67
+ void client_http_version(void *data, const char *at, size_t length)
68
+ {
69
+ VALUE req = (VALUE)data;
70
+ VALUE v = Qnil;
71
+
72
+ v = rb_str_new(at, length);
73
+
74
+ rb_ivar_set(req, id_version, v);
75
+ }
76
+
77
+ /** Finalizes the request header to have a bunch of stuff that's
78
+ needed. */
79
+ void client_header_done(void *data, const char *at, size_t length)
80
+ {
81
+ VALUE req = (VALUE)data;
82
+ VALUE v = Qnil;
83
+
84
+ v = rb_str_new(at, length);
85
+ rb_ivar_set(req, id_body, v);
86
+ }
87
+
88
+ void client_chunk_size(void *data, const char *at, size_t length)
89
+ {
90
+ VALUE req = (VALUE)data;
91
+ VALUE v = Qnil;
92
+
93
+ if(length <= 0) {
94
+ rb_raise(eHttpClientParserError, "Chunked Encoding gave <= 0 chunk size.");
95
+ }
96
+
97
+ v = rb_str_new(at, length);
98
+
99
+ rb_ivar_set(req, id_chunk_size, v);
100
+ }
101
+
102
+ void client_last_chunk(void *data, const char *at, size_t length) {
103
+ VALUE req = (VALUE)data;
104
+ rb_ivar_set(req, id_last_chunk,Qtrue);
105
+ }
106
+
107
+
108
+ void HttpClientParser_free(void *data) {
109
+ TRACE();
110
+
111
+ if(data) {
112
+ free(data);
113
+ }
114
+ }
115
+
116
+
117
+ VALUE HttpClientParser_alloc(VALUE klass)
118
+ {
119
+ VALUE obj;
120
+ httpclient_parser *hp = ALLOC_N(httpclient_parser, 1);
121
+ TRACE();
122
+ hp->http_field = client_http_field;
123
+ hp->status_code = client_status_code;
124
+ hp->reason_phrase = client_reason_phrase;
125
+ hp->http_version = client_http_version;
126
+ hp->header_done = client_header_done;
127
+ hp->chunk_size = client_chunk_size;
128
+ hp->last_chunk = client_last_chunk;
129
+ httpclient_parser_init(hp);
130
+
131
+ obj = Data_Wrap_Struct(klass, NULL, HttpClientParser_free, hp);
132
+
133
+ return obj;
134
+ }
135
+
136
+
137
+ /**
138
+ * call-seq:
139
+ * parser.new -> parser
140
+ *
141
+ * Creates a new parser.
142
+ */
143
+ VALUE HttpClientParser_init(VALUE self)
144
+ {
145
+ httpclient_parser *http = NULL;
146
+ DATA_GET(self, httpclient_parser, http);
147
+ httpclient_parser_init(http);
148
+
149
+ return self;
150
+ }
151
+
152
+
153
+ /**
154
+ * call-seq:
155
+ * parser.reset -> nil
156
+ *
157
+ * Resets the parser to it's initial state so that you can reuse it
158
+ * rather than making new ones.
159
+ */
160
+ VALUE HttpClientParser_reset(VALUE self)
161
+ {
162
+ httpclient_parser *http = NULL;
163
+ DATA_GET(self, httpclient_parser, http);
164
+ httpclient_parser_init(http);
165
+
166
+ return Qnil;
167
+ }
168
+
169
+
170
+ /**
171
+ * call-seq:
172
+ * parser.finish -> true/false
173
+ *
174
+ * Finishes a parser early which could put in a "good" or bad state.
175
+ * You should call reset after finish it or bad things will happen.
176
+ */
177
+ VALUE HttpClientParser_finish(VALUE self)
178
+ {
179
+ httpclient_parser *http = NULL;
180
+ DATA_GET(self, httpclient_parser, http);
181
+ httpclient_parser_finish(http);
182
+
183
+ return httpclient_parser_is_finished(http) ? Qtrue : Qfalse;
184
+ }
185
+
186
+
187
+ /**
188
+ * call-seq:
189
+ * parser.execute(req_hash, data, start) -> Integer
190
+ *
191
+ * Takes a Hash and a String of data, parses the String of data filling in the Hash
192
+ * returning an Integer to indicate how much of the data has been read. No matter
193
+ * what the return value, you should call HttpClientParser#finished? and HttpClientParser#error?
194
+ * to figure out if it's done parsing or there was an error.
195
+ *
196
+ * This function now throws an exception when there is a parsing error. This makes
197
+ * the logic for working with the parser much easier. You can still test for an
198
+ * error, but now you need to wrap the parser with an exception handling block.
199
+ *
200
+ * The third argument allows for parsing a partial request and then continuing
201
+ * the parsing from that position. It needs all of the original data as well
202
+ * so you have to append to the data buffer as you read.
203
+ */
204
+ VALUE HttpClientParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
205
+ {
206
+ httpclient_parser *http = NULL;
207
+ int from = 0;
208
+ char *dptr = NULL;
209
+ long dlen = 0;
210
+
211
+ REQUIRE_TYPE(req_hash, T_HASH);
212
+ REQUIRE_TYPE(data, T_STRING);
213
+ REQUIRE_TYPE(start, T_FIXNUM);
214
+
215
+ DATA_GET(self, httpclient_parser, http);
216
+
217
+ from = FIX2INT(start);
218
+ dptr = RSTRING_PTR(data);
219
+ dlen = RSTRING_LEN(data);
220
+
221
+ if(from >= dlen) {
222
+ rb_raise(eHttpClientParserError, "Requested start is after data buffer end.");
223
+ } else {
224
+ http->data = (void *)req_hash;
225
+ httpclient_parser_execute(http, dptr, dlen, from);
226
+
227
+ if(httpclient_parser_has_error(http)) {
228
+ rb_raise(eHttpClientParserError, "Invalid HTTP format, parsing fails.");
229
+ } else {
230
+ return INT2FIX(httpclient_parser_nread(http));
231
+ }
232
+ }
233
+ }
234
+
235
+
236
+
237
+ /**
238
+ * call-seq:
239
+ * parser.error? -> true/false
240
+ *
241
+ * Tells you whether the parser is in an error state.
242
+ */
243
+ VALUE HttpClientParser_has_error(VALUE self)
244
+ {
245
+ httpclient_parser *http = NULL;
246
+ DATA_GET(self, httpclient_parser, http);
247
+
248
+ return httpclient_parser_has_error(http) ? Qtrue : Qfalse;
249
+ }
250
+
251
+
252
+ /**
253
+ * call-seq:
254
+ * parser.finished? -> true/false
255
+ *
256
+ * Tells you whether the parser is finished or not and in a good state.
257
+ */
258
+ VALUE HttpClientParser_is_finished(VALUE self)
259
+ {
260
+ httpclient_parser *http = NULL;
261
+ DATA_GET(self, httpclient_parser, http);
262
+
263
+ return httpclient_parser_is_finished(http) ? Qtrue : Qfalse;
264
+ }
265
+
266
+
267
+ /**
268
+ * call-seq:
269
+ * parser.nread -> Integer
270
+ *
271
+ * Returns the amount of data processed so far during this processing cycle. It is
272
+ * set to 0 on initialize or reset calls and is incremented each time execute is called.
273
+ */
274
+ VALUE HttpClientParser_nread(VALUE self)
275
+ {
276
+ httpclient_parser *http = NULL;
277
+ DATA_GET(self, httpclient_parser, http);
278
+
279
+ return INT2FIX(http->nread);
280
+ }
281
+
282
+
283
+
284
+ void Init_http11_client()
285
+ {
286
+
287
+ mFastHttp = rb_define_module("FastHttp");
288
+
289
+ eHttpClientParserError = rb_define_class_under(mFastHttp, "HttpClientParserError", rb_eIOError);
290
+
291
+ cHttpClientParser = rb_define_class_under(mFastHttp, "HttpClientParser", rb_cObject);
292
+ rb_define_alloc_func(cHttpClientParser, HttpClientParser_alloc);
293
+ rb_define_method(cHttpClientParser, "initialize", HttpClientParser_init,0);
294
+ rb_define_method(cHttpClientParser, "reset", HttpClientParser_reset,0);
295
+ rb_define_method(cHttpClientParser, "finish", HttpClientParser_finish,0);
296
+ rb_define_method(cHttpClientParser, "execute", HttpClientParser_execute,3);
297
+ rb_define_method(cHttpClientParser, "error?", HttpClientParser_has_error,0);
298
+ rb_define_method(cHttpClientParser, "finished?", HttpClientParser_is_finished,0);
299
+ rb_define_method(cHttpClientParser, "nread", HttpClientParser_nread,0);
300
+ }
301
+
302
+