2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * @brief Support for MIME and allied protocols
39 /** @brief Match whitespace characters */
40 static int whitespace(int c) {
52 /** @brief Match RFC2045 tspecial characters */
53 static int tspecial(int c) {
76 /** @brief Match RFC2616 seprator characters */
77 static int http_separator(int c) {
104 /** @brief Match CRLF */
105 static int iscrlf(const char *ptr) {
106 return ptr[0] == '\r' && ptr[1] == '\n';
109 /** @brief Skip whitespace
110 * @param s Pointer into string
111 * @param rfc822_comments If true, skip RFC822 nested comments
112 * @return Pointer into string after whitespace
114 static const char *skipwhite(const char *s, int rfc822_comments) {
133 case '(': ++depth; break;
134 case ')': --depth; break;
149 /** @brief Test for a word character
150 * @param c Character to test
151 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
152 * @return 1 if @p c is a word character, else 0
154 static int iswordchar(int c, int (*special)(int)) {
155 return !(c <= ' ' || c > '~' || special(c));
158 /** @brief Parse an RFC1521/RFC2616 word
159 * @param s Pointer to start of word
160 * @param valuep Where to store value
161 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
162 * @return Pointer just after end of word or NULL if there's no word
164 * A word is a token or a quoted-string.
166 static const char *parseword(const char *s, char **valuep,
167 int (*special)(int)) {
168 struct dynstr value[1];
174 while((c = *s++) != '"') {
177 if(!(c = *s++)) return 0;
179 dynstr_append(value, c);
185 if(!iswordchar((unsigned char)*s, special))
188 while(iswordchar((unsigned char)*s, special))
189 dynstr_append(value, *s++);
191 dynstr_terminate(value);
192 *valuep = value->vec;
196 /** @brief Parse an RFC1521/RFC2616 token
197 * @param s Pointer to start of token
198 * @param valuep Where to store value
199 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
200 * @return Pointer just after end of token or NULL if there's no token
202 static const char *parsetoken(const char *s, char **valuep,
203 int (*special)(int)) {
204 if(*s == '"') return 0;
205 return parseword(s, valuep, special);
208 /** @brief Parse a MIME content-type field
209 * @param s Start of field
210 * @param typep Where to store type
211 * @param parameternamep Where to store parameter name
212 * @param parametervaluep Wher to store parameter value
213 * @return 0 on success, non-0 on error
215 * See <a href="http://tools.ietf.org/html/rfc2045#section-5">RFC 2045 s5</a>.
217 int mime_content_type(const char *s,
219 char **parameternamep,
220 char **parametervaluep) {
221 struct dynstr type, parametername;
224 if(!(s = skipwhite(s, 1))) return -1;
226 while(*s && !tspecial(*s) && !whitespace(*s))
227 dynstr_append(&type, tolower((unsigned char)*s++));
228 if(!(s = skipwhite(s, 1))) return -1;
229 if(*s++ != '/') return -1;
230 dynstr_append(&type, '/');
231 if(!(s = skipwhite(s, 1))) return -1;
232 while(*s && !tspecial(*s) && !whitespace(*s))
233 dynstr_append(&type, tolower((unsigned char)*s++));
234 if(!(s = skipwhite(s, 1))) return -1;
237 dynstr_init(¶metername);
239 if(!(s = skipwhite(s, 1))) return -1;
241 while(*s && !tspecial(*s) && !whitespace(*s))
242 dynstr_append(¶metername, tolower((unsigned char)*s++));
243 if(!(s = skipwhite(s, 1))) return -1;
244 if(*s++ != '=') return -1;
245 if(!(s = skipwhite(s, 1))) return -1;
246 if(!(s = parseword(s, parametervaluep, tspecial))) return -1;
247 if(!(s = skipwhite(s, 1))) return -1;
248 dynstr_terminate(¶metername);
249 *parameternamep = parametername.vec;
251 *parametervaluep = *parameternamep = 0;
252 dynstr_terminate(&type);
257 /** @brief Parse a MIME message
258 * @param s Start of message
259 * @param callback Called for each header field
260 * @param u Passed to callback
261 * @return Pointer to decoded body (might be in original string), or NULL on error
263 * This does an RFC 822-style parse and honors Content-Transfer-Encoding as
264 * described in <a href="http://tools.ietf.org/html/rfc2045#section-6">RFC 2045
265 * s6</a>. @p callback is called for each header field encountered, in order,
266 * with ASCII characters in the header name forced to lower case.
268 const char *mime_parse(const char *s,
269 int (*callback)(const char *name, const char *value,
272 struct dynstr name, value;
275 while(*s && !iscrlf(s)) {
278 while(*s && !tspecial(*s) && !whitespace(*s))
279 dynstr_append(&name, tolower((unsigned char)*s++));
280 if(!(s = skipwhite(s, 1))) return 0;
281 if(*s != ':') return 0;
283 while(*s && !(*s == '\n' && !(s[1] == ' ' || s[1] == '\t')))
284 dynstr_append(&value, *s++);
286 dynstr_terminate(&name);
287 dynstr_terminate(&value);
288 if(!strcmp(name.vec, "content-transfer-encoding")) {
289 cte = xstrdup(value.vec);
290 for(p = cte; *p; p++)
291 *p = tolower((unsigned char)*p);
293 if(callback(name.vec, value.vec, u)) return 0;
297 if(!strcmp(cte, "base64")) return mime_base64(s, 0);
298 if(!strcmp(cte, "quoted-printable")) return mime_qp(s);
303 /** @brief Match the boundary string */
304 static int isboundary(const char *ptr, const char *boundary, size_t bl) {
305 return (ptr[0] == '-'
307 && !strncmp(ptr + 2, boundary, bl)
308 && (iscrlf(ptr + bl + 2)
309 || (ptr[bl + 2] == '-'
310 && ptr[bl + 3] == '-'
311 && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0))));
314 /** @brief Match the final boundary string */
315 static int isfinal(const char *ptr, const char *boundary, size_t bl) {
316 return (ptr[0] == '-'
318 && !strncmp(ptr + 2, boundary, bl)
319 && ptr[bl + 2] == '-'
320 && ptr[bl + 3] == '-'
321 && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0));
324 /** @brief Parse a multipart MIME body
325 * @param s Start of message
326 * @param callback Callback for each part
327 * @param boundary Boundary string
328 * @param u Passed to callback
329 * @return 0 on success, non-0 on error
331 * See <a href="http://tools.ietf.org/html/rfc2046#section-5.1">RFC 2046
332 * s5.1</a>. @p callback is called for each part (not yet decoded in any way)
333 * in succession; you should probably call mime_parse() for each part.
335 int mime_multipart(const char *s,
336 int (*callback)(const char *s, void *u),
337 const char *boundary,
339 size_t bl = strlen(boundary);
340 const char *start, *e;
343 /* We must start with a boundary string */
344 if(!isboundary(s, boundary, bl))
346 /* Keep going until we hit a final boundary */
347 while(!isfinal(s, boundary, bl)) {
348 s = strstr(s, "\r\n") + 2;
350 while(!isboundary(s, boundary, bl)) {
351 if(!(e = strstr(s, "\r\n")))
355 if((ret = callback(xstrndup(start,
356 s == start ? 0 : s - start - 2),
363 /** @brief Parse an RFC2388-style content-disposition field
364 * @param s Start of field
365 * @param dispositionp Where to store disposition
366 * @param parameternamep Where to store parameter name
367 * @param parametervaluep Wher to store parameter value
368 * @return 0 on success, non-0 on error
370 * See <a href="http://tools.ietf.org/html/rfc2388#section-3">RFC 2388 s3</a>
371 * and <a href="http://tools.ietf.org/html/rfc2183">RFC 2183</a>.
373 int mime_rfc2388_content_disposition(const char *s,
375 char **parameternamep,
376 char **parametervaluep) {
377 struct dynstr disposition, parametername;
379 dynstr_init(&disposition);
380 if(!(s = skipwhite(s, 1))) return -1;
382 while(*s && !tspecial(*s) && !whitespace(*s))
383 dynstr_append(&disposition, tolower((unsigned char)*s++));
384 if(!(s = skipwhite(s, 1))) return -1;
387 dynstr_init(¶metername);
389 if(!(s = skipwhite(s, 1))) return -1;
391 while(*s && !tspecial(*s) && !whitespace(*s))
392 dynstr_append(¶metername, tolower((unsigned char)*s++));
393 if(!(s = skipwhite(s, 1))) return -1;
394 if(*s++ != '=') return -1;
395 if(!(s = skipwhite(s, 1))) return -1;
396 if(!(s = parseword(s, parametervaluep, tspecial))) return -1;
397 if(!(s = skipwhite(s, 1))) return -1;
398 dynstr_terminate(¶metername);
399 *parameternamep = parametername.vec;
401 *parametervaluep = *parameternamep = 0;
402 dynstr_terminate(&disposition);
403 *dispositionp = disposition.vec;
407 /** @brief Convert MIME quoted-printable
408 * @param s Quoted-printable data
409 * @return Decoded data
411 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.7">RFC 2045
414 char *mime_qp(const char *s) {
423 if((a = unhexdigitq(s[0])) != -1
424 && (b = unhexdigitq(s[1])) != -1) {
425 dynstr_append(&d, a * 16 + b);
429 while(*t == ' ' || *t == '\t') ++t;
431 /* soft line break */
440 while(*t == ' ' || *t == '\t') ++t;
442 /* trailing space is always eliminated */
445 dynstr_append(&d, c);
448 dynstr_append(&d, c);
452 dynstr_terminate(&d);
456 /** @brief Parse a RFC2109 Cookie: header
457 * @param s Header field value
458 * @param cd Where to store result
459 * @return 0 on success, non-0 on error
461 int parse_cookie(const char *s,
462 struct cookiedata *cd) {
465 memset(cd, 0, sizeof *cd);
468 /* Skip separators */
469 if(*s == ';' || *s == ',') {
474 if(!(s = parsetoken(s, &n, http_separator))) return -1;
476 if(*s++ != '=') return -1;
478 if(!(s = parseword(s, &v, http_separator))) return -1;
480 /* Some bit of meta-information */
481 if(!strcmp(n, "$Version"))
483 else if(!strcmp(n, "$Path")) {
484 if(cd->ncookies > 0 && cd->cookies[cd->ncookies-1].path == 0)
485 cd->cookies[cd->ncookies-1].path = v;
487 error(0, "redundant $Path in Cookie: header");
490 } else if(!strcmp(n, "$Domain")) {
491 if(cd->ncookies > 0 && cd->cookies[cd->ncookies-1].domain == 0)
492 cd->cookies[cd->ncookies-1].domain = v;
494 error(0, "redundant $Domain in Cookie: header");
499 /* It's a new cookie */
500 cd->cookies = xrealloc(cd->cookies,
501 (cd->ncookies + 1) * sizeof (struct cookie));
502 cd->cookies[cd->ncookies].name = n;
503 cd->cookies[cd->ncookies].value = v;
504 cd->cookies[cd->ncookies].path = 0;
505 cd->cookies[cd->ncookies].domain = 0;
509 if(*s && (*s != ',' && *s != ';')) {
510 error(0, "missing separator in Cookie: header");
517 /** @brief Find a named cookie
518 * @param cd Parse cookie data
519 * @param name Name of cookie
520 * @return Cookie structure or NULL if not found
522 const struct cookie *find_cookie(const struct cookiedata *cd,
526 for(n = 0; n < cd->ncookies; ++n)
527 if(!strcmp(cd->cookies[n].name, name))
528 return &cd->cookies[n];