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
36 /** @brief Match whitespace characters */
37 static int whitespace(int c
) {
49 /** @brief Match RFC2045 tspecial characters */
50 static int tspecial(int c
) {
73 /** @brief Mathc RFC2616 seprator characters */
74 static int http_separator(int c
) {
101 /** @brief Match CRLF */
102 static int iscrlf(const char *ptr
) {
103 return ptr
[0] == '\r' && ptr
[1] == '\n';
106 /** @brief Skip whitespace
107 * @param rfc822_comments If true, skip RFC822 nested comments
109 static const char *skipwhite(const char *s
, int rfc822_comments
) {
128 case '(': ++depth
; break;
129 case ')': --depth
; break;
144 /** @brief Test for a word character
145 * @param c Character to test
146 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
147 * @return 1 if @p c is a word character, else 0
149 static int iswordchar(int c
, int (*special
)(int)) {
150 return !(c
<= ' ' || c
> '~' || special(c
));
153 /** @brief Parse an RFC1521/RFC2616 word
154 * @param s Pointer to start of word
155 * @param valuep Where to store value
156 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
157 * @return Pointer just after end of word or NULL if there's no word
159 * A word is a token or a quoted-string.
161 static const char *parseword(const char *s
, char **valuep
,
162 int (*special
)(int)) {
163 struct dynstr value
[1];
169 while((c
= *s
++) != '"') {
172 if(!(c
= *s
++)) return 0;
174 dynstr_append(value
, c
);
180 if(!iswordchar((unsigned char)*s
, special
))
183 while(iswordchar((unsigned char)*s
, special
))
184 dynstr_append(value
, *s
++);
186 dynstr_terminate(value
);
187 *valuep
= value
->vec
;
191 /** @brief Parse an RFC1521/RFC2616 token
192 * @param s Pointer to start of token
193 * @param valuep Where to store value
194 * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
195 * @return Pointer just after end of token or NULL if there's no token
197 static const char *parsetoken(const char *s
, char **valuep
,
198 int (*special
)(int)) {
199 if(*s
== '"') return 0;
200 return parseword(s
, valuep
, special
);
203 /** @brief Parse a MIME content-type field
204 * @param s Start of field
205 * @param typep Where to store type
206 * @param parameternamep Where to store parameter name
207 * @param parameternvaluep Wher to store parameter value
208 * @return 0 on success, non-0 on error
210 int mime_content_type(const char *s
,
212 char **parameternamep
,
213 char **parametervaluep
) {
214 struct dynstr type
, parametername
;
217 if(!(s
= skipwhite(s
, 1))) return -1;
219 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
220 dynstr_append(&type
, tolower((unsigned char)*s
++));
221 if(!(s
= skipwhite(s
, 1))) return -1;
222 if(*s
++ != '/') return -1;
223 dynstr_append(&type
, '/');
224 if(!(s
= skipwhite(s
, 1))) return -1;
225 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
226 dynstr_append(&type
, tolower((unsigned char)*s
++));
227 if(!(s
= skipwhite(s
, 1))) return -1;
230 dynstr_init(¶metername
);
232 if(!(s
= skipwhite(s
, 1))) return -1;
234 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
235 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
236 if(!(s
= skipwhite(s
, 1))) return -1;
237 if(*s
++ != '=') return -1;
238 if(!(s
= skipwhite(s
, 1))) return -1;
239 if(!(s
= parseword(s
, parametervaluep
, tspecial
))) return -1;
240 if(!(s
= skipwhite(s
, 1))) return -1;
241 dynstr_terminate(¶metername
);
242 *parameternamep
= parametername
.vec
;
244 *parametervaluep
= *parameternamep
= 0;
245 dynstr_terminate(&type
);
250 /** @brief Parse a MIME message
251 * @param s Start of message
252 * @param callback Called for each header field
253 * @param u Passed to callback
254 * @return Pointer to decoded body (might be in original string)
256 const char *mime_parse(const char *s
,
257 int (*callback
)(const char *name
, const char *value
,
260 struct dynstr name
, value
;
263 while(*s
&& !iscrlf(s
)) {
266 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
267 dynstr_append(&name
, tolower((unsigned char)*s
++));
268 if(!(s
= skipwhite(s
, 1))) return 0;
269 if(*s
!= ':') return 0;
271 while(*s
&& !(*s
== '\n' && !(s
[1] == ' ' || s
[1] == '\t')))
272 dynstr_append(&value
, *s
++);
274 dynstr_terminate(&name
);
275 dynstr_terminate(&value
);
276 if(!strcmp(name
.vec
, "content-transfer-encoding")) {
277 cte
= xstrdup(value
.vec
);
278 for(p
= cte
; *p
; p
++)
279 *p
= tolower((unsigned char)*p
);
281 if(callback(name
.vec
, value
.vec
, u
)) return 0;
285 if(!strcmp(cte
, "base64")) return mime_base64(s
);
286 if(!strcmp(cte
, "quoted-printable")) return mime_qp(s
);
291 static int isboundary(const char *ptr
, const char *boundary
, size_t bl
) {
292 return (ptr
[0] == '-'
294 && !strncmp(ptr
+ 2, boundary
, bl
)
295 && (iscrlf(ptr
+ bl
+ 2)
296 || (ptr
[bl
+ 2] == '-'
297 && ptr
[bl
+ 3] == '-'
298 && iscrlf(ptr
+ bl
+ 4))));
301 static int isfinal(const char *ptr
, const char *boundary
, size_t bl
) {
302 return (ptr
[0] == '-'
304 && !strncmp(ptr
+ 2, boundary
, bl
)
305 && ptr
[bl
+ 2] == '-'
306 && ptr
[bl
+ 3] == '-'
307 && iscrlf(ptr
+ bl
+ 4));
310 /** @brief Parse a multipart MIME body
311 * @param s Start of message
312 * @param callback CAllback for each part
313 * @param boundary Boundary string
314 * @param u Passed to callback
315 * @return 0 on success, non-0 on error
317 int mime_multipart(const char *s
,
318 int (*callback
)(const char *s
, void *u
),
319 const char *boundary
,
321 size_t bl
= strlen(boundary
);
322 const char *start
, *e
;
325 if(!isboundary(s
, boundary
, bl
)) return -1;
326 while(!isfinal(s
, boundary
, bl
)) {
327 s
= strstr(s
, "\r\n") + 2;
329 while(!isboundary(s
, boundary
, bl
)) {
330 if(!(e
= strstr(s
, "\r\n"))) return -1;
333 if((ret
= callback(xstrndup(start
,
334 s
== start ?
0 : s
- start
- 2),
341 /** @brief Parse an RFC2388-style content-disposition field
342 * @param s Start of field
343 * @param typep Where to store type
344 * @param parameternamep Where to store parameter name
345 * @param parameternvaluep Wher to store parameter value
346 * @return 0 on success, non-0 on error
348 int mime_rfc2388_content_disposition(const char *s
,
350 char **parameternamep
,
351 char **parametervaluep
) {
352 struct dynstr disposition
, parametername
;
354 dynstr_init(&disposition
);
355 if(!(s
= skipwhite(s
, 1))) return -1;
357 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
358 dynstr_append(&disposition
, tolower((unsigned char)*s
++));
359 if(!(s
= skipwhite(s
, 1))) return -1;
362 dynstr_init(¶metername
);
364 if(!(s
= skipwhite(s
, 1))) return -1;
366 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
367 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
368 if(!(s
= skipwhite(s
, 1))) return -1;
369 if(*s
++ != '=') return -1;
370 if(!(s
= skipwhite(s
, 1))) return -1;
371 if(!(s
= parseword(s
, parametervaluep
, tspecial
))) return -1;
372 if(!(s
= skipwhite(s
, 1))) return -1;
373 dynstr_terminate(¶metername
);
374 *parameternamep
= parametername
.vec
;
376 *parametervaluep
= *parameternamep
= 0;
377 dynstr_terminate(&disposition
);
378 *dispositionp
= disposition
.vec
;
382 /** @brief Convert MIME quoted-printable
383 * @param s Quoted-printable data
384 * @return Decoded data
386 char *mime_qp(const char *s
) {
395 if((a
= unhexdigitq(s
[0])) != -1
396 && (b
= unhexdigitq(s
[1])) != -1) {
397 dynstr_append(&d
, a
* 16 + b
);
401 while(*t
== ' ' || *t
== '\t') ++t
;
403 /* soft line break */
412 while(*t
== ' ' || *t
== '\t') ++t
;
414 /* trailing space is always eliminated */
417 dynstr_append(&d
, c
);
420 dynstr_append(&d
, c
);
424 dynstr_terminate(&d
);
428 /** @brief Convert MIME base64
429 * @param s base64 data
430 * @return Decoded data
432 char *mime_base64(const char *s
) {
436 static const char table
[] =
437 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
441 while((c
= (unsigned char)*s
++)) {
442 if((t
= strchr(table
, c
))) {
445 dynstr_append(&d
, (b
[0] << 2) + (b
[1] >> 4));
446 dynstr_append(&d
, (b
[1] << 4) + (b
[2] >> 2));
447 dynstr_append(&d
, (b
[2] << 6) + b
[3]);
450 } else if(c
== '=') {
452 dynstr_append(&d
, (b
[0] << 2) + (b
[1] >> 4));
454 dynstr_append(&d
, (b
[1] << 4) + (b
[2] >> 2));
459 dynstr_terminate(&d
);
463 /** @brief Parse a RFC2109 Cookie: header
464 * @param s Header field value
465 * @param cd Where to store result
466 * @return 0 on success, non-0 on error
468 int parse_cookie(const char *s
,
469 struct cookiedata
*cd
) {
472 memset(cd
, 0, sizeof *cd
);
475 /* Skip separators */
476 if(*s
== ';' || *s
== ',') {
481 if(!(s
= parsetoken(s
, &n
, http_separator
))) return -1;
483 if(*s
++ != '=') return -1;
485 if(!(s
= parseword(s
, &v
, http_separator
))) return -1;
487 /* Some bit of meta-information */
488 if(!strcmp(n
, "$Version"))
490 else if(!strcmp(n
, "$Path")) {
491 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].path
== 0)
492 cd
->cookies
[cd
->ncookies
-1].path
= v
;
494 error(0, "redundant $Path in Cookie: header");
497 } else if(!strcmp(n
, "$Domain")) {
498 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].domain
== 0)
499 cd
->cookies
[cd
->ncookies
-1].domain
= v
;
501 error(0, "redundant $Domain in Cookie: header");
506 /* It's a new cookie */
507 cd
->cookies
= xrealloc(cd
->cookies
,
508 (cd
->ncookies
+ 1) * sizeof (struct cookie
));
509 cd
->cookies
[cd
->ncookies
].name
= n
;
510 cd
->cookies
[cd
->ncookies
].value
= v
;
511 cd
->cookies
[cd
->ncookies
].path
= 0;
512 cd
->cookies
[cd
->ncookies
].domain
= 0;
516 if(*s
&& (*s
!= ',' && *s
!= ';')) {
517 error(0, "missing separator in Cookie: header");
524 /** @brief Find a named cookie
525 * @param cd Parse cookie data
526 * @param name Name of cookie
527 * @return Cookie structure or NULL if not found
529 const struct cookie
*find_cookie(const struct cookiedata
*cd
,
533 for(n
= 0; n
< cd
->ncookies
; ++n
)
534 if(!strcmp(cd
->cookies
[n
].name
, name
))
535 return &cd
->cookies
[n
];