rjr 0.16.3 → 0.16.4
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.
- data/lib/rjr/common.rb +61 -0
- data/lib/rjr/message.rb +6 -10
- data/specs/dispatcher_spec.rb +1 -1
- metadata +5 -5
data/lib/rjr/common.rb
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
# Copyright (C) 2011-2013 Mohammed Morsi <mo@morsi.org>
|
|
6
6
|
# Licensed under the Apache License, Version 2.0
|
|
7
7
|
require 'logger'
|
|
8
|
+
require 'json'
|
|
8
9
|
|
|
9
10
|
# Return a random uuid
|
|
10
11
|
def gen_uuid
|
|
@@ -164,3 +165,63 @@ class Object
|
|
|
164
165
|
end
|
|
165
166
|
end
|
|
166
167
|
end
|
|
168
|
+
|
|
169
|
+
# Two stage json parsing required, for more details
|
|
170
|
+
# see json issue https://github.com/flori/json/issues/179
|
|
171
|
+
|
|
172
|
+
# FIXME this will only work for json >= 1.7.6 where
|
|
173
|
+
# create_additions is defined
|
|
174
|
+
|
|
175
|
+
class Class
|
|
176
|
+
class << self
|
|
177
|
+
attr_accessor :whitelist_json_classes
|
|
178
|
+
attr_accessor :permitted_json_classes
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def permit_json_create
|
|
182
|
+
Class.whitelist_json_classes = true
|
|
183
|
+
Class.permitted_json_classes ||= []
|
|
184
|
+
unless Class.permitted_json_classes.include?(self.name)
|
|
185
|
+
Class.permitted_json_classes << self.name
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
module RJR
|
|
191
|
+
def self.validate_json_class(jc)
|
|
192
|
+
Class.whitelist_json_classes ||= false
|
|
193
|
+
|
|
194
|
+
Class.whitelist_json_classes ?
|
|
195
|
+
# only permit classes user explicitly authorizes
|
|
196
|
+
!Class.permitted_json_classes.include?(jc) :
|
|
197
|
+
|
|
198
|
+
# allow any class
|
|
199
|
+
jc.to_s.split(/::/).inject(Object) do |p,c|
|
|
200
|
+
case
|
|
201
|
+
when c.empty? then p
|
|
202
|
+
when p.constants.collect { |c| c.to_s }.include?(c)
|
|
203
|
+
then p.const_get(c)
|
|
204
|
+
else
|
|
205
|
+
nil
|
|
206
|
+
end
|
|
207
|
+
end.nil?
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def self.validate_json_hash(jh)
|
|
211
|
+
jh.each { |k,v|
|
|
212
|
+
if k == ::JSON.create_id &&
|
|
213
|
+
validate_json_class(v)
|
|
214
|
+
raise ArgumentError, "can't create json class #{v}"
|
|
215
|
+
elsif v.is_a?(Hash)
|
|
216
|
+
validate_json_hash(v)
|
|
217
|
+
end
|
|
218
|
+
}
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def self.parse_json(js)
|
|
222
|
+
jp = ::JSON.parse js, :create_additions => false
|
|
223
|
+
return jp unless jp.is_a?(Hash)
|
|
224
|
+
validate_json_hash(jp)
|
|
225
|
+
::JSON.parse js, :create_additions => true
|
|
226
|
+
end
|
|
227
|
+
end
|
data/lib/rjr/message.rb
CHANGED
|
@@ -5,10 +5,6 @@
|
|
|
5
5
|
# Copyright (C) 2012-2013 Mohammed Morsi <mo@morsi.org>
|
|
6
6
|
# Licensed under the Apache License, Version 2.0
|
|
7
7
|
|
|
8
|
-
# FIXME https://github.com/flori/json/issues/179
|
|
9
|
-
# if pull request doesn't get accepted implement
|
|
10
|
-
# one of the workarounds in rjr
|
|
11
|
-
|
|
12
8
|
require 'json'
|
|
13
9
|
require 'rjr/common'
|
|
14
10
|
|
|
@@ -48,7 +44,7 @@ class RequestMessage
|
|
|
48
44
|
if args.has_key?(:message)
|
|
49
45
|
begin
|
|
50
46
|
@json_message = args[:message]
|
|
51
|
-
request =
|
|
47
|
+
request = RJR.parse_json(@json_message)
|
|
52
48
|
@jr_method = request['method']
|
|
53
49
|
@jr_args = request['params']
|
|
54
50
|
@msg_id = request['id']
|
|
@@ -79,7 +75,7 @@ class RequestMessage
|
|
|
79
75
|
def self.is_request_message?(message)
|
|
80
76
|
begin
|
|
81
77
|
# FIXME log error
|
|
82
|
-
parsed =
|
|
78
|
+
parsed = RJR.parse_json(message)
|
|
83
79
|
parsed.has_key?('method') && parsed.has_key?('id')
|
|
84
80
|
rescue Exception => e
|
|
85
81
|
false
|
|
@@ -129,7 +125,7 @@ class ResponseMessage
|
|
|
129
125
|
def initialize(args = {})
|
|
130
126
|
if args.has_key?(:message)
|
|
131
127
|
@json_message = args[:message]
|
|
132
|
-
response =
|
|
128
|
+
response = RJR.parse_json(@json_message)
|
|
133
129
|
@msg_id = response['id']
|
|
134
130
|
@result = Result.new
|
|
135
131
|
@result.success = response.has_key?('result')
|
|
@@ -168,7 +164,7 @@ class ResponseMessage
|
|
|
168
164
|
# @return [true,false] indicating if message is response message
|
|
169
165
|
def self.is_response_message?(message)
|
|
170
166
|
begin
|
|
171
|
-
json =
|
|
167
|
+
json = RJR.parse_json(message)
|
|
172
168
|
json.has_key?('result') || json.has_key?('error')
|
|
173
169
|
rescue Exception => e
|
|
174
170
|
# FIXME log error
|
|
@@ -232,7 +228,7 @@ class NotificationMessage
|
|
|
232
228
|
if args.has_key?(:message)
|
|
233
229
|
begin
|
|
234
230
|
@json_message = args[:message]
|
|
235
|
-
notification =
|
|
231
|
+
notification = RJR.parse_json(@json_message)
|
|
236
232
|
@jr_method = notification['method']
|
|
237
233
|
@jr_args = notification['params']
|
|
238
234
|
@headers = args.has_key?(:headers) ? {}.merge!(args[:headers]) : {}
|
|
@@ -262,7 +258,7 @@ class NotificationMessage
|
|
|
262
258
|
def self.is_notification_message?(message)
|
|
263
259
|
begin
|
|
264
260
|
# FIXME log error
|
|
265
|
-
parsed =
|
|
261
|
+
parsed = RJR.parse_json(message)
|
|
266
262
|
parsed.has_key?('method') && !parsed.has_key?('id')
|
|
267
263
|
rescue Exception => e
|
|
268
264
|
false
|
data/specs/dispatcher_spec.rb
CHANGED
|
@@ -80,7 +80,7 @@ module RJR
|
|
|
80
80
|
|
|
81
81
|
it "should be convertable from json" do
|
|
82
82
|
j = '{"json_class":"RJR::Request","data":{"request":{"rjr_method":"foobar","rjr_method_args":["a","b"],"rjr_headers":{"foo":"bar"},"rjr_node_type":"local","rjr_node_id":"loc1"},"result":{"result":42,"error_code":null,"error_msg":null,"error_class":null}}}'
|
|
83
|
-
r = JSON.parse(j)
|
|
83
|
+
r = JSON.parse(j, :create_additions => true)
|
|
84
84
|
|
|
85
85
|
r.class.should == RJR::Request
|
|
86
86
|
r.rjr_method.should == 'foobar'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rjr
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.16.
|
|
4
|
+
version: 0.16.4
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -48,17 +48,17 @@ dependencies:
|
|
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
|
49
49
|
none: false
|
|
50
50
|
requirements:
|
|
51
|
-
- -
|
|
51
|
+
- - ! '>='
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: 1.7.
|
|
53
|
+
version: 1.7.6
|
|
54
54
|
type: :runtime
|
|
55
55
|
prerelease: false
|
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
57
|
none: false
|
|
58
58
|
requirements:
|
|
59
|
-
- -
|
|
59
|
+
- - ! '>='
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: 1.7.
|
|
61
|
+
version: 1.7.6
|
|
62
62
|
description: Ruby Json Rpc library
|
|
63
63
|
email: mo@morsi.org
|
|
64
64
|
executables:
|