Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 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 | |||||
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
9 | |||||
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 10 | #include <string> |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 11 | #include <vector> |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 12 | |
13 | #include "base/strings/string_piece.h" | ||||
14 | #include "net/base/net_export.h" | ||||
15 | #include "net/websockets/websocket_extension.h" | ||||
16 | |||||
17 | namespace net { | ||||
18 | |||||
19 | class NET_EXPORT_PRIVATE WebSocketExtensionParser { | ||||
20 | public: | ||||
21 | WebSocketExtensionParser(); | ||||
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 22 | |
23 | WebSocketExtensionParser(const WebSocketExtensionParser&) = delete; | ||||
24 | WebSocketExtensionParser& operator=(const WebSocketExtensionParser&) = delete; | ||||
25 | |||||
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 26 | ~WebSocketExtensionParser(); |
27 | |||||
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 28 | // 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. | ||||
tyoshino | d90d293 | 2015-04-13 16:53:32 | [diff] [blame] | 32 | // |
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] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 37 | } |
38 | |||||
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 39 | // Returns the result of the last Parse() method call. |
40 | const std::vector<WebSocketExtension>& extensions() const { | ||||
41 | return extensions_; | ||||
42 | } | ||||
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 43 | |
44 | private: | ||||
Daniel Cheng | 76bb1e00 | 2022-01-14 01:34:42 | [diff] [blame] | 45 | [[nodiscard]] bool Consume(char c); |
46 | [[nodiscard]] bool ConsumeExtension(WebSocketExtension* extension); | ||||
47 | [[nodiscard]] bool ConsumeExtensionParameter( | ||||
tyoshino | d90d293 | 2015-04-13 16:53:32 | [diff] [blame] | 48 | WebSocketExtension::Parameter* parameter); |
Daniel Cheng | 76bb1e00 | 2022-01-14 01:34:42 | [diff] [blame] | 49 | [[nodiscard]] bool ConsumeToken(base::StringPiece* token); |
50 | [[nodiscard]] bool ConsumeQuotedToken(std::string* token); | ||||
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 51 | void ConsumeSpaces(); |
Daniel Cheng | 76bb1e00 | 2022-01-14 01:34:42 | [diff] [blame] | 52 | [[nodiscard]] bool Lookahead(char c); |
53 | [[nodiscard]] bool ConsumeIfMatch(char c); | ||||
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 54 | |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 55 | // The current position in the input string. |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 56 | const char* current_; |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 57 | // The pointer of the end of the input string. |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 58 | const char* end_; |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 59 | std::vector<WebSocketExtension> extensions_; |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 60 | }; |
61 | |||||
62 | } // namespace net | ||||
63 | |||||
64 | #endif // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_ |