class NchanTools::Subscriber::LongPollClient::HTTP2Bundle

Constants

GET_METHOD

Attributes

done[RW]
last_message_time[RW]
request_time[RW]
sock[RW]
stream[RW]
time_requested[RW]

Public Class Methods

new(uri, opt = {}) click to toggle source
Calls superclass method
# File lib/nchan_tools/pubsub.rb, line 864
def initialize(uri, opt = {})
  if HTTP2_MISSING
    raise SubscriberError, "HTTP/2 gem missing"
  end
  super
  @done = false
  @rcvbuf=""
  @head = {
    ':scheme' => uri.scheme,
    ':method' => GET_METHOD,
    ':path' => "#{uri.path}#{uri.query && "?#{uri.query}"}",
    ':authority' => [uri.host, uri.port].join(':'),
    'user-agent' => "#{opt[:useragent] || "HTTP2Bundle"}",
    'accept' => opt[:accept] || "*/*"
  }
  if opt[:headers]
    opt[:headers].each{ |h, v| @head[h.to_s.downcase]=v }
  end
  @client = HTTP2::Client.new
  @client.on(:frame) do |bytes|
    #puts "Sending bytes: #{bytes.unpack("H*").first}"
    @sock.print bytes
    @sock.flush
  end
  
  @client.on(:frame_sent) do |frame|
    #puts "Sent frame: #{frame.inspect}" if verbose
  end
  @client.on(:frame_received) do |frame|
    #puts "Received frame: #{frame.inspect}" if verbose
  end
  @resp_headers={}
  @resp_code=nil
end

Public Instance Methods

read() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 957
def read
  return false if @done || @sock.closed?
  begin
    @rcv = @sock.readpartial 1024
    @client << @rcv
  rescue EOFError => e
    if @rcv && @rcv[0..5]=="HTTP/1"
      on_error @rcv.match(/^HTTP\/1.*/)[0].chomp, e
    else
      on_error "Server closed connection...", e
    end
    @sock.close
  rescue => e
    on_error "#{e.class}: #{e.to_s}", e
    @sock.close
  end
  return false if @done || @sock.closed?
end
reconnect?() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 899
def reconnect?
  false
end
send_GET(msg_time=nil, msg_tag=nil) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 903
def send_GET(msg_time=nil, msg_tag=nil)
  @last_modified = msg_time.to_s if msg_time
  @etag = msg_tag.to_s if msg_tag
  @time_requested=Time.now.to_f
  if msg_time
    @head['if-modified-since'] = msg_time.to_s
  else
    @head.delete @head['if-modified-since']
  end
  
  if msg_tag
    @head['if-none-match'] = msg_tag.to_s
  else
    @head.delete @head['if-none-match']
  end
  
  @stream = @client.new_stream
  @resp_headers.clear
  @resp_code=0
  @stream.on(:close) do |k,v|
    on_response @resp_code, @resp_headers
  end
  @stream.on(:headers) do |h|
    h.each do |v|
      puts "< #{v.join ': '}" if verbose
      case v.first
      when ":status"
        @resp_code = v.last.to_i
      when /^:/
        @resp_headers[v.first] = v.last
      else
        @resp_headers[v.first.gsub(/(?<=^|\W)\w/) { |v| v.upcase }]=v.last
      end
    end
    @headers = @resp_headers
    @code = @resp_code
    on_headers @resp_code, @resp_headers
  end
  @stream.on(:data) do |d|
    #puts "got data chunk #{d}"
    on_chunk d
  end
  
  @stream.on(:altsvc) do |f|
    puts "received ALTSVC #{f}" if verbose
  end
  
  @stream.on(:half_close) do
    puts "", @head.map {|k,v| "> #{k}: #{v}"}.join("\r\n") if verbose
  end
  
  @stream.headers(@head, end_stream: true)
end