]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mime_header.cc
Moved some MIME header function prototypes to newly-created mime_header.h
[thirdparty/squid.git] / src / mime_header.cc
1 /*
2 * DEBUG: section 25 MiME Header Parsing
3 * AUTHOR: Harvest Derived
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34
35 #define GET_HDR_SZ 1024
36 #include "Debug.h"
37 #include "profiler/Profiler.h"
38
39 /*
40 * returns a pointer to a field-value of the first matching field-name where
41 * field-value matches prefix if any
42 */
43 char *
44 mime_get_header_field(const char *mime, const char *name, const char *prefix)
45 {
46 LOCAL_ARRAY(char, header, GET_HDR_SZ);
47 const char *p = NULL;
48 char *q = NULL;
49 char got = 0;
50 const int namelen = name ? strlen(name) : 0;
51 const int preflen = prefix ? strlen(prefix) : 0;
52 int l;
53
54 if (NULL == mime)
55 return NULL;
56
57 assert(NULL != name);
58
59 debugs(25, 5, "mime_get_header: looking for '" << name << "'");
60
61 for (p = mime; *p; p += strcspn(p, "\n\r")) {
62 if (strcmp(p, "\r\n\r\n") == 0 || strcmp(p, "\n\n") == 0)
63 return NULL;
64
65 while (xisspace(*p))
66 ++p;
67
68 if (strncasecmp(p, name, namelen))
69 continue;
70
71 if (!xisspace(p[namelen]) && p[namelen] != ':')
72 continue;
73
74 l = strcspn(p, "\n\r") + 1;
75
76 if (l > GET_HDR_SZ)
77 l = GET_HDR_SZ;
78
79 xstrncpy(header, p, l);
80
81 debugs(25, 5, "mime_get_header: checking '" << header << "'");
82
83 q = header;
84
85 q += namelen;
86
87 if (*q == ':') {
88 ++q;
89 got = 1;
90 }
91
92 while (xisspace(*q)) {
93 ++q;
94 got = 1;
95 }
96
97 if (got && prefix) {
98 /* we could process list entries here if we had strcasestr(). */
99 /* make sure we did not match a part of another field-value */
100 got = !strncasecmp(q, prefix, preflen) && !xisalpha(q[preflen]);
101 }
102
103 if (got) {
104 debugs(25, 5, "mime_get_header: returning '" << q << "'");
105 return q;
106 }
107 }
108
109 return NULL;
110 }
111
112 /* returns a pointer to a field-value of the first matching field-name */
113 char *
114 mime_get_header(const char *mime, const char *name)
115 {
116 return mime_get_header_field(mime, name, NULL);
117 }
118
119 size_t
120 headersEnd(const char *mime, size_t l)
121 {
122 size_t e = 0;
123 int state = 1;
124
125 PROF_start(headersEnd);
126
127 while (e < l && state < 3) {
128 switch (state) {
129
130 case 0:
131
132 if ('\n' == mime[e])
133 state = 1;
134
135 break;
136
137 case 1:
138 if ('\r' == mime[e])
139 state = 2;
140 else if ('\n' == mime[e])
141 state = 3;
142 else
143 state = 0;
144
145 break;
146
147 case 2:
148 if ('\n' == mime[e])
149 state = 3;
150 else
151 state = 0;
152
153 break;
154
155 default:
156 break;
157 }
158
159 ++e;
160 }
161 PROF_stop(headersEnd);
162
163 if (3 == state)
164 return e;
165
166 return 0;
167 }