Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/websocket/extensions/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class Parser
TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/
NOTOKEN = /([^!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z])/
QUOTED = /"((?:\\[\x00-\x7f]|[^\x00-\x08\x0a-\x1f\x7f"\\])*)"/
PARAM = %r{#{ TOKEN.source }(?:=(?:#{ TOKEN.source }|#{ QUOTED.source }))?}
EXT = %r{#{ TOKEN.source }(?: *; *#{ PARAM.source })*}
EXT_LIST = %r{^#{ EXT.source }(?: *, *#{ EXT.source })*$}
NUMBER = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/
PARAM = %r{#{ TOKEN.source }(?:[ \t]*=[ \t]*(?:#{ TOKEN.source }|#{ QUOTED.source }))?}
EXT = %r{#{ TOKEN.source }(?:[ \t]*;[ \t]*#{ PARAM.source })*}
EXT_START = %r{[ \t]*#{ EXT.source }}
EXT_LIST = %r{\A[ \t]*#{ EXT.source }(?:[ \t]*,[ \t]*#{ EXT.source })*[ \t]*\z}
NUMBER = /\A-?(0|[1-9][0-9]*)(\.[0-9]+)?\z/

ParseError = Class.new(ArgumentError)

Expand All @@ -23,7 +24,7 @@ def self.parse_header(header)
end

scanner = StringScanner.new(header)
value = scanner.scan(EXT)
value = scanner.scan(EXT_START)

until value.nil?
params = value.scan(PARAM)
Expand Down Expand Up @@ -51,7 +52,7 @@ def self.parse_header(header)

offers.push(name, offer)

scanner.scan(/ *, */)
scanner.skip(/[ \t]*,[ \t]*/)
value = scanner.scan(EXT)
end
offers
Expand Down
12 changes: 12 additions & 0 deletions spec/websocket/extensions/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def parse(string)
]
end

it "parses HTTP whitespace around delimiters" do
expect(parse " \ta \t; mode \t= compress \t, other \t").to eq [
{ :name => "a", :params => { "mode" => "compress" } },
{ :name => "other", :params => {} }
]
end

it "rejects input outside the complete header value" do
expect { parse "\na" }.to raise_error(WebSocket::Extensions::Parser::ParseError)
expect { parse "a\n" }.to raise_error(WebSocket::Extensions::Parser::ParseError)
end

it "parses two offers with no params" do
expect(parse 'a, b').to eq [
{ :name => "a", :params => {} }, { :name => "b", :params => {} }
Expand Down