1c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copyright (c) 2009 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Utility class that performs basic operations on header value tokens: parsing 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// them out, checking for presense of certain tokens, and removing them. 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef NET_TOOLS_FLIP_SERVER_BALSA_HEADERS_TOKEN_UTILS_H_ 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define NET_TOOLS_FLIP_SERVER_BALSA_HEADERS_TOKEN_UTILS_H_ 103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/tools/flip_server/balsa_headers.h" 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/string_piece.h" 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace net { 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass BalsaHeadersTokenUtils { 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // All the functions below respect multiple header lines with the same key. 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Checks whether the last header token matches a given value. Useful to 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // check the outer-most content or transfer-encoding, for example. In the 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // presence of multiple header lines with given key, the last token of the 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // last line is compared. 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static bool CheckHeaderForLastToken(const BalsaHeaders& headers, 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const base::StringPiece& key, 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const base::StringPiece& token); 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Tokenizes header value for a given key. In the presence of multiple lines 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // with that key, all of them will be tokenized and tokens will be added to 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // the list in the order in which they are encountered. 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static void TokenizeHeaderValue(const BalsaHeaders& headers, 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const base::StringPiece& key, 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott BalsaHeaders::HeaderTokenList* tokens); 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Removes the last token from the header value. In the presence of multiple 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // header lines with given key, will remove the last token of the last line. 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Can be useful if the last encoding has to be removed. 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static void RemoveLastTokenFromHeaderValue(const base::StringPiece& key, 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott BalsaHeaders* headers); 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Given a pointer to the beginning and the end of the header value 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // in some buffer, populates tokens list with beginning and end indices 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // of all tokens present in the value string. 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static void ParseTokenList(const char* start, 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char* end, 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott BalsaHeaders::HeaderTokenList* tokens); 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private: 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Helper function to tokenize a header line once we have its description. 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static void TokenizeHeaderLine( 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const BalsaHeaders& headers, 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const BalsaHeaders::HeaderLineDescription& line, 54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott BalsaHeaders::HeaderTokenList* tokens); 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott BalsaHeadersTokenUtils(); // Prohibit instantiation 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace net 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // NET_TOOLS_FLIP_SERVER_BALSA_HEADERS_TOKEN_UTILS_H_ 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 63