TSMIMEHDRF...ESTRINGGET(3ts)

πŸ“¦
version 9.2.5 Β· 4 versions
, in ubuntu noble, ubuntu questing

✨ TSMimeHdrFieldValueStringGet β€” get HTTP MIME header values ✨

okay bestie, so this is one of those pages from the Apache Traffic Server 3ts manual, and it's literally all about reading values out of MIME header fields. it's giving "small function, big responsibilities" πŸ’… let's get into it.


🌸 SYNOPSIS 🌸

first things first, you gotta pull in the header, no cap:

#include <ts/ts.h>

and here's the whole lil squad of getters 🫢:

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 πŸ’–

so MIME headers and fields? they can be part of request headers, response headers, or even standalone headers you just... made up inside a Traffic Server plugin. main character energy tbh. every single one of these functions is here to grab header values of a specific type, but β€” and this is the important part β€” it's on you, the caller, to know whether a header actually has the right semantics for the API you're using. the library is not gonna hold your hand on that one. πŸ™ˆ

for every function except TSMimeHdrFieldValueStringGet(), an appropriate data conversion algorithm gets applied to the header field string. so like, it'll convert the string into the type you asked for. slay.

okay now the shared deets, because they all take basically the same stuff:

  • bufp β€” a TSMBuffer marshal buffer. iconic.
  • hdr β€” a TSMLoc that says where the HTTP headers live.
  • field β€” the locator of one specific header value, the kind of thing you'd get back from an accessor function like TSMimeHdrFieldFind().

and then there's the indexing situation, which lowkey deserves its own moment:

  • inside a header field, comma-separated values can be pulled out with an index idx that runs from 0 up to the max number of fields for that value.
  • that max? you get it from TSMimeHdrFieldValuesCount(). very demure, very mindful.
  • an idx of -1 means "just give me the entire header value," no matter how many comma-separated values are hanging out in there.
  • if a header isn't comma-separated at all, idx of 0 and idx of -1 do the exact same thing β€” but -1 is the preferred one. do with that what you will. ✨

and the star of the show:

TSMimeHdrFieldValueStringGet() returns a pointer to the header value, and it populates value_len_ptr with the length of that value in bytes.

🚨 heads up bestie: the returned header value is not NUL-terminated. so please, for the love of everything, don't go treating it like a C string. 😭

πŸ¦‹ RETURN VALUES πŸ¦‹

all of these functions return the header value, with a type that matches the function name β€” so String gives you a string, Int gives you an int, and so on. the way that just works. πŸ‘‘

and one specific little caveat: if you call TSMimeHdrFieldValueDateGet() on a header that doesn't have date-time semantics, it always returns 0. every time. no exceptions. periodt.


πŸŽ€ EXAMPLES πŸŽ€

this example shows how to retrieve and copy a specific header. look at her go πŸ’…:

#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;
}

notice the -1 in there? yeah, that's the "gimme the whole value" move we talked about. and yes, she releases her locs like a responsible queen. 🫢


🌈 SEE ALSO 🌈

go read these too, bestie, they're all in the family:


copyright 2026, dev@trafficserver.apache.org β€” protect it at all costs. πŸ’œ


and that's the whole page, fr fr. this was Apache Traffic Server 3ts, version 9.2, dated Feb 12, 2026 β€” so go forth, read those MIME header values, and remember: not NUL-terminated, -1 for the whole thing, and always know your semantics. bye bestie, slay πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from