rsense-server 0.5.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.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +14 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +1 -0
- data/README.md +51 -0
- data/Rakefile +9 -0
- data/bin/_rsense.rb +115 -0
- data/config/puma.rb +2 -0
- data/lib/rsense/server/code.rb +38 -0
- data/lib/rsense/server/command/completion_result.rb +11 -0
- data/lib/rsense/server/command/special_meth.rb +18 -0
- data/lib/rsense/server/command/type_inference_method.rb +24 -0
- data/lib/rsense/server/command.rb +239 -0
- data/lib/rsense/server/config.rb +70 -0
- data/lib/rsense/server/gem_path.rb +18 -0
- data/lib/rsense/server/listeners/find_definition_event_listener.rb +91 -0
- data/lib/rsense/server/listeners/where_event_listener.rb +39 -0
- data/lib/rsense/server/load_path.rb +62 -0
- data/lib/rsense/server/options.rb +85 -0
- data/lib/rsense/server/parser.rb +17 -0
- data/lib/rsense/server/path_info.rb +17 -0
- data/lib/rsense/server/project.rb +24 -0
- data/lib/rsense/server/version.rb +5 -0
- data/lib/rsense/server.rb +18 -0
- data/rsense-server.gemspec +35 -0
- data/spec/fixtures/config_fixture/.rsense +4 -0
- data/spec/fixtures/deeply/nested/thing.rb +0 -0
- data/spec/fixtures/find_def_sample.json +10 -0
- data/spec/fixtures/sample.json +10 -0
- data/spec/fixtures/test_gem/.gitignore +22 -0
- data/spec/fixtures/test_gem/Gemfile +4 -0
- data/spec/fixtures/test_gem/LICENSE.txt +22 -0
- data/spec/fixtures/test_gem/README.md +29 -0
- data/spec/fixtures/test_gem/Rakefile +2 -0
- data/spec/fixtures/test_gem/lib/sample/version.rb +3 -0
- data/spec/fixtures/test_gem/lib/sample.rb +16 -0
- data/spec/fixtures/test_gem/sample.gemspec +23 -0
- data/spec/fixtures/test_gem/test.json +10 -0
- data/spec/rsense/server/code_spec.rb +44 -0
- data/spec/rsense/server/command/special_meth_spec.rb +23 -0
- data/spec/rsense/server/command_spec.rb +108 -0
- data/spec/rsense/server/config_spec.rb +27 -0
- data/spec/rsense/server/gem_path_spec.rb +16 -0
- data/spec/rsense/server/load_path_spec.rb +63 -0
- data/spec/rsense/server/options_spec.rb +33 -0
- data/spec/rsense/server/path_info_spec.rb +11 -0
- data/spec/rsense/server/project_spec.rb +18 -0
- data/spec/rsense/server_spec.rb +7 -0
- data/spec/spec_helper.rb +16 -0
- data/vendor/gems/puma-2.8.2-java/COPYING +55 -0
- data/vendor/gems/puma-2.8.2-java/DEPLOYMENT.md +92 -0
- data/vendor/gems/puma-2.8.2-java/Gemfile +17 -0
- data/vendor/gems/puma-2.8.2-java/History.txt +532 -0
- data/vendor/gems/puma-2.8.2-java/LICENSE +26 -0
- data/vendor/gems/puma-2.8.2-java/Manifest.txt +68 -0
- data/vendor/gems/puma-2.8.2-java/README.md +251 -0
- data/vendor/gems/puma-2.8.2-java/Rakefile +158 -0
- data/vendor/gems/puma-2.8.2-java/bin/puma +10 -0
- data/vendor/gems/puma-2.8.2-java/bin/puma-wild +17 -0
- data/vendor/gems/puma-2.8.2-java/bin/pumactl +12 -0
- data/vendor/gems/puma-2.8.2-java/docs/config.md +0 -0
- data/vendor/gems/puma-2.8.2-java/docs/nginx.md +80 -0
- data/vendor/gems/puma-2.8.2-java/docs/signals.md +42 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/PumaHttp11Service.java +17 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/ext_help.h +15 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/extconf.rb +8 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/http11_parser.c +1225 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/http11_parser.h +64 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/http11_parser.java.rl +161 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/http11_parser.rl +146 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/http11_parser_common.rl +54 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/io_buffer.c +155 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/mini_ssl.c +195 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/org/jruby/puma/Http11.java +225 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/org/jruby/puma/Http11Parser.java +488 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/org/jruby/puma/MiniSSL.java +289 -0
- data/vendor/gems/puma-2.8.2-java/ext/puma_http11/puma_http11.c +491 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/accept_nonblock.rb +23 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/app/status.rb +59 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/binder.rb +298 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/capistrano.rb +86 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/cli.rb +587 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/client.rb +289 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/cluster.rb +389 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/compat.rb +18 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/configuration.rb +377 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/const.rb +165 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/control_cli.rb +251 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/daemon_ext.rb +25 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/delegation.rb +11 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/detect.rb +4 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/events.rb +130 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/io_buffer.rb +7 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/java_io_buffer.rb +45 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/jruby_restart.rb +83 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/minissl.rb +148 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/null_io.rb +34 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/puma_http11.jar +0 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/rack_default.rb +7 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb +45 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/reactor.rb +183 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/runner.rb +146 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/server.rb +801 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/single.rb +102 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/tcp_logger.rb +32 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/thread_pool.rb +185 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma/util.rb +9 -0
- data/vendor/gems/puma-2.8.2-java/lib/puma.rb +14 -0
- data/vendor/gems/puma-2.8.2-java/lib/rack/handler/puma.rb +66 -0
- data/vendor/gems/puma-2.8.2-java/puma.gemspec +55 -0
- data/vendor/gems/puma-2.8.2-java/test/test_app_status.rb +92 -0
- data/vendor/gems/puma-2.8.2-java/test/test_cli.rb +173 -0
- data/vendor/gems/puma-2.8.2-java/test/test_config.rb +26 -0
- data/vendor/gems/puma-2.8.2-java/test/test_http10.rb +27 -0
- data/vendor/gems/puma-2.8.2-java/test/test_http11.rb +144 -0
- data/vendor/gems/puma-2.8.2-java/test/test_integration.rb +165 -0
- data/vendor/gems/puma-2.8.2-java/test/test_iobuffer.rb +38 -0
- data/vendor/gems/puma-2.8.2-java/test/test_minissl.rb +25 -0
- data/vendor/gems/puma-2.8.2-java/test/test_null_io.rb +31 -0
- data/vendor/gems/puma-2.8.2-java/test/test_persistent.rb +238 -0
- data/vendor/gems/puma-2.8.2-java/test/test_puma_server.rb +323 -0
- data/vendor/gems/puma-2.8.2-java/test/test_rack_handler.rb +10 -0
- data/vendor/gems/puma-2.8.2-java/test/test_rack_server.rb +141 -0
- data/vendor/gems/puma-2.8.2-java/test/test_tcp_rack.rb +42 -0
- data/vendor/gems/puma-2.8.2-java/test/test_thread_pool.rb +156 -0
- data/vendor/gems/puma-2.8.2-java/test/test_unix_socket.rb +39 -0
- data/vendor/gems/puma-2.8.2-java/test/test_ws.rb +89 -0
- data/vendor/gems/puma-2.8.2-java/tools/jungle/README.md +9 -0
- data/vendor/gems/puma-2.8.2-java/tools/jungle/init.d/README.md +54 -0
- data/vendor/gems/puma-2.8.2-java/tools/jungle/init.d/puma +332 -0
- data/vendor/gems/puma-2.8.2-java/tools/jungle/init.d/run-puma +3 -0
- data/vendor/gems/puma-2.8.2-java/tools/jungle/upstart/README.md +61 -0
- data/vendor/gems/puma-2.8.2-java/tools/jungle/upstart/puma-manager.conf +31 -0
- data/vendor/gems/puma-2.8.2-java/tools/jungle/upstart/puma.conf +63 -0
- data/vendor/gems/puma-2.8.2-java/tools/trickletest.rb +45 -0
- metadata +389 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#define RSTRING_NOT_MODIFIED 1
|
|
2
|
+
#include <ruby.h>
|
|
3
|
+
#include <rubyio.h>
|
|
4
|
+
#include <openssl/bio.h>
|
|
5
|
+
#include <openssl/ssl.h>
|
|
6
|
+
#include <openssl/err.h>
|
|
7
|
+
|
|
8
|
+
typedef struct {
|
|
9
|
+
BIO* read;
|
|
10
|
+
BIO* write;
|
|
11
|
+
SSL* ssl;
|
|
12
|
+
SSL_CTX* ctx;
|
|
13
|
+
} ms_conn;
|
|
14
|
+
|
|
15
|
+
void engine_free(ms_conn* conn) {
|
|
16
|
+
BIO_free(conn->read);
|
|
17
|
+
BIO_free(conn->write);
|
|
18
|
+
|
|
19
|
+
free(conn);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
ms_conn* engine_alloc(VALUE klass, VALUE* obj) {
|
|
23
|
+
ms_conn* conn;
|
|
24
|
+
|
|
25
|
+
*obj = Data_Make_Struct(klass, ms_conn, 0, engine_free, conn);
|
|
26
|
+
|
|
27
|
+
conn->read = BIO_new(BIO_s_mem());
|
|
28
|
+
BIO_set_nbio(conn->read, 1);
|
|
29
|
+
|
|
30
|
+
conn->write = BIO_new(BIO_s_mem());
|
|
31
|
+
BIO_set_nbio(conn->write, 1);
|
|
32
|
+
|
|
33
|
+
conn->ssl = 0;
|
|
34
|
+
conn->ctx = 0;
|
|
35
|
+
|
|
36
|
+
return conn;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
VALUE engine_init_server(VALUE self, VALUE key, VALUE cert) {
|
|
40
|
+
VALUE obj;
|
|
41
|
+
SSL_CTX* ctx;
|
|
42
|
+
SSL* ssl;
|
|
43
|
+
|
|
44
|
+
ms_conn* conn = engine_alloc(self, &obj);
|
|
45
|
+
|
|
46
|
+
StringValue(key);
|
|
47
|
+
StringValue(cert);
|
|
48
|
+
|
|
49
|
+
ctx = SSL_CTX_new(SSLv23_server_method());
|
|
50
|
+
conn->ctx = ctx;
|
|
51
|
+
|
|
52
|
+
SSL_CTX_use_certificate_file(ctx, RSTRING_PTR(cert), SSL_FILETYPE_PEM);
|
|
53
|
+
SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM);
|
|
54
|
+
/* SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE); */
|
|
55
|
+
|
|
56
|
+
ssl = SSL_new(ctx);
|
|
57
|
+
conn->ssl = ssl;
|
|
58
|
+
|
|
59
|
+
/* SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL); */
|
|
60
|
+
|
|
61
|
+
SSL_set_bio(ssl, conn->read, conn->write);
|
|
62
|
+
|
|
63
|
+
SSL_set_accept_state(ssl);
|
|
64
|
+
return obj;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
VALUE engine_init_client(VALUE klass) {
|
|
68
|
+
VALUE obj;
|
|
69
|
+
ms_conn* conn = engine_alloc(klass, &obj);
|
|
70
|
+
|
|
71
|
+
conn->ctx = SSL_CTX_new(DTLSv1_method());
|
|
72
|
+
conn->ssl = SSL_new(conn->ctx);
|
|
73
|
+
SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
|
|
74
|
+
|
|
75
|
+
SSL_set_bio(conn->ssl, conn->read, conn->write);
|
|
76
|
+
|
|
77
|
+
SSL_set_connect_state(conn->ssl);
|
|
78
|
+
return obj;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
VALUE engine_inject(VALUE self, VALUE str) {
|
|
82
|
+
ms_conn* conn;
|
|
83
|
+
long used;
|
|
84
|
+
|
|
85
|
+
Data_Get_Struct(self, ms_conn, conn);
|
|
86
|
+
|
|
87
|
+
StringValue(str);
|
|
88
|
+
|
|
89
|
+
used = BIO_write(conn->read, RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
|
90
|
+
|
|
91
|
+
if(used == 0 || used == -1) {
|
|
92
|
+
return Qfalse;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return INT2FIX(used);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static VALUE eError;
|
|
99
|
+
|
|
100
|
+
void raise_error(SSL* ssl, int result) {
|
|
101
|
+
int error = SSL_get_error(ssl, result);
|
|
102
|
+
char* msg = ERR_error_string(error, NULL);
|
|
103
|
+
|
|
104
|
+
ERR_clear_error();
|
|
105
|
+
rb_raise(eError, "OpenSSL error: %s - %d", msg, error);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
VALUE engine_read(VALUE self) {
|
|
109
|
+
ms_conn* conn;
|
|
110
|
+
char buf[512];
|
|
111
|
+
int bytes, n;
|
|
112
|
+
|
|
113
|
+
Data_Get_Struct(self, ms_conn, conn);
|
|
114
|
+
|
|
115
|
+
bytes = SSL_read(conn->ssl, (void*)buf, sizeof(buf));
|
|
116
|
+
|
|
117
|
+
if(bytes > 0) {
|
|
118
|
+
return rb_str_new(buf, bytes);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if(SSL_want_read(conn->ssl)) return Qnil;
|
|
122
|
+
|
|
123
|
+
if(SSL_get_error(conn->ssl, bytes) == SSL_ERROR_ZERO_RETURN) {
|
|
124
|
+
rb_eof_error();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
raise_error(conn->ssl, bytes);
|
|
128
|
+
|
|
129
|
+
return Qnil;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
VALUE engine_write(VALUE self, VALUE str) {
|
|
133
|
+
ms_conn* conn;
|
|
134
|
+
char buf[512];
|
|
135
|
+
int bytes;
|
|
136
|
+
|
|
137
|
+
Data_Get_Struct(self, ms_conn, conn);
|
|
138
|
+
|
|
139
|
+
StringValue(str);
|
|
140
|
+
|
|
141
|
+
bytes = SSL_write(conn->ssl, (void*)RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
|
142
|
+
if(bytes > 0) {
|
|
143
|
+
return INT2FIX(bytes);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if(SSL_want_write(conn->ssl)) return Qnil;
|
|
147
|
+
|
|
148
|
+
raise_error(conn->ssl, bytes);
|
|
149
|
+
|
|
150
|
+
return Qnil;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
VALUE engine_extract(VALUE self) {
|
|
154
|
+
ms_conn* conn;
|
|
155
|
+
int bytes;
|
|
156
|
+
size_t pending;
|
|
157
|
+
char buf[512];
|
|
158
|
+
|
|
159
|
+
Data_Get_Struct(self, ms_conn, conn);
|
|
160
|
+
|
|
161
|
+
pending = BIO_pending(conn->write);
|
|
162
|
+
if(pending > 0) {
|
|
163
|
+
bytes = BIO_read(conn->write, buf, sizeof(buf));
|
|
164
|
+
if(bytes > 0) {
|
|
165
|
+
return rb_str_new(buf, bytes);
|
|
166
|
+
} else if(!BIO_should_retry(conn->write)) {
|
|
167
|
+
raise_error(conn->ssl, bytes);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return Qnil;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
void Init_mini_ssl(VALUE puma) {
|
|
175
|
+
VALUE mod, eng;
|
|
176
|
+
|
|
177
|
+
SSL_library_init();
|
|
178
|
+
OpenSSL_add_ssl_algorithms();
|
|
179
|
+
SSL_load_error_strings();
|
|
180
|
+
ERR_load_crypto_strings();
|
|
181
|
+
|
|
182
|
+
mod = rb_define_module_under(puma, "MiniSSL");
|
|
183
|
+
eng = rb_define_class_under(mod, "Engine", rb_cObject);
|
|
184
|
+
|
|
185
|
+
eError = rb_define_class_under(mod, "SSLError", rb_eStandardError);
|
|
186
|
+
|
|
187
|
+
rb_define_singleton_method(eng, "server", engine_init_server, 2);
|
|
188
|
+
rb_define_singleton_method(eng, "client", engine_init_client, 0);
|
|
189
|
+
|
|
190
|
+
rb_define_method(eng, "inject", engine_inject, 1);
|
|
191
|
+
rb_define_method(eng, "read", engine_read, 0);
|
|
192
|
+
|
|
193
|
+
rb_define_method(eng, "write", engine_write, 1);
|
|
194
|
+
rb_define_method(eng, "extract", engine_extract, 0);
|
|
195
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
package org.jruby.puma;
|
|
2
|
+
|
|
3
|
+
import org.jruby.Ruby;
|
|
4
|
+
import org.jruby.RubyClass;
|
|
5
|
+
import org.jruby.RubyHash;
|
|
6
|
+
import org.jruby.RubyModule;
|
|
7
|
+
import org.jruby.RubyNumeric;
|
|
8
|
+
import org.jruby.RubyObject;
|
|
9
|
+
import org.jruby.RubyString;
|
|
10
|
+
|
|
11
|
+
import org.jruby.anno.JRubyMethod;
|
|
12
|
+
|
|
13
|
+
import org.jruby.runtime.ObjectAllocator;
|
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
|
16
|
+
|
|
17
|
+
import org.jruby.exceptions.RaiseException;
|
|
18
|
+
|
|
19
|
+
import org.jruby.util.ByteList;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
|
|
23
|
+
*/
|
|
24
|
+
public class Http11 extends RubyObject {
|
|
25
|
+
public final static int MAX_FIELD_NAME_LENGTH = 256;
|
|
26
|
+
public final static String MAX_FIELD_NAME_LENGTH_ERR = "HTTP element FIELD_NAME is longer than the 256 allowed length.";
|
|
27
|
+
public final static int MAX_FIELD_VALUE_LENGTH = 80 * 1024;
|
|
28
|
+
public final static String MAX_FIELD_VALUE_LENGTH_ERR = "HTTP element FIELD_VALUE is longer than the 81920 allowed length.";
|
|
29
|
+
public final static int MAX_REQUEST_URI_LENGTH = 1024 * 12;
|
|
30
|
+
public final static String MAX_REQUEST_URI_LENGTH_ERR = "HTTP element REQUEST_URI is longer than the 12288 allowed length.";
|
|
31
|
+
public final static int MAX_FRAGMENT_LENGTH = 1024;
|
|
32
|
+
public final static String MAX_FRAGMENT_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 1024 allowed length.";
|
|
33
|
+
public final static int MAX_REQUEST_PATH_LENGTH = 2048;
|
|
34
|
+
public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 2048 allowed length.";
|
|
35
|
+
public final static int MAX_QUERY_STRING_LENGTH = 1024 * 10;
|
|
36
|
+
public final static String MAX_QUERY_STRING_LENGTH_ERR = "HTTP element QUERY_STRING is longer than the 10240 allowed length.";
|
|
37
|
+
public final static int MAX_HEADER_LENGTH = 1024 * (80 + 32);
|
|
38
|
+
public final static String MAX_HEADER_LENGTH_ERR = "HTTP element HEADER is longer than the 114688 allowed length.";
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
|
42
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
|
43
|
+
return new Http11(runtime, klass);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
public static void createHttp11(Ruby runtime) {
|
|
48
|
+
RubyModule mPuma = runtime.defineModule("Puma");
|
|
49
|
+
mPuma.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator());
|
|
50
|
+
|
|
51
|
+
RubyClass cHttpParser = mPuma.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
|
|
52
|
+
cHttpParser.defineAnnotatedMethods(Http11.class);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private Ruby runtime;
|
|
56
|
+
private RubyClass eHttpParserError;
|
|
57
|
+
private Http11Parser hp;
|
|
58
|
+
private RubyString body;
|
|
59
|
+
|
|
60
|
+
public Http11(Ruby runtime, RubyClass clazz) {
|
|
61
|
+
super(runtime,clazz);
|
|
62
|
+
this.runtime = runtime;
|
|
63
|
+
this.eHttpParserError = (RubyClass)runtime.getModule("Puma").getConstant("HttpParserError");
|
|
64
|
+
this.hp = new Http11Parser();
|
|
65
|
+
this.hp.parser.http_field = http_field;
|
|
66
|
+
this.hp.parser.request_method = request_method;
|
|
67
|
+
this.hp.parser.request_uri = request_uri;
|
|
68
|
+
this.hp.parser.fragment = fragment;
|
|
69
|
+
this.hp.parser.request_path = request_path;
|
|
70
|
+
this.hp.parser.query_string = query_string;
|
|
71
|
+
this.hp.parser.http_version = http_version;
|
|
72
|
+
this.hp.parser.header_done = header_done;
|
|
73
|
+
this.hp.parser.init();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public void validateMaxLength(int len, int max, String msg) {
|
|
77
|
+
if(len>max) {
|
|
78
|
+
throw new RaiseException(runtime, eHttpParserError, msg, true);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private Http11Parser.FieldCB http_field = new Http11Parser.FieldCB() {
|
|
83
|
+
public void call(Object data, int field, int flen, int value, int vlen) {
|
|
84
|
+
RubyHash req = (RubyHash)data;
|
|
85
|
+
RubyString v,f;
|
|
86
|
+
validateMaxLength(flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
|
|
87
|
+
validateMaxLength(vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);
|
|
88
|
+
|
|
89
|
+
v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen));
|
|
90
|
+
ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen);
|
|
91
|
+
for(int i = 0,j = b.length();i<j;i++) {
|
|
92
|
+
if((b.get(i) & 0xFF) == '-') {
|
|
93
|
+
b.set(i, (byte)'_');
|
|
94
|
+
} else {
|
|
95
|
+
b.set(i, (byte)Character.toUpperCase((char)b.get(i)));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
String as = b.toString();
|
|
100
|
+
|
|
101
|
+
if(as.equals("CONTENT_LENGTH") || as.equals("CONTENT_TYPE")) {
|
|
102
|
+
f = RubyString.newString(runtime, b);
|
|
103
|
+
} else {
|
|
104
|
+
f = RubyString.newString(runtime, "HTTP_");
|
|
105
|
+
f.cat(b);
|
|
106
|
+
}
|
|
107
|
+
req.op_aset(req.getRuntime().getCurrentContext(), f,v);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
private Http11Parser.ElementCB request_method = new Http11Parser.ElementCB() {
|
|
112
|
+
public void call(Object data, int at, int length) {
|
|
113
|
+
RubyHash req = (RubyHash)data;
|
|
114
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
|
115
|
+
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_METHOD"),val);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
private Http11Parser.ElementCB request_uri = new Http11Parser.ElementCB() {
|
|
120
|
+
public void call(Object data, int at, int length) {
|
|
121
|
+
RubyHash req = (RubyHash)data;
|
|
122
|
+
validateMaxLength(length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
|
|
123
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
|
124
|
+
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_URI"),val);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
private Http11Parser.ElementCB fragment = new Http11Parser.ElementCB() {
|
|
129
|
+
public void call(Object data, int at, int length) {
|
|
130
|
+
RubyHash req = (RubyHash)data;
|
|
131
|
+
validateMaxLength(length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
|
|
132
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
|
133
|
+
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("FRAGMENT"),val);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
private Http11Parser.ElementCB request_path = new Http11Parser.ElementCB() {
|
|
138
|
+
public void call(Object data, int at, int length) {
|
|
139
|
+
RubyHash req = (RubyHash)data;
|
|
140
|
+
validateMaxLength(length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
|
|
141
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
|
142
|
+
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_PATH"),val);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
private Http11Parser.ElementCB query_string = new Http11Parser.ElementCB() {
|
|
147
|
+
public void call(Object data, int at, int length) {
|
|
148
|
+
RubyHash req = (RubyHash)data;
|
|
149
|
+
validateMaxLength(length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
|
|
150
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
|
151
|
+
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("QUERY_STRING"),val);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
private Http11Parser.ElementCB http_version = new Http11Parser.ElementCB() {
|
|
156
|
+
public void call(Object data, int at, int length) {
|
|
157
|
+
RubyHash req = (RubyHash)data;
|
|
158
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
|
159
|
+
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("HTTP_VERSION"),val);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() {
|
|
164
|
+
public void call(Object data, int at, int length) {
|
|
165
|
+
body = RubyString.newString(runtime, new ByteList(hp.parser.buffer, at, length));
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
@JRubyMethod
|
|
170
|
+
public IRubyObject initialize() {
|
|
171
|
+
this.hp.parser.init();
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@JRubyMethod
|
|
176
|
+
public IRubyObject reset() {
|
|
177
|
+
this.hp.parser.init();
|
|
178
|
+
return runtime.getNil();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
@JRubyMethod
|
|
182
|
+
public IRubyObject finish() {
|
|
183
|
+
this.hp.finish();
|
|
184
|
+
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
@JRubyMethod
|
|
188
|
+
public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) {
|
|
189
|
+
int from = 0;
|
|
190
|
+
from = RubyNumeric.fix2int(start);
|
|
191
|
+
ByteList d = ((RubyString)data).getByteList();
|
|
192
|
+
if(from >= d.length()) {
|
|
193
|
+
throw new RaiseException(runtime, eHttpParserError, "Requested start is after data buffer end.", true);
|
|
194
|
+
} else {
|
|
195
|
+
this.hp.parser.data = req_hash;
|
|
196
|
+
this.hp.execute(d,from);
|
|
197
|
+
validateMaxLength(this.hp.parser.nread,MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR);
|
|
198
|
+
if(this.hp.has_error()) {
|
|
199
|
+
throw new RaiseException(runtime, eHttpParserError, "Invalid HTTP format, parsing fails.", true);
|
|
200
|
+
} else {
|
|
201
|
+
return runtime.newFixnum(this.hp.parser.nread);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
@JRubyMethod(name = "error?")
|
|
207
|
+
public IRubyObject has_error() {
|
|
208
|
+
return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
@JRubyMethod(name = "finished?")
|
|
212
|
+
public IRubyObject is_finished() {
|
|
213
|
+
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
@JRubyMethod
|
|
217
|
+
public IRubyObject nread() {
|
|
218
|
+
return runtime.newFixnum(this.hp.parser.nread);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
@JRubyMethod
|
|
222
|
+
public IRubyObject body() {
|
|
223
|
+
return body;
|
|
224
|
+
}
|
|
225
|
+
}// Http11
|