Module: WebSocket::HTTP::Headers

Included in:
Request, Response
Defined in:
lib/websocket/http/headers.rb

Constant Summary collapse

MAX_LINE_LENGTH =
4096
CR =
0x0D
LF =
0x0A
HEADER_LINE =
/^([!#\$%&'\*\+\-\.\\\^_`\|~0-9a-z]+):\s*([\x20-\x7e]*?)\s*$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



11
12
13
# File 'lib/websocket/http/headers.rb', line 11

def headers
  @headers
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/websocket/http/headers.rb', line 19

def complete?
  @stage == 2
end

#error?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/websocket/http/headers.rb', line 23

def error?
  @stage == -1
end

#initializeObject



13
14
15
16
17
# File 'lib/websocket/http/headers.rb', line 13

def initialize
  @buffer  = []
  @headers = {}
  @stage   = 0
end

#parse(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/websocket/http/headers.rb', line 27

def parse(data)
  data.each_byte do |byte|
    if byte == LF and @stage < 2
      @buffer.pop if @buffer.last == CR
      if @buffer.empty?
        complete if @stage == 1
      else
        result = case @stage
                 when 0 then start_line(string_buffer)
                 when 1 then header_line(string_buffer)
                 end

        if result
          @stage = 1
        else
          error
        end
      end
      @buffer = []
    else
      @buffer << byte if @stage >= 0
      error if @stage < 2 and @buffer.size > MAX_LINE_LENGTH
    end
  end
  @env['rack.input'] = StringIO.new(string_buffer) if @env
end