Commit 67a37097 authored by Pekka Pessi's avatar Pekka Pessi

Added manipulation functions for header parameters.

msg_header_find_param(), msg_header_add_param(),
msg_header_replace_param(), and msg_header_remove_param().

darcs-hash:20051215205155-65a35-b3deaa9f1e4faa03e0142e34b83b3372dbbebce2.gz
parent aa8a3408
......@@ -1354,7 +1354,7 @@ HTTP_HEADER_CLASS_G(server, "Server", single);
static inline
void http_te_update(http_te_t *te)
{
te->te_q = msg_params_find(te->te_params, "q=");
te->te_q = msg_header_find_param(te->te_common, "q");
}
int http_te_d(su_home_t *home, msg_header_t *h, char *s, int slen)
......
......@@ -172,8 +172,8 @@ int auc_challenge(auth_client_t **auc_list,
int retval = 0;
for (; ch; ch = ch->au_next) {
msg_param_t scheme = ch->au_scheme;
msg_param_t realm = msg_params_find(ch->au_params, "realm=");
char const *scheme = ch->au_scheme;
char const *realm = msg_header_find_param(ch->au_common, "realm=");
int updated = 0, updated0;
if (!scheme || !realm)
......@@ -251,10 +251,11 @@ int ca_challenge(auth_client_t *ca,
ca->ca_challenge = msg_header_dup(home, (msg_header_t *)ch)->sh_auth;
ca->ca_challenge_class = ca->ca_challenge->au_common->h_class;
ca->ca_scheme = ca->ca_challenge->au_scheme;
ca->ca_realm = msg_params_find(ca->ca_challenge->au_params, "realm=");
ca->ca_realm = msg_header_find_param(ca->ca_challenge->au_common, "realm=");
#else
ca->ca_scheme = su_strdup(home, ch->au_scheme);
ca->ca_realm = su_strdup(home, msg_params_find(ch->au_params, "realm="));
ca->ca_realm = su_strdup(home,
msg_header_find_param(ch->au_common, "realm"));
#endif
if (auth_digest_challenge_get(home, ac, ch->au_params) < 0)
......
......@@ -1182,7 +1182,7 @@ msg_auth_t *auth_mod_credentials(msg_auth_t *auth,
if (!realm)
return auth;
arealm = msg_params_find(auth->au_params, "realm=");
arealm = msg_header_find_param(auth->au_common, "realm=");
if (!arealm)
continue;
......@@ -1223,7 +1223,7 @@ msg_auth_t *auth_digest_credentials(msg_auth_t *auth,
if (realm) {
int cmp = 1;
arealm = msg_params_find(auth->au_params, "realm=");
arealm = msg_header_find_param(auth->au_common, "realm=");
if (!arealm)
continue;
......@@ -1254,7 +1254,7 @@ msg_auth_t *auth_digest_credentials(msg_auth_t *auth,
if (opaque) {
int cmp = 1;
aopaque = msg_params_find(auth->au_params, "opaque=");
aopaque = msg_header_find_param(auth->au_common, "opaque=");
if (!aopaque)
continue;
......
......@@ -165,10 +165,8 @@ static inline void msg_fragment_clear(msg_common_t *h)
}
/** Pointer to header parameters. */
static inline
msg_param_t **msg_header_params(msg_header_t *h0)
msg_param_t **msg_header_params(msg_common_t const *h)
{
msg_common_t *h = (msg_common_t *)h0;
if (h && h->h_class->hc_params) {
return (msg_param_t **)((char *)h + h->h_class->hc_params);
}
......@@ -176,10 +174,16 @@ msg_param_t **msg_header_params(msg_header_t *h0)
}
#else
#define msg_fragment_clear(h) ((h)->h_data = NULL, (h)->h_len = 0)
#define msg_header_params(h) (((h) && (h)->sh_class->hc_params) ? \
(msg_param_t **)((char *)(h) + (h)->sh_class->hc_params) : NULL)
#define msg_header_params(h) \
(((h) && ((msg_common_t *)h)->h_class->hc_params) ? \
(msg_param_t **)((char *)(h) + ((msg_common_t *)h)->h_class->hc_params) : NULL)
#endif
char const *msg_header_find_param(msg_common_t const *h, char const *name);
int msg_header_add_param(su_home_t *, msg_common_t *h, char const *param);
int msg_header_replace_param(su_home_t *, msg_common_t *h, char const *param);
int msg_header_remove_param(msg_common_t *h, char const *name);
int msg_random_token(char token[], int tlen, void const *data, int dlen);
msg_param_t msg_params_find(msg_param_t const pp[], msg_param_t);
......
......@@ -417,7 +417,7 @@ msg_multipart_t *msg_multipart_parse(su_home_t *home,
msg->m_tail = &msg->m_chain;
/* Get boundary from Content-Type */
if (c && (param = msg_params_find(c->c_params, "boundary=")))
if (c && (param = msg_header_find_param(c->c_common, "boundary=")))
boundary = msg_multipart_boundary(msg_home(msg), param);
else
boundary = msg_multipart_search_boundary(msg_home(msg), p, len);
......@@ -605,7 +605,7 @@ int msg_multipart_complete(su_home_t *home,
if (c == NULL || mp == NULL)
return (errno = EINVAL), -1;
if (!(b = msg_params_find(c->c_params, "boundary="))) {
if (!(b = msg_header_find_param(c->c_common, "boundary="))) {
/* Generate boundary */
enum { tlen = 16 * 4 / 3 };
char token[sizeof("boundary=") + tlen + 1];
......@@ -1189,7 +1189,7 @@ char *msg_accept_dup_one(msg_header_t *dst, msg_header_t const *src,
static inline
void msg_accept_update(msg_accept_t *ac)
{
ac->ac_q = msg_params_find(ac->ac_params, "q=");
ac->ac_q = msg_header_find_param(ac->ac_common, "q=");
}
/* ====================================================================== */
......
......@@ -2343,8 +2343,8 @@ int msg_header_add_dup(msg_t *msg,
msg_header_t *h = *hh;
msg_param_t **d, **s;
d = msg_header_params(h); assert(d);
s = msg_header_params((msg_header_t *)src);
d = msg_header_params(h->sh_common); assert(d);
s = msg_header_params(src->sh_common);
if (!s || !*s)
return 0;
......@@ -2416,8 +2416,8 @@ int _msg_header_add_dup_as(msg_t *msg,
msg_header_t *h = *hh;
msg_param_t **d, **s;
d = msg_header_params(h); assert(d);
s = msg_header_params((msg_header_t *)src);
d = msg_header_params(h->sh_common); assert(d);
s = msg_header_params(src->sh_common);
if (!s || !*s)
return 0;
......@@ -2464,7 +2464,7 @@ int msg_header_add_make(msg_t *msg,
skip_lws(&s);
d = msg_header_params(h); assert(d);
d = msg_header_params(h->sh_common); assert(d);
msg_fragment_clear(h->sh_common);
......
......@@ -762,6 +762,65 @@ int msg_hostport_d(char **ss,
return 0;
}
/** Find a header parameter.
*
* Searches for given parameter @a name from the header. If parameter is
* found, it returns a non-NULL pointer to the parameter value. If there is
* no value for the name (in form "name" or "name=value"), the returned pointer
* points to a NUL character.
*
* @param params list (or vector) of parameters
* @param name parameter name (with or without "=" sign)
*
* @return
* A pointer to parameter value, or NULL if parameter was not found.
*/
char const *msg_header_find_param(msg_common_t const *h, char const *name)
{
if (h && h->h_class->hc_params) {
msg_param_t const **params = (msg_param_t const **)
((char *)h + h->h_class->hc_params);
return msg_params_find(*params, name);
}
return NULL;
}
int msg_header_add_param(su_home_t *home, msg_common_t *h, char const *param)
{
if (h && h->h_class->hc_params) {
msg_param_t **params = (msg_param_t **)
((char *)h + h->h_class->hc_params);
return msg_params_add(home, params, param);
}
return -1;
}
int msg_header_replace_param(su_home_t *home,
msg_common_t *h,
char const *param)
{
if (h && h->h_class->hc_params) {
msg_param_t **params = (msg_param_t **)
((char *)h + h->h_class->hc_params);
return msg_params_replace(home, params, param);
}
return -1;
}
int msg_header_remove_param(msg_common_t *h, char const *name)
{
if (h && h->h_class->hc_params) {
msg_param_t **params = (msg_param_t **)
((char *)h + h->h_class->hc_params);
return msg_params_remove(*params, name);
}
return -1;
}
/** Find a parameter from a parameter list.
*
* Searches for given parameter @a token from the parameter list. If
......@@ -870,6 +929,9 @@ int msg_params_replace(su_home_t *home,
/** Remove a parameter from a list.
*
* @retval 1 if parameter was removed
* @retval 0 if parameter was not found
* @retval -1 upon an error
*/
int msg_params_remove(msg_param_t *params, msg_param_t param)
{
......
......@@ -465,6 +465,30 @@ int test_header_parsing(void)
su_home_deinit(home);
}
{
/* Test parameter handling */
su_home_t home[1] = { SU_HOME_INIT(home) };
msg_content_encoding_t *ce;
ce = msg_content_encoding_make(home, "zip, zap, zup, lz, zl, zz, ll");
TEST_1(ce);
TEST_S(msg_header_find_param(ce->k_common, "zz"), "");
TEST(msg_header_find_param(ce->k_common, "k"), NULL);
TEST(msg_header_add_param(home, ce->k_common, "zip"), 0);
TEST(msg_header_remove_param(ce->k_common, "zip"), 1);
TEST_S(msg_header_find_param(ce->k_common, "zip"), "");
TEST(msg_header_remove_param(ce->k_common, "zip"), 1);
TEST(msg_header_find_param(ce->k_common, "zip"), NULL);
TEST(msg_header_remove_param(ce->k_common, "zip"), 0);
TEST(msg_header_replace_param(home, ce->k_common, "zip=zap"), 0);
TEST_S(msg_header_find_param(ce->k_common, "zip=1"), "zap");
TEST(msg_header_replace_param(home, ce->k_common, "zip=zup"), 0);
TEST_S(msg_header_find_param(ce->k_common, "zip"), "zup");
su_home_deinit(home);
}
END();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment