////////////////////////////////////////////////////////////////////// Set a header to a specific value. This will avoid going to through a remove / add sequence in case of an existing header. but clean.
| 52 | // remove / add sequence in case of an existing header. |
| 53 | // but clean. |
| 54 | bool |
| 55 | set_header(TSMBuffer bufp, TSMLoc hdr_loc, const char *header, int len, const char *val, int val_len) |
| 56 | { |
| 57 | if (!bufp || !hdr_loc || !header || len <= 0 || !val || val_len <= 0) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | bool ret = false; |
| 62 | TSMLoc field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, header, len); |
| 63 | |
| 64 | if (!field_loc) { |
| 65 | // No existing header, so create one |
| 66 | if (TS_SUCCESS == TSMimeHdrFieldCreateNamed(bufp, hdr_loc, header, len, &field_loc)) { |
| 67 | if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(bufp, hdr_loc, field_loc, -1, val, val_len)) { |
| 68 | TSMimeHdrFieldAppend(bufp, hdr_loc, field_loc); |
| 69 | ret = true; |
| 70 | } |
| 71 | TSHandleMLocRelease(bufp, hdr_loc, field_loc); |
| 72 | } |
| 73 | } else { |
| 74 | TSMLoc tmp = nullptr; |
| 75 | bool first = true; |
| 76 | |
| 77 | while (field_loc) { |
| 78 | tmp = TSMimeHdrFieldNextDup(bufp, hdr_loc, field_loc); |
| 79 | if (first) { |
| 80 | first = false; |
| 81 | if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(bufp, hdr_loc, field_loc, -1, val, val_len)) { |
| 82 | ret = true; |
| 83 | } |
| 84 | } else { |
| 85 | TSMimeHdrFieldDestroy(bufp, hdr_loc, field_loc); |
| 86 | } |
| 87 | TSHandleMLocRelease(bufp, hdr_loc, field_loc); |
| 88 | field_loc = tmp; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////// |
| 96 | // Dump a header on stderr, useful together with Dbg(). |
no test coverage detected