Bläddra i källkod

core: helper functions for header management with strz values

Daniel-Constantin Mierla 9 år sedan
förälder
incheckning
aaec020b5c
2 ändrade filer med 97 tillägg och 0 borttagningar
  1. 92 0
      data_lump.c
  2. 5 0
      data_lump.h

+ 92 - 0
data_lump.c

@@ -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 - 0
data_lump.h

@@ -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