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 <vector> 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(); 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(const std::string & data)35 bool Parse(const std::string& data) { 36 return Parse(data.data(), data.size()); 37 } 38 39 // Returns the result of the last Parse() method call. extensions()40 const std::vector<WebSocketExtension>& extensions() const { 41 return extensions_; 42 } 43 44 private: 45 [[nodiscard]] bool Consume(char c); 46 [[nodiscard]] bool ConsumeExtension(WebSocketExtension* extension); 47 [[nodiscard]] bool ConsumeExtensionParameter( 48 WebSocketExtension::Parameter* parameter); 49 [[nodiscard]] bool ConsumeToken(base::StringPiece* token); 50 [[nodiscard]] bool ConsumeQuotedToken(std::string* token); 51 void ConsumeSpaces(); 52 [[nodiscard]] bool Lookahead(char c); 53 [[nodiscard]] bool ConsumeIfMatch(char c); 54 55 // The current position in the input string. 56 const char* current_; 57 // The pointer of the end of the input string. 58 const char* end_; 59 std::vector<WebSocketExtension> extensions_; 60 }; 61 62 } // namespace net 63 64 #endif // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_ 65