balsa_headers_token_utils.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
13e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org// Copyright 2013 The Chromium Authors. All rights reserved.
23e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org// Use of this source code is governed by a BSD-style license that can be
33e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org// found in the LICENSE file.
43e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org
53e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org// Utility class that performs basic operations on header value tokens: parsing
63e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org// them out, checking for presense of certain tokens, and removing them.
73e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org
83e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org#ifndef NET_TOOLS_BALSA_BALSA_HEADERS_TOKEN_UTILS_H_
93e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org#define NET_TOOLS_BALSA_BALSA_HEADERS_TOKEN_UTILS_H_
103e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org
113e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org#include "base/strings/string_piece.h"
123e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org#include "net/tools/balsa/balsa_headers.h"
133e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org
143e87580939cb78c5802369f723680d4a16cc2902ager@chromium.orgnamespace net {
153e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org
163e87580939cb78c5802369f723680d4a16cc2902ager@chromium.orgclass BalsaHeadersTokenUtils {
173e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org public:
183e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // All the functions below respect multiple header lines with the same key.
193e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org
203e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // Checks whether the last header token matches a given value. Useful to
213e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // check the outer-most content or transfer-encoding, for example. In the
223e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // presence of multiple header lines with given key, the last token of the
233e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // last line is compared.
243e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  static bool CheckHeaderForLastToken(const BalsaHeaders& headers,
253e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org                                      const base::StringPiece& key,
263e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org                                      const base::StringPiece& token);
273e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org
283e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // Tokenizes header value for a given key. In the presence of multiple lines
293e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // with that key, all of them will be tokenized and tokens will be added to
303e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  // the list in the order in which they are encountered.
313e87580939cb78c5802369f723680d4a16cc2902ager@chromium.org  static void TokenizeHeaderValue(const BalsaHeaders& headers,
32                                  const base::StringPiece& key,
33                                  BalsaHeaders::HeaderTokenList* tokens);
34
35  // Removes the last token from the header value. In the presence of multiple
36  // header lines with given key, will remove the last token of the last line.
37  // Can be useful if the last encoding has to be removed.
38  static void RemoveLastTokenFromHeaderValue(const base::StringPiece& key,
39                                             BalsaHeaders* headers);
40
41  // Given a pointer to the beginning and the end of the header value
42  // in some buffer, populates tokens list with beginning and end indices
43  // of all tokens present in the value string.
44  static void ParseTokenList(const char* start,
45                             const char* end,
46                             BalsaHeaders::HeaderTokenList* tokens);
47
48 private:
49  // Helper function to tokenize a header line once we have its description.
50  static void TokenizeHeaderLine(
51      const BalsaHeaders& headers,
52      const BalsaHeaders::HeaderLineDescription& line,
53      BalsaHeaders::HeaderTokenList* tokens);
54
55  BalsaHeadersTokenUtils();  // Prohibit instantiation
56};
57
58}  // namespace net
59
60#endif  // NET_TOOLS_BALSA_BALSA_HEADERS_TOKEN_UTILS_H_
61
62