2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2007, 2008 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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
19 * @brief Support for MIME and allied protocols
34 /** @brief Match whitespace characters */
35 static int whitespace(int c
) {
47 /** @brief Match RFC2045 tspecial characters */
48 int mime_tspecial(int c
) {
71 /** @brief Match RFC2616 separator characters */
72 int mime_http_separator(int c
) {
99 /** @brief Match CRLF */
100 static int iscrlf(const char *ptr
) {
101 return ptr
[0] == '\r' && ptr
[1] == '\n';
104 /** @brief Skip whitespace
105 * @param s Pointer into string
106 * @param rfc822_comments If true, skip RFC822 nested comments
107 * @return Pointer into string after whitespace
109 static const char *skipwhite(const char *s
, int rfc822_comments
) {
128 case '(': ++depth
; break;
129 case ')': --depth
; break;
146 /** @brief Test for a word character
147 * @param c Character to test
148 * @param special mime_tspecial() (MIME/RFC2405) or mime_http_separator() (HTTP/RFC2616)
149 * @return 1 if @p c is a word character, else 0
151 static int iswordchar(int c
, int (*special
)(int)) {
152 return !(c
<= ' ' || c
> '~' || special(c
));
155 /** @brief Parse an RFC1521/RFC2616 word
156 * @param s Pointer to start of word
157 * @param valuep Where to store value
158 * @param special mime_tspecial() (MIME/RFC2405) or mime_http_separator() (HTTP/RFC2616)
159 * @return Pointer just after end of word or NULL if there's no word
161 * A word is a token or a quoted-string.
163 const char *mime_parse_word(const char *s
, char **valuep
,
164 int (*special
)(int)) {
165 struct dynstr value
[1];
171 while((c
= *s
++) != '"') {
177 dynstr_append(value
, c
);
184 if(!iswordchar((unsigned char)*s
, special
))
187 while(iswordchar((unsigned char)*s
, special
))
188 dynstr_append(value
, *s
++);
190 dynstr_terminate(value
);
191 *valuep
= value
->vec
;
195 /** @brief Parse an RFC1521/RFC2616 token
196 * @param s Pointer to start of token
197 * @param valuep Where to store value
198 * @param special mime_tspecial() (MIME/RFC2405) or mime_http_separator() (HTTP/RFC2616)
199 * @return Pointer just after end of token or NULL if there's no token
201 static const char *parsetoken(const char *s
, char **valuep
,
202 int (*special
)(int)) {
205 return mime_parse_word(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 parametersp Where to store parameter list
212 * @return 0 on success, non-0 on error
214 * See <a href="http://tools.ietf.org/html/rfc2045#section-5">RFC 2045 s5</a>.
216 int mime_content_type(const char *s
,
218 struct kvp
**parametersp
) {
219 struct dynstr type
, parametername
;
220 struct kvp
*parameters
= 0;
221 char *parametervalue
;
224 if(!(s
= skipwhite(s
, 1)))
228 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
229 dynstr_append(&type
, tolower((unsigned char)*s
++));
230 if(!(s
= skipwhite(s
, 1)))
234 dynstr_append(&type
, '/');
235 if(!(s
= skipwhite(s
, 1)))
237 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
238 dynstr_append(&type
, tolower((unsigned char)*s
++));
239 if(!(s
= skipwhite(s
, 1)))
243 dynstr_init(¶metername
);
245 if(!(s
= skipwhite(s
, 1)))
249 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
250 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
251 if(!(s
= skipwhite(s
, 1)))
255 if(!(s
= skipwhite(s
, 1)))
257 if(!(s
= mime_parse_word(s
, ¶metervalue
, mime_tspecial
)))
259 if(!(s
= skipwhite(s
, 1)))
261 dynstr_terminate(¶metername
);
262 kvp_set(¶meters
, parametername
.vec
, parametervalue
);
264 dynstr_terminate(&type
);
266 *parametersp
= parameters
;
270 /** @brief Parse a MIME message
271 * @param s Start of message
272 * @param callback Called for each header field
273 * @param u Passed to callback
274 * @return Pointer to decoded body (might be in original string), or NULL on error
276 * This does an RFC 822-style parse and honors Content-Transfer-Encoding as
277 * described in <a href="http://tools.ietf.org/html/rfc2045#section-6">RFC 2045
278 * s6</a>. @p callback is called for each header field encountered, in order,
279 * with ASCII characters in the header name forced to lower case.
281 const char *mime_parse(const char *s
,
282 int (*callback
)(const char *name
, const char *value
,
285 struct dynstr name
, value
;
288 while(*s
&& !iscrlf(s
)) {
291 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
292 dynstr_append(&name
, tolower((unsigned char)*s
++));
293 if(!(s
= skipwhite(s
, 1)))
298 while(*s
&& !(*s
== '\n' && !(s
[1] == ' ' || s
[1] == '\t'))) {
300 /* Strip leading whitespace */
301 if(value
.nvec
|| !(c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r'))
302 dynstr_append(&value
, c
);
304 /* Strip trailing whitespace */
305 while(value
.nvec
> 0 && (value
.vec
[value
.nvec
- 1] == ' '
306 || value
.vec
[value
.nvec
- 1] == '\t'
307 || value
.vec
[value
.nvec
- 1] == '\n'
308 || value
.vec
[value
.nvec
- 1] == '\r'))
312 dynstr_terminate(&name
);
313 dynstr_terminate(&value
);
314 if(!strcmp(name
.vec
, "content-transfer-encoding")) {
315 cte
= xstrdup(value
.vec
);
316 for(p
= cte
; *p
; p
++)
317 *p
= tolower((unsigned char)*p
);
319 if(callback(name
.vec
, value
.vec
, u
))
325 if(!strcmp(cte
, "base64"))
326 return mime_base64(s
, 0);
327 if(!strcmp(cte
, "quoted-printable"))
329 if(!strcmp(cte
, "7bit") || !strcmp(cte
, "8bit"))
331 error(0, "unknown content-transfer-encoding '%s'", cte
);
337 /** @brief Match the boundary string */
338 static int isboundary(const char *ptr
, const char *boundary
, size_t bl
) {
339 return (ptr
[0] == '-'
341 && !strncmp(ptr
+ 2, boundary
, bl
)
342 && (iscrlf(ptr
+ bl
+ 2)
343 || (ptr
[bl
+ 2] == '-'
344 && ptr
[bl
+ 3] == '-'
345 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0))));
348 /** @brief Match the final boundary string */
349 static int isfinal(const char *ptr
, const char *boundary
, size_t bl
) {
350 return (ptr
[0] == '-'
352 && !strncmp(ptr
+ 2, boundary
, bl
)
353 && ptr
[bl
+ 2] == '-'
354 && ptr
[bl
+ 3] == '-'
355 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0));
358 /** @brief Parse a multipart MIME body
359 * @param s Start of message
360 * @param callback Callback for each part
361 * @param boundary Boundary string
362 * @param u Passed to callback
363 * @return 0 on success, non-0 on error
365 * See <a href="http://tools.ietf.org/html/rfc2046#section-5.1">RFC 2046
366 * s5.1</a>. @p callback is called for each part (not yet decoded in any way)
367 * in succession; you should probably call mime_parse() for each part.
369 int mime_multipart(const char *s
,
370 int (*callback
)(const char *s
, void *u
),
371 const char *boundary
,
373 size_t bl
= strlen(boundary
);
374 const char *start
, *e
;
377 /* We must start with a boundary string */
378 if(!isboundary(s
, boundary
, bl
)) {
379 error(0, "mime_multipart: first line is not the boundary string");
382 /* Keep going until we hit a final boundary */
383 while(!isfinal(s
, boundary
, bl
)) {
384 s
= strstr(s
, "\r\n") + 2;
386 while(!isboundary(s
, boundary
, bl
)) {
387 if(!(e
= strstr(s
, "\r\n"))) {
388 error(0, "mime_multipart: line does not end CRLF");
393 if((ret
= callback(xstrndup(start
,
394 s
== start ?
0 : s
- start
- 2),
401 /** @brief Parse an RFC2388-style content-disposition field
402 * @param s Start of field
403 * @param dispositionp Where to store disposition
404 * @param parameternamep Where to store parameter name
405 * @param parametervaluep Wher to store parameter value
406 * @return 0 on success, non-0 on error
408 * See <a href="http://tools.ietf.org/html/rfc2388#section-3">RFC 2388 s3</a>
409 * and <a href="http://tools.ietf.org/html/rfc2183">RFC 2183</a>.
411 int mime_rfc2388_content_disposition(const char *s
,
413 char **parameternamep
,
414 char **parametervaluep
) {
415 struct dynstr disposition
, parametername
;
417 dynstr_init(&disposition
);
418 if(!(s
= skipwhite(s
, 1)))
422 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
423 dynstr_append(&disposition
, tolower((unsigned char)*s
++));
424 if(!(s
= skipwhite(s
, 1)))
428 dynstr_init(¶metername
);
430 if(!(s
= skipwhite(s
, 1)))
434 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
435 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
436 if(!(s
= skipwhite(s
, 1)))
440 if(!(s
= skipwhite(s
, 1)))
442 if(!(s
= mime_parse_word(s
, parametervaluep
, mime_tspecial
)))
444 if(!(s
= skipwhite(s
, 1)))
446 dynstr_terminate(¶metername
);
447 *parameternamep
= parametername
.vec
;
449 *parametervaluep
= *parameternamep
= 0;
450 dynstr_terminate(&disposition
);
451 *dispositionp
= disposition
.vec
;
455 /** @brief Convert MIME quoted-printable
456 * @param s Quoted-printable data
457 * @return Decoded data
459 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.7">RFC 2045
462 char *mime_qp(const char *s
) {
471 if((a
= unhexdigitq(s
[0])) != -1
472 && (b
= unhexdigitq(s
[1])) != -1) {
473 dynstr_append(&d
, a
* 16 + b
);
477 while(*t
== ' ' || *t
== '\t') ++t
;
479 /* soft line break */
488 while(*t
== ' ' || *t
== '\t') ++t
;
490 /* trailing space is always eliminated */
493 dynstr_append(&d
, c
);
496 dynstr_append(&d
, c
);
500 dynstr_terminate(&d
);
504 /** @brief Match cookie separator characters
506 * This is a subset of the RFC2616 specials, and technically is in breach of
507 * the specification. However rejecting (in particular) slashes is
508 * unreasonably strict and has broken at least one (admittedly somewhat
509 * obscure) browser, so we're more forgiving.
511 static int cookie_separator(int c
) {
528 /** @brief Match cookie value separator characters
530 * Same as cookie_separator() but allows for @c = in cookie values.
532 static int cookie_value_separator(int c
) {
548 /** @brief Parse a RFC2109 Cookie: header
549 * @param s Header field value
550 * @param cd Where to store result
551 * @return 0 on success, non-0 on error
553 * See <a href="http://tools.ietf.org/html/rfc2109">RFC 2109</a>.
555 int parse_cookie(const char *s
,
556 struct cookiedata
*cd
) {
559 memset(cd
, 0, sizeof *cd
);
562 /* Skip separators */
563 if(*s
== ';' || *s
== ',') {
568 if(!(s
= parsetoken(s
, &n
, cookie_separator
))) {
569 error(0, "parse_cookie: cannot parse attribute name");
574 error(0, "parse_cookie: did not find expected '='");
578 if(!(s
= mime_parse_word(s
, &v
, cookie_value_separator
))) {
579 error(0, "parse_cookie: cannot parse value for '%s'", n
);
583 /* Some bit of meta-information */
584 if(!strcmp(n
, "$Version"))
586 else if(!strcmp(n
, "$Path")) {
587 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].path
== 0)
588 cd
->cookies
[cd
->ncookies
-1].path
= v
;
590 error(0, "redundant $Path in Cookie: header");
593 } else if(!strcmp(n
, "$Domain")) {
594 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].domain
== 0)
595 cd
->cookies
[cd
->ncookies
-1].domain
= v
;
597 error(0, "redundant $Domain in Cookie: header");
602 /* It's a new cookie */
603 cd
->cookies
= xrealloc(cd
->cookies
,
604 (cd
->ncookies
+ 1) * sizeof (struct cookie
));
605 cd
->cookies
[cd
->ncookies
].name
= n
;
606 cd
->cookies
[cd
->ncookies
].value
= v
;
607 cd
->cookies
[cd
->ncookies
].path
= 0;
608 cd
->cookies
[cd
->ncookies
].domain
= 0;
612 if(*s
&& (*s
!= ',' && *s
!= ';')) {
613 error(0, "missing separator in Cookie: header");
620 /** @brief Find a named cookie
621 * @param cd Parse cookie data
622 * @param name Name of cookie
623 * @return Cookie structure or NULL if not found
625 const struct cookie
*find_cookie(const struct cookiedata
*cd
,
629 for(n
= 0; n
< cd
->ncookies
; ++n
)
630 if(!strcmp(cd
->cookies
[n
].name
, name
))
631 return &cd
->cookies
[n
];
635 /** @brief RFC822 quoting
636 * @param s String to quote
637 * @param force If non-0, always quote
638 * @return Possibly quoted string
640 char *quote822(const char *s
, int force
) {
646 /* See if we need to quote */
647 for(t
= s
; (c
= (unsigned char)*t
); ++t
) {
648 if(mime_tspecial(c
) || mime_http_separator(c
) || whitespace(c
))
659 dynstr_append(d
, '"');
660 for(t
= s
; (c
= (unsigned char)*t
); ++t
) {
661 if(c
== '"' || c
== '\\')
662 dynstr_append(d
, '\\');
665 dynstr_append(d
, '"');
670 /** @brief Return true if @p ptr points at trailing space */
671 static int is_trailing_space(const char *ptr
) {
672 if(*ptr
== ' ' || *ptr
== '\t') {
673 while(*ptr
== ' ' || *ptr
== '\t')
675 return *ptr
== '\n' || *ptr
== 0;
680 /** @brief Encoding text as quoted-printable
681 * @param text String to encode
682 * @return Encoded string
684 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.7">RFC2045
687 char *mime_to_qp(const char *text
) {
689 int linelength
= 0; /* length of current line */
694 * 1. Anything except newline can be replaced with =%02X
695 * 2. Newline, 33-60 and 62-126 stand for themselves (i.e. not '=')
696 * 3. Non-trailing space/tab stand for themselves.
697 * 4. Output lines are limited to 76 chars, with =<newline> being used
698 * as a soft line break
699 * 5. Newlines aren't counted towards the 76 char limit.
702 const int c
= (unsigned char)*text
;
704 /* Newline stands as itself */
705 dynstr_append(d
, '\n');
707 } else if((c
>= 33 && c
<= 126 && c
!= '=')
708 || ((c
== ' ' || c
== '\t')
709 && !is_trailing_space(text
))) {
710 /* Things that can stand for themselves */
714 /* Anything else that needs encoding */
715 snprintf(buffer
, sizeof buffer
, "=%02X", c
);
716 dynstr_append_string(d
, buffer
);
720 if(linelength
> 73 && *text
&& *text
!= '\n') {
721 /* Next character might overflow 76 character limit if encoded, so we
722 * insert a soft break */
723 dynstr_append_string(d
, "=\n");
727 /* Ensure there is a final newline */
729 dynstr_append(d
, '\n');
735 /** @brief Encode text
736 * @param text Underlying UTF-8 text
737 * @param charsetp Where to store charset string
738 * @param encodingp Where to store encoding string
739 * @return Encoded text (might be @p text)
741 const char *mime_encode_text(const char *text
,
742 const char **charsetp
,
743 const char **encodingp
) {
746 /* See if there are in fact any non-ASCII characters */
747 for(ptr
= text
; *ptr
; ++ptr
)
748 if((unsigned char)*ptr
>= 128)
751 /* Plain old ASCII, no encoding required */
752 *charsetp
= "us-ascii";
757 *encodingp
= "quoted-printable";
758 return mime_to_qp(text
);