Class: GripControl

Inherits:
Object
  • Object
show all
Defined in:
lib/gripcontrol.rb

Class Method Summary collapse

Class Method Details

.create_grip_channel_header(channels) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gripcontrol.rb', line 125

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



143
144
145
# File 'lib/gripcontrol.rb', line 143

def self.create_hold_response(channels, response=nil)
  return GripControl.create_hold('response', channels, response)
end

.create_hold_stream(channels, response = nil) ⇒ Object



147
148
149
# File 'lib/gripcontrol.rb', line 147

def self.create_hold_stream(channels, response=nil)
  return create_hold('stream', channels, response)
end

.decode_websocket_events(body) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gripcontrol.rb', line 151

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(' ')
    if !at.nil?
      etype = typeline[0..at - 1]
      clen = ('0x' + typeline[at + 1..-1]).to_s(16)
      content = body[start:start + clen - 1]
      start += clen + 2
      e = WebSocketEvent.new(etype, content)
    else
      e = WebSocketEvent.new(typeline)
    end
    out.push(e)
  end
  return out
end

.encode_websocket_events(events) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/gripcontrol.rb', line 176

def self.encode_websocket_events(events)
  out = ''
  events.each do |event|
    if !event.content.nil?
      out += '%s %x\r\n%s\r\n' % [e.type, event.content.length, event.content]
    else
      out += '%s\r\n' % [e.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
# 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 = build_query_string(params)
  path = uri.path
  if path.end_with?('/')
    path = path[0..-2]
  end
  control_uri = parsed.scheme + '://' + parsed.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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gripcontrol.rb', line 109

def self.validate_sig(token, key)
  token = token.encode('utf-8')
  begin
    claim = JWT.decode(token, key, verify_expiration=false)
  rescue
    return false
  end
  if !claim.key?('exp')
    return false
  end
  if Time.now.utc.to_i >= claim['exp']
    return false
  end
  return true
end

.websocket_control_message(type, args = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/gripcontrol.rb', line 188

def self.websocket_control_message(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