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 rfc822_comments If true, skip RFC822 nested comments
112 static const char *skipwhite(const char *s, int rfc822_comments) {
131 case '(': ++depth; break;
132 case ')': --depth; break;
147 /** @brief Test for a word character
148 * @param c Character to test
149 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
150 * @return 1 if @p c is a word character, else 0
152 static int iswordchar(int c, int (*special)(int)) {
153 return !(c <= ' ' || c > '~' || special(c));
156 /** @brief Parse an RFC1521/RFC2616 word
157 * @param s Pointer to start of word
158 * @param valuep Where to store value
159 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
160 * @return Pointer just after end of word or NULL if there's no word
162 * A word is a token or a quoted-string.
164 static const char *parseword(const char *s, char **valuep,
165 int (*special)(int)) {
166 struct dynstr value[1];
172 while((c = *s++) != '"') {
175 if(!(c = *s++)) return 0;
177 dynstr_append(value, c);
183 if(!iswordchar((unsigned char)*s, special))
186 while(iswordchar((unsigned char)*s, special))
187 dynstr_append(value, *s++);
189 dynstr_terminate(value);
190 *valuep = value->vec;
194 /** @brief Parse an RFC1521/RFC2616 token
195 * @param s Pointer to start of token
196 * @param valuep Where to store value
197 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
198 * @return Pointer just after end of token or NULL if there's no token
200 static const char *parsetoken(const char *s, char **valuep,
201 int (*special)(int)) {
202 if(*s == '"') return 0;
203 return parseword(s, valuep, special);
206 /** @brief Parse a MIME content-type field
207 * @param s Start of field
208 * @param typep Where to store type
209 * @param parameternamep Where to store parameter name
210 * @param parameternvaluep Wher to store parameter value
211 * @return 0 on success, non-0 on error
213 * See <a href="http://tools.ietf.org/html/rfc2045#section-5">RFC 2045 s5</a>.
215 int mime_content_type(const char *s,
217 char **parameternamep,
218 char **parametervaluep) {
219 struct dynstr type, parametername;
222 if(!(s = skipwhite(s, 1))) return -1;
224 while(*s && !tspecial(*s) && !whitespace(*s))
225 dynstr_append(&type, tolower((unsigned char)*s++));
226 if(!(s = skipwhite(s, 1))) return -1;
227 if(*s++ != '/') return -1;
228 dynstr_append(&type, '/');
229 if(!(s = skipwhite(s, 1))) return -1;
230 while(*s && !tspecial(*s) && !whitespace(*s))
231 dynstr_append(&type, tolower((unsigned char)*s++));
232 if(!(s = skipwhite(s, 1))) return -1;
235 dynstr_init(¶metername);
237 if(!(s = skipwhite(s, 1))) return -1;
239 while(*s && !tspecial(*s) && !whitespace(*s))
240 dynstr_append(¶metername, tolower((unsigned char)*s++));
241 if(!(s = skipwhite(s, 1))) return -1;
242 if(*s++ != '=') return -1;
243 if(!(s = skipwhite(s, 1))) return -1;
244 if(!(s = parseword(s, parametervaluep, tspecial))) return -1;
245 if(!(s = skipwhite(s, 1))) return -1;
246 dynstr_terminate(¶metername);
247 *parameternamep = parametername.vec;
249 *parametervaluep = *parameternamep = 0;
250 dynstr_terminate(&type);
255 /** @brief Parse a MIME message
256 * @param s Start of message
257 * @param callback Called for each header field
258 * @param u Passed to callback
259 * @return Pointer to decoded body (might be in original string), or NULL on error
261 * This does an RFC 822-style parse and honors Content-Transfer-Encoding as
262 * described in <a href="http://tools.ietf.org/html/rfc2045#section-6">RFC 2045
263 * s6</a>. @p callback is called for each header field encountered, in order,
264 * with ASCII characters in the header name forced to lower case.
266 const char *mime_parse(const char *s,
267 int (*callback)(const char *name, const char *value,
270 struct dynstr name, value;
273 while(*s && !iscrlf(s)) {
276 while(*s && !tspecial(*s) && !whitespace(*s))
277 dynstr_append(&name, tolower((unsigned char)*s++));
278 if(!(s = skipwhite(s, 1))) return 0;
279 if(*s != ':') return 0;
281 while(*s && !(*s == '\n' && !(s[1] == ' ' || s[1] == '\t')))
282 dynstr_append(&value, *s++);
284 dynstr_terminate(&name);
285 dynstr_terminate(&value);
286 if(!strcmp(name.vec, "content-transfer-encoding")) {
287 cte = xstrdup(value.vec);
288 for(p = cte; *p; p++)
289 *p = tolower((unsigned char)*p);
291 if(callback(name.vec, value.vec, u)) return 0;
295 if(!strcmp(cte, "base64")) return mime_base64(s, 0);
296 if(!strcmp(cte, "quoted-printable")) return mime_qp(s);
301 /** @brief Match the boundary string */
302 static int isboundary(const char *ptr, const char *boundary, size_t bl) {
303 return (ptr[0] == '-'
305 && !strncmp(ptr + 2, boundary, bl)
306 && (iscrlf(ptr + bl + 2)
307 || (ptr[bl + 2] == '-'
308 && ptr[bl + 3] == '-'
309 && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0))));
312 /** @brief Match the final boundary string */
313 static int isfinal(const char *ptr, const char *boundary, size_t bl) {
314 return (ptr[0] == '-'
316 && !strncmp(ptr + 2, boundary, bl)
317 && ptr[bl + 2] == '-'
318 && ptr[bl + 3] == '-'
319 && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0));
322 /** @brief Parse a multipart MIME body
323 * @param s Start of message
324 * @param callback Callback for each part
325 * @param boundary Boundary string
326 * @param u Passed to callback
327 * @return 0 on success, non-0 on error
329 * See <a href="http://tools.ietf.org/html/rfc2046#section-5.1">RFC 2046
330 * s5.1</a>. @p callback is called for each part (not yet decoded in any way)
331 * in succession; you should probably call mime_parse() for each part.
333 int mime_multipart(const char *s,
334 int (*callback)(const char *s, void *u),
335 const char *boundary,
337 size_t bl = strlen(boundary);
338 const char *start, *e;
341 /* We must start with a boundary string */
342 if(!isboundary(s, boundary, bl))
344 /* Keep going until we hit a final boundary */
345 while(!isfinal(s, boundary, bl)) {
346 s = strstr(s, "\r\n") + 2;
348 while(!isboundary(s, boundary, bl)) {
349 if(!(e = strstr(s, "\r\n")))
353 if((ret = callback(xstrndup(start,
354 s == start ? 0 : s - start - 2),
361 /** @brief Parse an RFC2388-style content-disposition field
362 * @param s Start of field
363 * @param typep Where to store type
364 * @param parameternamep Where to store parameter name
365 * @param parameternvaluep Wher to store parameter value
366 * @return 0 on success, non-0 on error
368 * See <a href="http://tools.ietf.org/html/rfc2388#section-3">RFC 2388 s3</a>
369 * and <a href="http://tools.ietf.org/html/rfc2183">RFC 2183</a>.
371 int mime_rfc2388_content_disposition(const char *s,
373 char **parameternamep,
374 char **parametervaluep) {
375 struct dynstr disposition, parametername;
377 dynstr_init(&disposition);
378 if(!(s = skipwhite(s, 1))) return -1;
380 while(*s && !tspecial(*s) && !whitespace(*s))
381 dynstr_append(&disposition, tolower((unsigned char)*s++));
382 if(!(s = skipwhite(s, 1))) return -1;
385 dynstr_init(¶metername);
387 if(!(s = skipwhite(s, 1))) return -1;
389 while(*s && !tspecial(*s) && !whitespace(*s))
390 dynstr_append(¶metername, tolower((unsigned char)*s++));
391 if(!(s = skipwhite(s, 1))) return -1;
392 if(*s++ != '=') return -1;
393 if(!(s = skipwhite(s, 1))) return -1;
394 if(!(s = parseword(s, parametervaluep, tspecial))) return -1;
395 if(!(s = skipwhite(s, 1))) return -1;
396 dynstr_terminate(¶metername);
397 *parameternamep = parametername.vec;
399 *parametervaluep = *parameternamep = 0;
400 dynstr_terminate(&disposition);
401 *dispositionp = disposition.vec;
405 /** @brief Convert MIME quoted-printable
406 * @param s Quoted-printable data
407 * @return Decoded data
409 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.7">RFC 2045
412 char *mime_qp(const char *s) {
421 if((a = unhexdigitq(s[0])) != -1
422 && (b = unhexdigitq(s[1])) != -1) {
423 dynstr_append(&d, a * 16 + b);
427 while(*t == ' ' || *t == '\t') ++t;
429 /* soft line break */
438 while(*t == ' ' || *t == '\t') ++t;
440 /* trailing space is always eliminated */
443 dynstr_append(&d, c);
446 dynstr_append(&d, c);
450 dynstr_terminate(&d);
454 /** @brief Parse a RFC2109 Cookie: header
455 * @param s Header field value
456 * @param cd Where to store result
457 * @return 0 on success, non-0 on error
459 int parse_cookie(const char *s,
460 struct cookiedata *cd) {
463 memset(cd, 0, sizeof *cd);
466 /* Skip separators */
467 if(*s == ';' || *s == ',') {
472 if(!(s = parsetoken(s, &n, http_separator))) return -1;
474 if(*s++ != '=') return -1;
476 if(!(s = parseword(s, &v, http_separator))) return -1;
478 /* Some bit of meta-information */
479 if(!strcmp(n, "$Version"))
481 else if(!strcmp(n, "$Path")) {
482 if(cd->ncookies > 0 && cd->cookies[cd->ncookies-1].path == 0)
483 cd->cookies[cd->ncookies-1].path = v;
485 error(0, "redundant $Path in Cookie: header");
488 } else if(!strcmp(n, "$Domain")) {
489 if(cd->ncookies > 0 && cd->cookies[cd->ncookies-1].domain == 0)
490 cd->cookies[cd->ncookies-1].domain = v;
492 error(0, "redundant $Domain in Cookie: header");
497 /* It's a new cookie */
498 cd->cookies = xrealloc(cd->cookies,
499 (cd->ncookies + 1) * sizeof (struct cookie));
500 cd->cookies[cd->ncookies].name = n;
501 cd->cookies[cd->ncookies].value = v;
502 cd->cookies[cd->ncookies].path = 0;
503 cd->cookies[cd->ncookies].domain = 0;
507 if(*s && (*s != ',' && *s != ';')) {
508 error(0, "missing separator in Cookie: header");
515 /** @brief Find a named cookie
516 * @param cd Parse cookie data
517 * @param name Name of cookie
518 * @return Cookie structure or NULL if not found
520 const struct cookie *find_cookie(const struct cookiedata *cd,
524 for(n = 0; n < cd->ncookies; ++n)
525 if(!strcmp(cd->cookies[n].name, name))
526 return &cd->cookies[n];