[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 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 | |
| 5 | #ifndef NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_ |
| 7 | |
| 8 | #include <string> |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 9 | #include <vector> |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 10 | |
tyoshino | d90d293 | 2015-04-13 16:53:32 | [diff] [blame^] | 11 | #include "base/compiler_specific.h" |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
| 13 | #include "net/base/net_export.h" |
| 14 | #include "net/websockets/websocket_extension.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | class NET_EXPORT_PRIVATE WebSocketExtensionParser { |
| 19 | public: |
| 20 | WebSocketExtensionParser(); |
| 21 | ~WebSocketExtensionParser(); |
| 22 | |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 23 | // Parses the given string as a Sec-WebSocket-Extensions header value. |
| 24 | // |
| 25 | // There must be no newline characters in the input. LWS-concatenation must |
| 26 | // have already been done before calling this method. |
tyoshino | d90d293 | 2015-04-13 16:53:32 | [diff] [blame^] | 27 | // |
| 28 | // Returns true if the method was successful (no syntax error was found). |
| 29 | bool Parse(const char* data, size_t size); |
| 30 | bool Parse(const std::string& data) { |
| 31 | return Parse(data.data(), data.size()); |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 32 | } |
| 33 | |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 34 | // Returns the result of the last Parse() method call. |
| 35 | const std::vector<WebSocketExtension>& extensions() const { |
| 36 | return extensions_; |
| 37 | } |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 38 | |
| 39 | private: |
tyoshino | d90d293 | 2015-04-13 16:53:32 | [diff] [blame^] | 40 | WARN_UNUSED_RESULT bool Consume(char c); |
| 41 | WARN_UNUSED_RESULT bool ConsumeExtension(WebSocketExtension* extension); |
| 42 | WARN_UNUSED_RESULT bool ConsumeExtensionParameter( |
| 43 | WebSocketExtension::Parameter* parameter); |
| 44 | WARN_UNUSED_RESULT bool ConsumeToken(base::StringPiece* token); |
| 45 | WARN_UNUSED_RESULT bool ConsumeQuotedToken(std::string* token); |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 46 | void ConsumeSpaces(); |
tyoshino | d90d293 | 2015-04-13 16:53:32 | [diff] [blame^] | 47 | WARN_UNUSED_RESULT bool Lookahead(char c); |
| 48 | WARN_UNUSED_RESULT bool ConsumeIfMatch(char c); |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 49 | size_t UnconsumedBytes() const { return end_ - current_; } |
| 50 | |
| 51 | static bool IsControl(char c); |
| 52 | static bool IsSeparator(char c); |
| 53 | |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 54 | // The current position in the input string. |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 55 | const char* current_; |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 56 | // The pointer of the end of the input string. |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 57 | const char* end_; |
tyoshino | 38ee68c | 2015-04-01 05:52:52 | [diff] [blame] | 58 | std::vector<WebSocketExtension> extensions_; |
[email protected] | d28e67a | 2013-09-19 10:31:43 | [diff] [blame] | 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(WebSocketExtensionParser); |
| 61 | }; |
| 62 | |
| 63 | } // namespace net |
| 64 | |
| 65 | #endif // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_ |