blob: d96e1019e00486bd1a4bb41298021f1ed3ca92ab [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2013 The Chromium Authors
[email protected]d28e67a2013-09-19 10:31:432// 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
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9
[email protected]d28e67a2013-09-19 10:31:4310#include <string>
tyoshino38ee68c2015-04-01 05:52:5211#include <vector>
[email protected]d28e67a2013-09-19 10:31:4312
13#include "base/strings/string_piece.h"
14#include "net/base/net_export.h"
15#include "net/websockets/websocket_extension.h"
16
17namespace net {
18
19class NET_EXPORT_PRIVATE WebSocketExtensionParser {
20 public:
21 WebSocketExtensionParser();
Peter Boström293b1342021-09-22 17:31:4322
23 WebSocketExtensionParser(const WebSocketExtensionParser&) = delete;
24 WebSocketExtensionParser& operator=(const WebSocketExtensionParser&) = delete;
25
[email protected]d28e67a2013-09-19 10:31:4326 ~WebSocketExtensionParser();
27
tyoshino38ee68c2015-04-01 05:52:5228 // Parses the given string as a Sec-WebSocket-Extensions header value.
29 //
30 // There must be no newline characters in the input. LWS-concatenation must
31 // have already been done before calling this method.
tyoshinod90d2932015-04-13 16:53:3232 //
33 // Returns true if the method was successful (no syntax error was found).
34 bool Parse(const char* data, size_t size);
35 bool Parse(const std::string& data) {
36 return Parse(data.data(), data.size());
[email protected]d28e67a2013-09-19 10:31:4337 }
38
tyoshino38ee68c2015-04-01 05:52:5239 // Returns the result of the last Parse() method call.
40 const std::vector<WebSocketExtension>& extensions() const {
41 return extensions_;
42 }
[email protected]d28e67a2013-09-19 10:31:4343
44 private:
Daniel Cheng76bb1e002022-01-14 01:34:4245 [[nodiscard]] bool Consume(char c);
46 [[nodiscard]] bool ConsumeExtension(WebSocketExtension* extension);
47 [[nodiscard]] bool ConsumeExtensionParameter(
tyoshinod90d2932015-04-13 16:53:3248 WebSocketExtension::Parameter* parameter);
Daniel Cheng76bb1e002022-01-14 01:34:4249 [[nodiscard]] bool ConsumeToken(base::StringPiece* token);
50 [[nodiscard]] bool ConsumeQuotedToken(std::string* token);
[email protected]d28e67a2013-09-19 10:31:4351 void ConsumeSpaces();
Daniel Cheng76bb1e002022-01-14 01:34:4252 [[nodiscard]] bool Lookahead(char c);
53 [[nodiscard]] bool ConsumeIfMatch(char c);
[email protected]d28e67a2013-09-19 10:31:4354
tyoshino38ee68c2015-04-01 05:52:5255 // The current position in the input string.
[email protected]d28e67a2013-09-19 10:31:4356 const char* current_;
tyoshino38ee68c2015-04-01 05:52:5257 // The pointer of the end of the input string.
[email protected]d28e67a2013-09-19 10:31:4358 const char* end_;
tyoshino38ee68c2015-04-01 05:52:5259 std::vector<WebSocketExtension> extensions_;
[email protected]d28e67a2013-09-19 10:31:4360};
61
62} // namespace net
63
64#endif // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_