Sha256: 5688621eb735293040ba734b4bd661e4d723e061183f1eebb35cfbd5eba9421e

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

require 'mongrel2'

module Mongrel2
  class Request
    attr_reader :headers, :body, :uuid, :conn_id, :path

    class << self
      def parse(msg)
        # UUID CONN_ID PATH SIZE:HEADERS,SIZE:BODY,
        uuid, conn_id, path, rest = msg.split(' ', 4)
        headers, rest = parse_netstring(rest)
        body, _ = parse_netstring(rest)
        headers = Mongrel2::JSON.parse(headers)
        new(uuid, conn_id, path, headers, body)
      end

      def parse_netstring(ns)
        # SIZE:HEADERS,

        len, rest = ns.split(':', 2)
        len = len.to_i
        raise "Netstring did not end in ','" unless rest[len].chr == ','
        [rest[0, len], rest[(len + 1)..-1]]
      end
    end

    def initialize(uuid, conn_id, path, headers, body)
      @uuid, @conn_id, @path, @headers, @body = uuid, conn_id, path, headers, body
      @data = headers['METHOD'] == 'JSON' ? Mongrel2::JSON.parse(body) : {}
    end

    def disconnect?
      headers['METHOD'] == 'JSON' && @data['type'] == 'disconnect'
    end

    def close?
      headers['connection'] == 'close' || headers['VERSION'] == 'HTTP/1.0'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-mongrel2-0.2.4 lib/mongrel2/request.rb
rack-mongrel2-0.2.3 lib/mongrel2/request.rb
rack-mongrel2-0.2.2 lib/mongrel2/request.rb