agoo 2.5.1 → 2.5.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of agoo might be problematic. Click here for more details.

Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/README.md +12 -1
  4. data/ext/agoo/agoo.c +4 -3
  5. data/ext/agoo/bind.c +51 -11
  6. data/ext/agoo/bind.h +9 -0
  7. data/ext/agoo/con.c +127 -89
  8. data/ext/agoo/con.h +24 -8
  9. data/ext/agoo/debug.c +2 -0
  10. data/ext/agoo/debug.h +1 -0
  11. data/ext/agoo/err.c +1 -1
  12. data/ext/agoo/err.h +2 -5
  13. data/ext/agoo/foo/agoo.c +109 -0
  14. data/ext/agoo/foo/con.c +1220 -0
  15. data/ext/agoo/foo/con.h +65 -0
  16. data/ext/agoo/foo/page.c +699 -0
  17. data/ext/agoo/foo/pub.c +131 -0
  18. data/ext/agoo/foo/pub.h +40 -0
  19. data/ext/agoo/foo/rserver.c +1016 -0
  20. data/ext/agoo/foo/server.c +303 -0
  21. data/ext/agoo/foo/server.h +67 -0
  22. data/ext/agoo/foo/upgraded.c +182 -0
  23. data/ext/agoo/hook.c +31 -0
  24. data/ext/agoo/hook.h +9 -10
  25. data/ext/agoo/{types.h → kinds.h} +3 -3
  26. data/ext/agoo/log.c +168 -83
  27. data/ext/agoo/log.h +6 -2
  28. data/ext/agoo/page.c +38 -32
  29. data/ext/agoo/pub.c +16 -0
  30. data/ext/agoo/pub.h +1 -0
  31. data/ext/agoo/queue.h +1 -0
  32. data/ext/agoo/req.c +95 -0
  33. data/ext/agoo/req.h +48 -0
  34. data/ext/agoo/request.c +10 -38
  35. data/ext/agoo/request.h +1 -34
  36. data/ext/agoo/res.h +1 -3
  37. data/ext/agoo/response.c +6 -248
  38. data/ext/agoo/response.h +2 -6
  39. data/ext/agoo/rlog.c +2 -1
  40. data/ext/agoo/rresponse.c +252 -0
  41. data/ext/agoo/rresponse.h +14 -0
  42. data/ext/agoo/rserver.c +43 -45
  43. data/ext/agoo/rserver.h +3 -24
  44. data/ext/agoo/rupgraded.c +303 -0
  45. data/ext/agoo/rupgraded.h +17 -0
  46. data/ext/agoo/server.c +125 -8
  47. data/ext/agoo/server.h +26 -4
  48. data/ext/agoo/sse.c +3 -1
  49. data/ext/agoo/upgraded.c +42 -280
  50. data/ext/agoo/upgraded.h +16 -8
  51. data/ext/agoo/websocket.c +13 -9
  52. data/lib/agoo/base.rb +84 -0
  53. data/lib/agoo/client.rb +25 -0
  54. data/lib/agoo/version.rb +1 -1
  55. data/test/bind_test.rb +2 -2
  56. metadata +22 -4
@@ -3,9 +3,10 @@
3
3
  #ifndef __AGOO_UPGRADED_H__
4
4
  #define __AGOO_UPGRADED_H__
5
5
 
6
+ #include <pthread.h>
7
+ #include <stdatomic.h>
6
8
  #include <stdint.h>
7
-
8
- #include <ruby.h>
9
+ #include <stdbool.h>
9
10
 
10
11
  struct _Con;
11
12
  struct _Subject;
@@ -14,21 +15,24 @@ typedef struct _Upgraded {
14
15
  struct _Upgraded *next;
15
16
  struct _Upgraded *prev;
16
17
  struct _Con *con;
17
- VALUE handler;
18
- VALUE wrap;
19
- VALUE env;
20
18
  atomic_int pending;
21
19
  atomic_int ref_cnt;
22
20
  struct _Subject *subjects;
21
+
22
+ void *ctx;
23
+ void *wrap;
24
+ void *env;
25
+
23
26
  bool on_empty;
24
27
  bool on_close;
25
28
  bool on_shut;
26
29
  bool on_msg;
27
30
  bool on_error;
31
+ void (*on_destroy)(struct _Upgraded *up);
28
32
  } *Upgraded;
29
33
 
30
- extern void upgraded_init(VALUE mod);
31
- extern Upgraded upgraded_create(struct _Con *c, VALUE obj, VALUE env);
34
+ extern Upgraded upgraded_create(struct _Con *c, void * ctx, void *env);
35
+
32
36
  extern void upgraded_release(Upgraded up);
33
37
  extern void upgraded_release_con(Upgraded up);
34
38
 
@@ -38,6 +42,10 @@ extern void upgraded_add_subject(Upgraded up, struct _Subject *subject);
38
42
  extern void upgraded_del_subject(Upgraded up, struct _Subject *subject);
39
43
  extern bool upgraded_match(Upgraded up, const char *subject);
40
44
 
41
- extern const char* extract_subject(VALUE subject, int *slen);
45
+ extern bool upgraded_write(Upgraded up, const char *message, size_t mlen, bool bin, bool inc_ref);
46
+ extern void upgraded_subscribe(Upgraded up, const char *subject, int slen, bool inc_ref);
47
+ extern void upgraded_unsubscribe(Upgraded up, const char *subject, int slen, bool inc_ref);
48
+ extern void upgraded_close(Upgraded up, bool inc_ref);
49
+ extern int upgraded_pending(Upgraded up);
42
50
 
43
51
  #endif // __AGOO_UPGRADED_H__
@@ -1,14 +1,18 @@
1
1
  // Copyright (c) 2018, Peter Ohler, All rights reserved.
2
2
 
3
3
  #include <stdint.h>
4
+ #include <string.h>
4
5
 
5
6
  #include "base64.h"
6
7
  #include "con.h"
7
8
  #include "debug.h"
8
- #include "request.h"
9
- #include "rserver.h"
9
+ #include "log.h"
10
+ #include "req.h"
11
+ #include "res.h"
10
12
  #include "sha1.h"
13
+ #include "server.h"
11
14
  #include "text.h"
15
+ #include "upgraded.h"
12
16
  #include "websocket.h"
13
17
 
14
18
  #define MAX_KEY_LEN 1024
@@ -164,11 +168,11 @@ bool
164
168
  ws_create_req(Con c, long mlen) {
165
169
  uint8_t op = 0x0F & *c->buf;
166
170
 
167
- if (NULL == (c->req = request_create(mlen))) {
171
+ if (NULL == (c->req = req_create(mlen))) {
168
172
  log_cat(&error_cat, "Out of memory attempting to allocate request.");
169
173
  return true;
170
174
  }
171
- if (NULL == c->up || Qnil == c->up->handler) {
175
+ if (NULL == c->up || the_server.ctx_nil_value == c->up->ctx) {
172
176
  return true;
173
177
  }
174
178
  memset(c->req, 0, sizeof(struct _Req));
@@ -187,21 +191,21 @@ ws_create_req(Con c, long mlen) {
187
191
  c->req->up = c->up;
188
192
  c->req->res = NULL;
189
193
  if (c->up->on_msg) {
190
- c->req->hook = hook_create(NONE, NULL, (void*)c->up->handler, PUSH_HOOK, &the_rserver.eval_queue);
194
+ c->req->hook = hook_create(NONE, NULL, c->up->ctx, PUSH_HOOK, &the_server.eval_queue);
191
195
  }
192
196
  return false;
193
197
  }
194
198
 
195
199
  void
196
200
  ws_req_close(Con c) {
197
- if (NULL != c->up && Qnil != c->up->handler && c->up->on_close) {
198
- Req req = request_create(0);
201
+ if (NULL != c->up && the_server.ctx_nil_value != c->up->ctx && c->up->on_close) {
202
+ Req req = req_create(0);
199
203
 
200
204
  req->up = c->up;
201
205
  req->method = ON_CLOSE;
202
- req->hook = hook_create(NONE, NULL, (void*)c->up->handler, PUSH_HOOK, &the_rserver.eval_queue);
206
+ req->hook = hook_create(NONE, NULL, c->up->ctx, PUSH_HOOK, &the_server.eval_queue);
203
207
  atomic_fetch_add(&c->up->ref_cnt, 1);
204
- queue_push(&the_rserver.eval_queue, (void*)req);
208
+ queue_push(&the_server.eval_queue, (void*)req);
205
209
  }
206
210
  }
207
211
 
@@ -0,0 +1,84 @@
1
+
2
+ module Agoo
3
+
4
+ # Maybe not the best class name but this class implements the base methds
5
+ # for a Rack WebSocket/SSE handler. A handler does not have to inherit from
6
+ # this class but the class does identify the methods supported.
7
+ class Base
8
+ attr_accessor :connection
9
+
10
+ def initialize(env)
11
+ # Nothing needs to be done on initialize although the #call env is
12
+ # available if desired for some reason on creaton. After #on_open is
13
+ # called the same env can be accessed through the client.
14
+ end
15
+
16
+ # Called when the WebSocket or SSE connection has been established. The
17
+ # client represents the connection and cab be used to send messages to the
18
+ # JavaScript WebSocket or SSE listener.
19
+ def on_open(client)
20
+ # save the client to support a send later on.
21
+ @connection = client
22
+ end
23
+
24
+ # Called when the WebSocket or SSE connection is closed.
25
+ def on_close(client)
26
+ @connection = nil
27
+ end
28
+
29
+ # This indicates the send delivery queue is empty. Useful if attempting to
30
+ # restrict the sends to one at a time.
31
+ def on_drained(client)
32
+ end
33
+
34
+ # Called when the JavaScript listener sends a WebSocket message.
35
+ def on_message(client, data)
36
+ # Handle a received message.
37
+ end
38
+
39
+ # Called when an error occurs on the WebSocket or SSE connection.
40
+ def on_error(client, err_msg)
41
+ # Handle an error.
42
+ end
43
+
44
+ # Sends a message to the WebSocket or SSE listener.
45
+ def send_message(msg)
46
+ check_connection
47
+ connection.write(msg)
48
+ end
49
+
50
+ # Closes the WebSocket or SSE connection. Note the connection can also be
51
+ # closed by the JavaScript listener.
52
+ def close
53
+ check_connection
54
+ connection.close
55
+ end
56
+
57
+ # The #subscribe, #unsubscribe, and #publish methods are not part of the
58
+ # Rack SPEC but add added here to demonstrate the publish and subscribe
59
+ # add-on that Agoo and Iodine provide. If you think it is something that
60
+ # should be included in the Rack SPEC we can add that. It was left out as
61
+ # it made the API more complex and
62
+ def subscribe(subject)
63
+ check_connection
64
+ connection.subscribe(subject)
65
+ end
66
+
67
+ def unsubscribe(subject=nil)
68
+ check_connection
69
+ connection.unsubscribe(subject)
70
+ end
71
+
72
+ def publish(subject, message)
73
+ check_connection
74
+ connection.publish(subject, message)
75
+ end
76
+
77
+ private
78
+
79
+ def check_connection
80
+ raise StandardError.new('Not connected') if connection.nil?
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,25 @@
1
+ module Agoo
2
+
3
+ # A WebSocket/SSE middleware class. If a WebSocket connection has been
4
+ # requested by a JavaScript listener then a success status (200) is returned
5
+ # by the call method otherwise a 404 status is returned.
6
+ class Client
7
+
8
+ attr_accessor :handler_class
9
+
10
+ def initialize(handler_class)
11
+ @handler_class = handler_class
12
+ end
13
+
14
+ def call(env)
15
+ unless env['rack.upgrade?'].nil?
16
+ env['rack.upgrade'] = @handler_class.new(env)
17
+ [ 200, { }, [ ] ]
18
+ else
19
+ [ 404, { }, [ ] ]
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Agoo
3
3
  # Agoo version.
4
- VERSION = '2.5.1'
4
+ VERSION = '2.5.2'
5
5
  end
@@ -31,7 +31,7 @@ class BindTest < Minitest::Test
31
31
  eval: true,
32
32
  })
33
33
 
34
- addr = ''
34
+ addr = '127.0.0.1'
35
35
  Socket.ip_address_list.each { |ai|
36
36
  if ai.ipv4? && '127.0.0.1' != ai.ip_address
37
37
  addr = ai.ip_address
@@ -52,7 +52,7 @@ class BindTest < Minitest::Test
52
52
  localhost_test
53
53
  ipv4_test(addr)
54
54
  ipv6_test(addr6)
55
- restrict_test
55
+ restrict_test unless '127.0.0.1' == addr
56
56
  named_test(name)
57
57
  ensure
58
58
  Agoo::shutdown
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.1
4
+ version: 2.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-30 00:00:00.000000000 Z
11
+ date: 2018-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -61,10 +61,21 @@ files:
61
61
  - ext/agoo/error_stream.c
62
62
  - ext/agoo/error_stream.h
63
63
  - ext/agoo/extconf.rb
64
+ - ext/agoo/foo/agoo.c
65
+ - ext/agoo/foo/con.c
66
+ - ext/agoo/foo/con.h
67
+ - ext/agoo/foo/page.c
68
+ - ext/agoo/foo/pub.c
69
+ - ext/agoo/foo/pub.h
70
+ - ext/agoo/foo/rserver.c
71
+ - ext/agoo/foo/server.c
72
+ - ext/agoo/foo/server.h
73
+ - ext/agoo/foo/upgraded.c
64
74
  - ext/agoo/hook.c
65
75
  - ext/agoo/hook.h
66
76
  - ext/agoo/http.c
67
77
  - ext/agoo/http.h
78
+ - ext/agoo/kinds.h
68
79
  - ext/agoo/log.c
69
80
  - ext/agoo/log.h
70
81
  - ext/agoo/log_queue.h
@@ -77,6 +88,8 @@ files:
77
88
  - ext/agoo/queue.h
78
89
  - ext/agoo/rack_logger.c
79
90
  - ext/agoo/rack_logger.h
91
+ - ext/agoo/req.c
92
+ - ext/agoo/req.h
80
93
  - ext/agoo/request.c
81
94
  - ext/agoo/request.h
82
95
  - ext/agoo/res.c
@@ -87,8 +100,12 @@ files:
87
100
  - ext/agoo/rhook.h
88
101
  - ext/agoo/rlog.c
89
102
  - ext/agoo/rlog.h
103
+ - ext/agoo/rresponse.c
104
+ - ext/agoo/rresponse.h
90
105
  - ext/agoo/rserver.c
91
106
  - ext/agoo/rserver.h
107
+ - ext/agoo/rupgraded.c
108
+ - ext/agoo/rupgraded.h
92
109
  - ext/agoo/seg.h
93
110
  - ext/agoo/server.c
94
111
  - ext/agoo/server.h
@@ -102,12 +119,13 @@ files:
102
119
  - ext/agoo/subject.h
103
120
  - ext/agoo/text.c
104
121
  - ext/agoo/text.h
105
- - ext/agoo/types.h
106
122
  - ext/agoo/upgraded.c
107
123
  - ext/agoo/upgraded.h
108
124
  - ext/agoo/websocket.c
109
125
  - ext/agoo/websocket.h
110
126
  - lib/agoo.rb
127
+ - lib/agoo/base.rb
128
+ - lib/agoo/client.rb
111
129
  - lib/agoo/version.rb
112
130
  - lib/rack/handler/agoo.rb
113
131
  - test/base_handler_test.rb
@@ -149,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
167
  requirements:
150
168
  - Linux or macOS
151
169
  rubyforge_project: agoo
152
- rubygems_version: 2.7.6
170
+ rubygems_version: 2.7.7
153
171
  signing_key:
154
172
  specification_version: 4
155
173
  summary: An HTTP server