Sha256: 5b4d519051152bf14fade58a5246ffb263771f09f8c5de2208cd7c5d8ddeedc7

Contents?: true

Size: 994 Bytes

Versions: 1

Compression:

Stored size: 994 Bytes

Contents

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

    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 = Yajl::Parser.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' ? Yajl::Parser.parse(body) : {}
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-mongrel2-0.1.0 lib/mongrel2/request.rb