Class: NchanTools::Subscriber::LongPollClient::HTTPBundle
- Inherits:
-
ParserBundle
- Object
- ParserBundle
- NchanTools::Subscriber::LongPollClient::HTTPBundle
- Defined in:
- lib/nchan_tools/pubsub.rb
Instance Attribute Summary collapse
-
#done ⇒ Object
Returns the value of attribute done.
-
#last_message_time ⇒ Object
Returns the value of attribute last_message_time.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#request_time ⇒ Object
Returns the value of attribute request_time.
-
#sock ⇒ Object
Returns the value of attribute sock.
-
#stop_after_headers ⇒ Object
Returns the value of attribute stop_after_headers.
-
#time_requested ⇒ Object
Returns the value of attribute time_requested.
Instance Method Summary collapse
-
#initialize(uri, opt = {}) ⇒ HTTPBundle
constructor
A new instance of HTTPBundle.
- #read ⇒ Object
- #reconnect? ⇒ Boolean
- #send_GET(msg_time = nil, msg_tag = nil) ⇒ Object
Constructor Details
#initialize(uri, opt = {}) ⇒ HTTPBundle
Returns a new instance of HTTPBundle.
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 |
# File 'lib/nchan_tools/pubsub.rb', line 727 def initialize(uri, opt={}) super @accept = opt[:accept] or "*/*" @rcvbuf="" @sndbuf="" @parser = Http::Parser.new @done = false extra_headers = (opt[:headers] or opt[:extra_headers] or {}).map{|k,v| "#{k}: #{v}\n"}.join "" host = uri.host.match "[^/]+$" request_uri = "#{uri.path}#{uri.query && "?#{uri.query}"}" @send_noid_str= <<-END.gsub(/^ {10}/, '') GET #{request_uri} HTTP/1.1 Host: #{host}#{uri.default_port == uri.port ? "" : ":#{uri.port}"} #{extra_headers}Accept: #{@accept} User-Agent: #{opt[:useragent] || "HTTPBundle"} END @send_withid_fmt= <<-END.gsub(/^ {10}/, '') GET #{request_uri.gsub("%", "%%")} HTTP/1.1 Host: #{host}#{uri.default_port == uri.port ? "" : ":#{uri.port}"} #{extra_headers}Accept: #{@accept} User-Agent: #{opt[:useragent] || "HTTPBundle"} If-Modified-Since: %s If-None-Match: %s END @send_withid_no_etag_fmt= <<-END.gsub(/^ {10}/, '') GET #{request_uri.gsub("%", "%%")} HTTP/1.1 Host: #{host}#{uri.default_port == uri.port ? "" : ":#{uri.port}"} #{extra_headers}Accept: #{@accept} User-Agent: #{opt[:useragent] || "HTTPBundle"} If-Modified-Since: %s END @parser.on_headers_complete = proc do |h| if verbose puts "< HTTP/1.1 #{@parser.status_code} [...]\r\n#{h.map {|k,v| "< #{k}: #{v}"}.join "\r\n"}" end @headers=h @last_modified = h['Last-Modified'] @etag = h['Etag'] @chunky = h['Transfer-Encoding']=='chunked' @gzipped = h['Content-Encoding']=='gzip' @code=@parser.status_code on_headers @parser.status_code, h if @stop_after_headers @bypass_parser = true :stop end end @parser.on_body = proc do |chunk| handle_chunk chunk end @parser. = proc do @chunky = nil @gzipped = nil on_response @parser.status_code, @parser.headers end end |
Instance Attribute Details
#done ⇒ Object
Returns the value of attribute done.
725 726 727 |
# File 'lib/nchan_tools/pubsub.rb', line 725 def done @done end |
#last_message_time ⇒ Object
Returns the value of attribute last_message_time.
725 726 727 |
# File 'lib/nchan_tools/pubsub.rb', line 725 def @last_message_time end |
#parser ⇒ Object
Returns the value of attribute parser.
725 726 727 |
# File 'lib/nchan_tools/pubsub.rb', line 725 def parser @parser end |
#request_time ⇒ Object
Returns the value of attribute request_time.
725 726 727 |
# File 'lib/nchan_tools/pubsub.rb', line 725 def request_time @request_time end |
#sock ⇒ Object
Returns the value of attribute sock.
725 726 727 |
# File 'lib/nchan_tools/pubsub.rb', line 725 def sock @sock end |
#stop_after_headers ⇒ Object
Returns the value of attribute stop_after_headers.
725 726 727 |
# File 'lib/nchan_tools/pubsub.rb', line 725 def stop_after_headers @stop_after_headers end |
#time_requested ⇒ Object
Returns the value of attribute time_requested.
725 726 727 |
# File 'lib/nchan_tools/pubsub.rb', line 725 def time_requested @time_requested end |
Instance Method Details
#read ⇒ Object
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
# File 'lib/nchan_tools/pubsub.rb', line 833 def read @rcvbuf.clear begin sock.readpartial(1024*10000, @rcvbuf) while @rcvbuf.size > 0 unless @bypass_parser offset = @parser << @rcvbuf if offset < @rcvbuf.size @rcvbuf = @rcvbuf[offset..-1] else @rcvbuf.clear end else handle_chunk @rcvbuf @rcvbuf.clear end end rescue HTTP::Parser::Error => e on_error "Invalid HTTP Respose - #{e}", e rescue EOFError => e on_error "Server closed connection...", e rescue => e on_error "#{e.class}: #{e}", e end return false if @done || sock.closed? end |
#reconnect? ⇒ Boolean
800 801 802 |
# File 'lib/nchan_tools/pubsub.rb', line 800 def reconnect? true end |
#send_GET(msg_time = nil, msg_tag = nil) ⇒ Object
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/nchan_tools/pubsub.rb', line 804 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 @sndbuf.clear begin data = if @last_modified @etag ? sprintf(@send_withid_fmt, @last_modified, @etag) : sprintf(@send_withid_no_etag_fmt, @last_modified) else @send_noid_str end rescue Exception => e binding.pry end @sndbuf << data if @headers && @headers["Connection"]=="close" && [200, 201, 202, 304, 408].member?(@parser.status_code) && reconnect? sock.close open_socket @parser.reset! end @time_requested=Time.now.to_f if verbose puts "", data.gsub(/^.*$/, "> \\0") end sock << @sndbuf end |