1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/** @file
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
/**
An ATS Http header exists in a marshall buffer at a given location.
Unfortunately how that marshall buffer is created and how that
location is determined depends on where those buffers came from.
A TSHttpTxn manages the buffer itself and creates a location which
has to be managed.
A TSHttpParsed populates a created buffer that has had TSHttpHdrCreate
run against it which creates a location against it. End users
need to manage the created buffer, the location and invoke
TSHttpHdrDestroy.
*/
#include "ts/ts.h"
#include <string>
constexpr std::string_view SLICE_CRR_HEADER = {"Slice-Crr-Status"};
constexpr std::string_view SLICE_CRR_VAL = "1";
/**
Designed to be a cheap throwaway struct which allows a
consumer to make various calls to manipulate headers.
*/
struct HttpHeader {
TSMBuffer const m_buffer;
TSMLoc const m_lochdr;
explicit HttpHeader(TSMBuffer buffer, TSMLoc lochdr) : m_buffer(buffer), m_lochdr(lochdr) {}
bool
isValid() const
{
return nullptr != m_buffer && nullptr != m_lochdr;
}
int
byteSize() const
{
if (isValid()) {
return TSHttpHdrLengthGet(m_buffer, m_lochdr);
} else {
return 0;
}
}
// TS_HTTP_TYPE_UNKNOWN, TS_HTTP_TYPE_REQUEST, TS_HTTP_TYPE_RESPONSE
TSHttpType type() const;
TSHttpStatus status() const;
bool setStatus(TSHttpStatus const newstatus);
bool setUrl(TSMBuffer const bufurl, TSMLoc const locurl);
typedef char const *(*CharPtrGetFunc)(TSMBuffer, TSMLoc, int *);
// request method TS_HTTP_METHOD_*
char const *
method(int *const len = nullptr) const
{
return getCharPtr(TSHttpHdrMethodGet, len);
}
// request method version
int
version() const
{
return TSHttpHdrVersionGet(m_buffer, m_lochdr);
}
// Returns string representation of the url. Caller gets ownership!
char *urlString(int *const urllen) const;
// host
char const *
hostname(int *const len) const
{
return getCharPtr(TSHttpHdrHostGet, len);
}
// response reason
char const *
reason(int *const len) const
{
return getCharPtr(TSHttpHdrReasonGet, len);
}
bool setReason(char const *const valstr, int const vallen);
bool hasKey(char const *const key, int const keylen) const;
// returns false if header invalid or something went wrong with removal.
bool removeKey(char const *const key, int const keylen);
// retrieves header value as a char*
bool valueForKey(char const *const keystr, int const keylen,
char *const valstr, // <-- return string value
int *const vallen, // <-- pass in capacity, returns len of string
int const index = -1 // retrieves all values
) const;
/**
Sets or adds a key/value
*/
bool setKeyVal(char const *const key, int const keylen, char const *const val, int const vallen,
int const index = -1 // sets all values
);
// retrieves header value as a time_t
bool timeForKey(char const *const keystr, int const keylen, time_t *const timeval) const;
// sets header value as a time_t
bool setKeyTime(char const *const key, int const keylen, time_t const timeval);
/** dump header into provided char buffer
*/
std::string toString() const;
private:
/**
To be used with
TSHttpHdrMethodGet
TSHttpHdrHostGet
TSHttpHdrReasonGet
*/
char const *getCharPtr(CharPtrGetFunc func, int *const len) const;
};
struct TxnHdrMgr {
TxnHdrMgr(TxnHdrMgr const &) = delete;
TxnHdrMgr &operator=(TxnHdrMgr const &) = delete;
TSMBuffer m_buffer{nullptr};
TSMLoc m_lochdr{nullptr};
TxnHdrMgr() : m_buffer(nullptr), m_lochdr(nullptr) {}
~TxnHdrMgr()
{
if (nullptr != m_lochdr) {
TSHandleMLocRelease(m_buffer, TS_NULL_MLOC, m_lochdr);
}
}
typedef TSReturnCode (*HeaderGetFunc)(TSHttpTxn, TSMBuffer *, TSMLoc *);
/** use one of the following:
TSHttpTxnClientReqGet
TSHttpTxnClientRespGet
TSHttpTxnServerReqGet
TSHttpTxnServerRespGet
TSHttpTxnCachedReqGet
TSHttpTxnCachedRespGet
*/
bool
populateFrom(TSHttpTxn const &txnp, HeaderGetFunc const &func)
{
return TS_SUCCESS == func(txnp, &m_buffer, &m_lochdr);
}
bool
isValid() const
{
return nullptr != m_lochdr;
}
};
struct HdrMgr {
HdrMgr(HdrMgr const &) = delete;
HdrMgr &operator=(HdrMgr const &) = delete;
TSMBuffer m_buffer{nullptr};
TSMLoc m_lochdr{nullptr};
HdrMgr() : m_buffer(nullptr), m_lochdr(nullptr) {}
~HdrMgr()
{
if (nullptr != m_buffer) {
if (nullptr != m_lochdr) {
TSHttpHdrDestroy(m_buffer, m_lochdr);
TSHandleMLocRelease(m_buffer, TS_NULL_MLOC, m_lochdr);
}
TSMBufferDestroy(m_buffer);
}
}
void
resetHeader()
{
if (nullptr != m_buffer && nullptr != m_lochdr) {
TSHttpHdrDestroy(m_buffer, m_lochdr);
TSHandleMLocRelease(m_buffer, TS_NULL_MLOC, m_lochdr);
m_lochdr = nullptr;
}
}
typedef TSParseResult (*HeaderParseFunc)(TSHttpParser, TSMBuffer, TSMLoc, char const **, char const *);
/** Clear/create the parser before calling this and don't
use the parser on another header until done with this one.
use one of the following:
TSHttpHdrParseReq
TSHttpHdrParseResp
Call this multiple times if necessary.
*/
TSParseResult populateFrom(TSHttpParser const http_parser, TSIOBufferReader const reader, HeaderParseFunc const parsefunc,
int64_t *const consumed);
bool
isValid() const
{
return nullptr != m_lochdr;
}
};
|