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 <stddef.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "base/compiler_specific.h"
14 #include "base/macros.h"
15 #include "base/strings/string_piece.h"
16 #include "net/base/net_export.h"
17 #include "net/websockets/websocket_extension.h"
18 
19 namespace net {
20 
21 class NET_EXPORT_PRIVATE WebSocketExtensionParser {
22  public:
23   WebSocketExtensionParser();
24   ~WebSocketExtensionParser();
25 
26   // Parses the given string as a Sec-WebSocket-Extensions header value.
27   //
28   // There must be no newline characters in the input. LWS-concatenation must
29   // have already been done before calling this method.
30   //
31   // Returns true if the method was successful (no syntax error was found).
32   bool Parse(const char* data, size_t size);
Parse(const std::string & data)33   bool Parse(const std::string& data) {
34     return Parse(data.data(), data.size());
35   }
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   WARN_UNUSED_RESULT bool Consume(char c);
44   WARN_UNUSED_RESULT bool ConsumeExtension(WebSocketExtension* extension);
45   WARN_UNUSED_RESULT bool ConsumeExtensionParameter(
46       WebSocketExtension::Parameter* parameter);
47   WARN_UNUSED_RESULT bool ConsumeToken(base::StringPiece* token);
48   WARN_UNUSED_RESULT bool ConsumeQuotedToken(std::string* token);
49   void ConsumeSpaces();
50   WARN_UNUSED_RESULT bool Lookahead(char c);
51   WARN_UNUSED_RESULT 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   DISALLOW_COPY_AND_ASSIGN(WebSocketExtensionParser);
60 };
61 
62 }  // namespace net
63 
64 #endif  // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_
65