GError* metautils_message_extract_header_encoded(MESSAGE msg, const gchar *n, gboolean mandatory, GSList **result, body_decoder_f decoder) { EXTRA_ASSERT(result != NULL); EXTRA_ASSERT(decoder != NULL); gsize bsize = 0; void *b = metautils_message_get_field(msg, n, &bsize); if (!b || !bsize) { *result = NULL; if (mandatory) return NEWERROR(CODE_BAD_REQUEST, "Missing header [%s]", n); return NULL; } GError *err = NULL; int rc = decoder(result, b, bsize, &err); if (rc <= 0) { EXTRA_ASSERT(err != NULL); err->code = CODE_BAD_REQUEST; g_prefix_error(&err, "Invalid header: "); return err; } return NULL; }
gchar * metautils_message_extract_string_copy(MESSAGE msg, const gchar *n) { gsize fsize = 0; void *f = metautils_message_get_field(msg, n, &fsize); if (!f || !fsize) return NULL; return g_strndup(f, fsize); }
GError * metautils_message_extract_cid(MESSAGE msg, const gchar *n, container_id_t *cid) { gsize fsize = 0; void *f = metautils_message_get_field(msg, n, &fsize); if (!f || !fsize) return NEWERROR(CODE_BAD_REQUEST, "Missing container ID at '%s'", n); if (fsize != sizeof(container_id_t)) return NEWERROR(CODE_BAD_REQUEST, "Invalid container ID at '%s'", n); memcpy(cid, f, sizeof(container_id_t)); return NULL; }
gboolean metautils_message_extract_flag(MESSAGE msg, const gchar *n, gboolean def) { gsize fsize = 0; void *f = metautils_message_get_field(msg, n, &fsize); if (!f || !fsize) return def; guint8 *b, _flag = 0; for (b=(guint8*)f + fsize; b > (guint8*)f;) _flag |= *(--b); return _flag; }
GError * metautils_message_extract_prefix(MESSAGE msg, const gchar *n, guint8 *d, gsize *dsize) { gsize fsize = 0; void *f = metautils_message_get_field(msg, n, &fsize); if (!f || fsize) return NEWERROR(CODE_BAD_REQUEST, "Missing ID prefix at '%s'", n); if (fsize > *dsize) return NEWERROR(CODE_BAD_REQUEST, "Invalid ID prefix at '%s'", n); memset(d, 0, *dsize); memcpy(d, f, fsize); *dsize = fsize; return NULL; }
GError * metautils_message_extract_string(MESSAGE msg, const gchar *n, gchar *dst, gsize dst_size) { gsize fsize = 0; void *f = metautils_message_get_field(msg, n, &fsize); if (!f) return NEWERROR(CODE_BAD_REQUEST, "missing '%s'", n); if (!fsize) return NEWERROR(CODE_BAD_REQUEST, "empty '%s'", n); if ((gssize)fsize < 0 || fsize >= dst_size) return NEWERROR(CODE_BAD_REQUEST, "too long '%s' (%"G_GSIZE_FORMAT")", n, fsize); if (fsize) memcpy(dst, f, fsize); memset(dst+fsize, 0, dst_size-fsize); return NULL; }
GError* metautils_message_extract_flags32(MESSAGE msg, const gchar *n, gboolean mandatory, guint32 *flags) { EXTRA_ASSERT(flags != NULL); *flags = 0; gsize fsize = 0; void *f = metautils_message_get_field(msg, n, &fsize); if (!f || !fsize) { if (mandatory) return NEWERROR(CODE_BAD_REQUEST, "Missing field '%s'", n); return NULL; } if (fsize != 4) return NEWERROR(CODE_BAD_REQUEST, "Invalid 32bit flag set"); *flags = g_ntohl(*((guint32*)f)); return NULL; }