Class: Subscriber::LongPollClient::HTTPBundle
- Inherits:
-
ParserBundle
- Object
- ParserBundle
- 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.
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 792 |
# File 'lib/nchan_tools/pubsub.rb', line 728 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= " GET \#{request_uri} HTTP/1.1\n Host: \#{host}\#{uri.default_port == uri.port ? \"\" : \":\#{uri.port}\"}\n \#{extra_headers}Accept: \#{@accept}\n User-Agent: \#{opt[:useragent] || \"HTTPBundle\"}\n \n END\n \n @send_withid_fmt= <<-END.gsub(/^ {10}/, '')\n GET \#{request_uri.gsub(\"%\", \"%%\")} HTTP/1.1\n Host: \#{host}\#{uri.default_port == uri.port ? \"\" : \":\#{uri.port}\"}\n \#{extra_headers}Accept: \#{@accept}\n User-Agent: \#{opt[:useragent] || \"HTTPBundle\"}\n If-Modified-Since: %s\n If-None-Match: %s\n \n END\n \n @send_withid_no_etag_fmt= <<-END.gsub(/^ {10}/, '')\n GET \#{request_uri.gsub(\"%\", \"%%\")} HTTP/1.1\n Host: \#{host}\#{uri.default_port == uri.port ? \"\" : \":\#{uri.port}\"}\n \#{extra_headers}Accept: \#{@accept}\n User-Agent: \#{opt[:useragent] || \"HTTPBundle\"}\n If-Modified-Since: %s\n \n END\n \n @parser.on_headers_complete = proc do |h|\n if verbose \n puts \"< HTTP/1.1 \#{@parser.status_code} [...]\\r\\n\#{h.map {|k,v| \"< \#{k}: \#{v}\"}.join \"\\r\\n\"}\"\n end\n @headers=h\n @last_modified = h['Last-Modified']\n @etag = h['Etag']\n @chunky = h['Transfer-Encoding']=='chunked'\n @gzipped = h['Content-Encoding']=='gzip'\n @[email protected]_code\n on_headers @parser.status_code, h\n if @stop_after_headers\n @bypass_parser = true\n :stop\n end\n end\n \n @parser.on_body = proc do |chunk|\n handle_chunk chunk\n end\n \n @parser.on_message_complete = proc do\n @chunky = nil\n @gzipped = nil\n on_response @parser.status_code, @parser.headers\n end\n \nend\n".gsub(/^ {10}/, '') |
Instance Attribute Details
#done ⇒ Object
Returns the value of attribute done.
726 727 728 |
# File 'lib/nchan_tools/pubsub.rb', line 726 def done @done end |
#last_message_time ⇒ Object
Returns the value of attribute last_message_time.
726 727 728 |
# File 'lib/nchan_tools/pubsub.rb', line 726 def end |
#parser ⇒ Object
Returns the value of attribute parser.
726 727 728 |
# File 'lib/nchan_tools/pubsub.rb', line 726 def parser @parser end |
#request_time ⇒ Object
Returns the value of attribute request_time.
726 727 728 |
# File 'lib/nchan_tools/pubsub.rb', line 726 def request_time @request_time end |
#sock ⇒ Object
Returns the value of attribute sock.
726 727 728 |
# File 'lib/nchan_tools/pubsub.rb', line 726 def sock @sock end |
#stop_after_headers ⇒ Object
Returns the value of attribute stop_after_headers.
726 727 728 |
# File 'lib/nchan_tools/pubsub.rb', line 726 def stop_after_headers @stop_after_headers end |
#time_requested ⇒ Object
Returns the value of attribute time_requested.
726 727 728 |
# File 'lib/nchan_tools/pubsub.rb', line 726 def time_requested @time_requested end |
Instance Method Details
#read ⇒ Object
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 859 |
# File 'lib/nchan_tools/pubsub.rb', line 834 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
801 802 803 |
# File 'lib/nchan_tools/pubsub.rb', line 801 def reconnect? true end |
#send_GET(msg_time = nil, msg_tag = nil) ⇒ Object
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 832 |
# File 'lib/nchan_tools/pubsub.rb', line 805 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 |