class Webtube::Location

Represents a parsed WebSocket URL.

Attributes

default_port[R]
host[R]
port[R]
requestee[R]

Public Class Methods

new(url) click to toggle source
Calls superclass method
# File lib/webtube.rb, line 484
def initialize url
  super()
  # force into a single-byte encoding so urlencoding can
  # work correctly
  url = url.dup.force_encoding Encoding::ASCII_8BIT
  # ensure that any whitespace, ASCII on-printables, and
  # some popular text delimiters (parens, brokets, brackets,
  # and broken bar) in [[url]] are properly urlencoded
  url.gsub! ' ', '+'
  url.gsub!(/[^\x21-\x7E]/){'%%%02X' % $&.ord}
  url.gsub!(/[()<>\[\]\|]/){'%%%02X' % $&.ord}
  # We'll replace the WebSocket protocol prefix with an
  # HTTP-based one so [[URI::parse]] would know how to
  # parse the rest of the URL.
  case url
    when /\A(ws|http):/ then
      http_url = 'http:' + $'
      @ssl = false
      @default_port = 80
    when /\A(wss|https):/ then
      http_url = 'https:' + $'
      @ssl = true
      @default_port = 443
    else
      raise "unknown URI scheme; use ws: or wss: instead"
  end
  http_uri = URI.parse http_url
  @host = http_uri.host
  @port = http_uri.port
  @requestee = http_uri.path
  if @requestee.empty? then
    @requestee = '/'
  end
  @requestee += '?' + http_uri.query \
      if http_uri.query
  return
end

Public Instance Methods

host_and_maybe_port() click to toggle source

Returns the hostname and, if non-default, the port number separated by colon. This combination is used in HTTP 1.1

[Host]

header fields but also in URIs.

# File lib/webtube.rb, line 542
def host_and_maybe_port
  h = @host
  h += ":#@port" \
      unless @port == @default_port
  return h
end
ssl?() click to toggle source
# File lib/webtube.rb, line 530
def ssl?
  return @ssl
end
to_s() click to toggle source
# File lib/webtube.rb, line 522
def to_s
  s = !ssl? ? 'ws:' : 'wss:'
  s += '//' + host_and_maybe_port
  s += @requestee \
      unless @requestee == '/'
  return s
end