jubilee 2.1.0.rc1-java → 3.0.0.beta2-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -14
- data/Guardfile +24 -0
- data/README.md +34 -66
- data/Rakefile +1 -1
- data/examples/chatapp/Gemfile +2 -1
- data/examples/chatapp/Gemfile.lock +2 -2
- data/examples/chatapp/README.md +2 -14
- data/examples/chatapp/app.rb +19 -11
- data/examples/chatapp/public/assets/javascripts/application.js +1 -1
- data/examples/chatapp/public/assets/javascripts/vertxbus.js +60 -48
- data/examples/client/vertxbus.js +60 -48
- data/examples/shims_example/log/development.log +0 -0
- data/jars/hazelcast-3.5.2.jar +0 -0
- data/jars/jackson-annotations-2.6.0.jar +0 -0
- data/jars/jackson-core-2.6.1.jar +0 -0
- data/jars/jackson-databind-2.6.1.jar +0 -0
- data/jars/netty-buffer-4.0.33.Final.jar +0 -0
- data/jars/netty-codec-4.0.33.Final.jar +0 -0
- data/jars/netty-codec-http-4.0.33.Final.jar +0 -0
- data/jars/netty-common-4.0.33.Final.jar +0 -0
- data/jars/netty-handler-4.0.33.Final.jar +0 -0
- data/jars/netty-transport-4.0.33.Final.jar +0 -0
- data/jars/vertx-auth-common-3.2.1.jar +0 -0
- data/jars/vertx-core-3.2.1.jar +0 -0
- data/jars/vertx-hazelcast-3.2.1.jar +0 -0
- data/jars/vertx-lang-ruby-3.2.1.jar +0 -0
- data/jars/vertx-web-3.2.1.jar +0 -0
- data/jubilee.gemspec +40 -42
- data/lib/jubilee.rb +7 -2
- data/lib/jubilee/cli.rb +4 -4
- data/lib/jubilee/jubilee.jar +0 -0
- data/lib/jubilee/response.rb +1 -1
- data/lib/jubilee/version.rb +3 -3
- data/pom.xml +10 -94
- data/spec/apps/rails4/basic/Gemfile +2 -2
- data/spec/apps/rails4/basic/Gemfile.lock +5 -6
- data/src/main/java/org/jruby/jubilee/Const.java +1 -4
- data/src/main/java/org/jruby/jubilee/JubileeVerticle.java +41 -46
- data/src/main/java/org/jruby/jubilee/RackApplication.java +37 -57
- data/src/main/java/org/jruby/jubilee/RackEnvironment.java +38 -35
- data/src/main/java/org/jruby/jubilee/RackEnvironmentHash.java +1 -7
- data/src/main/java/org/jruby/jubilee/RubyHttpServerResponse.java +12 -6
- data/src/main/java/org/jruby/jubilee/RubyNetSocket.java +9 -12
- data/src/main/java/org/jruby/jubilee/RubyPlatformManager.java +39 -43
- data/src/main/java/org/jruby/jubilee/impl/RubyIORackInput.java +2 -1
- data/src/main/java/org/jruby/jubilee/vertx/JubileeVertx.java +1 -1
- metadata +30 -54
- data/examples/chatapp/config.json +0 -4
- data/jars/annotations-1.3.2.jar +0 -0
- data/jars/hazelcast-3.2.3.jar +0 -0
- data/jars/jackson-annotations-2.2.2.jar +0 -0
- data/jars/jackson-core-2.2.2.jar +0 -0
- data/jars/jackson-databind-2.2.2.jar +0 -0
- data/jars/lang-jruby-2.1.0-final.jar +0 -0
- data/jars/log4j-1.2.16.jar +0 -0
- data/jars/netty-all-4.0.20.Final.jar +0 -0
- data/jars/slf4j-api-1.6.2.jar +0 -0
- data/jars/vertx-core-2.1.2.jar +0 -0
- data/jars/vertx-hazelcast-2.1.2.jar +0 -0
- data/jars/vertx-platform-2.1.2.jar +0 -0
- data/lib/container.rb +0 -117
- data/lib/core/buffer.rb +0 -251
- data/lib/core/datagram.rb +0 -280
- data/lib/core/dns.rb +0 -143
- data/lib/core/event_bus.rb +0 -277
- data/lib/core/file_system.rb +0 -479
- data/lib/core/http.rb +0 -1307
- data/lib/core/net.rb +0 -251
- data/lib/core/network_support.rb +0 -77
- data/lib/core/parsetools.rb +0 -105
- data/lib/core/shared_data.rb +0 -214
- data/lib/core/sock_js.rb +0 -116
- data/lib/core/ssl_support.rb +0 -163
- data/lib/core/streams.rb +0 -183
- data/lib/core/tcp_support.rb +0 -136
- data/lib/core/timers.rb +0 -73
- data/lib/core/vertx_require.rb +0 -25
- data/lib/core/wrapped_handler.rb +0 -28
- data/lib/jubilee/jubilee_require.rb +0 -24
- data/lib/test_utils.rb +0 -66
- data/lib/vertx.rb +0 -30
- data/lib/vertx_tests.rb +0 -8
- data/src/main/assembly/mod.xml +0 -21
- data/src/main/java/org/jruby/jubilee/JubileeVerticleFactory.java +0 -258
- data/src/main/java/org/jruby/jubilee/RubyCallable.java +0 -52
- data/src/main/resources/META-INF/services/org.vertx.java.deploy.impl.jruby.JubileeVerticleFactory +0 -1
- data/src/main/resources/mod.json +0 -11
- data/vertx_classpath.txt +0 -12
data/lib/core/http.rb
DELETED
@@ -1,1307 +0,0 @@
|
|
1
|
-
# Copyright 2011 the original author or authors.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
|
15
|
-
require 'core/streams'
|
16
|
-
require 'core/ssl_support'
|
17
|
-
require 'core/tcp_support'
|
18
|
-
require 'core/wrapped_handler'
|
19
|
-
|
20
|
-
module Vertx
|
21
|
-
|
22
|
-
# An HTTP and WebSockets server
|
23
|
-
#
|
24
|
-
# @author {http://tfox.org Tim Fox}
|
25
|
-
class HttpServer
|
26
|
-
|
27
|
-
include SSLSupport, ServerSSLSupport, TCPSupport, ServerTCPSupport
|
28
|
-
|
29
|
-
# Letting the developer choose between compression? or compression while developing
|
30
|
-
#alias_method :compression?, :compression
|
31
|
-
|
32
|
-
# Create a new HttpServer.
|
33
|
-
# using an optional hash to configure the HttpServer on instantiation.
|
34
|
-
# @param [Hash] config an Hash of configurations.
|
35
|
-
def initialize(config = {})
|
36
|
-
@j_del = org.vertx.java.platform.impl.JRubyVerticleFactory.vertx.createHttpServer
|
37
|
-
self.compression = config[:compression] if config.has_key?(:compression)
|
38
|
-
self
|
39
|
-
end
|
40
|
-
|
41
|
-
# Set the HTTP request handler for the server.
|
42
|
-
# As HTTP requests arrive on the server a new {HttpServerRequest} instance will be created and passed to the handler.
|
43
|
-
# @param [Block] hndlr A block to be used as the handler
|
44
|
-
def request_handler(rm = nil, &hndlr)
|
45
|
-
if (rm && (rm.is_a? RouteMatcher))
|
46
|
-
@j_del.requestHandler { |j_del| rm.input(HttpServerRequest.new(j_del))}
|
47
|
-
else
|
48
|
-
@j_del.requestHandler { |j_del| hndlr.call(HttpServerRequest.new(j_del)) }
|
49
|
-
end
|
50
|
-
self
|
51
|
-
end
|
52
|
-
|
53
|
-
# Set the WebSocket handler for the server.
|
54
|
-
# As WebSocket requests arrive on the server and are accepted a new {WebSocket} instance will be created and
|
55
|
-
# passed to the handler.
|
56
|
-
# @param [Block] hndlr A block to be used as the handler
|
57
|
-
def websocket_handler(&hndlr)
|
58
|
-
@j_del.websocketHandler do |param|
|
59
|
-
hndlr.call(ServerWebSocket.new(param))
|
60
|
-
end
|
61
|
-
self
|
62
|
-
end
|
63
|
-
|
64
|
-
# Enables HTTP compression.
|
65
|
-
# exposes a = method to set or unset compression support.
|
66
|
-
# @param [Boolean] supported whether enable compression or not
|
67
|
-
def compression=(supported)
|
68
|
-
@j_del.setCompressionSupported(supported)
|
69
|
-
end
|
70
|
-
|
71
|
-
# Tests if compression is supported.
|
72
|
-
# @return [Boolean] true if compression is supported or false if it doesn't
|
73
|
-
def compression
|
74
|
-
@j_del.isCompressionSupported
|
75
|
-
end
|
76
|
-
|
77
|
-
# Letting the developer choose between compression? or compression while developing
|
78
|
-
alias_method :compression?, :compression
|
79
|
-
|
80
|
-
# Instruct the server to listen for incoming connections.
|
81
|
-
# @param [FixNum] port. The port to listen on.
|
82
|
-
# @param [FixNum] host. The host name or ip address to listen on.
|
83
|
-
# @param [Block] hndlr The handler will be called when the server is listening, or it failed to listen
|
84
|
-
def listen(port, host = "0.0.0.0", &hndlr)
|
85
|
-
@j_del.listen(port, host, ARWrappedHandler.new(hndlr) { |j_del| self })
|
86
|
-
end
|
87
|
-
|
88
|
-
# Close the server. The handler will be called when the close is complete.
|
89
|
-
def close(&hndlr)
|
90
|
-
if hndlr
|
91
|
-
@j_del.close(ARWrappedHandler.new(hndlr))
|
92
|
-
else
|
93
|
-
@j_del.close
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
# Gets the max WebSocket Frame size in bytes
|
98
|
-
# @return [int] Max WebSocket frame size
|
99
|
-
def max_websocket_frame_size
|
100
|
-
@j_del.getMaxWebSocketFrameSize
|
101
|
-
end
|
102
|
-
|
103
|
-
# Sets the maximum WebSocket frame size in bytes. Default is 65536 bytes.
|
104
|
-
# @param [int] size
|
105
|
-
def max_websocket_frame_size=(size)
|
106
|
-
@j_del.setMaxWebSocketFrameSize(size)
|
107
|
-
end
|
108
|
-
|
109
|
-
# @private
|
110
|
-
def _to_java_server
|
111
|
-
@j_del
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
# An HTTP client.
|
116
|
-
# A client maintains a pool of connections to a specific host, at a specific port. The HTTP connections can act
|
117
|
-
# as pipelines for HTTP requests.
|
118
|
-
# It is used as a factory for {HttpClientRequest} instances which encapsulate the actual HTTP requests. It is also
|
119
|
-
# used as a factory for {WebSocket WebSockets}.
|
120
|
-
#
|
121
|
-
# @author {http://tfox.org Tim Fox}
|
122
|
-
class HttpClient
|
123
|
-
|
124
|
-
include SSLSupport, ClientSSLSupport, TCPSupport
|
125
|
-
|
126
|
-
# Create a new HttpClient
|
127
|
-
def initialize(config = {})
|
128
|
-
@j_del = org.vertx.java.platform.impl.JRubyVerticleFactory.vertx.createHttpClient
|
129
|
-
self.compression = config[:try_compression] if config.has_key?(:try_compression)
|
130
|
-
end
|
131
|
-
|
132
|
-
# Enables HTTP compression.
|
133
|
-
# exposes a = method to set or unset compression support.
|
134
|
-
# @param [Boolean] supported whether enable compression or not
|
135
|
-
def compression=(supported)
|
136
|
-
@j_del.setTryUseCompression(supported)
|
137
|
-
end
|
138
|
-
|
139
|
-
# Tests if client is trying to use HTTP compression or not
|
140
|
-
# @return [Boolean] true if Client is trying using HTTP compression, false if it doesn't
|
141
|
-
def compression
|
142
|
-
@j_del.getTryUseCompression
|
143
|
-
end
|
144
|
-
|
145
|
-
# Letting the developer choose between compression? or compression while developing
|
146
|
-
alias_method :compression?, :compression
|
147
|
-
|
148
|
-
# Set the exception handler.
|
149
|
-
# @param [Block] hndlr A block to be used as the handler
|
150
|
-
def exception_handler(&hndlr)
|
151
|
-
@j_del.exceptionHandler(hndlr)
|
152
|
-
self
|
153
|
-
end
|
154
|
-
|
155
|
-
# Set the maximum pool size.
|
156
|
-
# The client will maintain up to this number of HTTP connections in an internal pool
|
157
|
-
# @param [FixNum] val. The maximum number of connections (default to 1).
|
158
|
-
def max_pool_size=(val)
|
159
|
-
@j_del.setMaxPoolSize(val)
|
160
|
-
self
|
161
|
-
end
|
162
|
-
|
163
|
-
# Get or set the max pool size
|
164
|
-
def max_pool_size(val = nil)
|
165
|
-
if val
|
166
|
-
@j_del.setMaxPoolSize(val)
|
167
|
-
self
|
168
|
-
else
|
169
|
-
@j_del.getMaxPoolSize
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
# If val is true then, after the request has ended the connection will be returned to the pool
|
174
|
-
# where it can be used by another request. In this manner, many HTTP requests can be pipe-lined over an HTTP connection.
|
175
|
-
# Keep alive connections will not be closed until the {#close} method is invoked.
|
176
|
-
# If val is false then a new connection will be created for each request and it won't ever go in the pool,
|
177
|
-
# the connection will closed after the response has been received. Even with no keep alive, the client will not allow more
|
178
|
-
# than {#max_pool_size} connections to be created at any one time.
|
179
|
-
# @param [Boolean] val. The value to use for keep_alive
|
180
|
-
def keep_alive=(val)
|
181
|
-
@j_del.setTCPKeepAlive(val)
|
182
|
-
self
|
183
|
-
end
|
184
|
-
|
185
|
-
# Get or set keep alive
|
186
|
-
def keep_alive(val = nil)
|
187
|
-
if val
|
188
|
-
@j_del.setKeepAlive(val)
|
189
|
-
self
|
190
|
-
else
|
191
|
-
@j_del.isKeepAlive
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
# Set the port that the client will attempt to connect to on the server on. The default value is 80
|
196
|
-
# @param [FixNum] val. The port value.
|
197
|
-
def port=(val)
|
198
|
-
@j_del.setPort(val)
|
199
|
-
self
|
200
|
-
end
|
201
|
-
|
202
|
-
# Get or set port
|
203
|
-
def port(val = nil)
|
204
|
-
if val
|
205
|
-
@j_del.setPort(val)
|
206
|
-
self
|
207
|
-
else
|
208
|
-
@j_del.getPort
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
# Set the host name or ip address that the client will attempt to connect to on the server on.
|
213
|
-
# @param [String] host. The host name or ip address to connect to.
|
214
|
-
def host=(val)
|
215
|
-
@j_del.setHost(val)
|
216
|
-
self
|
217
|
-
end
|
218
|
-
|
219
|
-
# Get or set host
|
220
|
-
def host(val = nil)
|
221
|
-
if val
|
222
|
-
@j_del.setHost(val)
|
223
|
-
self
|
224
|
-
else
|
225
|
-
@j_del.getHost
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
# Get or set verify host
|
230
|
-
def verify_host(val = nil)
|
231
|
-
if val
|
232
|
-
@j_del.setVerifyHost(val)
|
233
|
-
self
|
234
|
-
else
|
235
|
-
@j_del.isVerifyHost
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
# Gets the max WebSocket Frame size in bytes
|
240
|
-
# @return [int] Max WebSocket frame size
|
241
|
-
def max_websocket_frame_size
|
242
|
-
@j_del.getMaxWebSocketFrameSize
|
243
|
-
end
|
244
|
-
|
245
|
-
# Sets the maximum WebSocket frame size in bytes. Default is 65536 bytes.
|
246
|
-
# @param [int] size
|
247
|
-
def max_websocket_frame_size=(size)
|
248
|
-
@j_del.setMaxWebSocketFrameSize(size)
|
249
|
-
end
|
250
|
-
|
251
|
-
# Attempt to connect a WebSocket to the specified URI.
|
252
|
-
# The connect is done asynchronously and the handler is called with a {WebSocket} on success.
|
253
|
-
# @param [String] uri. A relative URI where to connect the WebSocket on the host, e.g. /some/path
|
254
|
-
# @param [Block] hndlr. The handler to be called with the {WebSocket}
|
255
|
-
def connect_web_socket(uri, &hndlr)
|
256
|
-
@j_del.connectWebsocket(uri) { |j_ws| hndlr.call(WebSocket.new(j_ws)) }
|
257
|
-
self
|
258
|
-
end
|
259
|
-
|
260
|
-
# This is a quick version of the {#get} method where you do not want to do anything with the request
|
261
|
-
# before sending.
|
262
|
-
# Normally with any of the HTTP methods you create the request then when you are ready to send it you call
|
263
|
-
# {HttpClientRequest#end} on it. With this method the request is immediately sent.
|
264
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
265
|
-
# @param [String] uri. A relative URI where to perform the GET on the server.
|
266
|
-
# @param [Hash] headers. A Hash of headers to pass with the request.
|
267
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
268
|
-
def get_now(uri, headers = nil, &hndlr)
|
269
|
-
@j_del.getNow(uri, headers, resp_handler(hndlr))
|
270
|
-
self
|
271
|
-
end
|
272
|
-
|
273
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP OPTIONS request with the specified uri.
|
274
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
275
|
-
# @param [String] uri. A relative URI where to perform the OPTIONS on the server.
|
276
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
277
|
-
def options(uri, &hndlr)
|
278
|
-
HttpClientRequest.new(@j_del.options(uri, resp_handler(hndlr)))
|
279
|
-
end
|
280
|
-
|
281
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP GET request with the specified uri.
|
282
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
283
|
-
# @param [String] uri. A relative URI where to perform the GET on the server.
|
284
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
285
|
-
def get(uri, &hndlr)
|
286
|
-
HttpClientRequest.new(@j_del.get(uri, resp_handler(hndlr)))
|
287
|
-
end
|
288
|
-
|
289
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP HEAD request with the specified uri.
|
290
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
291
|
-
# @param [String] uri. A relative URI where to perform the HEAD on the server.
|
292
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
293
|
-
def head(uri, &hndlr)
|
294
|
-
HttpClientRequest.new(@j_del.head(uri, resp_handler(hndlr)))
|
295
|
-
end
|
296
|
-
|
297
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP POST request with the specified uri.
|
298
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
299
|
-
# @param [String] uri. A relative URI where to perform the POST on the server.
|
300
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
301
|
-
def post(uri, &hndlr)
|
302
|
-
HttpClientRequest.new(@j_del.post(uri, resp_handler(hndlr)))
|
303
|
-
end
|
304
|
-
|
305
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP PUT request with the specified uri.
|
306
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
307
|
-
# @param [String] uri. A relative URI where to perform the PUT on the server.
|
308
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
309
|
-
def put(uri, &hndlr)
|
310
|
-
HttpClientRequest.new(@j_del.put(uri, resp_handler(hndlr)))
|
311
|
-
end
|
312
|
-
|
313
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP DELETE request with the specified uri.
|
314
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
315
|
-
# @param [String] uri. A relative URI where to perform the DELETE on the server.
|
316
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
317
|
-
def delete(uri, &hndlr)
|
318
|
-
HttpClientRequest.new(@j_del.delete(uri, resp_handler(hndlr)))
|
319
|
-
end
|
320
|
-
|
321
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP TRACE request with the specified uri.
|
322
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
323
|
-
# @param [String] uri. A relative URI where to perform the TRACE on the server.
|
324
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
325
|
-
def trace(uri, &hndlr)
|
326
|
-
HttpClientRequest.new(@j_del.trace(uri, resp_handler(hndlr)))
|
327
|
-
end
|
328
|
-
|
329
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP CONNECT request with the specified uri.
|
330
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
331
|
-
# @param [String] uri. A relative URI where to perform the CONNECT on the server.
|
332
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
333
|
-
def connect(uri, &hndlr)
|
334
|
-
HttpClientRequest.new(@j_del.connect(uri, resp_handler(hndlr)))
|
335
|
-
end
|
336
|
-
|
337
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP PATCH request with the specified uri.
|
338
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
339
|
-
# @param [String] uri. A relative URI where to perform the PATCH on the server.
|
340
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
341
|
-
def patch(uri, &hndlr)
|
342
|
-
HttpClientRequest.new(@j_del.patch(uri, resp_handler(hndlr)))
|
343
|
-
end
|
344
|
-
|
345
|
-
# This method returns an {HttpClientRequest} instance which represents an HTTP request with the specified method and uri.
|
346
|
-
# When an HTTP response is received from the server the handler is called passing in the response.
|
347
|
-
# @param [String] method. The HTTP method. Can be one of OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT.
|
348
|
-
# @param [String] uri. A relative URI where to perform the OPTIONS on the server.
|
349
|
-
# @param [Block] hndlr. The handler to be called with the {HttpClientResponse}
|
350
|
-
def request(method, uri, &hndlr)
|
351
|
-
HttpClientRequest.new(@j_del.request(method, uri, resp_handler(hndlr)))
|
352
|
-
end
|
353
|
-
|
354
|
-
# Close the client. Any unclosed connections will be closed.
|
355
|
-
def close
|
356
|
-
@j_del.close
|
357
|
-
end
|
358
|
-
|
359
|
-
# @private
|
360
|
-
def resp_handler(hndlr)
|
361
|
-
Proc.new { |j_del| hndlr.call(HttpClientResponse.new(j_del)) }
|
362
|
-
end
|
363
|
-
|
364
|
-
private :resp_handler
|
365
|
-
|
366
|
-
end
|
367
|
-
|
368
|
-
# Encapsulates a client-side HTTP request.
|
369
|
-
#
|
370
|
-
# Instances of this class are created by an {HttpClient} instance, via one of the methods corresponding to the
|
371
|
-
# specific HTTP methods, or the generic {HttpClient#request} method.
|
372
|
-
#
|
373
|
-
# Once an instance of this class has been obtained, headers can be set on it, and data can be written to its body,
|
374
|
-
# if required. Once you are ready to send the request, the {#end} method must called.
|
375
|
-
#
|
376
|
-
# Nothing is sent until the request has been internally assigned an HTTP connection. The {HttpClient} instance
|
377
|
-
# will return an instance of this class immediately, even if there are no HTTP connections available in the pool. Any requests
|
378
|
-
# sent before a connection is assigned will be queued internally and actually sent when an HTTP connection becomes
|
379
|
-
# available from the pool.
|
380
|
-
#
|
381
|
-
# The headers of the request are actually sent either when the {#end} method is called, or, when the first
|
382
|
-
# part of the body is written, whichever occurs first.
|
383
|
-
#
|
384
|
-
# This class supports both chunked and non-chunked HTTP.
|
385
|
-
#
|
386
|
-
# @author {http://tfox.org Tim Fox}
|
387
|
-
class HttpClientRequest
|
388
|
-
|
389
|
-
include WriteStream
|
390
|
-
|
391
|
-
# @private
|
392
|
-
def initialize(j_del)
|
393
|
-
@j_del = j_del
|
394
|
-
end
|
395
|
-
|
396
|
-
# MultiMap of headers for the request
|
397
|
-
def headers
|
398
|
-
if !@headers
|
399
|
-
@headers = MultiMap.new(@j_del.headers)
|
400
|
-
end
|
401
|
-
@headers
|
402
|
-
end
|
403
|
-
|
404
|
-
# Inserts a header into the request.
|
405
|
-
# @param [String] key The header key
|
406
|
-
# @param [Object] value The header value. to_s will be called on the value to determine the actual String value to insert.
|
407
|
-
# @return [HttpClientRequest] self So multiple operations can be chained.
|
408
|
-
def put_header(key, value)
|
409
|
-
@j_del.putHeader(key, value.to_s)
|
410
|
-
self
|
411
|
-
end
|
412
|
-
|
413
|
-
# Write a [String] to the request body.
|
414
|
-
# @param [String] str. The string to write.
|
415
|
-
# @param [String] enc. The encoding to use.
|
416
|
-
# @return [HttpClientRequest] self So multiple operations can be chained.
|
417
|
-
def write_str(str, enc = "UTF-8")
|
418
|
-
@j_del.write(str, enc)
|
419
|
-
self
|
420
|
-
end
|
421
|
-
|
422
|
-
# Forces the head of the request to be written before {#end} is called on the request. This is normally used
|
423
|
-
# to implement HTTP 100-continue handling, see {#continue_handler} for more information.
|
424
|
-
# @return [HttpClientRequest] self So multiple operations can be chained.
|
425
|
-
def send_head
|
426
|
-
@j_del.sendHead
|
427
|
-
self
|
428
|
-
end
|
429
|
-
|
430
|
-
# Ends the request. If no data has been written to the request body, and {#send_head} has not been called then
|
431
|
-
# the actual request won't get written until this method gets called.
|
432
|
-
# Once the request has ended, it cannot be used any more, and if keep alive is true the underlying connection will
|
433
|
-
# be returned to the {HttpClient} pool so it can be assigned to another request.
|
434
|
-
def end
|
435
|
-
@j_del.end
|
436
|
-
self
|
437
|
-
end
|
438
|
-
|
439
|
-
# Same as {#write_buffer_and_end} but writes a String
|
440
|
-
# @param [String] str The String to write
|
441
|
-
# @param [String] enc The encoding
|
442
|
-
def write_str_and_end(str, enc = "UTF-8")
|
443
|
-
@j_del.end(str, enc)
|
444
|
-
self
|
445
|
-
end
|
446
|
-
|
447
|
-
# Same as {#end} but writes some data to the response body before ending. If the response is not chunked and
|
448
|
-
# no other data has been written then the Content-Length header will be automatically set
|
449
|
-
# @param [Buffer] chunk The Buffer to write
|
450
|
-
def write_buffer_and_end(chunk)
|
451
|
-
@j_del.end(chunk._to_java_buffer)
|
452
|
-
self
|
453
|
-
end
|
454
|
-
|
455
|
-
# Sets whether the request should used HTTP chunked encoding or not.
|
456
|
-
# @param [Boolean] val. If val is true, this request will use HTTP chunked encoding, and each call to write to the body
|
457
|
-
# will correspond to a new HTTP chunk sent on the wire. If chunked encoding is used the HTTP header
|
458
|
-
# 'Transfer-Encoding' with a value of 'Chunked' will be automatically inserted in the request.
|
459
|
-
# If chunked is false, this request will not use HTTP chunked encoding, and therefore if any data is written the
|
460
|
-
# body of the request, the total size of that data must be set in the 'Content-Length' header before any
|
461
|
-
# data is written to the request body.
|
462
|
-
# @return [HttpClientRequest] self So multiple operations can be chained.
|
463
|
-
def chunked=(val)
|
464
|
-
@j_del.setChunked(val)
|
465
|
-
self
|
466
|
-
end
|
467
|
-
|
468
|
-
# Get or set chunked
|
469
|
-
def chunked(val = nil)
|
470
|
-
if val
|
471
|
-
@j_del.setChunked(val)
|
472
|
-
self
|
473
|
-
else
|
474
|
-
@j_del.getChunked
|
475
|
-
end
|
476
|
-
end
|
477
|
-
|
478
|
-
# If you send an HTTP request with the header 'Expect' set to the value '100-continue'
|
479
|
-
# and the server responds with an interim HTTP response with a status code of '100' and a continue handler
|
480
|
-
# has been set using this method, then the handler will be called.
|
481
|
-
# You can then continue to write data to the request body and later end it. This is normally used in conjunction with
|
482
|
-
# the {#send_head} method to force the request header to be written before the request has ended.
|
483
|
-
# @param [Block] hndlr. The handler
|
484
|
-
def continue_handler(&hndlr)
|
485
|
-
@j_del.continueHandler(hndlr)
|
486
|
-
self
|
487
|
-
end
|
488
|
-
|
489
|
-
# Get or set timeout
|
490
|
-
def timeout(val = nil)
|
491
|
-
if val
|
492
|
-
@j_del.setTimeout(val)
|
493
|
-
self
|
494
|
-
else
|
495
|
-
@j_del.getTimeout
|
496
|
-
end
|
497
|
-
end
|
498
|
-
|
499
|
-
end
|
500
|
-
|
501
|
-
# Encapsulates a client-side HTTP response.
|
502
|
-
#
|
503
|
-
# An instance of this class is provided to the user via a handler that was specified when one of the
|
504
|
-
# HTTP method operations, or the generic {HttpClient#request} method was called on an instance of {HttpClient}.
|
505
|
-
#
|
506
|
-
# @author {http://tfox.org Tim Fox}
|
507
|
-
class HttpClientResponse
|
508
|
-
|
509
|
-
include ReadStream
|
510
|
-
|
511
|
-
# @private
|
512
|
-
def initialize(j_del)
|
513
|
-
@j_del = j_del
|
514
|
-
end
|
515
|
-
|
516
|
-
# @return [FixNum] the HTTP status code of the response.
|
517
|
-
def status_code
|
518
|
-
@j_del.statusCode
|
519
|
-
end
|
520
|
-
|
521
|
-
# @return [String] the status message
|
522
|
-
def status_message
|
523
|
-
@j_del.statusMessage
|
524
|
-
end
|
525
|
-
|
526
|
-
# Get a header value
|
527
|
-
# @param [String] key. The key of the header.
|
528
|
-
# @return [String] the header value.
|
529
|
-
def header(key)
|
530
|
-
@j_del.getHeader(key)
|
531
|
-
end
|
532
|
-
|
533
|
-
# Get all the headers in the response.
|
534
|
-
# @return [MultiMap]. The headers
|
535
|
-
def headers
|
536
|
-
if !@headers
|
537
|
-
@headers = MultiMap.new(@j_del.headers)
|
538
|
-
end
|
539
|
-
@headers
|
540
|
-
end
|
541
|
-
|
542
|
-
# Get all the trailers in the response.
|
543
|
-
# @return [MultiMap]. The trailers
|
544
|
-
def trailers
|
545
|
-
if !@trailers
|
546
|
-
@trailers = MultiMap.new(@j_del.trailers)
|
547
|
-
end
|
548
|
-
@trailers
|
549
|
-
end
|
550
|
-
|
551
|
-
# Get all cookies
|
552
|
-
def cookies
|
553
|
-
if !@cookies
|
554
|
-
@cookies = @j_del.cookies
|
555
|
-
end
|
556
|
-
@cookies
|
557
|
-
end
|
558
|
-
|
559
|
-
# Set a handler to receive the entire body in one go - do not use this for large bodies
|
560
|
-
def body_handler(&hndlr)
|
561
|
-
@j_del.bodyHandler(hndlr)
|
562
|
-
self
|
563
|
-
end
|
564
|
-
|
565
|
-
end
|
566
|
-
|
567
|
-
# Encapsulates a server-side HTTP request.
|
568
|
-
#
|
569
|
-
# An instance of this class is created for each request that is handled by the server and is passed to the user via the
|
570
|
-
# handler specified using {HttpServer#request_handler}.
|
571
|
-
#
|
572
|
-
# Each instance of this class is associated with a corresponding {HttpServerResponse} instance via the field {#response}.
|
573
|
-
#
|
574
|
-
# @author {http://tfox.org Tim Fox}
|
575
|
-
class HttpServerRequest
|
576
|
-
|
577
|
-
include ReadStream
|
578
|
-
|
579
|
-
# @private
|
580
|
-
def initialize(j_del)
|
581
|
-
@j_del = j_del
|
582
|
-
@resp = HttpServerResponse.new(@j_del.response)
|
583
|
-
end
|
584
|
-
|
585
|
-
# @return [String] The Http version
|
586
|
-
def version
|
587
|
-
if !@vrsn
|
588
|
-
@vrsn = @j_del.version.toString
|
589
|
-
end
|
590
|
-
@vrsn
|
591
|
-
end
|
592
|
-
|
593
|
-
# @return [String] The HTTP method, one of HEAD, OPTIONS, GET, POST, PUT, DELETE, CONNECT, TRACE
|
594
|
-
def method
|
595
|
-
@j_del.method
|
596
|
-
end
|
597
|
-
|
598
|
-
# @return [String] The uri of the request. For example 'http://www.somedomain.com/somepath/somemorepath/somresource.foo?someparam=32&someotherparam=x'
|
599
|
-
def uri
|
600
|
-
@j_del.uri
|
601
|
-
end
|
602
|
-
|
603
|
-
# @return [String] The path part of the uri. For example /somepath/somemorepath/somresource.foo
|
604
|
-
def path
|
605
|
-
@j_del.path
|
606
|
-
end
|
607
|
-
|
608
|
-
# @return [String] The query part of the uri. For example someparam=32&someotherparam=x
|
609
|
-
def query
|
610
|
-
@j_del.query
|
611
|
-
end
|
612
|
-
|
613
|
-
# @return [MultiMap] The request parameters
|
614
|
-
def params
|
615
|
-
if !@params
|
616
|
-
@params = MultiMap.new(@j_del.params)
|
617
|
-
end
|
618
|
-
@params
|
619
|
-
end
|
620
|
-
|
621
|
-
# @return [HttpServerResponse] The response. Each instance of this class has an {HttpServerResponse} instance attached to it. This is used
|
622
|
-
# to send the response back to the client.
|
623
|
-
def response
|
624
|
-
@resp
|
625
|
-
end
|
626
|
-
|
627
|
-
# @return [MultiMap] The request headers
|
628
|
-
def headers
|
629
|
-
if !@headers
|
630
|
-
@headers = MultiMap.new(@j_del.headers)
|
631
|
-
end
|
632
|
-
@headers
|
633
|
-
end
|
634
|
-
|
635
|
-
# You must call this function with true before receiving the request body if you expect it to
|
636
|
-
# contain a multi-part form
|
637
|
-
def expect_multipart=(expect)
|
638
|
-
@j_del.expectMultiPart(expect)
|
639
|
-
self
|
640
|
-
end
|
641
|
-
|
642
|
-
# @return [MultiMap] Returns a map of all form attributes which was found in the request. Be aware that this
|
643
|
-
# message should only get
|
644
|
-
# called after the endHandler was notified as the map will be filled on-the-fly.
|
645
|
-
def form_attributes
|
646
|
-
if !@attrs
|
647
|
-
@attrs = MultiMap.new(@j_del.formAttributes)
|
648
|
-
end
|
649
|
-
@attrs
|
650
|
-
end
|
651
|
-
|
652
|
-
# Set the upload handler. The handler will get notified once a new file upload was received and so allow to
|
653
|
-
# get notified by the upload in progress.
|
654
|
-
def upload_handler(&hndlr)
|
655
|
-
@j_del.uploadHandler do |j_upload|
|
656
|
-
hndlr.call(HttpServerFileUpload.new(j_upload))
|
657
|
-
end
|
658
|
-
self
|
659
|
-
end
|
660
|
-
|
661
|
-
# Set a handler to receive the entire body in one go - do not use this for large bodies
|
662
|
-
def body_handler(&hndlr)
|
663
|
-
@j_del.bodyHandler(hndlr)
|
664
|
-
self
|
665
|
-
end
|
666
|
-
|
667
|
-
# Get the remote address
|
668
|
-
def remote_address
|
669
|
-
@j_del.remoteAddress
|
670
|
-
end
|
671
|
-
|
672
|
-
# Get the absolute URI
|
673
|
-
def absolute_uri
|
674
|
-
@j_del.absoluteURI
|
675
|
-
end
|
676
|
-
|
677
|
-
def _to_java_request
|
678
|
-
@j_del
|
679
|
-
end
|
680
|
-
|
681
|
-
end
|
682
|
-
|
683
|
-
# Encapsulates a server-side HTTP response.
|
684
|
-
#
|
685
|
-
# An instance of this class is created and associated to every instance of {HttpServerRequest} that is created.
|
686
|
-
#
|
687
|
-
# It allows the developer to control the HTTP response that is sent back to the client for the corresponding HTTP
|
688
|
-
# request. It contains methods that allow HTTP headers and trailers to be set, and for a body to be written out
|
689
|
-
# to the response.
|
690
|
-
#
|
691
|
-
# @author {http://tfox.org Tim Fox}
|
692
|
-
class HttpServerResponse
|
693
|
-
|
694
|
-
include WriteStream
|
695
|
-
|
696
|
-
# @private
|
697
|
-
def initialize(j_del)
|
698
|
-
@j_del = j_del
|
699
|
-
end
|
700
|
-
|
701
|
-
def status_code=(val)
|
702
|
-
@j_del.setStatusCode(val)
|
703
|
-
end
|
704
|
-
|
705
|
-
# Get or set the status code
|
706
|
-
def status_code(val = nil)
|
707
|
-
if val
|
708
|
-
@j_del.setStatusCode(val)
|
709
|
-
self
|
710
|
-
else
|
711
|
-
@j_del.getStatusCode
|
712
|
-
end
|
713
|
-
end
|
714
|
-
|
715
|
-
# Set the status message
|
716
|
-
def status_message=(val)
|
717
|
-
@j_del.setStatusMessage(val)
|
718
|
-
end
|
719
|
-
|
720
|
-
# Get or set the status message
|
721
|
-
def status_message(val = nil)
|
722
|
-
if val
|
723
|
-
@j_del.setStatusMessage(val)
|
724
|
-
self
|
725
|
-
else
|
726
|
-
@j_del.getStatusMessage
|
727
|
-
end
|
728
|
-
end
|
729
|
-
|
730
|
-
# Sets whether this response uses HTTP chunked encoding or not.
|
731
|
-
# @param [Boolean] val. If val is true, this response will use HTTP chunked encoding, and each call to write to the body
|
732
|
-
# will correspond to a new HTTP chunk sent on the wire. If chunked encoding is used the HTTP header
|
733
|
-
# 'Transfer-Encoding' with a value of 'Chunked' will be automatically inserted in the response.
|
734
|
-
# If chunked is false, this response will not use HTTP chunked encoding, and therefore if any data is written the
|
735
|
-
# body of the response, the total size of that data must be set in the 'Content-Length' header before any
|
736
|
-
# data is written to the response body.
|
737
|
-
# An HTTP chunked response is typically used when you do not know the total size of the request body up front.
|
738
|
-
# @return [HttpServerResponse] self So multiple operations can be chained.
|
739
|
-
def chunked=(val)
|
740
|
-
@j_del.setChunked(val)
|
741
|
-
self
|
742
|
-
end
|
743
|
-
|
744
|
-
# Get or set chunked
|
745
|
-
def chunked(val = nil)
|
746
|
-
if val
|
747
|
-
@j_del.setChunked(val)
|
748
|
-
self
|
749
|
-
else
|
750
|
-
@j_del.getChunked
|
751
|
-
end
|
752
|
-
end
|
753
|
-
|
754
|
-
# @return [MultiMap] The response headers
|
755
|
-
def headers
|
756
|
-
if !@headers
|
757
|
-
@headers = MultiMap.new(@j_del.headers)
|
758
|
-
end
|
759
|
-
@headers
|
760
|
-
end
|
761
|
-
|
762
|
-
# Inserts a header into the response.
|
763
|
-
# @param [String] key The header key
|
764
|
-
# @param [Object] value The header value. to_s will be called on the value to determine the actual String value to insert.
|
765
|
-
# @return [HttpClientRequest] self So multiple operations can be chained.
|
766
|
-
def put_header(key, value)
|
767
|
-
@j_del.putHeader(key, value.to_s)
|
768
|
-
self
|
769
|
-
end
|
770
|
-
|
771
|
-
# Inserts a trailer into the response.
|
772
|
-
# @param [String] key The header key
|
773
|
-
# @param [Object] value The header value. to_s will be called on the value to determine the actual String value to insert.
|
774
|
-
# @return [HttpClientRequest] self So multiple operations can be chained.
|
775
|
-
def put_trailer(key, value)
|
776
|
-
@j_del.putTrailer(key, value.to_s)
|
777
|
-
self
|
778
|
-
end
|
779
|
-
|
780
|
-
# The response trailers
|
781
|
-
def trailers
|
782
|
-
if !@trailers
|
783
|
-
@trailers = MultiMap.new(@j_del.trailers)
|
784
|
-
end
|
785
|
-
@trailers
|
786
|
-
end
|
787
|
-
|
788
|
-
# Write a String to the response. The handler will be called when the String has actually been written to the wire.
|
789
|
-
# @param [String] str. The string to write
|
790
|
-
# @param [String] enc. Encoding to use.
|
791
|
-
# @return [HttpServerResponse] self So multiple operations can be chained.
|
792
|
-
def write_str(str, enc = "UTF-8")
|
793
|
-
@j_del.write(str, enc)
|
794
|
-
self
|
795
|
-
end
|
796
|
-
|
797
|
-
# Tell the kernel to stream a file directly from disk to the outgoing connection, bypassing user-space altogether
|
798
|
-
# (where supported by the underlying operating system. This is a very efficient way to serve files.
|
799
|
-
# @param [String] path. Path to file to send.
|
800
|
-
# @param [String] not_found_file Path to file containing 404 resource in case resource can't be found
|
801
|
-
# @return [HttpServerResponse] self So multiple operations can be chained.
|
802
|
-
def send_file(path, not_found_file = nil, &block)
|
803
|
-
if not_found_file.nil?
|
804
|
-
if block_given?
|
805
|
-
@j_del.sendFile(path, ARWrappedHandler.new(block))
|
806
|
-
else
|
807
|
-
@j_del.sendFile(path)
|
808
|
-
end
|
809
|
-
else
|
810
|
-
if block_given?
|
811
|
-
@j_del.sendFile(path, not_found_file, ARWrappendHandler.new(block))
|
812
|
-
else
|
813
|
-
@j_del.sendFile(path, not_found_file)
|
814
|
-
end
|
815
|
-
end
|
816
|
-
self
|
817
|
-
end
|
818
|
-
|
819
|
-
# Ends the response. If no data has been written to the response body, the actual response won't get written until this method gets called.
|
820
|
-
# Once the response has ended, it cannot be used any more, and if keep alive is true the underlying connection will
|
821
|
-
# be closed.
|
822
|
-
# @param [String,Buffer] data. Optional String or Buffer to write before ending the response
|
823
|
-
def end(data = nil)
|
824
|
-
if (data.is_a? String) || (data.is_a? Buffer)
|
825
|
-
@j_del.end(data)
|
826
|
-
else
|
827
|
-
@j_del.end
|
828
|
-
end
|
829
|
-
end
|
830
|
-
|
831
|
-
# Close the underlying TCP connection
|
832
|
-
def close
|
833
|
-
@j_del.close
|
834
|
-
end
|
835
|
-
|
836
|
-
end
|
837
|
-
|
838
|
-
# Represents a WebSocket.
|
839
|
-
#
|
840
|
-
# Instances of this class are created by an {HttpClient} instance when a client succeeds in a WebSocket handshake with a server.
|
841
|
-
# Once an instance has been obtained it can be used to send or receive buffers of data from the connection,
|
842
|
-
# a bit like a TCP socket.
|
843
|
-
#
|
844
|
-
# @author {http://tfox.org Tim Fox}
|
845
|
-
class WebSocket
|
846
|
-
|
847
|
-
include ReadStream, WriteStream
|
848
|
-
|
849
|
-
# @private
|
850
|
-
def initialize(j_ws)
|
851
|
-
@j_del = j_ws
|
852
|
-
@binary_handler_id = EventBus.register_simple_handler { |msg|
|
853
|
-
write_binary_frame(msg.body)
|
854
|
-
}
|
855
|
-
@text_handler_id = EventBus.register_simple_handler { |msg|
|
856
|
-
write_text_frame(msg.body)
|
857
|
-
}
|
858
|
-
@j_del.closeHandler(Proc.new {
|
859
|
-
EventBus.unregister_handler(@binary_handler_id)
|
860
|
-
EventBus.unregister_handler(@text_handler_id)
|
861
|
-
@close_handler.call if @close_handler
|
862
|
-
})
|
863
|
-
end
|
864
|
-
|
865
|
-
# Write data to the WebSocket as a binary frame
|
866
|
-
# @param [Buffer] buffer. Data to write.
|
867
|
-
def write_binary_frame(buffer)
|
868
|
-
@j_del.writeBinaryFrame(buffer._to_java_buffer)
|
869
|
-
self
|
870
|
-
end
|
871
|
-
|
872
|
-
# Write data to the WebSocket as a text frame
|
873
|
-
# @param [String] str. String to write.
|
874
|
-
def write_text_frame(str)
|
875
|
-
@j_del.writeTextFrame(str)
|
876
|
-
self
|
877
|
-
end
|
878
|
-
|
879
|
-
# Close the WebSocket
|
880
|
-
def close
|
881
|
-
@j_del.close
|
882
|
-
end
|
883
|
-
|
884
|
-
# When a Websocket is created it automatically registers an event handler with the system, the ID of that
|
885
|
-
# handler is given by {#binary_handler_id}.
|
886
|
-
# Given this ID, a different event loop can send a binary frame to that event handler using the event bus. This
|
887
|
-
# allows you to write data to other WebSockets which are owned by different event loops.
|
888
|
-
def binary_handler_id
|
889
|
-
@binary_handler_id
|
890
|
-
end
|
891
|
-
|
892
|
-
# When a Websocket is created it automatically registers an event handler with the system, the ID of that
|
893
|
-
# handler is given by {#text_handler_id}.
|
894
|
-
# Given this ID, a different event loop can send a text frame to that event handler using the event bus. This
|
895
|
-
# allows you to write data to other WebSockets which are owned by different event loops.
|
896
|
-
def text_handler_id
|
897
|
-
@text_handler_id
|
898
|
-
end
|
899
|
-
|
900
|
-
# Set a closed handler on the WebSocket.
|
901
|
-
# @param [Block] hndlr A block to be used as the handler
|
902
|
-
def close_handler(&hndlr)
|
903
|
-
@close_handler = hndlr;
|
904
|
-
end
|
905
|
-
|
906
|
-
end
|
907
|
-
|
908
|
-
# Instances of this class are created when a WebSocket is accepted on the server.
|
909
|
-
# It extends {WebSocket} and adds methods to reject the WebSocket and to get path and headers
|
910
|
-
class ServerWebSocket < WebSocket
|
911
|
-
|
912
|
-
# @private
|
913
|
-
def initialize(j_ws)
|
914
|
-
super(j_ws)
|
915
|
-
@j_del = j_ws
|
916
|
-
end
|
917
|
-
|
918
|
-
# Reject the WebSocket
|
919
|
-
# This can be called in the WebSocket connect handler on the server side and
|
920
|
-
# will cause the WebSocket connection attempt to be rejected, returning a
|
921
|
-
# 404 to the client.
|
922
|
-
def reject
|
923
|
-
@j_del.reject
|
924
|
-
end
|
925
|
-
|
926
|
-
# Return the headers of the handshake request
|
927
|
-
# @return [MultiMap] The handshake headers
|
928
|
-
def headers
|
929
|
-
if !@headers
|
930
|
-
@headers = MultiMap.new(@j_del.headers)
|
931
|
-
end
|
932
|
-
@headers
|
933
|
-
end
|
934
|
-
|
935
|
-
# The path the WebSocket connect was attempted at.
|
936
|
-
def path
|
937
|
-
@j_del.path
|
938
|
-
end
|
939
|
-
end
|
940
|
-
|
941
|
-
# This class allows you to do route requests based on the HTTP verb and the request URI, in a manner similar
|
942
|
-
# to <a href="http://www.sinatrarb.com/">Sinatra</a> or <a href="http://expressjs.com/">Express</a>.
|
943
|
-
#
|
944
|
-
# RouteMatcher also lets you extract parameters from the request URI either a simple pattern or using
|
945
|
-
# regular expressions for more complex matches. Any parameters extracted will be added to the requests parameters
|
946
|
-
# which will be available to you in your request handler.
|
947
|
-
#
|
948
|
-
# It's particularly useful when writing REST-ful web applications.
|
949
|
-
#
|
950
|
-
# To use a simple pattern to extract parameters simply prefix the parameter name in the pattern with a ':' (colon).
|
951
|
-
#
|
952
|
-
# Different handlers can be specified for each of the HTTP verbs, GET, POST, PUT, DELETE etc.
|
953
|
-
#
|
954
|
-
# For more complex matches regular expressions can be used in the pattern. When regular expressions are used, the extracted
|
955
|
-
# parameters do not have a name, so they are put into the HTTP request with names of param0, param1, param2 etc.
|
956
|
-
#
|
957
|
-
# Multiple matches can be specified for each HTTP verb. In the case there are more than one matching patterns for
|
958
|
-
# a particular request, the first matching one will be used.
|
959
|
-
#
|
960
|
-
# @author {http://tfox.org Tim Fox}
|
961
|
-
class RouteMatcher
|
962
|
-
def initialize
|
963
|
-
@j_del = org.vertx.java.core.http.RouteMatcher.new
|
964
|
-
end
|
965
|
-
|
966
|
-
# This method is called to provide the matcher with data.
|
967
|
-
# @param [HttpServerRequest] request. Input request to the parser.
|
968
|
-
def input(request)
|
969
|
-
@j_del.handle(request._to_java_request)
|
970
|
-
end
|
971
|
-
|
972
|
-
# Specify a handler that will be called for a matching HTTP GET
|
973
|
-
# @param [String] The simple pattern
|
974
|
-
# @param [Block] hndlr A block to be used as the handler
|
975
|
-
def get(pattern, &hndlr)
|
976
|
-
@j_del.get(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
977
|
-
end
|
978
|
-
|
979
|
-
# Specify a handler that will be called for a matching HTTP PUT
|
980
|
-
# @param [String] The simple pattern
|
981
|
-
# @param [Block] hndlr A block to be used as the handler
|
982
|
-
def put(pattern, &hndlr)
|
983
|
-
@j_del.put(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
984
|
-
end
|
985
|
-
|
986
|
-
# Specify a handler that will be called for a matching HTTP POST
|
987
|
-
# @param [String] The simple pattern
|
988
|
-
# @param [Block] hndlr A block to be used as the handler
|
989
|
-
def post(pattern, &hndlr)
|
990
|
-
@j_del.post(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
991
|
-
end
|
992
|
-
|
993
|
-
# Specify a handler that will be called for a matching HTTP DELETE
|
994
|
-
# @param [String] The simple pattern
|
995
|
-
# @param [Block] hndlr A block to be used as the handler
|
996
|
-
def delete(pattern, &hndlr)
|
997
|
-
@j_del.delete(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
998
|
-
end
|
999
|
-
|
1000
|
-
# Specify a handler that will be called for a matching HTTP OPTIONS
|
1001
|
-
# @param [String] The simple pattern
|
1002
|
-
# @param [Block] hndlr A block to be used as the handler
|
1003
|
-
def options(pattern, &hndlr)
|
1004
|
-
@j_del.options(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1005
|
-
end
|
1006
|
-
|
1007
|
-
# Specify a handler that will be called for a matching HTTP HEAD
|
1008
|
-
# @param [String] The simple pattern
|
1009
|
-
# @param [Block] hndlr A block to be used as the handler
|
1010
|
-
def head(pattern, &hndlr)
|
1011
|
-
@j_del.head(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1012
|
-
end
|
1013
|
-
|
1014
|
-
# Specify a handler that will be called for a matching HTTP TRACE
|
1015
|
-
# @param [String] The simple pattern
|
1016
|
-
# @param [Block] hndlr A block to be used as the handler
|
1017
|
-
def trace(pattern, &hndlr)
|
1018
|
-
@j_del.trace(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1019
|
-
end
|
1020
|
-
|
1021
|
-
# Specify a handler that will be called for a matching HTTP PATCH
|
1022
|
-
# @param [String] The simple pattern
|
1023
|
-
# @param [Block] hndlr A block to be used as the handler
|
1024
|
-
def patch(pattern, &hndlr)
|
1025
|
-
@j_del.patch(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1026
|
-
end
|
1027
|
-
|
1028
|
-
# Specify a handler that will be called for a matching HTTP CONNECT
|
1029
|
-
# @param [String] The simple pattern
|
1030
|
-
# @param [Block] hndlr A block to be used as the handler
|
1031
|
-
def connect(pattern, &hndlr)
|
1032
|
-
@j_del.connect(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1033
|
-
end
|
1034
|
-
|
1035
|
-
# Specify a handler that will be called for any matching HTTP request
|
1036
|
-
# @param [String] The simple pattern
|
1037
|
-
# @param [Block] hndlr A block to be used as the handler
|
1038
|
-
def all(pattern, &hndlr)
|
1039
|
-
@j_del.all(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1040
|
-
end
|
1041
|
-
|
1042
|
-
# Specify a handler that will be called for a matching HTTP GET
|
1043
|
-
# @param [String] A regular expression for a pattern
|
1044
|
-
# @param [Block] hndlr A block to be used as the handler
|
1045
|
-
def get_re(pattern, &hndlr)
|
1046
|
-
|
1047
|
-
@j_del.getWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1048
|
-
end
|
1049
|
-
|
1050
|
-
# Specify a handler that will be called for a matching HTTP PUT
|
1051
|
-
# @param [String] A regular expression for a pattern
|
1052
|
-
# @param [Block] hndlr A block to be used as the handler
|
1053
|
-
def put_re(pattern, &hndlr)
|
1054
|
-
@j_del.putWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1055
|
-
end
|
1056
|
-
|
1057
|
-
# Specify a handler that will be called for a matching HTTP POST
|
1058
|
-
# @param [String] A regular expression for a pattern
|
1059
|
-
# @param [Block] hndlr A block to be used as the handler
|
1060
|
-
def post_re(pattern, &hndlr)
|
1061
|
-
@j_del.postWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1062
|
-
end
|
1063
|
-
|
1064
|
-
# Specify a handler that will be called for a matching HTTP DELETE
|
1065
|
-
# @param [String] A regular expression for a pattern
|
1066
|
-
# @param [Block] hndlr A block to be used as the handler
|
1067
|
-
def delete_re(pattern, &hndlr)
|
1068
|
-
@j_del.deleteWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1069
|
-
end
|
1070
|
-
|
1071
|
-
# Specify a handler that will be called for a matching HTTP OPTIONS
|
1072
|
-
# @param [String] A regular expression for a pattern
|
1073
|
-
# @param [Block] hndlr A block to be used as the handler
|
1074
|
-
def options_re(pattern, &hndlr)
|
1075
|
-
@j_del.optionsWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1076
|
-
end
|
1077
|
-
|
1078
|
-
# Specify a handler that will be called for a matching HTTP HEAD
|
1079
|
-
# @param [String] A regular expression for a pattern
|
1080
|
-
# @param [Block] hndlr A block to be used as the handler
|
1081
|
-
def head_re(pattern, &hndlr)
|
1082
|
-
@j_del.headWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1083
|
-
end
|
1084
|
-
|
1085
|
-
# Specify a handler that will be called for a matching HTTP TRACE
|
1086
|
-
# @param [String] A regular expression for a pattern
|
1087
|
-
# @param [Block] hndlr A block to be used as the handler
|
1088
|
-
def trace_re(pattern, proc = nil, &hndlr)
|
1089
|
-
@j_del.traceWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1090
|
-
end
|
1091
|
-
|
1092
|
-
# Specify a handler that will be called for a matching HTTP PATCH
|
1093
|
-
# @param [String] A regular expression for a pattern
|
1094
|
-
# @param [Block] hndlr A block to be used as the handler
|
1095
|
-
def patch_re(pattern, &hndlr)
|
1096
|
-
@j_del.patchWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1097
|
-
end
|
1098
|
-
|
1099
|
-
# Specify a handler that will be called for a matching HTTP CONNECT
|
1100
|
-
# @param [String] A regular expression for a pattern
|
1101
|
-
# @param [Block] hndlr A block to be used as the handler
|
1102
|
-
def connect_re(pattern, &hndlr)
|
1103
|
-
@j_del.connectWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1104
|
-
end
|
1105
|
-
|
1106
|
-
# Specify a handler that will be called for any matching HTTP request
|
1107
|
-
# @param [String] A regular expression for a pattern
|
1108
|
-
# @param [Block] hndlr A block to be used as the handler
|
1109
|
-
def all_re(pattern, &hndlr)
|
1110
|
-
@j_del.allWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1111
|
-
end
|
1112
|
-
|
1113
|
-
# Specify a handler that will be called when nothing matches
|
1114
|
-
# Default behaviour is to return a 404
|
1115
|
-
# @param [Block] hndlr A block to be used as the handler
|
1116
|
-
def no_match(&hndlr)
|
1117
|
-
@j_del.noMatch { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
|
1118
|
-
end
|
1119
|
-
|
1120
|
-
end
|
1121
|
-
|
1122
|
-
|
1123
|
-
# A map which can hold multiple values for one name / key
|
1124
|
-
#
|
1125
|
-
# @author Norman Maurer
|
1126
|
-
class MultiMap
|
1127
|
-
|
1128
|
-
# @private
|
1129
|
-
def initialize(j_map)
|
1130
|
-
@j_map = j_map
|
1131
|
-
end
|
1132
|
-
|
1133
|
-
# Call the handler for each header name and its value. If there are multiple values are stored for
|
1134
|
-
# a header name it will be called for each of them
|
1135
|
-
#
|
1136
|
-
def each(&hndlr)
|
1137
|
-
names.each do |name|
|
1138
|
-
values = @j_map.getAll(name)
|
1139
|
-
for v in values
|
1140
|
-
hndlr.call(name, v)
|
1141
|
-
end
|
1142
|
-
end
|
1143
|
-
end
|
1144
|
-
|
1145
|
-
# Returns the value of with the specified name. If there are
|
1146
|
-
# more than one values for the specified name, the first value is returned.
|
1147
|
-
#
|
1148
|
-
# @param name The name of the header to search
|
1149
|
-
# @return The first header value or nil if there is no such entry
|
1150
|
-
def get(name)
|
1151
|
-
@j_map.get(name)
|
1152
|
-
end
|
1153
|
-
|
1154
|
-
# Returns the value of with the specified name. If there are
|
1155
|
-
# more than one values for the specified name, the first value is returned.
|
1156
|
-
#
|
1157
|
-
# @param name The name of the header to search
|
1158
|
-
# @return The first header value or nil if there is no such entry
|
1159
|
-
def [](name)
|
1160
|
-
@j_map.get(name)
|
1161
|
-
end
|
1162
|
-
|
1163
|
-
|
1164
|
-
# Set a value with the specified name and value.
|
1165
|
-
#
|
1166
|
-
# @param name The name
|
1167
|
-
# @param value The value being added
|
1168
|
-
# @return self
|
1169
|
-
def []=(name, value)
|
1170
|
-
@j_map.set(name, value)
|
1171
|
-
self
|
1172
|
-
end
|
1173
|
-
|
1174
|
-
# Returns the values with the specified name
|
1175
|
-
#
|
1176
|
-
# @param name The name to search
|
1177
|
-
# @return [Array] A immutable array of values which will be empty if no values
|
1178
|
-
# are found
|
1179
|
-
def get_all(name)
|
1180
|
-
@j_map.getAll(name).to_a
|
1181
|
-
end
|
1182
|
-
|
1183
|
-
# Returns true if an entry with the given name was found
|
1184
|
-
def contains(name)
|
1185
|
-
@j_map.contains(name)
|
1186
|
-
end
|
1187
|
-
|
1188
|
-
# Returns true if the map is empty
|
1189
|
-
def empty?
|
1190
|
-
@j_map.isEmpty()
|
1191
|
-
end
|
1192
|
-
|
1193
|
-
# Return a Set which holds all names of the entries
|
1194
|
-
#
|
1195
|
-
# @return [Set] The set which holds all names or an empty set if it is empty
|
1196
|
-
def names
|
1197
|
-
@j_map.names()
|
1198
|
-
end
|
1199
|
-
|
1200
|
-
|
1201
|
-
# Adds a new value with the specified name and value.
|
1202
|
-
#
|
1203
|
-
# @param name The name
|
1204
|
-
# @param value The value being added
|
1205
|
-
# @return self
|
1206
|
-
def add(name, value)
|
1207
|
-
@j_map.add(name, value)
|
1208
|
-
self
|
1209
|
-
end
|
1210
|
-
|
1211
|
-
# Set a value with the specified name and value.
|
1212
|
-
#
|
1213
|
-
# @param name The name
|
1214
|
-
# @param value The value being added
|
1215
|
-
# @return self
|
1216
|
-
def set(name, value)
|
1217
|
-
@j_map.set(name, value)
|
1218
|
-
self
|
1219
|
-
end
|
1220
|
-
|
1221
|
-
# Set a value with the specified name and value.
|
1222
|
-
#
|
1223
|
-
# @param name The name
|
1224
|
-
# @param value The value being added
|
1225
|
-
# @return self
|
1226
|
-
def set_all(map)
|
1227
|
-
@j_map.set(map._j_map)
|
1228
|
-
self
|
1229
|
-
end
|
1230
|
-
|
1231
|
-
# Remove the values with the given name
|
1232
|
-
#
|
1233
|
-
# @param name The name
|
1234
|
-
# @return self
|
1235
|
-
def remove(name)
|
1236
|
-
@j_map.remove(name)
|
1237
|
-
self
|
1238
|
-
end
|
1239
|
-
|
1240
|
-
# Remove all entries
|
1241
|
-
#
|
1242
|
-
# @return self
|
1243
|
-
def clear
|
1244
|
-
@j_map.clear()
|
1245
|
-
self
|
1246
|
-
end
|
1247
|
-
|
1248
|
-
# Return the number of names in this instance
|
1249
|
-
def size
|
1250
|
-
@j_map.size()
|
1251
|
-
end
|
1252
|
-
|
1253
|
-
def _j_map
|
1254
|
-
@j_map
|
1255
|
-
end
|
1256
|
-
end
|
1257
|
-
|
1258
|
-
# An Upload which was found in the HttpServerMultipartRequest while handling it.
|
1259
|
-
#
|
1260
|
-
# @author Norman Maurer
|
1261
|
-
#
|
1262
|
-
class HttpServerFileUpload
|
1263
|
-
|
1264
|
-
include ReadStream
|
1265
|
-
|
1266
|
-
# @private
|
1267
|
-
def initialize(j_del)
|
1268
|
-
@j_del = j_del
|
1269
|
-
end
|
1270
|
-
|
1271
|
-
# Stream the content of this upload to the given filename.
|
1272
|
-
def stream_to_file_system(filename)
|
1273
|
-
@j_del.streamToFileSystem(filename)
|
1274
|
-
self
|
1275
|
-
end
|
1276
|
-
|
1277
|
-
# Returns the filename of the attribute
|
1278
|
-
def filename
|
1279
|
-
@j_del.filename()
|
1280
|
-
end
|
1281
|
-
|
1282
|
-
# Returns the name of the attribute
|
1283
|
-
def name
|
1284
|
-
@j_del.name()
|
1285
|
-
end
|
1286
|
-
|
1287
|
-
# Returns the contentType for the upload
|
1288
|
-
def content_type
|
1289
|
-
@j_del.contentType()
|
1290
|
-
end
|
1291
|
-
|
1292
|
-
#Returns the contentTransferEncoding for the upload
|
1293
|
-
def content_transfer_encoding
|
1294
|
-
@j_del.contentTransferEncoding()
|
1295
|
-
end
|
1296
|
-
|
1297
|
-
# Returns the charset for the upload
|
1298
|
-
def charset
|
1299
|
-
@j_del.charset().toString()
|
1300
|
-
end
|
1301
|
-
|
1302
|
-
#Returns the size of the upload (in bytes)
|
1303
|
-
def size
|
1304
|
-
@j_del.size()
|
1305
|
-
end
|
1306
|
-
end
|
1307
|
-
end
|