proxygen
HTTPCommonHeaders.template.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <cstdint>
13 #include <string>
14 
16 
17 namespace proxygen {
18 
23  // code reserved to indicate the absence of an HTTP header
24  HTTP_HEADER_NONE = 0,
25  // code for any HTTP header name not in the list of common headers
27 
28  /* the following is a placeholder for the build script to generate a list
29  * of enum values from the list in HTTPCommonHeaders.txt
30  *
31  * enum name of Some-Header is HTTP_HEADER_SOME_HEADER,
32  * so an example fragment of the generated list could be:
33  * ...
34  * HTTP_HEADER_WARNING = 65,
35  * HTTP_HEADER_WWW_AUTHENTICATE = 66,
36  * HTTP_HEADER_X_BACKEND = 67,
37  * HTTP_HEADER_X_BLOCKID = 68,
38  * ...
39  */
40 %%%%%
41 
42 };
43 
45 
47  TABLE_CAMELCASE = 0,
48  TABLE_LOWERCASE = 1,
49 };
50 
51 class HTTPCommonHeaders {
52  public:
53  // Perfect hash function to match common HTTP header names
54  FB_EXPORT static HTTPHeaderCode hash(const char* name, size_t len);
55 
56  FB_EXPORT inline static HTTPHeaderCode hash(const std::string& name) {
57  return hash(name.data(), name.length());
58  }
59 
61 $$$$$
62 
65 
68  return getPointerToCommonHeaderTable(type) + code;
69  }
70 
71  inline static bool isHeaderNameFromTable(const std::string* headerName,
73  return getHeaderCodeFromTableCommonHeaderName(headerName, type) >=
75  }
76 
77  // This method supplements hash(). If dealing with string pointers, some
78  // pointing to entries in the the common header name table and some not, this
79  // method can be used in place of hash to reverse map a string from the common
80  // header name table to its HTTPHeaderCode
82  const std::string* headerName, HTTPCommonHeaderTableType type) {
83  if (headerName == nullptr) {
84  return HTTP_HEADER_NONE;
85  } else {
86  auto diff = headerName - getPointerToCommonHeaderTable(type);
87  if (diff >= HTTPHeaderCodeCommonOffset && diff < (long)num_header_codes) {
88  return static_cast<HTTPHeaderCode>(diff);
89  } else {
90  return HTTP_HEADER_OTHER;
91  }
92  }
93  }
94 
95 };
96 
97 } // proxygen
#define FB_EXPORT
Definition: Export.h:26
static FB_EXPORT HTTPHeaderCode hash(const std::string &name)
static FB_EXPORT std::string * initHeaderNames(HTTPCommonHeaderTableType type)
PskType type
static HTTPHeaderCode getHeaderCodeFromTableCommonHeaderName(const std::string *headerName, HTTPCommonHeaderTableType type)
const char * name
Definition: http_parser.c:437
static bool isHeaderNameFromTable(const std::string *headerName, HTTPCommonHeaderTableType type)
static const std::string * getPointerToHeaderName(HTTPHeaderCode code, HTTPCommonHeaderTableType type=TABLE_CAMELCASE)
uint64_t diff(uint64_t a, uint64_t b)
Definition: FutexTest.cpp:135
static constexpr uint64_t num_header_codes
const char * string
Definition: Conv.cpp:212
static const std::string * getPointerToCommonHeaderTable(HTTPCommonHeaderTableType type)
const uint8_t HTTPHeaderCodeCommonOffset
static FB_EXPORT HTTPHeaderCode hash(const char *name, size_t len)