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 int mime_content_type(const char *s
,
215 char **parameternamep
,
216 char **parametervaluep
) {
217 struct dynstr type
, parametername
;
220 if(!(s
= skipwhite(s
, 1))) return -1;
222 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
223 dynstr_append(&type
, tolower((unsigned char)*s
++));
224 if(!(s
= skipwhite(s
, 1))) return -1;
225 if(*s
++ != '/') return -1;
226 dynstr_append(&type
, '/');
227 if(!(s
= skipwhite(s
, 1))) return -1;
228 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
229 dynstr_append(&type
, tolower((unsigned char)*s
++));
230 if(!(s
= skipwhite(s
, 1))) return -1;
233 dynstr_init(¶metername
);
235 if(!(s
= skipwhite(s
, 1))) return -1;
237 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
238 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
239 if(!(s
= skipwhite(s
, 1))) return -1;
240 if(*s
++ != '=') return -1;
241 if(!(s
= skipwhite(s
, 1))) return -1;
242 if(!(s
= parseword(s
, parametervaluep
, tspecial
))) return -1;
243 if(!(s
= skipwhite(s
, 1))) return -1;
244 dynstr_terminate(¶metername
);
245 *parameternamep
= parametername
.vec
;
247 *parametervaluep
= *parameternamep
= 0;
248 dynstr_terminate(&type
);
253 /** @brief Parse a MIME message
254 * @param s Start of message
255 * @param callback Called for each header field
256 * @param u Passed to callback
257 * @return Pointer to decoded body (might be in original string)
259 const char *mime_parse(const char *s
,
260 int (*callback
)(const char *name
, const char *value
,
263 struct dynstr name
, value
;
266 while(*s
&& !iscrlf(s
)) {
269 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
270 dynstr_append(&name
, tolower((unsigned char)*s
++));
271 if(!(s
= skipwhite(s
, 1))) return 0;
272 if(*s
!= ':') return 0;
274 while(*s
&& !(*s
== '\n' && !(s
[1] == ' ' || s
[1] == '\t')))
275 dynstr_append(&value
, *s
++);
277 dynstr_terminate(&name
);
278 dynstr_terminate(&value
);
279 if(!strcmp(name
.vec
, "content-transfer-encoding")) {
280 cte
= xstrdup(value
.vec
);
281 for(p
= cte
; *p
; p
++)
282 *p
= tolower((unsigned char)*p
);
284 if(callback(name
.vec
, value
.vec
, u
)) return 0;
288 if(!strcmp(cte
, "base64")) return mime_base64(s
, 0);
289 if(!strcmp(cte
, "quoted-printable")) return mime_qp(s
);
294 static int isboundary(const char *ptr
, const char *boundary
, size_t bl
) {
295 return (ptr
[0] == '-'
297 && !strncmp(ptr
+ 2, boundary
, bl
)
298 && (iscrlf(ptr
+ bl
+ 2)
299 || (ptr
[bl
+ 2] == '-'
300 && ptr
[bl
+ 3] == '-'
301 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0))));
304 static int isfinal(const char *ptr
, const char *boundary
, size_t bl
) {
305 return (ptr
[0] == '-'
307 && !strncmp(ptr
+ 2, boundary
, bl
)
308 && ptr
[bl
+ 2] == '-'
309 && ptr
[bl
+ 3] == '-'
310 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0));
313 /** @brief Parse a multipart MIME body
314 * @param s Start of message
315 * @param callback CAllback for each part
316 * @param boundary Boundary string
317 * @param u Passed to callback
318 * @return 0 on success, non-0 on error
320 int mime_multipart(const char *s
,
321 int (*callback
)(const char *s
, void *u
),
322 const char *boundary
,
324 size_t bl
= strlen(boundary
);
325 const char *start
, *e
;
328 /* We must start with a boundary string */
329 if(!isboundary(s
, boundary
, bl
))
331 /* Keep going until we hit a final boundary */
332 while(!isfinal(s
, boundary
, bl
)) {
333 s
= strstr(s
, "\r\n") + 2;
335 while(!isboundary(s
, boundary
, bl
)) {
336 if(!(e
= strstr(s
, "\r\n")))
340 if((ret
= callback(xstrndup(start
,
341 s
== start ?
0 : s
- start
- 2),
348 /** @brief Parse an RFC2388-style content-disposition field
349 * @param s Start of field
350 * @param typep Where to store type
351 * @param parameternamep Where to store parameter name
352 * @param parameternvaluep Wher to store parameter value
353 * @return 0 on success, non-0 on error
355 int mime_rfc2388_content_disposition(const char *s
,
357 char **parameternamep
,
358 char **parametervaluep
) {
359 struct dynstr disposition
, parametername
;
361 dynstr_init(&disposition
);
362 if(!(s
= skipwhite(s
, 1))) return -1;
364 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
365 dynstr_append(&disposition
, tolower((unsigned char)*s
++));
366 if(!(s
= skipwhite(s
, 1))) return -1;
369 dynstr_init(¶metername
);
371 if(!(s
= skipwhite(s
, 1))) return -1;
373 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
374 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
375 if(!(s
= skipwhite(s
, 1))) return -1;
376 if(*s
++ != '=') return -1;
377 if(!(s
= skipwhite(s
, 1))) return -1;
378 if(!(s
= parseword(s
, parametervaluep
, tspecial
))) return -1;
379 if(!(s
= skipwhite(s
, 1))) return -1;
380 dynstr_terminate(¶metername
);
381 *parameternamep
= parametername
.vec
;
383 *parametervaluep
= *parameternamep
= 0;
384 dynstr_terminate(&disposition
);
385 *dispositionp
= disposition
.vec
;
389 /** @brief Convert MIME quoted-printable
390 * @param s Quoted-printable data
391 * @return Decoded data
393 char *mime_qp(const char *s
) {
402 if((a
= unhexdigitq(s
[0])) != -1
403 && (b
= unhexdigitq(s
[1])) != -1) {
404 dynstr_append(&d
, a
* 16 + b
);
408 while(*t
== ' ' || *t
== '\t') ++t
;
410 /* soft line break */
419 while(*t
== ' ' || *t
== '\t') ++t
;
421 /* trailing space is always eliminated */
424 dynstr_append(&d
, c
);
427 dynstr_append(&d
, c
);
431 dynstr_terminate(&d
);
435 /** @brief Parse a RFC2109 Cookie: header
436 * @param s Header field value
437 * @param cd Where to store result
438 * @return 0 on success, non-0 on error
440 int parse_cookie(const char *s
,
441 struct cookiedata
*cd
) {
444 memset(cd
, 0, sizeof *cd
);
447 /* Skip separators */
448 if(*s
== ';' || *s
== ',') {
453 if(!(s
= parsetoken(s
, &n
, http_separator
))) return -1;
455 if(*s
++ != '=') return -1;
457 if(!(s
= parseword(s
, &v
, http_separator
))) return -1;
459 /* Some bit of meta-information */
460 if(!strcmp(n
, "$Version"))
462 else if(!strcmp(n
, "$Path")) {
463 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].path
== 0)
464 cd
->cookies
[cd
->ncookies
-1].path
= v
;
466 error(0, "redundant $Path in Cookie: header");
469 } else if(!strcmp(n
, "$Domain")) {
470 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].domain
== 0)
471 cd
->cookies
[cd
->ncookies
-1].domain
= v
;
473 error(0, "redundant $Domain in Cookie: header");
478 /* It's a new cookie */
479 cd
->cookies
= xrealloc(cd
->cookies
,
480 (cd
->ncookies
+ 1) * sizeof (struct cookie
));
481 cd
->cookies
[cd
->ncookies
].name
= n
;
482 cd
->cookies
[cd
->ncookies
].value
= v
;
483 cd
->cookies
[cd
->ncookies
].path
= 0;
484 cd
->cookies
[cd
->ncookies
].domain
= 0;
488 if(*s
&& (*s
!= ',' && *s
!= ';')) {
489 error(0, "missing separator in Cookie: header");
496 /** @brief Find a named cookie
497 * @param cd Parse cookie data
498 * @param name Name of cookie
499 * @return Cookie structure or NULL if not found
501 const struct cookie
*find_cookie(const struct cookiedata
*cd
,
505 for(n
= 0; n
< cd
->ncookies
; ++n
)
506 if(!strcmp(cd
->cookies
[n
].name
, name
))
507 return &cd
->cookies
[n
];