Commit bbc3d6bb authored by Pekka Pessi's avatar Pekka Pessi

sip_header.h: added sip_headers_as_url_query()

darcs-hash:20061005094851-65a35-28a936a700ddbf28565b7e1464da8860f4d6ebfb.gz
parent b975afe1
......@@ -33,12 +33,6 @@
#include "config.h"
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <sofia-sip/su.h>
#include "sofia-sip/sip_parser.h"
#include <sofia-sip/su_tag_class.h>
......@@ -46,6 +40,12 @@
#include <sofia-sip/sip_tag_class.h>
#include <sofia-sip/sip_tag.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_strlst.h>
#include <ctype.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
/** Tag class for SIP header tags. @HIDE */
tag_class_t siphdrtag_class[1] =
......@@ -190,11 +190,11 @@ int sip_add_tagis(msg_t *msg, sip_t *sip, tagi_t const **inout_list)
if (!msg || !inout_list)
return -1;
for (t = *inout_list; t; t = tl_next(t)) {
for (t = *inout_list; t; t = t_next(t)) {
tag = t->t_tag, value = t->t_value;
if (tag == NULL || tag == siptag_end) {
t = tl_next(t);
t = t_next(t);
break;
}
......@@ -234,3 +234,111 @@ int sip_add_tagis(msg_t *msg, sip_t *sip, tagi_t const **inout_list)
return 0;
}
static char const *append_escaped(su_strlst_t *l,
msg_hclass_t const *hc,
char const *s);
/** Convert SIP headers to a URL-encoded headers list.
*
* @par Example
* @code
* url->url_headers =
* sip_headers_as_url_query(home, SIPTAG_REPLACES(replaces), TAG_END());
*
* @endcode
*
*/
char *sip_headers_as_url_query(su_home_t *home,
tag_type_t tag, tag_value_t value,
...)
{
ta_list ta;
tagi_t const *t;
su_strlst_t *l = su_strlst_create(home);
su_home_t *lhome = su_strlst_home(l);
char const *retval = "";
if (!l)
return NULL;
ta_start(ta, tag, value);
for (t = ta_args(ta); t && retval; t = t_next(t)) {
msg_hclass_t const *hc;
if (t->t_value == 0 || t->t_value == -1)
continue;
hc = (msg_hclass_t *)t->t_tag->tt_magic;
if (SIPTAG_P(t->t_tag)) {
sip_header_t const *h = (sip_header_t const *)t->t_value;
char *s = sip_header_as_string(lhome, h);
retval = append_escaped(l, hc, s);
if (retval != s)
su_free(lhome, s);
}
else if (SIPTAG_STR_P(t->t_tag)) {
retval = append_escaped(l, hc, (char const *)t->t_value);
}
}
ta_end(ta);
if (retval)
retval = su_strlst_join(l, home, "");
su_strlst_destroy(l);
return (char *)retval;
}
/** Append a string to list and url-escape it if needed */
static
char const *append_escaped(su_strlst_t *l,
msg_hclass_t const *hc,
char const *s)
{
char const *name;
if (hc == NULL)
return NULL;
if (hc->hc_hash == sip_payload_hash)
name = "body";
else /* XXX - could we use short form? */
name = hc->hc_name;
if (name == NULL)
return NULL;
if (s) {
su_home_t *lhome = su_strlst_home(l);
size_t slen;
isize_t elen;
char *n, *escaped;
char *sep = su_strlst_len(l) > 0 ? "&" : "";
n = su_sprintf(lhome, "%s%s=", sep, name);
if (!su_strlst_append(l, n))
return NULL;
for (;*n; n++)
if (isupper(*n))
*n = tolower(*n);
slen = strlen(s); elen = url_esclen(s, NULL);
if ((size_t)elen == slen)
return su_strlst_append(l, s);
escaped = su_alloc(lhome, elen + 1);
if (escaped)
return su_strlst_append(l, url_escape(escaped, s, NULL));
}
return NULL;
}
......@@ -104,6 +104,11 @@ SOFIAPUBFUN int sip_add_tagis(msg_t *, sip_t *, tagi_t const **inout_list);
/** Parse a string as a header and add it to the SIP message. */
SOFIAPUBFUN int sip_add_make(msg_t *, sip_t *, msg_hclass_t *hc, char const *s);
/** Convert headers from taglist as URL query. */
SOFIAPUBFUN char *sip_headers_as_url_query(su_home_t *home,
tag_type_t tag, tag_value_t value,
...);
/** Complete SIP message. */
SOFIAPUBFUN int sip_complete_message(msg_t *msg);
......
......@@ -71,6 +71,39 @@ msg_mclass_t *test_mclass = NULL;
static msg_t *read_message(int flags, char const string[]);
int test_url_headers(void)
{
BEGIN();
su_home_t *home;
char *s;
TEST_1(home = su_home_new(sizeof *home));
s = sip_headers_as_url_query
(home,
SIPTAG_TO_STR("\"Joe\" <sip:joe@example.com>;tag=foofaa"),
SIPTAG_SUBJECT_STR("foo"),
TAG_END());
TEST_1(s);
TEST_S(s, "to=%22Joe%22%20%3Csip%3Ajoe%40example.com%3E%3Btag%3Dfoofaa"
"&subject=foo");
s = sip_headers_as_url_query
(home,
SIPTAG_FROM_STR("<sip:joe@example.com>"),
SIPTAG_PAYLOAD_STR("hello"),
TAG_END());
TEST_S(s, "from=%3Csip%3Ajoe%40example.com%3E&body=hello");
TEST_1(!sip_headers_as_url_query(home, SIPTAG_SEPARATOR_STR(""), TAG_END()));
TEST_VOID(su_home_unref(home));
END();
}
int test_manipulation(void)
{
BEGIN();
......@@ -3073,7 +3106,8 @@ int main(int argc, char *argv[])
if (!test_mclass)
test_mclass = msg_mclass_clone(sip_default_mclass(), 0, 0);
retval |= test_url_headers(); fflush(stdout);
retval |= test_manipulation(); fflush(stdout);
retval |= test_methods(); fflush(stdout);
retval |= test_basic(); fflush(stdout);
......
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