Browse code

add addOptionTag/removeOptionTag functions

Stefan Sayer authored on 04/03/2011 16:30:41
Showing 4 changed files
... ...
@@ -37,6 +37,7 @@
37 37
 
38 38
 #define SIP_HDR_COL(_hdr)       _hdr ":"
39 39
 #define SIP_HDR_COLSP(_hdr)     SIP_HDR_COL(_hdr) " "
40
+#define COLSP                   ": "
40 41
 
41 42
 #define CRLF                    "\r\n"
42 43
 #define SIP_HDR_LEN(_hdr)       (sizeof(_hdr) - /*0-term*/1)
... ...
@@ -2,7 +2,7 @@
2 2
 #include <stdlib.h>
3 3
 #include "AmUtils.h"
4 4
 #include "AmSipMsg.h"
5
-
5
+#include "AmSipHeaders.h"
6 6
 
7 7
 string getHeader(const string& hdrs,const string& hdr_name, bool single)
8 8
 {
... ...
@@ -125,6 +125,65 @@ bool removeHeader(string& hdrs, const string& hdr_name) {
125 125
   return found;
126 126
 }
127 127
 
128
+void addOptionTag(string& hdrs, const string& hdr_name, const string& tag) {
129
+  // see if option tag already exists
130
+  string options = getHeader(hdrs, hdr_name);
131
+  if (options.size()) {
132
+    std::vector<string> option_entries = explode(options, ",");
133
+    for (std::vector<string>::iterator it=option_entries.begin();
134
+	 it != option_entries.end(); it++) {
135
+      if (trim(*it," ") == tag)
136
+	// found - no need to add again
137
+	return;
138
+    }
139
+    // tag not found - add our tag to the (first) hdr_name header
140
+    size_t pos1; size_t pos2; size_t hdr_start;
141
+    if (!findHeader(hdrs, hdr_name, 0, pos1, pos2, hdr_start)) {
142
+      ERROR("internal error: header '%s' disappeared in-between (hdrs = '%s'!\n",
143
+	    hdr_name.c_str(), hdrs.c_str());
144
+      hdrs += hdr_name + COLSP + tag + CRLF;
145
+      return;
146
+    }
147
+
148
+    hdrs.insert(pos1, tag+", ");
149
+
150
+  } else {
151
+    // hdr does not exist - add it
152
+    hdrs += hdr_name + COLSP + tag + CRLF;
153
+  }
154
+}
155
+
156
+void removeOptionTag(string& hdrs, const string& hdr_name, const string& tag) {
157
+  string options = getHeader(hdrs, hdr_name);
158
+
159
+  // does hdr hdr_name exist?
160
+  if (options.empty())
161
+    return;
162
+
163
+  // todo: optimize by doing inplace
164
+  std::vector<string> options_v = explode(options, ",");
165
+  string o_hdr;
166
+  bool found = false;
167
+  for (std::vector<string>::iterator it=options_v.begin();
168
+       it != options_v.end(); it++) {
169
+    if (trim(*it, " ")==tag) {
170
+      found = true;
171
+      continue;
172
+    }
173
+    if (it != options_v.begin())
174
+      o_hdr = ", ";
175
+    o_hdr+=*it;
176
+  }
177
+  if (!found)
178
+    return;
179
+
180
+  removeHeader(hdrs, hdr_name);
181
+  if (o_hdr.empty())
182
+    return;
183
+  hdrs += hdr_name + COLSP + o_hdr + CRLF;
184
+}
185
+
186
+
128 187
 /* Print Member */
129 188
 #define _PM(member, name)			\
130 189
   do {						\
... ...
@@ -94,6 +94,13 @@ bool findHeader(const string& hdrs,const string& hdr_name, const size_t skip,
94 94
 		size_t& hdr_start);
95 95
 
96 96
 bool removeHeader(string& hdrs, const string& hdr_name);
97
+
98
+/** add an option tag @param tag to list @param hdr_name */
99
+void addOptionTag(string& hdrs, const string& hdr_name, const string& tag);
100
+
101
+/** remove an option tag @param tag from list @param hdr_name */
102
+void removeOptionTag(string& hdrs, const string& hdr_name, const string& tag);
103
+
97 104
 #endif /* __AMSIPMSG_H__ */
98 105
 
99 106
 
... ...
@@ -59,4 +59,47 @@ FCTMF_SUITE_BGN(test_headers) {
59 59
       fct_chk(get_header_keyvalue("u=sayer;d=iptel.org;p=abcdef", "d") == "iptel.org");
60 60
     } FCT_TEST_END();
61 61
 
62
+    FCT_TEST_BGN(addOptionTag) {
63
+      string hdrs =
64
+	"Supported: timer" CRLF
65
+	"Session-Expires: 110;refresher=uas" CRLF
66
+	"Supported: timer" CRLF;
67
+
68
+      string hdrs1;
69
+      addOptionTag(hdrs1, "Supported", "blub");
70
+      // DBG("hdrs1 '%s'\n", hdrs1.c_str());
71
+      fct_chk(hdrs1.find("Supported: blub") != string::npos);
72
+
73
+      hdrs1 = hdrs;
74
+      addOptionTag(hdrs1, "Supported", "something");
75
+      // DBG("hdrs1 '%s'\n", hdrs1.c_str());
76
+      fct_chk(hdrs1.find("something") != string::npos);
77
+
78
+      hdrs1 = hdrs;
79
+      addOptionTag(hdrs1, "Supported", "timer");
80
+      // DBG("hdrs1 '%s'\n", hdrs1.c_str());
81
+      fct_chk(hdrs1 == hdrs);
82
+
83
+    } FCT_TEST_END();
84
+
85
+    FCT_TEST_BGN(removeOptionTag) {
86
+      string hdrs =
87
+	"Supported: timer" CRLF;
88
+
89
+      string hdrs1 = hdrs;
90
+      removeOptionTag(hdrs1, "Supported", "notexisting");
91
+      // DBG("hdrs1 = '%s'\n", hdrs1.c_str());
92
+      fct_chk(hdrs1 == hdrs); // dont touch
93
+
94
+      hdrs1 = hdrs;
95
+      removeOptionTag(hdrs1, "Supported", "timer");
96
+      // DBG("hdrs1 = '%s'\n", hdrs1.c_str());
97
+      fct_chk(hdrs1.empty() == true); // last one
98
+
99
+      hdrs1 = hdrs + "Supported: timer" CRLF;
100
+      removeOptionTag(hdrs1, "Supported", "timer");
101
+      // DBG("hdrs1 = '%s'\n", hdrs1.c_str());
102
+      fct_chk(hdrs1.empty()== true); // last one
103
+
104
+    } FCT_TEST_END();
62 105
 } FCTMF_SUITE_END();