Module: WebSocket::HTTP::Headers

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

Constant Summary collapse

MAX_REQUEST_SIZE =
32768
CR =
0x0D
LF =
0x0A
HEADER_LINE =

RFC 2616 grammar rules:

CHAR           = <any US-ASCII character (octets 0 - 127)>

CTL            = <any US-ASCII control character
                 (octets 0 - 31) and DEL (127)>

SP             = <US-ASCII SP, space (32)>

HT             = <US-ASCII HT, horizontal-tab (9)>

token          = 1*<any CHAR except CTLs or separators>

separators     = "(" | ")" | "<" | ">" | "@"
               | "," | ";" | ":" | "\" | <">
               | "/" | "[" | "]" | "?" | "="
               | "{" | "}" | SP | HT

Or, as redefined in RFC 7230:

token          = 1*tchar

tchar          = "!" / "#" / "$" / "%" / "&" / "'" / "*"
               / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
               / DIGIT / ALPHA
               ; any VCHAR, except delimiters
/^([!#\$%&'\*\+\-\.\^_`\|~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.



38
39
40
# File 'lib/websocket/http/headers.rb', line 38

def headers
  @headers
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/websocket/http/headers.rb', line 48

def complete?
  @stage == 2
end

#error?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/websocket/http/headers.rb', line 52

def error?
  @stage == -1
end

#initializeObject



40
41
42
43
44
45
46
# File 'lib/websocket/http/headers.rb', line 40

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

#parse(chunk) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/websocket/http/headers.rb', line 56

def parse(chunk)
  chunk.each_byte do |octet|
    @size += 1
    return error if @size > MAX_REQUEST_SIZE

    if octet == 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 = []
    elsif @stage >= 0
      @buffer << octet
    end
  end
  @env['rack.input'] = StringIO.new(string_buffer)
end