proxygen
|
#include <HTTPHeaders.h>
Classes | |
struct | HTTPHeaderName |
Public Types | |
using | headers_initializer_list = std::initializer_list< std::pair< HTTPHeaderName, folly::StringPiece >> |
Static Public Member Functions | |
static std::bitset< 256 > & | perHopHeaderCodes () |
Static Public Attributes | |
static const std::string | COMBINE_SEPARATOR = ", " |
Private Member Functions | |
bool | transferHeaderIfPresent (folly::StringPiece name, HTTPHeaders &dest) |
void | disposeOfHeaderNames () |
Private Attributes | |
folly::fbvector< HTTPHeaderCode > | codes_ |
folly::fbvector< const std::string * > | headerNames_ |
folly::fbvector< std::string > | headerValues_ |
size_t | deletedCount_ |
Static Private Attributes | |
static const size_t | kInitialVectorReserve = 16 |
A collection of HTTP headers.
This is broken out from HTTPMessage, as it's convenient for other things to be able to use collections of HTTP headers that are easy to work with. The structure is optimized for real-life header collection sizes.
Headers are stored as Name/Value pairs, in the order they are received on the wire. We hash the names of all common HTTP headers (using a static perfect hash function generated using gperf from HTTPCommonHeaders.gperf) into 1-byte hashes (we call them "codes") and only store these. We search them using memchr, which has an x86_64 assembly implementation with complexity O(n/16) ;)
Instead of creating strings with header names, we point to a static array of strings in HTTPCommonHeaders. If the header name is not in our set of common header names (this is considered unlikely, because we intend this set to be very complete), then we create a new string with its name (we own that pointer then). For such headers, we store the code HTTP_HEADER_OTHER.
The code HTTP_HEADER_NONE signifies a header that has been removed.
Most methods which take a header name have two versions: one accepting a string, and one accepting a code. It is recommended to use the latter if possible, as in: headers.add(HTTP_HEADER_LOCATION, location); rather than: headers.add("Location", location);
Definition at line 68 of file HTTPHeaders.h.
using proxygen::HTTPHeaders::headers_initializer_list = std::initializer_list< std::pair<HTTPHeaderName,folly::StringPiece>> |
Definition at line 86 of file HTTPHeaders.h.
proxygen::HTTPHeaders::HTTPHeaders | ( | ) |
Definition at line 45 of file HTTPHeaders.cpp.
References codes_, headerNames_, headerValues_, kInitialVectorReserve, and folly::fbvector< T, Allocator >::reserve().
proxygen::HTTPHeaders::~HTTPHeaders | ( | ) |
Definition at line 157 of file HTTPHeaders.cpp.
References disposeOfHeaderNames().
proxygen::HTTPHeaders::HTTPHeaders | ( | const HTTPHeaders & | hdrs | ) |
Definition at line 161 of file HTTPHeaders.cpp.
References codes_, headerNames_, proxygen::HTTP_HEADER_OTHER, i, folly::fbvector< T, Allocator >::size(), and string.
|
noexcept |
Definition at line 173 of file HTTPHeaders.cpp.
void proxygen::HTTPHeaders::add | ( | folly::StringPiece | name, |
folly::StringPiece | value | ||
) |
Add the header 'name' with value 'value'; if other instances of this header name exist, they will be retained.
Definition at line 52 of file HTTPHeaders.cpp.
References codes_, folly::Range< Iter >::data(), folly::fbvector< T, Allocator >::emplace_back(), proxygen::HTTPCommonHeaders::getPointerToHeaderName(), proxygen::HTTPCommonHeaders::hash(), headerNames_, headerValues_, proxygen::HTTP_HEADER_OTHER, folly::fbvector< T, Allocator >::push_back(), folly::Range< Iter >::size(), and string.
Referenced by add(), proxygen::HTTPMessage::constructDirectResponse(), TestPriorityMapBuilder::createVirtualStreams(), CurlService::CurlClient::CurlClient(), proxygen::HTTPMessage::ensureHostHeader(), proxygen::getBigGetRequest(), proxygen::compress::SimStreamingCallback::onHeader(), proxygen::HTTPDirectResponseHandler::onHeadersComplete(), CurlService::CurlClient::parseHeaders(), proxygen::SPDYCodec::parseHeaders(), proxygen::HTTP1xCodec::pushHeaderNameAndValue(), rawAdd(), proxygen::HTTPHandlerBase::sendChunkedReplyWithBody(), proxygen::HTTPHandlerBase::sendHeaders(), proxygen::MockHTTPPushHandler::sendPushHeaders(), proxygen::HTTPHandlerBase::sendReplyWithBody(), CurlService::CurlClient::sendRequest(), set(), proxygen::HTTPRequestVerifier::setAuthority(), TEST(), TEST_F(), TEST_P(), and HTTP2CodecTest::testHeaderListSize().
void proxygen::HTTPHeaders::add | ( | folly::StringPiece | name, |
T && | value | ||
) |
Definition at line 297 of file HTTPHeaders.h.
References codes_, folly::Range< Iter >::data(), folly::fbvector< T, Allocator >::emplace_back(), proxygen::HTTPCommonHeaders::getPointerToHeaderName(), proxygen::HTTPCommonHeaders::hash(), headerNames_, headerValues_, proxygen::HTTP_HEADER_OTHER, folly::fbvector< T, Allocator >::push_back(), folly::Range< Iter >::size(), string, and value.
void proxygen::HTTPHeaders::add | ( | HTTPHeaderCode | code, |
T && | value | ||
) |
Definition at line 308 of file HTTPHeaders.h.
References codes_, folly::fbvector< T, Allocator >::emplace_back(), proxygen::HTTPCommonHeaders::getPointerToHeaderName(), headerNames_, headerValues_, folly::fbvector< T, Allocator >::push_back(), and value.
void proxygen::HTTPHeaders::add | ( | HTTPHeaders::headers_initializer_list | l | ) |
Definition at line 62 of file HTTPHeaders.cpp.
References add(), proxygen::HTTPHeaders::HTTPHeaderName::CODE, and string.
void proxygen::HTTPHeaders::addFromCodec | ( | const char * | str, |
size_t | len, | ||
std::string && | value | ||
) |
Definition at line 77 of file HTTPHeaders.cpp.
References codes_, folly::fbvector< T, Allocator >::emplace_back(), proxygen::HTTPCommonHeaders::getPointerToHeaderName(), proxygen::HTTPCommonHeaders::hash(), headerNames_, headerValues_, proxygen::HTTP_HEADER_OTHER, folly::gen::move, folly::fbvector< T, Allocator >::push_back(), and value.
Referenced by proxygen::HTTP1xCodec::pushHeaderNameAndValue().
std::string proxygen::HTTPHeaders::combine | ( | const T & | header, |
const std::string & | separator = COMBINE_SEPARATOR |
||
) | const |
combine all the value for this header into a string
Definition at line 382 of file HTTPHeaders.h.
References forEachValueOfHeader(), string, and value.
Referenced by proxygen::HeaderDecodeInfo::onHeadersComplete(), proxygen::compress::SimStreamingCallback::onHeadersComplete(), rawExists(), and TEST().
void proxygen::HTTPHeaders::copyTo | ( | HTTPHeaders & | hdrs | ) | const |
Copy all headers from this to hdrs.
Definition at line 312 of file HTTPHeaders.cpp.
References codes_, headerNames_, headerValues_, proxygen::HTTP_HEADER_NONE, proxygen::HTTP_HEADER_OTHER, i, folly::fbvector< T, Allocator >::push_back(), and folly::fbvector< T, Allocator >::size().
Referenced by rawRemove().
|
private |
Definition at line 149 of file HTTPHeaders.cpp.
References codes_, headerNames_, proxygen::HTTP_HEADER_OTHER, i, and folly::fbvector< T, Allocator >::size().
Referenced by operator=(), removeAll(), and ~HTTPHeaders().
bool proxygen::HTTPHeaders::exists | ( | folly::StringPiece | name | ) | const |
Do we have an instance of the given header?
Definition at line 86 of file HTTPHeaders.cpp.
References folly::Range< Iter >::data(), proxygen::HTTPCommonHeaders::hash(), proxygen::HTTP_HEADER_OTHER, ITERATE_OVER_STRINGS, and folly::Range< Iter >::size().
Referenced by proxygen::RFC2616::bodyImplied(), proxygen::HTTPMessage::constructDirectResponse(), proxygen::HTTPMessage::ensureHostHeader(), proxygen::SPDYCodec::parseHeaders(), rawExists(), rawSet(), proxygen::SPDYCodec::serializeRequestHeaders(), proxygen::SPDYCodec::serializeResponseHeaders(), and TEST_F().
bool proxygen::HTTPHeaders::exists | ( | HTTPHeaderCode | code | ) | const |
Definition at line 97 of file HTTPHeaders.cpp.
References codes_, folly::fbvector< T, Allocator >::data(), and folly::fbvector< T, Allocator >::size().
|
inline |
Process the list of all headers, in the order that they were seen: for each header:value pair, the function/functor/lambda-expression given as the second parameter will be executed. It should take two const string & parameters and return void. Example use: hdrs.forEach([&] (const string& header, const string& val) { std::cout << header << ": " << val; });
Definition at line 337 of file HTTPHeaders.h.
References codes_, headerNames_, headerValues_, proxygen::HTTP_HEADER_NONE, i, and folly::fbvector< T, Allocator >::size().
Referenced by proxygen::HTTPArchive::convertToHPACK(), CurlService::CurlClient::CurlClient(), proxygen::HTTPMessage::describe(), proxygen::HTTP2Codec::encodeHeaders(), proxygen::HTTP1xCodec::generateTrailers(), proxygen::HTTPArchive::getSize(), proxygen::HasHTTPHeaderMatcherImpl::MatchAndExplain(), and rawExists().
|
inline |
Process the ordered list of values for the given header name: for each value, the function/functor/lambda-expression given as the second parameter will be executed. It should take one const string & parameter and return bool (false to keep processing, true to stop it). Example use: hdrs.forEachValueOfHeader("someheader", [&] (const string& val) { std::cout << val; return false; }); This method returns true if processing was stopped (by func returning true), and false otherwise.
Definition at line 355 of file HTTPHeaders.h.
References folly::Range< Iter >::data(), proxygen::HTTPCommonHeaders::hash(), headerValues_, proxygen::HTTP_HEADER_OTHER, ITERATE_OVER_STRINGS, and folly::Range< Iter >::size().
Referenced by combine(), proxygen::HTTPMessage::doHeaderTokenCheck(), getNumberOfValues(), getSingleOrEmpty(), proxygen::HTTPMessage::parseCookies(), rawGet(), and stripPerHopHeaders().
|
inline |
Definition at line 371 of file HTTPHeaders.h.
References headerValues_, and ITERATE_OVER_CODES.
|
inline |
Process the list of all headers, in the order that they were seen: for each header:value pair, the function/functor/lambda-expression given as the second parameter will be executed. It should take one HTTPHeaderCode (code) parameter, two const string & parameters and return void. Example use: hdrs.forEachWithCode([&] (HTTPHeaderCode code, const string& header, const string& val) { std::cout << header << "(" << code << "): " << val; });
Definition at line 346 of file HTTPHeaders.h.
References codes_, headerNames_, headerValues_, proxygen::HTTP_HEADER_NONE, i, and folly::fbvector< T, Allocator >::size().
Referenced by proxygen::CodecUtil::appendHeaders(), proxygen::SPDYCodec::encodeHeaders(), proxygen::HTTP1xCodec::generateHeader(), proxygen::compress::prepareMessageForCompression(), and rawExists().
size_t proxygen::HTTPHeaders::getNumberOfValues | ( | HTTPHeaderCode | code | ) | const |
Get the number of values corresponding to a given header name.
Definition at line 104 of file HTTPHeaders.cpp.
References count, and ITERATE_OVER_CODES.
Referenced by proxygen::HTTP2Codec::onIngressUpgradeMessage(), rawGet(), CurlService::CurlClient::sendRequest(), TEST(), and TEST_F().
size_t proxygen::HTTPHeaders::getNumberOfValues | ( | folly::StringPiece | name | ) | const |
Definition at line 113 of file HTTPHeaders.cpp.
References count, and forEachValueOfHeader().
const std::string & proxygen::HTTPHeaders::getSingleOrEmpty | ( | const T & | nameOrCode | ) | const |
Returns the value of the header if it's found in the message and is the only value under the given name. If either of these is violated, returns empty_string.
Definition at line 420 of file HTTPHeaders.h.
References proxygen::empty_string, forEachValueOfHeader(), string, and value.
Referenced by HTTPDownstreamTest< SPDY3_1CodecPair >::expect101(), PushService::generateUrl(), CurlService::CurlClient::getServerName(), proxygen::HTTP2Codec::onIngressUpgradeMessage(), proxygen::SPDYCodec::parseHeaders(), proxygen::compress::prepareMessageForCompression(), proxygen::CodecUtil::prepareMessageForCompression(), proxygen::HTTPMessage::processMaxForwards(), rawExists(), rawGet(), proxygen::HTTPTransaction::sendHeadersWithOptionalEOM(), proxygen::SPDYCodec::serializeRequestHeaders(), TEST(), and TEST_F().
HTTPHeaders & proxygen::HTTPHeaders::operator= | ( | const HTTPHeaders & | hdrs | ) |
Definition at line 181 of file HTTPHeaders.cpp.
References codes_, deletedCount_, disposeOfHeaderNames(), headerNames_, headerValues_, proxygen::HTTP_HEADER_OTHER, i, folly::fbvector< T, Allocator >::size(), and string.
HTTPHeaders & proxygen::HTTPHeaders::operator= | ( | HTTPHeaders && | hdrs | ) |
Definition at line 197 of file HTTPHeaders.cpp.
References codes_, deletedCount_, headerNames_, headerValues_, and folly::gen::move.
|
static |
Determines whether header with a given code is a per-hop header, which should be stripped by stripPerHopHeaders().
Definition at line 26 of file HTTPHeaders.cpp.
References proxygen::HTTP_HEADER_CONNECTION, proxygen::HTTP_HEADER_KEEP_ALIVE, proxygen::HTTP_HEADER_PROXY_AUTHENTICATE, proxygen::HTTP_HEADER_PROXY_AUTHORIZATION, proxygen::HTTP_HEADER_PROXY_CONNECTION, proxygen::HTTP_HEADER_TE, proxygen::HTTP_HEADER_TRAILER, proxygen::HTTP_HEADER_TRANSFER_ENCODING, and proxygen::HTTP_HEADER_UPGRADE.
Referenced by rawRemove(), and stripPerHopHeaders().
void proxygen::HTTPHeaders::rawAdd | ( | const std::string & | name, |
const std::string & | value | ||
) |
Definition at line 73 of file HTTPHeaders.cpp.
References add().
|
inline |
Definition at line 137 of file HTTPHeaders.h.
References combine(), exists(), forEach(), forEachWithCode(), getSingleOrEmpty(), removeByPredicate(), string, and T.
|
inline |
Definition at line 199 of file HTTPHeaders.h.
References forEachValueOfHeader(), getNumberOfValues(), getSingleOrEmpty(), and name.
|
inline |
Definition at line 232 of file HTTPHeaders.h.
References copyTo(), name, perHopHeaderCodes(), removeAll(), size(), and stripPerHopHeaders().
|
inline |
bool proxygen::HTTPHeaders::remove | ( | folly::StringPiece | name | ) |
Remove all instances of the given header, returning true if anything was removed and false if this header didn't exist in our set.
Definition at line 122 of file HTTPHeaders.cpp.
References codes_, folly::Range< Iter >::data(), deletedCount_, proxygen::HTTPCommonHeaders::hash(), headerNames_, proxygen::HTTP_HEADER_NONE, proxygen::HTTP_HEADER_OTHER, ITERATE_OVER_STRINGS, and folly::Range< Iter >::size().
Referenced by TEST().
bool proxygen::HTTPHeaders::remove | ( | HTTPHeaderCode | code | ) |
Definition at line 139 of file HTTPHeaders.cpp.
References codes_, deletedCount_, proxygen::HTTP_HEADER_NONE, and ITERATE_OVER_CODES.
void proxygen::HTTPHeaders::removeAll | ( | ) |
Remove all headers.
Definition at line 210 of file HTTPHeaders.cpp.
References folly::fbvector< T, Allocator >::clear(), codes_, deletedCount_, disposeOfHeaderNames(), headerNames_, and headerValues_.
Referenced by rawRemove(), and proxygen::HTTPMessage::stripPerHopHeaders().
|
inline |
Process the list of all headers, in the order that they were seen: for each header:value pair, the function/functor/lambda-expression given as the parameter will be executed to determine whether the header should be removed. Example use:
hdrs.removeByPredicate([&] (HTTPHeaderCode code, const string& header, const string& val) { return boost::regex_match(header, "^X-Fb-.*"); });
return true only if one or more headers are removed.
Definition at line 398 of file HTTPHeaders.h.
References codes_, deletedCount_, headerNames_, headerValues_, proxygen::HTTP_HEADER_NONE, proxygen::HTTP_HEADER_OTHER, i, and folly::fbvector< T, Allocator >::size().
Referenced by rawExists().
|
inline |
For the header 'name', set its value to the single header 'value', removing any other instances of this header.
Definition at line 119 of file HTTPHeaders.h.
Referenced by proxygen::HTTPMessage::constructDirectResponse(), TestPriorityMapBuilder::createVirtualStreams(), doEmptyHeaderValueTest(), ZlibServerFilterTest::exercise_compression(), proxygen::getBigGetRequest(), proxygen::getChunkedPostRequest(), proxygen::getGetRequest(), proxygen::getPostRequest(), proxygen::getPubRequest(), proxygen::getResponse(), getSynStream(), proxygen::getUpgradeRequest(), proxygen::HeaderDecodeInfo::onHeadersComplete(), proxygen::compress::SimStreamingCallback::onHeadersComplete(), proxygen::HTTPDownstreamSession::onNativeProtocolUpgrade(), proxygen::SPDYCodec::parseHeaders(), proxygen::HTTPMessage::processMaxForwards(), proxygen::HTTP2Codec::requestUpgrade(), proxygen::MockHTTPPushHandler::sendPushHeaders(), TEST(), TEST_F(), and HTTPUpstreamTest< C >::testBasicRequestHttp10().
|
inline |
Definition at line 124 of file HTTPHeaders.h.
size_t proxygen::HTTPHeaders::size | ( | ) | const |
Get the total number of headers.
Definition at line 219 of file HTTPHeaders.cpp.
References codes_, deletedCount_, and folly::fbvector< T, Allocator >::size().
Referenced by proxygen::HTTPMessage::describe(), proxygen::HTTP2Codec::encodeHeaders(), HTTPDownstreamTest< SPDY3_1CodecPair >::expect101(), proxygen::HTTPArchive::fromFile(), proxygen::HTTPArchive::fromPublicFile(), rawRemove(), proxygen::SPDYCodec::serializeRequestHeaders(), proxygen::SPDYCodec::serializeResponseHeaders(), TEST(), and TEST_F().
void proxygen::HTTPHeaders::stripPerHopHeaders | ( | HTTPHeaders & | strippedHeaders | ) |
Remove per-hop-headers and headers named in the Connection header and place the value in strippedHeaders
Definition at line 253 of file HTTPHeaders.cpp.
References codes_, deletedCount_, forEachValueOfHeader(), headerNames_, headerValues_, proxygen::HTTP_HEADER_CONNECTION, proxygen::HTTP_HEADER_NONE, i, proxygen::isLWS(), perHopHeaderCodes(), folly::fbvector< T, Allocator >::push_back(), folly::fbvector< T, Allocator >::size(), and transferHeaderIfPresent().
Referenced by rawRemove(), and proxygen::HTTPMessage::stripPerHopHeaders().
|
private |
Moves the named header and values from this group to the destination group. No-op if the header doesn't exist. Returns true if header(s) were moved.
Definition at line 224 of file HTTPHeaders.cpp.
References codes_, folly::Range< Iter >::data(), deletedCount_, proxygen::HTTPCommonHeaders::hash(), headerNames_, headerValues_, proxygen::HTTP_HEADER_NONE, proxygen::HTTP_HEADER_OTHER, ITERATE_OVER_CODES, ITERATE_OVER_STRINGS, folly::fbvector< T, Allocator >::push_back(), and folly::Range< Iter >::size().
Referenced by stripPerHopHeaders().
|
private |
Definition at line 265 of file HTTPHeaders.h.
Referenced by add(), addFromCodec(), copyTo(), disposeOfHeaderNames(), exists(), forEach(), forEachWithCode(), HTTPHeaders(), operator=(), remove(), removeAll(), removeByPredicate(), size(), stripPerHopHeaders(), and transferHeaderIfPresent().
|
static |
Definition at line 92 of file HTTPHeaders.h.
|
private |
Definition at line 275 of file HTTPHeaders.h.
Referenced by operator=(), remove(), removeAll(), removeByPredicate(), size(), stripPerHopHeaders(), and transferHeaderIfPresent().
|
private |
Vector storing pointers to header names; we own those pointers which correspond to HTTP_HEADER_OTHER codes.
Definition at line 271 of file HTTPHeaders.h.
Referenced by add(), addFromCodec(), copyTo(), disposeOfHeaderNames(), forEach(), forEachWithCode(), HTTPHeaders(), operator=(), remove(), removeAll(), removeByPredicate(), stripPerHopHeaders(), and transferHeaderIfPresent().
|
private |
Definition at line 273 of file HTTPHeaders.h.
Referenced by add(), addFromCodec(), copyTo(), forEach(), forEachValueOfHeader(), forEachWithCode(), HTTPHeaders(), operator=(), removeAll(), removeByPredicate(), stripPerHopHeaders(), and transferHeaderIfPresent().
|
staticprivate |
The initial capacity of the three vectors, reserved right after construction.
Definition at line 281 of file HTTPHeaders.h.
Referenced by HTTPHeaders().