Home | History | Annotate | Download | only in websockets
      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>
      9 
     10 #include "base/strings/string_piece.h"
     11 #include "net/base/net_export.h"
     12 #include "net/websockets/websocket_extension.h"
     13 
     14 namespace net {
     15 
     16 class NET_EXPORT_PRIVATE WebSocketExtensionParser {
     17  public:
     18   WebSocketExtensionParser();
     19   ~WebSocketExtensionParser();
     20 
     21   // Parses the given string as a WebSocket extension header value.
     22   // This parser assumes some preprocesses are made.
     23   //  - The parser parses single extension at a time. This means that
     24   //    the parser parses |extension| in RFC6455 9.1, not |extension-list|.
     25   //  - There is no newline characters in the input. LWS-concatenation must
     26   //    have already been done.
     27   void Parse(const char* data, size_t size);
     28   void Parse(const std::string& data) {
     29     Parse(data.data(), data.size());
     30   }
     31 
     32   bool has_error() const { return has_error_; }
     33   const WebSocketExtension& extension() const { return extension_; }
     34 
     35  private:
     36   void Consume(char c);
     37   void ConsumeExtension(WebSocketExtension* extension);
     38   void ConsumeExtensionParameter(WebSocketExtension::Parameter* parameter);
     39   void ConsumeToken(base::StringPiece* token);
     40   void ConsumeQuotedToken(std::string* token);
     41   void ConsumeSpaces();
     42   bool Lookahead(char c);
     43   bool ConsumeIfMatch(char c);
     44   size_t UnconsumedBytes() const { return end_ - current_; }
     45 
     46   static bool IsControl(char c);
     47   static bool IsSeparator(char c);
     48 
     49   const char* current_;
     50   const char* end_;
     51   bool has_error_;
     52   WebSocketExtension extension_;
     53 
     54   DISALLOW_COPY_AND_ASSIGN(WebSocketExtensionParser);
     55 };
     56 
     57 }  // namespace net
     58 
     59 #endif  // NET_WEBSOCKETS_WEBSOCKET_EXTENSION_PARSER_H_
     60