class Stub::Request
Attributes
Public Class Methods
Source
# File lib/uaa/stub/server.rb, line 31 def initialize; @state, @prelude = :init, "" end
Public Instance Methods
Source
# File lib/uaa/stub/server.rb, line 68 def completed?(str) str, @prelude = @prelude + str, "" unless @prelude.empty? add_lines(str) return unless @state == :body && @body.bytesize >= @content_length @prelude = bslice(@body, @content_length..-1) @body = bslice(@body, 0..@content_length) @state = :init end
adds data to the request, returns true if request is complete
Private Instance Methods
Source
# File lib/uaa/stub/server.rb, line 40 def add_lines(str) return @body << str if @state == :body processed = 0 str.each_line("\r\n") do |ln| processed += ln.bytesize unless ln.chomp!("\r\n") raise BadHeader unless ln.ascii_only? return @prelude = ln # must be partial header at end of str end if @state == :init start = ln.split(/\s+/) @method, @path, @headers, @body = start[0].downcase, start[1], {}, "" raise BadHeader unless @method.ascii_only? && @path.ascii_only? @state = :headers elsif ln.empty? @state, @content_length = :body, headers["content-length"].to_i return @body << bslice(str, processed..-1) else raise BadHeader unless ln.ascii_only? key, sep, val = ln.partition(/:\s+/) @headers[key.downcase] = val end end end
Source
# File lib/uaa/stub/server.rb, line 35 def bslice(str, range) # byteslice is available in ruby 1.9.3 str.respond_to?(:byteslice) ? str.byteslice(range) : str.slice(range) end