cool.io 1.0.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/test.yaml +54 -0
  3. data/.gitignore +30 -0
  4. data/.rspec +3 -0
  5. data/{CHANGES → CHANGES.md} +175 -79
  6. data/{README.markdown → README.md} +43 -49
  7. data/Rakefile +44 -69
  8. data/appveyor.yml +18 -0
  9. data/cool.io.gemspec +18 -131
  10. data/examples/callbacked_echo_server.rb +24 -0
  11. data/ext/cool.io/buffer.c +781 -0
  12. data/ext/cool.io/cool.io.h +13 -0
  13. data/ext/cool.io/cool.io_ext.c +1 -0
  14. data/ext/cool.io/ev_wrap.h +4 -2
  15. data/ext/cool.io/extconf.rb +27 -16
  16. data/ext/cool.io/iowatcher.c +17 -8
  17. data/ext/cool.io/loop.c +36 -84
  18. data/ext/cool.io/stat_watcher.c +103 -25
  19. data/ext/cool.io/timer_watcher.c +3 -3
  20. data/ext/cool.io/utils.c +5 -0
  21. data/ext/cool.io/watcher.h +1 -1
  22. data/ext/libev/Changes +152 -3
  23. data/ext/libev/LICENSE +2 -1
  24. data/ext/libev/README +8 -8
  25. data/ext/libev/ev.c +1719 -348
  26. data/ext/libev/ev.h +146 -118
  27. data/ext/libev/ev_epoll.c +71 -20
  28. data/ext/libev/ev_kqueue.c +36 -16
  29. data/ext/libev/ev_poll.c +14 -11
  30. data/ext/libev/ev_port.c +43 -18
  31. data/ext/libev/ev_select.c +82 -25
  32. data/ext/libev/ev_vars.h +28 -21
  33. data/ext/libev/ev_win32.c +19 -10
  34. data/ext/libev/ev_wrap.h +166 -152
  35. data/ext/libev/test_libev_win32.c +1 -1
  36. data/gems.rb +9 -0
  37. data/lib/cool.io/async_watcher.rb +1 -1
  38. data/lib/cool.io/custom_require.rb +9 -0
  39. data/lib/cool.io/dns_resolver.rb +30 -22
  40. data/lib/cool.io/dsl.rb +32 -28
  41. data/lib/cool.io/io.rb +55 -35
  42. data/lib/cool.io/iowatcher.rb +1 -1
  43. data/lib/cool.io/listener.rb +35 -17
  44. data/lib/cool.io/loop.rb +17 -25
  45. data/lib/cool.io/meta.rb +6 -5
  46. data/lib/cool.io/server.rb +5 -4
  47. data/lib/cool.io/socket.rb +41 -30
  48. data/lib/cool.io/timer_watcher.rb +1 -1
  49. data/lib/cool.io/version.rb +7 -0
  50. data/lib/cool.io.rb +11 -10
  51. data/lib/coolio.rb +1 -1
  52. data/libev_ruby_gil.diff +175 -0
  53. data/libev_win_select.diff +130 -0
  54. data/spec/async_watcher_spec.rb +5 -5
  55. data/spec/dns_spec.rb +27 -8
  56. data/spec/iobuffer_spec.rb +147 -0
  57. data/spec/spec_helper.rb +15 -1
  58. data/spec/stat_watcher_spec.rb +81 -0
  59. data/spec/tcp_server_spec.rb +225 -0
  60. data/spec/tcp_socket_spec.rb +185 -0
  61. data/spec/timer_watcher_spec.rb +23 -19
  62. data/spec/udp_socket_spec.rb +58 -0
  63. data/spec/unix_listener_spec.rb +8 -8
  64. data/spec/unix_server_spec.rb +10 -8
  65. metadata +109 -112
  66. data/VERSION +0 -1
  67. data/examples/httpclient.rb +0 -38
  68. data/ext/http11_client/.gitignore +0 -5
  69. data/ext/http11_client/LICENSE +0 -31
  70. data/ext/http11_client/ext_help.h +0 -14
  71. data/ext/http11_client/extconf.rb +0 -6
  72. data/ext/http11_client/http11_client.c +0 -300
  73. data/ext/http11_client/http11_parser.c +0 -403
  74. data/ext/http11_client/http11_parser.h +0 -48
  75. data/ext/http11_client/http11_parser.rl +0 -173
  76. data/lib/cool.io/eventmachine.rb +0 -234
  77. data/lib/cool.io/http_client.rb +0 -419
  78. data/lib/rev.rb +0 -4
  79. data/spec/possible_tests/schedules_other_threads.rb +0 -48
  80. data/spec/possible_tests/test_on_resolve_failed.rb +0 -9
  81. data/spec/possible_tests/test_resolves.rb +0 -27
  82. data/spec/possible_tests/test_write_during_resolve.rb +0 -27
  83. data/spec/possible_tests/works_straight.rb +0 -71
metadata CHANGED
@@ -1,79 +1,99 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cool.io
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.9.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Tony Arcieri
14
- autorequire:
8
+ - Masahiro Nakagawa
9
+ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-12-13 00:00:00 -07:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: iobuffer
12
+ date: 2024-10-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake-compiler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :development
23
22
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 29
30
- segments:
31
- - 0
32
- - 1
33
- - 3
34
- version: 0.1.3
35
- type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake-compiler-dock
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
42
+ - !ruby/object:Gem::Dependency
38
43
  name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.13.0
49
+ type: :development
39
50
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.13.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: rdoc
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
43
60
  - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 11
46
- segments:
47
- - 2
48
- - 1
49
- - 0
50
- version: 2.1.0
61
+ - !ruby/object:Gem::Version
62
+ version: 3.6.0
51
63
  type: :development
52
- version_requirements: *id002
53
- description: A Ruby wrapper around the libev high performance event library
54
- email: tony@medioh.com
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.6.0
70
+ description: Cool.io provides a high performance event framework for Ruby which uses
71
+ the libev C library
72
+ email:
73
+ - tony.arcieri@gmail.com
74
+ - repeatedly@gmail.com
55
75
  executables: []
56
-
57
- extensions:
76
+ extensions:
58
77
  - ext/cool.io/extconf.rb
59
- - ext/http11_client/extconf.rb
60
- extra_rdoc_files:
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".github/workflows/test.yaml"
81
+ - ".gitignore"
82
+ - ".rspec"
83
+ - CHANGES.md
61
84
  - LICENSE
62
- - README.markdown
63
- files:
64
- - CHANGES
65
- - LICENSE
66
- - README.markdown
85
+ - README.md
67
86
  - Rakefile
68
- - VERSION
87
+ - appveyor.yml
69
88
  - cool.io.gemspec
89
+ - examples/callbacked_echo_server.rb
70
90
  - examples/dslified_echo_client.rb
71
91
  - examples/dslified_echo_server.rb
72
92
  - examples/echo_client.rb
73
93
  - examples/echo_server.rb
74
94
  - examples/google.rb
75
- - examples/httpclient.rb
76
95
  - ext/cool.io/.gitignore
96
+ - ext/cool.io/buffer.c
77
97
  - ext/cool.io/cool.io.h
78
98
  - ext/cool.io/cool.io_ext.c
79
99
  - ext/cool.io/ev_wrap.h
@@ -86,14 +106,6 @@ files:
86
106
  - ext/cool.io/utils.c
87
107
  - ext/cool.io/watcher.c
88
108
  - ext/cool.io/watcher.h
89
- - ext/http11_client/.gitignore
90
- - ext/http11_client/LICENSE
91
- - ext/http11_client/ext_help.h
92
- - ext/http11_client/extconf.rb
93
- - ext/http11_client/http11_client.c
94
- - ext/http11_client/http11_parser.c
95
- - ext/http11_client/http11_parser.h
96
- - ext/http11_client/http11_parser.rl
97
109
  - ext/libev/Changes
98
110
  - ext/libev/LICENSE
99
111
  - ext/libev/README
@@ -109,13 +121,13 @@ files:
109
121
  - ext/libev/ev_win32.c
110
122
  - ext/libev/ev_wrap.h
111
123
  - ext/libev/test_libev_win32.c
124
+ - gems.rb
112
125
  - lib/.gitignore
113
126
  - lib/cool.io.rb
114
127
  - lib/cool.io/async_watcher.rb
128
+ - lib/cool.io/custom_require.rb
115
129
  - lib/cool.io/dns_resolver.rb
116
130
  - lib/cool.io/dsl.rb
117
- - lib/cool.io/eventmachine.rb
118
- - lib/cool.io/http_client.rb
119
131
  - lib/cool.io/io.rb
120
132
  - lib/cool.io/iowatcher.rb
121
133
  - lib/cool.io/listener.rb
@@ -124,68 +136,53 @@ files:
124
136
  - lib/cool.io/server.rb
125
137
  - lib/cool.io/socket.rb
126
138
  - lib/cool.io/timer_watcher.rb
139
+ - lib/cool.io/version.rb
127
140
  - lib/coolio.rb
128
- - lib/rev.rb
141
+ - libev_ruby_gil.diff
142
+ - libev_win_select.diff
129
143
  - spec/async_watcher_spec.rb
130
144
  - spec/dns_spec.rb
131
- - spec/possible_tests/schedules_other_threads.rb
132
- - spec/possible_tests/test_on_resolve_failed.rb
133
- - spec/possible_tests/test_resolves.rb
134
- - spec/possible_tests/test_write_during_resolve.rb
135
- - spec/possible_tests/works_straight.rb
145
+ - spec/iobuffer_spec.rb
136
146
  - spec/spec_helper.rb
147
+ - spec/stat_watcher_spec.rb
148
+ - spec/tcp_server_spec.rb
149
+ - spec/tcp_socket_spec.rb
137
150
  - spec/timer_watcher_spec.rb
151
+ - spec/udp_socket_spec.rb
138
152
  - spec/unix_listener_spec.rb
139
153
  - spec/unix_server_spec.rb
140
- has_rdoc: true
141
- homepage: http://github.com/tarcieri/cool.io
142
- licenses: []
143
-
144
- post_install_message:
154
+ homepage: https://github.com/socketry/cool.io
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
145
159
  rdoc_options: []
146
-
147
- require_paths:
160
+ require_paths:
148
161
  - lib
149
- required_ruby_version: !ruby/object:Gem::Requirement
150
- none: false
151
- requirements:
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
152
164
  - - ">="
153
- - !ruby/object:Gem::Version
154
- hash: 3
155
- segments:
156
- - 0
157
- version: "0"
158
- required_rubygems_version: !ruby/object:Gem::Requirement
159
- none: false
160
- requirements:
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
161
169
  - - ">="
162
- - !ruby/object:Gem::Version
163
- hash: 3
164
- segments:
165
- - 0
166
- version: "0"
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
167
172
  requirements: []
168
-
169
- rubyforge_project:
170
- rubygems_version: 1.3.7
171
- signing_key:
172
- specification_version: 3
173
- summary: The cool event framework for Ruby
174
- test_files:
175
- - examples/dslified_echo_client.rb
176
- - examples/dslified_echo_server.rb
177
- - examples/echo_client.rb
178
- - examples/echo_server.rb
179
- - examples/google.rb
180
- - examples/httpclient.rb
173
+ rubygems_version: 3.5.11
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: A cool framework for doing high performance I/O in Ruby
177
+ test_files:
181
178
  - spec/async_watcher_spec.rb
182
179
  - spec/dns_spec.rb
183
- - spec/possible_tests/schedules_other_threads.rb
184
- - spec/possible_tests/test_on_resolve_failed.rb
185
- - spec/possible_tests/test_resolves.rb
186
- - spec/possible_tests/test_write_during_resolve.rb
187
- - spec/possible_tests/works_straight.rb
180
+ - spec/iobuffer_spec.rb
188
181
  - spec/spec_helper.rb
182
+ - spec/stat_watcher_spec.rb
183
+ - spec/tcp_server_spec.rb
184
+ - spec/tcp_socket_spec.rb
189
185
  - spec/timer_watcher_spec.rb
186
+ - spec/udp_socket_spec.rb
190
187
  - spec/unix_listener_spec.rb
191
188
  - spec/unix_server_spec.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.0.0
@@ -1,38 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
-
3
- require 'rubygems'
4
- require 'cool.io'
5
-
6
- class MyHttpClient < Coolio::HttpClient
7
- def on_connect
8
- super
9
- STDERR.puts "Connected to #{remote_host}:#{remote_port}"
10
- end
11
-
12
- def on_connect_failed
13
- super
14
- STDERR.puts "Connection failed"
15
- end
16
-
17
- def on_response_header(header)
18
- STDERR.puts "Response: #{header.http_version} #{header.status} #{header.http_reason}"
19
- end
20
-
21
- def on_body_data(data)
22
- STDOUT.write data
23
- STDOUT.flush
24
- end
25
-
26
- def on_request_complete
27
- STDERR.puts "Request complete!"
28
- end
29
-
30
- def on_error(reason)
31
- STDERR.puts "Error: #{reason}"
32
- end
33
- end
34
-
35
- l = Coolio::Loop.default
36
- c = MyHttpClient.connect("www.google.com", 80).attach(l)
37
- c.request('GET', '/search', :query => { :q => 'foobar' })
38
- l.run
@@ -1,5 +0,0 @@
1
- Makefile
2
- mkmf.log
3
- *.o
4
- *.so
5
- *.bundle
@@ -1,31 +0,0 @@
1
- Copyright (c) 2010, Zed A. Shaw and Mongrel2 Project Contributors.
2
- All rights reserved.
3
-
4
- Redistribution and use in source and binary forms, with or without
5
- modification, are permitted provided that the following conditions are
6
- met:
7
-
8
- * Redistributions of source code must retain the above copyright
9
- notice, this list of conditions and the following disclaimer.
10
-
11
- * Redistributions in binary form must reproduce the above copyright
12
- notice, this list of conditions and the following disclaimer in the
13
- documentation and/or other materials provided with the distribution.
14
-
15
- * Neither the name of the Mongrel2 Project, Zed A. Shaw, nor the names
16
- of its contributors may be used to endorse or promote products
17
- derived from this software without specific prior written
18
- permission.
19
-
20
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
- IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
@@ -1,14 +0,0 @@
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
@@ -1,6 +0,0 @@
1
- require 'mkmf'
2
-
3
- dir_config("http11_client")
4
- have_library("c", "main")
5
-
6
- create_makefile("http11_client")
@@ -1,300 +0,0 @@
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 mCoolio;
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
- mCoolio = rb_define_module("Coolio");
288
-
289
- eHttpClientParserError = rb_define_class_under(mCoolio, "HttpClientParserError", rb_eIOError);
290
-
291
- cHttpClientParser = rb_define_class_under(mCoolio, "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
- }