14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Utility class that performs basic operations on header value tokens: parsing
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// them out, checking for presense of certain tokens, and removing them.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
84e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifndef NET_TOOLS_BALSA_BALSA_HEADERS_TOKEN_UTILS_H_
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define NET_TOOLS_BALSA_BALSA_HEADERS_TOKEN_UTILS_H_
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h"
124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/tools/balsa/balsa_headers.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BalsaHeadersTokenUtils {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // All the functions below respect multiple header lines with the same key.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Checks whether the last header token matches a given value. Useful to
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // check the outer-most content or transfer-encoding, for example. In the
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // presence of multiple header lines with given key, the last token of the
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // last line is compared.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool CheckHeaderForLastToken(const BalsaHeaders& headers,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const base::StringPiece& key,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const base::StringPiece& token);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tokenizes header value for a given key. In the presence of multiple lines
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with that key, all of them will be tokenized and tokens will be added to
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the list in the order in which they are encountered.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void TokenizeHeaderValue(const BalsaHeaders& headers,
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const base::StringPiece& key,
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  BalsaHeaders::HeaderTokenList* tokens);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the last token from the header value. In the presence of multiple
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // header lines with given key, will remove the last token of the last line.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Can be useful if the last encoding has to be removed.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void RemoveLastTokenFromHeaderValue(const base::StringPiece& key,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             BalsaHeaders* headers);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Given a pointer to the beginning and the end of the header value
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in some buffer, populates tokens list with beginning and end indices
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of all tokens present in the value string.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void ParseTokenList(const char* start,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const char* end,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             BalsaHeaders::HeaderTokenList* tokens);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function to tokenize a header line once we have its description.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void TokenizeHeaderLine(
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const BalsaHeaders& headers,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const BalsaHeaders::HeaderLineDescription& line,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BalsaHeaders::HeaderTokenList* tokens);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BalsaHeadersTokenUtils();  // Prohibit instantiation
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif  // NET_TOOLS_BALSA_BALSA_HEADERS_TOKEN_UTILS_H_
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
62