class Webtube::Request

Represents an HTTP request (well, request header, since a WebSocket open request shouldn't have a body) being prepared to open a WebSocket connection.

Attributes

location[R]

Public Class Methods

new(location, custom_fields = {}) click to toggle source
Calls superclass method
# File lib/webtube.rb, line 556
def initialize location, custom_fields = {}
  super()
  @location = location
  @fields = {} # capitalised-name => value
  # Since Ruby hashes are case-sensitive but HTTP header
  # field names are case-insensitive, we may have to combine
  # fields whose names only differ in case.
  custom_fields.each_pair do |name, value|
    name = name.capitalize
    if @fields.has_key? name then
      @fields[name] += ', ' + value
    else
      @fields[name] = value
    end
  end

  # Add in the WebSocket header fields but give precedence
  # to user-specified values
  @fields['Host'] ||= @location.host_and_maybe_port
  @fields['Upgrade'] ||= 'websocket'
  @fields['Connection'] ||= 'upgrade'
  @fields['Sec-websocket-key'] ||=
      SecureRandom.base64(16)
  @fields['Sec-websocket-version'] ||= '13'

  return
end

Public Instance Methods

[](name) click to toggle source
# File lib/webtube.rb, line 584
def [] name
  return @fields[name.capitalize]
end
[]=(name, value) click to toggle source
# File lib/webtube.rb, line 588
def []= name, value
  name = name.capitalize
  unless value.nil? then
    @fields[name] = value
  else
    @fields.delete name
  end
  return value
end
each_pair(&thunk) click to toggle source
# File lib/webtube.rb, line 598
def each_pair &thunk
  @fields.each_pair &thunk
  return self
end
expected_accept() click to toggle source
# File lib/webtube.rb, line 616
def expected_accept
  return OpenSSL::Digest::SHA1.base64digest(
      self['Sec-WebSocket-Key'] +
      '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
end
to_s() click to toggle source

Constructs an HTTP request header in string form, together with CRLF line terminators and the terminal blank line, ready to be transmitted to the server.

# File lib/webtube.rb, line 606
def to_s
  s = ''
  s << "GET #{@location.requestee} HTTP/1.1\r\n"
  each_pair do |k, v|
    s << "#{k}: #{v}\r\n"
  end
  s << "\r\n"
  return s
end