Skip to content

Commit

Permalink
core: helper functions for header management with strz values
Browse files Browse the repository at this point in the history
(cherry picked from commit aaec020)
  • Loading branch information
miconda committed Sep 8, 2016
1 parent 35deae6 commit 1b68b03
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
92 changes: 92 additions & 0 deletions data_lump.c
Expand Up @@ -734,3 +734,95 @@ int remove_lump(sip_msg_t *msg, struct lump *l)
}
return 0;
}

/**
*
*/
int sr_hdr_add_zz(sip_msg_t *msg, char *hname, char *hbody)
{
struct lump* anchor;
str h;
str sname;
str sbody;

sname.s = hname;
sname.len = strlen(sname.s);
sbody.s = hbody;
sbody.len = strlen(sbody.s);

h.len = sname.len + 2 + sbody.len + 1 + CRLF_LEN;
h.s = (char*)pkg_malloc(h.len+1);
if(h.s == 0) {
LM_ERR("no more pkg\n");
return -1;
}
anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
if(anchor == 0)
{
LM_ERR("cannot get the anchor\n");
pkg_free(h.s);
return -1;
}
memcpy(h.s, sname.s, sname.len);
memcpy(h.s+sname.len, ": ", 2);
memcpy(h.s+sname.len+2, sbody.s, sbody.len);
memcpy(h.s+sname.len+2+sbody.len+1, CRLF, CRLF_LEN);
h.s[h.len] = '\0';
if (insert_new_lump_before(anchor, h.s, h.len, 0) == 0)
{
LM_ERR("cannot insert lump\n");
pkg_free(h.s);
return -1;
}
LM_DBG("added new header [%s]\n", h.s);
return 0;
}

/**
*
*/
hdr_field_t *sr_hdr_get_z(sip_msg_t *msg, char *hname)
{
hdr_field_t *hf;
str sname;

sname.s = hname;
sname.len = strlen(sname.s);

for (hf=msg->headers; hf; hf=hf->next) {
if (hf->name.len==sname.len
&& strncasecmp(hf->name.s, sname.s,
sname.len)==0) {
return hf;

}
}
return NULL;
}

/**
*
*/
int sr_hdr_del_z(sip_msg_t *msg, char *hname)
{
hdr_field_t *hf;
struct lump* l;
str sname;

sname.s = hname;
sname.len = strlen(sname.s);

for (hf=msg->headers; hf; hf=hf->next) {
if (hf->name.len==sname.len
&& strncasecmp(hf->name.s, sname.s,
sname.len)==0) {
l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
if (l==0) {
LM_ERR("unable to delete cookie header\n");
return -1;
}
return 0;
}
}
return 0;
}
5 changes: 5 additions & 0 deletions data_lump.h
Expand Up @@ -86,4 +86,9 @@ void del_nonshm_lump( struct lump** lump_list );

/*! \brief remove the lump from the internal lists */
int remove_lump(sip_msg_t *msg, struct lump *l);

int sr_hdr_add_zz(sip_msg_t *msg, char *hname, char *hbody);
int sr_hdr_del_z(sip_msg_t *msg, char *hname);
hdr_field_t *sr_hdr_get_z(sip_msg_t *msg, char *hname);

#endif

0 comments on commit 1b68b03

Please sign in to comment.