Sha256: d6efa2c76f9651112f87183175ff5ea8f899483a62b48e925affbe8653e91e2b

Contents?: true

Size: 1.85 KB

Versions: 17

Compression:

Stored size: 1.85 KB

Contents

module Invoker
  module Power
    class HttpParser
      attr_accessor :host, :parser, :protocol

      def initialize(protocol)
        @protocol = protocol
        @parser = HTTP::Parser.new
        @header = {}
        initialize_message_content
        parser.on_headers_complete { complete_headers_received }
        parser.on_header_field { |field_name| @last_key = field_name }
        parser.on_header_value { |field_value| header_value_received(field_value) }

        parser.on_message_complete { complete_message_received }
      end

      # define a callback for invoking when complete header is parsed
      def on_headers_complete(&block)
        @on_headers_complete_callback = block
      end

      def header_value_received(value)
        @header[@last_key] = value
      end

      # define a callback to invoke when a full http message is received
      def on_message_complete(&block)
        @on_message_complete_callback = block
      end

      def reset
        @header = {}
        initialize_message_content
        parser.reset
      end

      def << data
        @full_message.write(data)
        parser << data
      end

      private

      def complete_message_received
        full_message_string = @full_message.string.dup
        if full_message_string =~ /\r\n\r\n/
          full_message_string.sub!(/\r\n\r\n/, "\r\nX_FORWARDED_PROTO: #{protocol}\r\n\r\n")
        end
        if @on_message_complete_callback
          @on_message_complete_callback.call(full_message_string)
        end
      end

      def initialize_message_content
        @full_message = StringIO.new
        @full_message.set_encoding('ASCII-8BIT')
      end

      # gets invoker when complete header is received
      def complete_headers_received
        if @on_headers_complete_callback
          @on_headers_complete_callback.call(@header)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
itrg-invoker-1.6.1 lib/invoker/power/http_parser.rb
itrg-invoker-1.6.0 lib/invoker/power/http_parser.rb
invoker-1.5.8 lib/invoker/power/http_parser.rb
invoker-1.5.7 lib/invoker/power/http_parser.rb
invoker-1.5.6 lib/invoker/power/http_parser.rb
invoker-1.5.5 lib/invoker/power/http_parser.rb
invoker-1.5.4 lib/invoker/power/http_parser.rb
invoker-1.5.3 lib/invoker/power/http_parser.rb
meepo-1.5.2 lib/invoker/power/http_parser.rb
invoker-1.5.1 lib/invoker/power/http_parser.rb
invoker-1.4.1 lib/invoker/power/http_parser.rb
invoker-1.4.0 lib/invoker/power/http_parser.rb
invoker-1.3.2 lib/invoker/power/http_parser.rb
invoker-1.3.1 lib/invoker/power/http_parser.rb
invoker-1.3.0 lib/invoker/power/http_parser.rb
invoker-1.2.0 lib/invoker/power/http_parser.rb
invoker-1.2.0.pre2 lib/invoker/power/http_parser.rb