websocket_extension_parser.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // 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. #ifndef NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_
  5. #define NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/strings/string_piece.h"
  10. #include "net/base/net_export.h"
  11. #include "net/websockets/websocket_extension.h"
  12. namespace net {
  13. class NET_EXPORT_PRIVATE WebSocketExtensionParser {
  14. public:
  15. WebSocketExtensionParser();
  16. WebSocketExtensionParser(const WebSocketExtensionParser&) = delete;
  17. WebSocketExtensionParser& operator=(const WebSocketExtensionParser&) = delete;
  18. ~WebSocketExtensionParser();
  19. // Parses the given string as a Sec-WebSocket-Extensions header value.
  20. //
  21. // There must be no newline characters in the input. LWS-concatenation must
  22. // have already been done before calling this method.
  23. //
  24. // Returns true if the method was successful (no syntax error was found).
  25. bool Parse(const char* data, size_t size);
  26. bool Parse(const std::string& data) {
  27. return Parse(data.data(), data.size());
  28. }
  29. // Returns the result of the last Parse() method call.
  30. const std::vector<WebSocketExtension>& extensions() const {
  31. return extensions_;
  32. }
  33. private:
  34. [[nodiscard]] bool Consume(char c);
  35. [[nodiscard]] bool ConsumeExtension(WebSocketExtension* extension);
  36. [[nodiscard]] bool ConsumeExtensionParameter(
  37. WebSocketExtension::Parameter* parameter);
  38. [[nodiscard]] bool ConsumeToken(base::StringPiece* token);
  39. [[nodiscard]] bool ConsumeQuotedToken(std::string* token);
  40. void ConsumeSpaces();
  41. [[nodiscard]] bool Lookahead(char c);
  42. [[nodiscard]] bool ConsumeIfMatch(char c);
  43. // The current position in the input string.
  44. const char* current_;
  45. // The pointer of the end of the input string.
  46. const char* end_;
  47. std::vector<WebSocketExtension> extensions_;
  48. };
  49. } // namespace net
  50. #endif // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_