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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
/**
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.
*/
/**
* @file Headers.h
*/
#pragma once
#include "tscpp/api/noncopyable.h"
#include <string>
namespace atscppapi
{
struct HeadersState;
struct HeaderFieldIteratorState;
struct HeaderFieldValueIteratorState;
class Request;
class ClientRequest;
class Response;
/**
* @brief A HeaderFieldName is a lightweight wrapper around a string that allows for case insensitive comparisons.
* Because header field names must be case insensitive this allows easy case insensitive comparisons of names.
*
*/
class HeaderFieldName
{
private:
std::string name_;
public:
typedef std::string::size_type size_type;
/**
* Constructor: build a new HeaderField name with the given string
*/
HeaderFieldName(const std::string &name);
/**
* std::string conversion
* @return a string which is this HeaderFieldName
*/
operator std::string();
/**
* const char * conversion
* @return a const char * which is this HeaderFieldName
*/
operator const char *();
/**
* @return the length of this HeaderFieldName
*/
size_type length();
/**
* @return a string which is this HeaderFieldName
*/
std::string str();
/**
* @return a const char * which points to the name of this HeaderFieldName
*/
const char *c_str();
/**
* Case insensitive comparison of this HeaderFieldName
* @return true if the two strings are equal.
*/
bool operator==(const char *field_name);
/**
* Case insensitive comparison of this HeaderFieldName
* @return true if the two strings are equal.
*/
bool operator==(const std::string &field_name);
/**
* Case insensitive comparison of this HeaderFieldName
* @return true if the two strings are not equal.
*/
bool operator!=(const char *field_name);
/**
* Case insensitive comparison of this HeaderFieldName
* @return true if the two strings are not equal.
*/
bool operator!=(const std::string &field_name);
};
class HeaderField;
/**
* @brief A header field value iterator iterates through all header fields.
*/
class header_field_value_iterator
{
private:
HeaderFieldValueIteratorState *state_;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = int;
/**
* Constructor for header_field_value_iterator, this shouldn't need to be used directly.
* @param bufp the TSMBuffer associated with the headers
* @param mloc the TSMLoc associated with the headers.
* @param field_loc the TSMLoc associated with the field.
* @param index the index of the value in the HeaderField
* @warning This shouldn't need to be used directly!
*/
header_field_value_iterator(void *bufp, void *hdr_loc, void *field_loc, int index);
/**
* Copy Constructor for header_field_value_iterator, this shouldn't need to be used directly.
* @param header_field_value_iterator an existing iterator to copy
* @warning This shouldn't need to be used directly!
*/
header_field_value_iterator(const header_field_value_iterator &it);
~header_field_value_iterator();
/**
* Dereference this iterator into a string (get the value pointed to by this iterator)
* @return a string which is the value pointed to by this iterator
*/
std::string operator*();
/**
* Advance the iterator to the next header field value
* @return a reference to a the next iterator
*/
header_field_value_iterator &operator++();
/**
* Advance the current iterator to the next header field
* @return a new iterator which points to the next element
*/
header_field_value_iterator operator++(int);
/**
* Compare two iterators returning true if they are equal
* @return true if two iterators are equal
*/
bool operator==(const header_field_value_iterator &rhs) const;
/**
* Compare two iterators returning true if they are NOT equal
* @return true if two iterators are not equal.
*/
bool operator!=(const header_field_value_iterator &rhs) const;
friend class HeaderField;
};
/**
* @brief A header field iterator is an iterator that dereferences to a HeaderField.
*/
class header_field_iterator
{
private:
HeaderFieldIteratorState *state_;
header_field_iterator(void *hdr_buf, void *hdr_loc, void *field_loc);
public:
using iterator_category = std::forward_iterator_tag;
using value_type = int;
~header_field_iterator();
/**
* Copy Constructor for header_field_iterator, this shouldn't need to be used directly.
* @param header_field_iterator: for constructing the iterator.
* @warning This shouldn't need to be used directly!
*/
header_field_iterator(const header_field_iterator &it);
header_field_iterator &operator=(const header_field_iterator &rhs);
/**
* Advance the iterator to the next header field
* @return a reference to a the next iterator
*/
header_field_iterator &operator++();
/**
* Advance the current iterator to the next header field
* @return a new iterator which points to the next element
*/
header_field_iterator operator++(int);
/**
* Advance the iterator to the next header field with the same name
* @return a reference to a the next iterator
*/
header_field_iterator &nextDup();
/**
* Comparison operator, compare two iterators
* @return true if the two iterators point to the same HeaderField
*/
bool operator==(const header_field_iterator &rhs) const;
/**
* Inequality Operator, compare two iterators
* @return false if the two iterators are the same.
*/
bool operator!=(const header_field_iterator &rhs) const;
/**
* Dereference an iterator
* @return a HeaderField pointed to by this iterator
*/
HeaderField operator*();
friend class HeaderField;
friend class Headers;
};
/**
* @brief A HeaderField is a class that contains the header field name and all of the values.
* @note You may have several HeaderFields with the same name for a given set of Headers.
*/
class HeaderField
{
private:
header_field_iterator iter_;
HeaderField(const header_field_iterator &iter) : iter_(iter) {}
public:
typedef unsigned int size_type;
typedef header_field_value_iterator iterator;
~HeaderField();
/**
* Get the size of the HeaderField, this is the number of values associated with the header field.
* @return the number of values in this HeaderField.
*/
size_type size() const;
/**
* Returns an iterator to the start of the values
* @return an iterator point to the start of this header field's values
*/
iterator begin();
/**
* Returns an iterator to the end of this header field's values.
* @return an iterator that points beyond the last element of the header field's values.
*/
iterator end();
/**
* Get the name of this HeaderField
* @return a HeaderFieldName object.
* @see HeaderFieldName which is a thin wrapper around a string to allow for case insensitive comparisons.
*/
HeaderFieldName name() const;
/**
* Join all the values of this HeaderField into a single string separated by the join string.
* @param an optional join string (defaults to ",")
* @return a string which is all of the joined values of this HeaderField
*/
std::string values(const char *join = ",");
/**
* Join all the values of this HeaderField into a single string separated by the join string.
* @param a join string
* @return a string which is all of the joined values of this HeaderField
*/
std::string values(const std::string &join);
/**
* Join all the values of this HeaderField into a single string separated by the join string.
* @param a optional join character
* @return a string which is all of the joined values of this HeaderField
*/
std::string values(const char join);
/**
* Check if this HeaderField is empty (no values).
* @return a boolean value representing whether or not the header field has values.
*/
bool empty();
/**
* Remove all values from this HeaderField
* @return true if the clear succeeds.
*/
bool clear();
/**
* Remove a single value from this HeaderField which is pointed to by the given iterator
* @param an iterator which points to a single HeaderField value.
* @return true if the header value was successfully erased.
*/
bool erase(const iterator &it);
/**
* Append a value or a separated list of values to this HeaderField
* @param a string containing the value(s).
* @return true if the values was appended.
*/
bool append(const std::string &value);
/**
* Append a value or a separated list of values to this HeaderField
* @param a string containing the value.
* @param the length of the value that is being appended.
* @return true if the values was appended.
*/
bool append(const char *value, int length = -1);
/**
* Change the name of this HeaderField to the given key.
* @param string - the new name of the header field
* @return true if the header field name was successfully changed.
*/
bool setName(const std::string &str);
/**
* Compares the name of the header field only (not the values).
* @note This is a case insensitive comparison.
* @return true if the name is equal (case insensitive comparison).
*/
bool operator==(const char *field_name) const;
/** Compares the name of the header field only (not the values).
* @note This is a case insensitive comparison.
* @return true if the name is equal (case insensitive comparison).
*/
bool operator==(const std::string &field_name) const;
/** Compares the name of the header field only (not the values).
* @note This is a case insensitive comparison.
* @return true if the name is NOT equal (case insensitive comparison).
*/
bool operator!=(const char *field_name) const;
/** Compares the name of the header field only (not the values).
* @note This is a case insensitive comparison.
* @return true if the name is NOT equal (case insensitive comparison).
*/
bool operator!=(const std::string &field_name) const;
/**
* Set the VALUES of the header field to the given value string
* @param string - the values to set on the current header field
* @return true if the value is successfully changed.
*/
bool operator=(const std::string &field_value);
/**
* Set the VALUES of the header field to the given value string
* @param the values to set on the current header field
* @return true if the value is successfully changed.
*/
bool operator=(const char *field_value);
/**
* Get the index value from this HeaderField
* @param the index to retrieve a copy of
* @return a copy of the string which is the index^th value in this HeaderField
* @note as currently written this returns an immutable string, it will NOT allow you to
* change the value.
*/
std::string operator[](const int index);
/**
* Get a string representing all the header field's values
* @return a string representation of all the header fields
*/
friend std::ostream &operator<<(std::ostream &os, HeaderField &obj);
/**
* Get a string representing all the header field's values.
* @return a string representation of all the header fields
*/
std::string str();
friend class Headers;
friend class header_field_iterator;
};
/**
* @brief Encapsulates the headers portion of a request or response.
*/
class Headers : noncopyable
{
public:
/**
* Constructor for Headers. This creates a "detached" headers, i.e., not tied to any transaction.
*/
Headers();
/**
* Constructor for Headers, this shouldn't be used directly unless you're trying to mix the C++ and C apis.
* @param bufp the TSMBuffer associated with the headers
* @param mloc the TSMLoc associated with the headers.
* @warning This should only be used if you're mixing the C++ and C apis, it will be constructed automatically if using only the
* C++ api.
*/
Headers(void *bufp, void *mloc);
/**
* Context Values are a way to share data between plugins, the key is always a string
* and the value can be a std::shared_ptr to any type that extends ContextValue.
* @param bufp the TSMBuffer associated with the headers
* @param mloc the TSMLoc associated with the headers.
* @warning This should only be used if you're mixing the C++ and C apis.
*/
void reset(void *bufp, void *mloc);
/**
* Check if the header class has been initialized. If you're only using the C++ api then this
* should always return true.
* @return a boolean value representing whether or not the Headers have been initialized..
*/
bool isInitialized() const;
typedef unsigned int size_type;
typedef header_field_iterator iterator;
/**
* Check if the headers are empty
* @return a boolean value representing whether or not the Headers are empty
*/
bool empty();
/**
* Get the size of the headers (the number of HeaderFields).
* @return the number of HeaderFields
*/
size_type size() const;
/**
* Get the size of the headers (the number of HeaderFields).
* @return the number of HeaderFields
*/
size_type lengthBytes() const;
/**
* Returns an iterator to the start of the HeaderFields.
* @return an iterator point to the start of the header fields.
*/
iterator begin();
/**
* Returns an iterator to the end of the HeaderFields (beyond the last element).
* @return an iterator that points beyond the last element in the header fields.
*/
iterator end();
/**
* Clears all headers.
* @return true if the headers were successfully cleared.
*/
bool clear();
/**
* Erase a single header field pointed to by an iterator
* @param an iterator pointing to a header field.
* @return true if the header field pointed to by the iterator was erased.
*/
bool erase(const iterator &it);
/**
* Erase all headers whose name matches key (this is a case insensitive match).
* @param the name of the header fields to erase
* @return the number of elements erased that matched the key
*/
size_type erase(const std::string &key);
/**
* Erase all headers whose name matches key (this is a case insensitive match).
* @param the name of the header fields to erase
* @param the length of the key (optional).
* @return the number of elements erased that matched the key
*/
size_type erase(const char *key, int length = -1);
/**
* Count all headers whose name matches key (this is a case insensitive match).
* @param the name of the header fields to erase
* @param the length of the key (optional).
* @return the number of elements erased that matched the key
*/
size_type count(const char *key, int length = -1);
/**
* Count all headers whose name matches key (this is a case insensitive match).
* @param the name of the header fields to count
* @return the number of elements whose name is key.
*/
size_type count(const std::string &key);
/**
* Join all headers whos name is key with the optionally specified join string
* @param the name of the headers to join into a single string
* @param an optional join string (defaults to ",")
* @return a string which is all of the joined values of headers matching key.
*/
std::string values(const std::string &key, const char *join = ",");
/**
* Join all headers whos name is key with the optionally specified join string
* @param the name of the headers to join into a single string
* @param the string to join the fields with
* @return a string which is all of the joined values of headers matching key.
*/
std::string values(const std::string &key, const std::string &join);
/**
* Join all headers whos name is key with the optionally specified join character
* @param the name of the headers to join into a single string
* @param the join character.
* @return a string which is all of the joined values of headers matching key.
*/
std::string values(const std::string &key, const char join);
/**
* Returns the value at given position of header with given name
* @param name of header
* @param position of value
* @return value
*/
std::string value(const std::string &key, size_type index = 0);
/**
* Returns an iterator to the first HeaderField with the name key.
* @param key the name of first header field ot find.
* @return an iterator that points to the first matching header field with name key.
*/
iterator find(const std::string &key);
/**
* Returns an iterator to the first HeaderField with the name key.
* @param key the name of first header field ot find.
* @param the length of the key specified (optional).
* @return an iterator that points to the first matching header field with name key.
*/
iterator find(const char *key, int length = -1);
/**
* Append a HeaderField.
* @param key the name of the header field to append
* @param value the value of the header field to append
* @return an iterator to the appended header field or the end() iterator if append fails.
*/
iterator append(const std::string &key, const std::string &value);
/**
* Erase all headers with name specified by key and then re-create the header with the specified values.
* @param key the name of the header field to erased and re-created.
* @param value the value of the header field to set.
* @return an iterator to the new header field or the end() iterator if append fails.
*/
iterator set(const std::string &key, const std::string &value);
/**
* Set the header field values to the value given, the header field will be created
* if it does not already exist.
* @param key the name of the header field whose value to set.
* @return an iterator to the new header field or the end() iterator if append fails.
* @warning This will create a header field with the given name, thus it should not be
* used to check for the existence of a header, use count() or find() instead.
*/
HeaderField operator[](const std::string &key);
/**
* Get a human-readable/log-friendly string representing all the header fields.
* @return a string representation of all the header fields
*/
std::string str();
/**
* Get a string that can be put on the wire
* @return a string representation of all the header fields
*/
std::string wireStr();
friend std::ostream &operator<<(std::ostream &os, Headers &obj);
~Headers();
private:
HeadersState *state_;
friend class Request;
friend class ClientRequest;
friend class Response;
};
} // namespace atscppapi
|