TSMimeHdrFieldValueStringGet

Get HTTP MIME header values.

Synopsis

include <ts/ts.h>

const char * TSMimeHdrFieldValueStringGet(TSMBuffer bufp, TSMLoc hdr, TSMLoc field, int idx, int * value_len_ptr)

int TSMimeHdrFieldValueIntGet(TSMBuffer bufp, TSMLoc hdr, TSMLoc field, int idx)

int64_t TSMimeHdrFieldValueInt64Get(TSMBuffer bufp, TSMLoc hdr, TSMLoc field, int idx)

unsigned int TSMimeHdrFieldValueUintGet(TSMBuffer bufp, TSMLoc hdr, TSMLoc field, int idx)

time_t TSMimeHdrFieldValueDateGet(TSMBuffer bufp, TSMLoc hdr, TSMLoc field)

Description

MIME headers and fields can be components of request headers, response headers, or standalone headers created within a Traffic Server plugin. The functions here are all used to access header values of specific types, but it is up to the caller to know if a header has appropriate semantics for the API used. For all but TSMimeHdrFieldValueStringGet(), an appropriate data conversion algorithm is applied to the header field string.

All the APIs take a TSMBuffer marshal buffer argument bufp, and a TSMLoc argument hdr indicating the location of the HTTP headers. The required field argument is the locator of a specific header value, as returned by an accessor function such as TSMimeHdrFieldFind().

Within the header field, comma-separated values can be retrieved with an index idx ranging from 0 to the maximum number of fields for this value; this maximum is retrieved using TSMimeHdrFieldValuesCount(). An idx value of -1 has the semantics of retrieving the entire header value, regardless of how many comma-separated values there are. If a header is not comma-separated, an idx of 0 or -1 are the same, but the latter is preferred.

TSMimeHdrFieldValueStringGet() returns a pointer to the header value, and populated value_len_ptr with the length of the value in bytes. The returned header value is not NUL-terminated.

Return Values

All functions returns the header value with a type matching the respective function name. Using TSMimeHdrFieldValueDateGet() on a header which does not have date-time semantics always returns 0.

Examples

This examples show how to retrieve and copy a specific header.

  1. #include <string.h>
  2. #include <ts/ts.h>
  3. int
  4. get_content_type(TSHttpTxn txnp, char* buf, size_t buf_size)
  5. {
  6. TSMBuffer bufp;
  7. TSMLoc hdrs;
  8. TSMLoc ctype_field;
  9. int len = -1;
  10. if (TS_SUCCESS == TSHttpTxnServerRespGet(txnp, &bufp, &hdrs)) {
  11. ctype_field = TSMimeHdrFieldFind(bufp, hdrs, TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE);
  12. if (TS_NULL_MLOC != ctype_field) {
  13. const char* str = TSMimeHdrFieldValueStringGet(bufp, hdrs, ctype_field, -1, &len);
  14. if (len > buf_size)
  15. len = buf_size;
  16. memcpy(buf, str, len);
  17. TSHandleMLocRelease(bufp, hdrs, ctype_field);
  18. }
  19. TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdrs);
  20. }
  21. return len;
  22. }

See Slso

TSAPI(3ts), TSMBufferCreate(3ts), TSMimeHdrFieldValuesCount(3ts)