Class: GripControl
- Inherits:
-
Object
- Object
- GripControl
- Defined in:
- lib/gripcontrol.rb
Class Method Summary collapse
- .create_grip_channel_header(channels) ⇒ Object
- .create_hold(mode, channels, response) ⇒ Object
- .create_hold_response(channels, response = nil) ⇒ Object
- .create_hold_stream(channels, response = nil) ⇒ Object
- .decode_websocket_events(body) ⇒ Object
- .encode_websocket_events(events) ⇒ Object
- .parse_grip_uri(uri) ⇒ Object
- .validate_sig(token, key) ⇒ Object
- .websocket_control_message(type, args = nil) ⇒ Object
Class Method Details
.create_grip_channel_header(channels) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/gripcontrol.rb', line 131 def self.create_grip_channel_header(channels) if channels.is_a?(Channel) channels = [channels] elsif channels.is_a?(String) channels = [Channel.new(channels)] end raise 'channels.length equal to 0' unless channels.length > 0 parts = [] channels.each do |channel| s = channel.name if !channel.prev_id.nil? s += '; prev-id=%s' % [channel.prev_id] end parts.push(s) end return parts.join(', ') end |
.create_hold(mode, channels, response) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gripcontrol.rb', line 21 def self.create_hold(mode, channels, response) hold = Hash.new hold['mode'] = mode if channels.is_a?(Channel) channels = [channels] elsif channels.is_a?(String) channels = [Channel.new(channels)] end raise 'channels.length equal to 0' unless channels.length > 0 ichannels = [] channels.each do |channel| if channel.is_a?(String) channel = Channel(channel) end ichannel = Hash.new ichannel['name'] = channel.name if !channel.prev_id.nil? ichannel['prev-id'] = channel.prev_id end ichannels.push(ichannel) end hold['channels'] = ichannels iresponse = nil if !response.nil? if response.is_a?(String) response = Response(nil, nil, nil, response) end iresponse = Hash.new if !response.code.nil? iresponse['code'] = response.code end if !response.reason.nil? iresponse['reason'] = response.reason end if !response.headers.nil? and response.headers.length > 0 iresponse['headers'] = response.headers end if !response.body.nil? if response.body.encoding.name == 'ASCII-8BIT' iresponse['body-bin'] = Base64.encode64(response.body) else iresponse['body'] = response.body end end end instruct = Hash.new instruct['hold'] = hold if !iresponse.nil? instruct['response'] = iresponse end return instruct.to_json end |
.create_hold_response(channels, response = nil) ⇒ Object
149 150 151 |
# File 'lib/gripcontrol.rb', line 149 def self.create_hold_response(channels, response=nil) return GripControl.create_hold('response', channels, response) end |
.create_hold_stream(channels, response = nil) ⇒ Object
153 154 155 |
# File 'lib/gripcontrol.rb', line 153 def self.create_hold_stream(channels, response=nil) return create_hold('stream', channels, response) end |
.decode_websocket_events(body) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/gripcontrol.rb', line 157 def self.decode_websocket_events(body) out = [] start = 0 while start < body.length do at = body.index("\r\n", start) if at.nil? raise 'bad format' end typeline = body[start..at - 1] start = at + 2 at = typeline.index(' ') event = nil if !at.nil? etype = typeline[0..at - 1] clen = ('0x' + typeline[at + 1..-1]).to_i(16) content = body[start..start + clen - 1] start += clen + 2 event = WebSocketEvent.new(etype, content) else event = WebSocketEvent.new(typeline) end out.push(event) end return out end |
.encode_websocket_events(events) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/gripcontrol.rb', line 183 def self.encode_websocket_events(events) out = '' events.each do |event| if !event.content.nil? out += "%s %x\r\n%s\r\n" % [event.type, event.content.length, event.content] else out += "%s\r\n" % [event.type] end end return out end |
.parse_grip_uri(uri) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gripcontrol.rb', line 74 def self.parse_grip_uri(uri) uri = URI(uri) params = CGI.parse(uri.query) iss = nil key = nil if params.key?('iss') iss = params['iss'][0] params.delete('iss') end if params.key?('key') key = params['key'][0] params.delete('key') end if !key.nil? and key.start_with?('base64:') key = Base64.decode64(key[7..-1]) end qs = [] params.map do |name,values| values.map do |value| qs.push('#{CGI.escape name}=#{CGI.escape value}') end end qs = qs.join('&') path = uri.path if path.end_with?('/') path = path[0..-2] end control_uri = uri.scheme + '://' + uri.host + path if !qs.nil? and !qs.empty? control_uri += '?' + qs end out = {'control_uri' => control_uri} if !iss.nil? out['control_iss'] = iss end if !key.nil? out['key'] = key end return out end |
.validate_sig(token, key) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/gripcontrol.rb', line 115 def self.validate_sig(token, key) token = token.encode('utf-8') begin claim = JWT.decode(token, key, true, {verify_expiration: false}) rescue return false end if claim.length == 0 or !claim[0].key?('exp') return false end if Time.now.utc.to_i >= claim[0]['exp'] return false end return true end |
.websocket_control_message(type, args = nil) ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/gripcontrol.rb', line 196 def self.(type, args=nil) if !args.nil? out = Marshal.load(Marshal.dump(args)) else out = Hash.new end out['type'] = type return out.to_json end |