Sha256: 9b00f20dc8b915bcb7d066dc637081a389a857c58e127813692702f42f6f137e

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

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 << 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 read(size)
        return if @chunk.nil?

        if @chunk.bytesize <= size
          chunk  = @chunk
          @chunk = nil
        else
          chunk = @chunk.byteslice(0, size)
          @chunk[0, size] = ""
        end

        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

12 entries across 12 versions & 1 rubygems

Version Path
http-4.1.1 lib/http/response/parser.rb
http-4.1.0 lib/http/response/parser.rb
http-4.0.5 lib/http/response/parser.rb
http-4.0.4 lib/http/response/parser.rb
http-4.0.3 lib/http/response/parser.rb
http-4.0.2 lib/http/response/parser.rb
http-4.0.1 lib/http/response/parser.rb
http-4.0.0 lib/http/response/parser.rb
http-3.3.0 lib/http/response/parser.rb
http-3.2.1 lib/http/response/parser.rb
http-3.2.0 lib/http/response/parser.rb
http-3.1.0 lib/http/response/parser.rb