1 // Copyright 2013 The Chromium Authors 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 <stddef.h> 9 10 #include <string> 11 #include <string_view> 12 #include <vector> 13 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(); 22 23 WebSocketExtensionParser(const WebSocketExtensionParser&) = delete; 24 WebSocketExtensionParser& operator=(const WebSocketExtensionParser&) = delete; 25 26 ~WebSocketExtensionParser(); 27 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. 32 // 33 // Returns true if the method was successful (no syntax error was found). 34 bool Parse(const char* data, size_t size); Parse(std::string_view data)35 bool Parse(std::string_view data) { return Parse(data.data(), data.size()); } 36 37 // Returns the result of the last Parse() method call. extensions()38 const std::vector<WebSocketExtension>& extensions() const { 39 return extensions_; 40 } 41 42 private: 43 [[nodiscard]] bool Consume(char c); 44 [[nodiscard]] bool ConsumeExtension(WebSocketExtension* extension); 45 [[nodiscard]] bool ConsumeExtensionParameter( 46 WebSocketExtension::Parameter* parameter); 47 [[nodiscard]] bool ConsumeToken(std::string_view* token); 48 [[nodiscard]] bool ConsumeQuotedToken(std::string* token); 49 void ConsumeSpaces(); 50 [[nodiscard]] bool Lookahead(char c); 51 [[nodiscard]] bool ConsumeIfMatch(char c); 52 53 // The current position in the input string. 54 const char* current_; 55 // The pointer of the end of the input string. 56 const char* end_; 57 std::vector<WebSocketExtension> extensions_; 58 }; 59 60 } // namespace net 61 62 #endif // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_ 63