Class: Subscriber::LongPollClient::HTTP2Bundle

Inherits:
ParserBundle
  • Object
show all
Defined in:
lib/nchan_tools/pubsub.rb

Constant Summary collapse

GET_METHOD =
"GET"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, opt = {}) ⇒ HTTP2Bundle

Returns a new instance of HTTP2Bundle.



865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/nchan_tools/pubsub.rb', line 865

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

Instance Attribute Details

#doneObject

Returns the value of attribute done.



863
864
865
# File 'lib/nchan_tools/pubsub.rb', line 863

def done
  @done
end

#last_message_timeObject

Returns the value of attribute last_message_time.



863
864
865
# File 'lib/nchan_tools/pubsub.rb', line 863

def last_message_time
  @last_message_time
end

#request_timeObject

Returns the value of attribute request_time.



863
864
865
# File 'lib/nchan_tools/pubsub.rb', line 863

def request_time
  @request_time
end

#sockObject

Returns the value of attribute sock.



863
864
865
# File 'lib/nchan_tools/pubsub.rb', line 863

def sock
  @sock
end

#streamObject

Returns the value of attribute stream.



863
864
865
# File 'lib/nchan_tools/pubsub.rb', line 863

def stream
  @stream
end

#time_requestedObject

Returns the value of attribute time_requested.



863
864
865
# File 'lib/nchan_tools/pubsub.rb', line 863

def time_requested
  @time_requested
end

Instance Method Details

#readObject



958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'lib/nchan_tools/pubsub.rb', line 958

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?Boolean

Returns:

  • (Boolean)


900
901
902
# File 'lib/nchan_tools/pubsub.rb', line 900

def reconnect?
  false
end

#send_GET(msg_time = nil, msg_tag = nil) ⇒ Object



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
# File 'lib/nchan_tools/pubsub.rb', line 904

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