blob: 5979a3b2cec0f147d12148995b0bae4df1634408 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From 7e3dfd8a15fd0f98dbf0e04d2d7a5bded90ee401 Mon Sep 17 00:00:00 2001
2From: George Joseph <gjoseph@sangoma.com>
3Date: Tue, 11 Jan 2022 09:27:23 -0700
4Subject: [PATCH] Create generic pjsip_hdr_find functions
5
6pjsip_msg_find_hdr(), pjsip_msg_find_hdr_by_name(), and
7pjsip_msg_find_hdr_by_names() require a pjsip_msg to be passed in
8so if you need to search a header list that's not in a pjsip_msg,
9you have to do it yourself. This commit adds generic versions of
10those 3 functions that take in the actual header list head instead
11of a pjsip_msg so if you need to search a list of headers in
12something like a pjsip_multipart_part, you can do so easily.
13---
14 pjsip/include/pjsip/sip_msg.h | 53 +++++++++++++++++++++++++++++++++++
15 pjsip/src/pjsip/sip_msg.c | 51 +++++++++++++++++++++++----------
16 2 files changed, 89 insertions(+), 15 deletions(-)
17
18--- a/pjsip/include/pjsip/sip_msg.h
19+++ b/pjsip/include/pjsip/sip_msg.h
20@@ -363,6 +363,59 @@ PJ_DECL(void*) pjsip_hdr_shallow_clone(
21 PJ_DECL(int) pjsip_hdr_print_on( void *hdr, char *buf, pj_size_t len);
22
23 /**
24+ * Find a header in a header list by the header type.
25+ *
26+ * @param hdr_list The "head" of the header list.
27+ * @param type The header type to find.
28+ * @param start The first header field where the search should begin.
29+ * If NULL is specified, then the search will begin from the
30+ * first header, otherwise the search will begin at the
31+ * specified header.
32+ *
33+ * @return The header field, or NULL if no header with the specified
34+ * type is found.
35+ */
36+PJ_DECL(void*) pjsip_hdr_find( const void *hdr_list,
37+ pjsip_hdr_e type,
38+ const void *start);
39+
40+/**
41+ * Find a header in a header list by its name.
42+ *
43+ * @param hdr_list The "head" of the header list.
44+ * @param name The header name to find.
45+ * @param start The first header field where the search should begin.
46+ * If NULL is specified, then the search will begin from the
47+ * first header, otherwise the search will begin at the
48+ * specified header.
49+ *
50+ * @return The header field, or NULL if no header with the specified
51+ * type is found.
52+ */
53+PJ_DECL(void*) pjsip_hdr_find_by_name( const void *hdr_list,
54+ const pj_str_t *name,
55+ const void *start);
56+
57+/**
58+ * Find a header in a header list by its name and short name version.
59+ *
60+ * @param hdr_list The "head" of the header list.
61+ * @param name The header name to find.
62+ * @param sname The short name version of the header name.
63+ * @param start The first header field where the search should begin.
64+ * If NULL is specified, then the search will begin from the
65+ * first header, otherwise the search will begin at the
66+ * specified header.
67+ *
68+ * @return The header field, or NULL if no header with the specified
69+ * type is found.
70+ */
71+PJ_DECL(void*) pjsip_hdr_find_by_names( const void *hdr_list,
72+ const pj_str_t *name,
73+ const pj_str_t *sname,
74+ const void *start);
75+
76+/**
77 * @}
78 */
79
80--- a/pjsip/src/pjsip/sip_msg.c
81+++ b/pjsip/src/pjsip/sip_msg.c
82@@ -334,13 +334,13 @@ PJ_DEF(pjsip_msg*) pjsip_msg_clone( pj_p
83 return dst;
84 }
85
86-PJ_DEF(void*) pjsip_msg_find_hdr( const pjsip_msg *msg,
87- pjsip_hdr_e hdr_type, const void *start)
88+PJ_DEF(void*) pjsip_hdr_find( const void *hdr_list,
89+ pjsip_hdr_e hdr_type, const void *start)
90 {
91- const pjsip_hdr *hdr=(const pjsip_hdr*) start, *end=&msg->hdr;
92+ const pjsip_hdr *hdr=(const pjsip_hdr*) start, *end=hdr_list;
93
94 if (hdr == NULL) {
95- hdr = msg->hdr.next;
96+ hdr = end->next;
97 }
98 for (; hdr!=end; hdr = hdr->next) {
99 if (hdr->type == hdr_type)
100@@ -349,14 +349,14 @@ PJ_DEF(void*) pjsip_msg_find_hdr( const
101 return NULL;
102 }
103
104-PJ_DEF(void*) pjsip_msg_find_hdr_by_name( const pjsip_msg *msg,
105- const pj_str_t *name,
106- const void *start)
107+PJ_DEF(void*) pjsip_hdr_find_by_name( const void *hdr_list,
108+ const pj_str_t *name,
109+ const void *start)
110 {
111- const pjsip_hdr *hdr=(const pjsip_hdr*)start, *end=&msg->hdr;
112+ const pjsip_hdr *hdr=(const pjsip_hdr*) start, *end=hdr_list;
113
114 if (hdr == NULL) {
115- hdr = msg->hdr.next;
116+ hdr = end->next;
117 }
118 for (; hdr!=end; hdr = hdr->next) {
119 if (pj_stricmp(&hdr->name, name) == 0)
120@@ -365,15 +365,15 @@ PJ_DEF(void*) pjsip_msg_find_hdr_by_nam
121 return NULL;
122 }
123
124-PJ_DEF(void*) pjsip_msg_find_hdr_by_names( const pjsip_msg *msg,
125- const pj_str_t *name,
126- const pj_str_t *sname,
127- const void *start)
128+PJ_DEF(void*) pjsip_hdr_find_by_names( const void *hdr_list,
129+ const pj_str_t *name,
130+ const pj_str_t *sname,
131+ const void *start)
132 {
133- const pjsip_hdr *hdr=(const pjsip_hdr*)start, *end=&msg->hdr;
134+ const pjsip_hdr *hdr=(const pjsip_hdr*) start, *end=hdr_list;
135
136 if (hdr == NULL) {
137- hdr = msg->hdr.next;
138+ hdr = end->next;
139 }
140 for (; hdr!=end; hdr = hdr->next) {
141 if (pj_stricmp(&hdr->name, name) == 0)
142@@ -384,6 +384,27 @@ PJ_DEF(void*) pjsip_msg_find_hdr_by_nam
143 return NULL;
144 }
145
146+PJ_DEF(void*) pjsip_msg_find_hdr( const pjsip_msg *msg,
147+ pjsip_hdr_e hdr_type, const void *start)
148+{
149+ return pjsip_hdr_find(&msg->hdr, hdr_type, start);
150+}
151+
152+PJ_DEF(void*) pjsip_msg_find_hdr_by_name( const pjsip_msg *msg,
153+ const pj_str_t *name,
154+ const void *start)
155+{
156+ return pjsip_hdr_find_by_name(&msg->hdr, name, start);
157+}
158+
159+PJ_DEF(void*) pjsip_msg_find_hdr_by_names( const pjsip_msg *msg,
160+ const pj_str_t *name,
161+ const pj_str_t *sname,
162+ const void *start)
163+{
164+ return pjsip_hdr_find_by_names(&msg->hdr, name, sname, start);
165+}
166+
167 PJ_DEF(void*) pjsip_msg_find_remove_hdr( pjsip_msg *msg,
168 pjsip_hdr_e hdr_type, void *start)
169 {