gripcontrol 0.0.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gripcontrol.rb +78 -14
- data/lib/grippubcontrol.rb +17 -0
- data/lib/response.rb +20 -0
- data/lib/websocketmessageformat.rb +5 -5
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d709d64f9facb387326983acb791366354fd8512
|
4
|
+
data.tar.gz: 667c73728317fe50cf690de577980bfaff52e4b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa392d7bcdb2eb01dec64a6530457d69d0485c01bc26ca7deaabd53b64ada73cf81b64770ff4eb6e63661ba4af7c351f3c7ee15f5f0c4d3afde315587322c3c4
|
7
|
+
data.tar.gz: c1e903c08c0112e5a919ea5e8194b35b4038397dfd664f9db58e4bb0d913a4d715596ae12f5e42f8376c278ca6c710ddb5b9549e825eee113f32bd6fca1c2b1b
|
data/lib/gripcontrol.rb
CHANGED
@@ -7,12 +7,15 @@
|
|
7
7
|
|
8
8
|
require 'base64'
|
9
9
|
require 'jwt'
|
10
|
+
require 'uri'
|
11
|
+
require 'cgi'
|
10
12
|
require_relative 'channel.rb'
|
11
13
|
require_relative 'httpresponseformat.rb'
|
12
14
|
require_relative 'httpstreamformat.rb'
|
13
15
|
require_relative 'websocketmessageformat.rb'
|
14
16
|
require_relative 'websocketevent.rb'
|
15
17
|
require_relative 'grippubcontrol.rb'
|
18
|
+
require_relative 'response.rb'
|
16
19
|
|
17
20
|
class GripControl
|
18
21
|
def self.create_hold(mode, channels, response)
|
@@ -54,9 +57,9 @@ class GripControl
|
|
54
57
|
end
|
55
58
|
if !response.body.nil?
|
56
59
|
if response.body.encoding.name == 'ASCII-8BIT'
|
57
|
-
iresponse['body'] = response.body
|
58
|
-
else
|
59
60
|
iresponse['body-bin'] = Base64.encode64(response.body)
|
61
|
+
else
|
62
|
+
iresponse['body'] = response.body
|
60
63
|
end
|
61
64
|
end
|
62
65
|
end
|
@@ -68,24 +71,49 @@ class GripControl
|
|
68
71
|
return instruct.to_json
|
69
72
|
end
|
70
73
|
|
71
|
-
def self.
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
74
|
+
def self.parse_grip_uri(uri)
|
75
|
+
uri = URI(uri)
|
76
|
+
params = CGI.parse(uri.query)
|
77
|
+
iss = nil
|
78
|
+
key = nil
|
79
|
+
if params.key?('iss')
|
80
|
+
iss = params['iss'][0]
|
81
|
+
params.delete('iss')
|
82
|
+
end
|
83
|
+
if params.key?('key')
|
84
|
+
key = params['key'][0]
|
85
|
+
params.delete('key')
|
86
|
+
end
|
87
|
+
if !key.nil? and key.start_with?('base64:')
|
88
|
+
key = Base64.decode64(key[7..-1])
|
89
|
+
end
|
90
|
+
qs = build_query_string(params)
|
91
|
+
path = uri.path
|
92
|
+
if path.end_with?('/')
|
93
|
+
path = path[0..-2]
|
94
|
+
end
|
95
|
+
control_uri = parsed.scheme + '://' + parsed.host + path
|
96
|
+
if !qs.nil? and !qs.empty?
|
97
|
+
control_uri += '?' + qs
|
98
|
+
end
|
99
|
+
out = {'control_uri' => control_uri}
|
100
|
+
if !iss.nil?
|
101
|
+
out['control_iss'] = iss
|
102
|
+
end
|
103
|
+
if !key.nil?
|
104
|
+
out['key'] = key
|
105
|
+
end
|
106
|
+
return out
|
77
107
|
end
|
78
108
|
|
79
109
|
def self.validate_sig(token, key)
|
80
110
|
token = token.encode('utf-8')
|
81
111
|
begin
|
82
|
-
claim = JWT.
|
83
|
-
verify_expiration=false)
|
112
|
+
claim = JWT.decode(token, key, verify_expiration=false)
|
84
113
|
rescue
|
85
114
|
return false
|
86
115
|
end
|
87
|
-
|
88
|
-
if !claim.has_key?('exp')
|
116
|
+
if !claim.key?('exp')
|
89
117
|
return false
|
90
118
|
end
|
91
119
|
if Time.now.utc.to_i >= claim['exp']
|
@@ -94,6 +122,32 @@ class GripControl
|
|
94
122
|
return true
|
95
123
|
end
|
96
124
|
|
125
|
+
def self.create_grip_channel_header(channels)
|
126
|
+
if channels.is_a?(Channel)
|
127
|
+
channels = [channels]
|
128
|
+
elsif channels.is_a?(String)
|
129
|
+
channels = [Channel.new(channels)]
|
130
|
+
end
|
131
|
+
raise 'channels.length equal to 0' unless channels.length > 0
|
132
|
+
parts = []
|
133
|
+
channels.each do |channel|
|
134
|
+
s = channel.name
|
135
|
+
if !channel.prev_id.nil?
|
136
|
+
s += '; prev-id=%s' % [channel.prev_id]
|
137
|
+
end
|
138
|
+
parts.push(s)
|
139
|
+
end
|
140
|
+
return parts.join(', ')
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.create_hold_response(channels, response=nil)
|
144
|
+
return GripControl.create_hold('response', channels, response)
|
145
|
+
end
|
146
|
+
|
147
|
+
def self.create_hold_stream(channels, response=nil)
|
148
|
+
return create_hold('stream', channels, response)
|
149
|
+
end
|
150
|
+
|
97
151
|
def self.decode_websocket_events(body)
|
98
152
|
out = []
|
99
153
|
start = 0
|
@@ -123,7 +177,7 @@ class GripControl
|
|
123
177
|
out = ''
|
124
178
|
events.each do |event|
|
125
179
|
if !event.content.nil?
|
126
|
-
out += '%s %x\r\n%s\r\n' % [e.type,
|
180
|
+
out += '%s %x\r\n%s\r\n' % [e.type, event.content.length, event.content]
|
127
181
|
else
|
128
182
|
out += '%s\r\n' % [e.type]
|
129
183
|
end
|
@@ -133,7 +187,6 @@ class GripControl
|
|
133
187
|
|
134
188
|
def self.websocket_control_message(type, args=nil)
|
135
189
|
if !args.nil?
|
136
|
-
# REVIEW: is this deep copy workaround effective in this case?
|
137
190
|
out = Marshal.load(Marshal.dump(args))
|
138
191
|
else
|
139
192
|
out = Hash.new
|
@@ -141,4 +194,15 @@ class GripControl
|
|
141
194
|
out['type'] = type
|
142
195
|
return out.to_json
|
143
196
|
end
|
197
|
+
|
198
|
+
private
|
199
|
+
|
200
|
+
def build_query_string(params)
|
201
|
+
params.map do |name,values|
|
202
|
+
values.map do |value|
|
203
|
+
'#{CGI.escape name}=#{CGI.escape value}'
|
204
|
+
end
|
205
|
+
end.flatten.join('&')
|
206
|
+
end
|
207
|
+
|
144
208
|
end
|
data/lib/grippubcontrol.rb
CHANGED
@@ -8,9 +8,26 @@
|
|
8
8
|
require 'pubcontrol'
|
9
9
|
|
10
10
|
class GripPubControl < PubControl
|
11
|
+
alias super_add_client add_client
|
11
12
|
alias super_publish publish
|
12
13
|
alias super_publish_async publish_async
|
13
14
|
|
15
|
+
def apply_grip_config(config)
|
16
|
+
if !config.is_a?(Array)
|
17
|
+
config = [config]
|
18
|
+
end
|
19
|
+
config.each do |entry|
|
20
|
+
if !entry.key?('control_uri')
|
21
|
+
next
|
22
|
+
end
|
23
|
+
client = PubControlClient.new(entry['control_uri'])
|
24
|
+
if entry.key?('control_iss')
|
25
|
+
client.set_auth_jwt({'iss' => entry['control_iss']}, entry['key'])
|
26
|
+
end
|
27
|
+
super_add_client(client)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
14
31
|
def publish_http_response(channel, http_response, id=nil, prev_id=nil)
|
15
32
|
if http_response.is_a?(String)
|
16
33
|
http_response = HttpResponseFormat.new(nil, nil, nil, http_response)
|
data/lib/response.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# response.rb
|
2
|
+
# ~~~~~~~~~
|
3
|
+
# This module implements the Response class.
|
4
|
+
# :authors: Konstantin Bokarius.
|
5
|
+
# :copyright: (c) 2015 by Fanout, Inc.
|
6
|
+
# :license: MIT, see LICENSE for more details.
|
7
|
+
|
8
|
+
class Response
|
9
|
+
attr_accessor :code
|
10
|
+
attr_accessor :reason
|
11
|
+
attr_accessor :headers
|
12
|
+
attr_accessor :body
|
13
|
+
|
14
|
+
def initialize(code=nil, reason=nil, headers=nil, body=nil)
|
15
|
+
@code = code
|
16
|
+
@reason = reason
|
17
|
+
@headers = headers
|
18
|
+
@body = body
|
19
|
+
end
|
20
|
+
end
|
@@ -21,11 +21,11 @@ class WebSocketMessageFormat < Format
|
|
21
21
|
|
22
22
|
def export
|
23
23
|
out = Hash.new
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
if @content.encoding.name == 'ASCII-8BIT'
|
25
|
+
out['body-bin'] = Base64.encode64(@content)
|
26
|
+
else
|
27
|
+
out['body'] = @content
|
28
|
+
end
|
29
29
|
return out
|
30
30
|
end
|
31
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gripcontrol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstantin Bokarius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pubcontrol
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: jwt
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- lib/grippubcontrol.rb
|
50
50
|
- lib/httpresponseformat.rb
|
51
51
|
- lib/httpstreamformat.rb
|
52
|
+
- lib/response.rb
|
52
53
|
- lib/websocketevent.rb
|
53
54
|
- lib/websocketmessageformat.rb
|
54
55
|
homepage: http://rubygems.org/gems/gripcontrol
|