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.

#include <string.h>
#include <ts/ts.h>

int
get_content_type(TSHttpTxn txnp, char* buf, size_t buf_size)
{
  TSMBuffer bufp;
  TSMLoc hdrs;
  TSMLoc ctype_field;
  int len = -1;

  if (TS_SUCCESS == TSHttpTxnServerRespGet(txnp, &bufp, &hdrs)) {
    ctype_field = TSMimeHdrFieldFind(bufp, hdrs, TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE);

    if (TS_NULL_MLOC != ctype_field) {
      const char* str = TSMimeHdrFieldValueStringGet(bufp, hdrs, ctype_field, -1, &len);

      if (len > buf_size)
        len = buf_size;
      memcpy(buf, str, len);
      TSHandleMLocRelease(bufp, hdrs, ctype_field);
    }
    TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdrs);
  }

  return len;
}

See Slso

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