Sha256: f5936cd1d26bfc7e629057b09df579ec3f476beb21b6a6e3772b99ec84069136

Contents?: true

Size: 1017 Bytes

Versions: 8

Compression:

Stored size: 1017 Bytes

Contents

module HTTP
  class Response
    class Parser
      attr_reader :headers

      def initialize
        @parser = HTTP::Parser.new(self)
        reset
      end

      def add(data)
        @parser << data
      end
      alias_method :<<, :add

      def headers?
        !!@headers
      end

      def http_version
        @parser.http_version.join('.')
      end

      def status_code
        @parser.status_code
      end

      #
      # HTTP::Parser callbacks
      #

      def on_headers_complete(headers)
        @headers = headers
      end

      def on_body(chunk)
        if @chunk
          @chunk << chunk
        else
          @chunk = chunk
        end
      end

      def chunk
        chunk, @chunk = @chunk, nil
        chunk
      end

      def on_message_complete
        @finished = true
      end

      def reset
        @parser.reset!

        @finished = false
        @headers  = nil
        @chunk    = nil
      end

      def finished?
        @finished
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
http-0.7.4 lib/http/response/parser.rb
http-0.6.4 lib/http/response/parser.rb
http-0.7.3 lib/http/response/parser.rb
http-0.7.2 lib/http/response/parser.rb
http-0.7.1 lib/http/response/parser.rb
http-0.7.0 lib/http/response/parser.rb
http-0.6.3 lib/http/response/parser.rb
http-0.6.2 lib/http/response/parser.rb