blob: 007040de291408aa411fa24eb172a7db6c6246e7 [file] [log] [blame]
[email protected]d28e67a2013-09-19 10:31:431// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_
6#define NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_
7
8#include <string>
tyoshino38ee68c2015-04-01 05:52:529#include <vector>
[email protected]d28e67a2013-09-19 10:31:4310
tyoshinod90d2932015-04-13 16:53:3211#include "base/compiler_specific.h"
[email protected]d28e67a2013-09-19 10:31:4312#include "base/strings/string_piece.h"
13#include "net/base/net_export.h"
14#include "net/websockets/websocket_extension.h"
15
16namespace net {
17
18class NET_EXPORT_PRIVATE WebSocketExtensionParser {
19 public:
20 WebSocketExtensionParser();
21 ~WebSocketExtensionParser();
22
tyoshino38ee68c2015-04-01 05:52:5223 // Parses the given string as a Sec-WebSocket-Extensions header value.
24 //
25 // There must be no newline characters in the input. LWS-concatenation must
26 // have already been done before calling this method.
tyoshinod90d2932015-04-13 16:53:3227 //
28 // Returns true if the method was successful (no syntax error was found).
29 bool Parse(const char* data, size_t size);
30 bool Parse(const std::string& data) {
31 return Parse(data.data(), data.size());
[email protected]d28e67a2013-09-19 10:31:4332 }
33
tyoshino38ee68c2015-04-01 05:52:5234 // Returns the result of the last Parse() method call.
35 const std::vector<WebSocketExtension>& extensions() const {
36 return extensions_;
37 }
[email protected]d28e67a2013-09-19 10:31:4338
39 private:
tyoshinod90d2932015-04-13 16:53:3240 WARN_UNUSED_RESULT bool Consume(char c);
41 WARN_UNUSED_RESULT bool ConsumeExtension(WebSocketExtension* extension);
42 WARN_UNUSED_RESULT bool ConsumeExtensionParameter(
43 WebSocketExtension::Parameter* parameter);
44 WARN_UNUSED_RESULT bool ConsumeToken(base::StringPiece* token);
45 WARN_UNUSED_RESULT bool ConsumeQuotedToken(std::string* token);
[email protected]d28e67a2013-09-19 10:31:4346 void ConsumeSpaces();
tyoshinod90d2932015-04-13 16:53:3247 WARN_UNUSED_RESULT bool Lookahead(char c);
48 WARN_UNUSED_RESULT bool ConsumeIfMatch(char c);
[email protected]d28e67a2013-09-19 10:31:4349 size_t UnconsumedBytes() const { return end_ - current_; }
50
51 static bool IsControl(char c);
52 static bool IsSeparator(char c);
53
tyoshino38ee68c2015-04-01 05:52:5254 // The current position in the input string.
[email protected]d28e67a2013-09-19 10:31:4355 const char* current_;
tyoshino38ee68c2015-04-01 05:52:5256 // The pointer of the end of the input string.
[email protected]d28e67a2013-09-19 10:31:4357 const char* end_;
tyoshino38ee68c2015-04-01 05:52:5258 std::vector<WebSocketExtension> extensions_;
[email protected]d28e67a2013-09-19 10:31:4359
60 DISALLOW_COPY_AND_ASSIGN(WebSocketExtensionParser);
61};
62
63} // namespace net
64
65#endif // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_