Class: Reel::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
RequestMixin
Defined in:
lib/reel/request.rb,
lib/reel/request_parser.rb

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestMixin

#[], #fragment, #headers, #method, #path, #query_string, #uri, #url, #version

Constructor Details

#initialize(request_info, connection = nil) ⇒ Request

request_info is a RequestInfo object including the headers and the url, method and http version.

Access it through the RequestMixin methods.



15
16
17
18
19
20
21
22
23
# File 'lib/reel/request.rb', line 15

def initialize(request_info, connection = nil)
  @request_info  = request_info
  @connection    = connection
  @finished      = false
  @buffer        = ""
  @body          = RequestBody.new(self)
  @finished_read = false
  @websocket     = nil
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/reel/request.rb', line 9

def body
  @body
end

Instance Method Details

#fill_buffer(chunk) ⇒ Object

Fill the request buffer with data as it becomes available



35
36
37
# File 'lib/reel/request.rb', line 35

def fill_buffer(chunk)
  @buffer << chunk
end

#finish_reading!Object

When HTTP Parser marks the message parsing as complete, this will be set.

Raises:

  • (StateError)


29
30
31
32
# File 'lib/reel/request.rb', line 29

def finish_reading!
  raise StateError, "already finished" if @finished_read
  @finished_read = true
end

#finished_reading?Boolean

Returns true if request fully finished reading



26
# File 'lib/reel/request.rb', line 26

def finished_reading?; @finished_read; end

#read(length = nil, buffer = nil) ⇒ Object

Read a number of bytes, looping until they are available or until readpartial returns nil, indicating there are no more bytes to read

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/reel/request.rb', line 41

def read(length = nil, buffer = nil)
  raise ArgumentError, "negative length #{length} given" if length && length < 0

  return '' if length == 0
  res = buffer.nil? ? '' : buffer.clear

  chunk_size = length.nil? ? @connection.buffer_size : length
  begin
    while chunk_size > 0
      chunk = readpartial(chunk_size)
      break unless chunk
      res << chunk
      chunk_size = length - res.length unless length.nil?
    end
  rescue EOFError
  end
  return length && res.length == 0 ? nil : res
end

#readpartial(length = nil) ⇒ Object

Read a string up to the given number of bytes, blocking until some data is available but returning immediately if some data is available



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/reel/request.rb', line 62

def readpartial(length = nil)
  if length.nil? && @buffer.length > 0
    slice = @buffer
    @buffer = ""
  else
    unless finished_reading? || (length && length <= @buffer.length)
      @connection.readpartial(length ? length - @buffer.length : Connection::BUFFER_SIZE)
    end

    if length
      slice = @buffer.slice!(0, length)
    else
      slice = @buffer
      @buffer = ""
    end
  end

  slice && slice.length == 0 ? nil : slice
end

#websocketObject

Return a Reel::WebSocket for this request, hijacking the socket from the underlying connection



87
88
89
90
91
92
# File 'lib/reel/request.rb', line 87

def websocket
  @websocket ||= begin
    raise StateError, "can't upgrade this request to a websocket" unless websocket?
    WebSocket.new(@request_info, @connection.hijack_socket)
  end
end

#websocket?Boolean

Can the current request be upgraded to a WebSocket?



83
# File 'lib/reel/request.rb', line 83

def websocket?; @request_info.websocket_request?; end