2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2007-10, 2013 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
35 /** @brief Match whitespace characters */
36 static int whitespace(int c
) {
48 /** @brief Match RFC2045 tspecial characters */
49 int mime_tspecial(int c
) {
72 /** @brief Match RFC2616 separator characters */
73 int mime_http_separator(int c
) {
100 /** @brief Match CRLF */
101 static int iscrlf(const char *ptr
) {
102 return ptr
[0] == '\r' && ptr
[1] == '\n';
105 /** @brief Skip whitespace
106 * @param s Pointer into string
107 * @param rfc822_comments If true, skip RFC822 nested comments
108 * @return Pointer into string after whitespace
110 static const char *skipwhite(const char *s
, int rfc822_comments
) {
129 case '(': ++depth
; break;
130 case ')': --depth
; break;
147 /** @brief Test for a word character
148 * @param c Character to test
149 * @param special mime_tspecial() (MIME/RFC2405) or mime_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 mime_tspecial() (MIME/RFC2405) or mime_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 const char *mime_parse_word(const char *s
, char **valuep
,
165 int (*special
)(int)) {
166 struct dynstr value
[1];
172 while((c
= *s
++) != '"') {
177 /* else fall through ... */
179 dynstr_append(value
, c
);
186 if(!iswordchar((unsigned char)*s
, special
))
189 while(iswordchar((unsigned char)*s
, special
))
190 dynstr_append(value
, *s
++);
192 dynstr_terminate(value
);
193 *valuep
= value
->vec
;
197 /** @brief Parse an RFC1521/RFC2616 token
198 * @param s Pointer to start of token
199 * @param valuep Where to store value
200 * @param special mime_tspecial() (MIME/RFC2405) or mime_http_separator() (HTTP/RFC2616)
201 * @return Pointer just after end of token or NULL if there's no token
203 static const char *parsetoken(const char *s
, char **valuep
,
204 int (*special
)(int)) {
207 return mime_parse_word(s
, valuep
, special
);
210 /** @brief Parse a MIME content-type field
211 * @param s Start of field
212 * @param typep Where to store type
213 * @param parametersp Where to store parameter list
214 * @return 0 on success, non-0 on error
216 * See <a href="http://tools.ietf.org/html/rfc2045#section-5">RFC 2045 s5</a>.
218 int mime_content_type(const char *s
,
220 struct kvp
**parametersp
) {
221 struct dynstr type
, parametername
;
222 struct kvp
*parameters
= 0;
223 char *parametervalue
;
226 if(!(s
= skipwhite(s
, 1)))
230 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
231 dynstr_append(&type
, tolower((unsigned char)*s
++));
232 if(!(s
= skipwhite(s
, 1)))
236 dynstr_append(&type
, '/');
237 if(!(s
= skipwhite(s
, 1)))
239 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
240 dynstr_append(&type
, tolower((unsigned char)*s
++));
241 if(!(s
= skipwhite(s
, 1)))
245 dynstr_init(¶metername
);
247 if(!(s
= skipwhite(s
, 1)))
251 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
252 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
253 if(!(s
= skipwhite(s
, 1)))
257 if(!(s
= skipwhite(s
, 1)))
259 if(!(s
= mime_parse_word(s
, ¶metervalue
, mime_tspecial
)))
261 if(!(s
= skipwhite(s
, 1)))
263 dynstr_terminate(¶metername
);
264 kvp_set(¶meters
, parametername
.vec
, parametervalue
);
266 dynstr_terminate(&type
);
268 *parametersp
= parameters
;
272 /** @brief Parse a MIME message
273 * @param s Start of message
274 * @param callback Called for each header field
275 * @param u Passed to callback
276 * @return Pointer to decoded body (might be in original string), or NULL on error
278 * This does an RFC 822-style parse and honors Content-Transfer-Encoding as
279 * described in <a href="http://tools.ietf.org/html/rfc2045#section-6">RFC 2045
280 * s6</a>. @p callback is called for each header field encountered, in order,
281 * with ASCII characters in the header name forced to lower case.
283 const char *mime_parse(const char *s
,
284 int (*callback
)(const char *name
, const char *value
,
287 struct dynstr name
, value
;
290 while(*s
&& !iscrlf(s
)) {
293 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
294 dynstr_append(&name
, tolower((unsigned char)*s
++));
295 if(!(s
= skipwhite(s
, 1)))
300 while(*s
&& !(*s
== '\n' && !(s
[1] == ' ' || s
[1] == '\t'))) {
302 /* Strip leading whitespace */
303 if(value
.nvec
|| !(c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r'))
304 dynstr_append(&value
, c
);
306 /* Strip trailing whitespace */
307 while(value
.nvec
> 0 && (value
.vec
[value
.nvec
- 1] == ' '
308 || value
.vec
[value
.nvec
- 1] == '\t'
309 || value
.vec
[value
.nvec
- 1] == '\n'
310 || value
.vec
[value
.nvec
- 1] == '\r'))
314 dynstr_terminate(&name
);
315 dynstr_terminate(&value
);
316 if(!strcmp(name
.vec
, "content-transfer-encoding")) {
317 cte
= xstrdup(value
.vec
);
318 for(p
= cte
; *p
; p
++)
319 *p
= tolower((unsigned char)*p
);
321 if(callback(name
.vec
, value
.vec
, u
))
327 if(!strcmp(cte
, "base64"))
328 return mime_base64(s
, 0);
329 if(!strcmp(cte
, "quoted-printable"))
331 if(!strcmp(cte
, "7bit") || !strcmp(cte
, "8bit"))
333 disorder_error(0, "unknown content-transfer-encoding '%s'", cte
);
339 /** @brief Match the boundary string */
340 static int isboundary(const char *ptr
, const char *boundary
, size_t bl
) {
341 return (ptr
[0] == '-'
343 && !strncmp(ptr
+ 2, boundary
, bl
)
344 && (iscrlf(ptr
+ bl
+ 2)
345 || (ptr
[bl
+ 2] == '-'
346 && ptr
[bl
+ 3] == '-'
347 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0))));
350 /** @brief Match the final boundary string */
351 static int isfinal(const char *ptr
, const char *boundary
, size_t bl
) {
352 return (ptr
[0] == '-'
354 && !strncmp(ptr
+ 2, boundary
, bl
)
355 && ptr
[bl
+ 2] == '-'
356 && ptr
[bl
+ 3] == '-'
357 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0));
360 /** @brief Parse a multipart MIME body
361 * @param s Start of message
362 * @param callback Callback for each part
363 * @param boundary Boundary string
364 * @param u Passed to callback
365 * @return 0 on success, non-0 on error
367 * See <a href="http://tools.ietf.org/html/rfc2046#section-5.1">RFC 2046
368 * s5.1</a>. @p callback is called for each part (not yet decoded in any way)
369 * in succession; you should probably call mime_parse() for each part.
371 int mime_multipart(const char *s
,
372 int (*callback
)(const char *s
, void *u
),
373 const char *boundary
,
375 size_t bl
= strlen(boundary
);
376 const char *start
, *e
;
379 /* We must start with a boundary string */
380 if(!isboundary(s
, boundary
, bl
)) {
381 disorder_error(0, "mime_multipart: first line is not the boundary string");
384 /* Keep going until we hit a final boundary */
385 while(!isfinal(s
, boundary
, bl
)) {
386 s
= strstr(s
, "\r\n") + 2;
388 while(!isboundary(s
, boundary
, bl
)) {
389 if(!(e
= strstr(s
, "\r\n"))) {
390 disorder_error(0, "mime_multipart: line does not end CRLF");
395 if((ret
= callback(xstrndup(start
,
396 s
== start ?
0 : s
- start
- 2),
403 /** @brief Parse an RFC2388-style content-disposition field
404 * @param s Start of field
405 * @param dispositionp Where to store disposition
406 * @param parameternamep Where to store parameter name
407 * @param parametervaluep Where to store parameter value
408 * @return 0 on success, non-0 on error
410 * See <a href="http://tools.ietf.org/html/rfc2388#section-3">RFC 2388 s3</a>
411 * and <a href="http://tools.ietf.org/html/rfc2183">RFC 2183</a>.
413 int mime_rfc2388_content_disposition(const char *s
,
415 char **parameternamep
,
416 char **parametervaluep
) {
417 struct dynstr disposition
, parametername
;
419 dynstr_init(&disposition
);
420 if(!(s
= skipwhite(s
, 1)))
424 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
425 dynstr_append(&disposition
, tolower((unsigned char)*s
++));
426 if(!(s
= skipwhite(s
, 1)))
430 dynstr_init(¶metername
);
432 if(!(s
= skipwhite(s
, 1)))
436 while(*s
&& !mime_tspecial(*s
) && !whitespace(*s
))
437 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
438 if(!(s
= skipwhite(s
, 1)))
442 if(!(s
= skipwhite(s
, 1)))
444 if(!(s
= mime_parse_word(s
, parametervaluep
, mime_tspecial
)))
446 if(!(s
= skipwhite(s
, 1)))
448 dynstr_terminate(¶metername
);
449 *parameternamep
= parametername
.vec
;
451 *parametervaluep
= *parameternamep
= 0;
452 dynstr_terminate(&disposition
);
453 *dispositionp
= disposition
.vec
;
457 /** @brief Convert MIME quoted-printable
458 * @param s Quoted-printable data
459 * @return Decoded data
461 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.7">RFC 2045
464 char *mime_qp(const char *s
) {
473 if((a
= unhexdigitq(s
[0])) != -1
474 && (b
= unhexdigitq(s
[1])) != -1) {
475 dynstr_append(&d
, a
* 16 + b
);
479 while(*t
== ' ' || *t
== '\t') ++t
;
481 /* soft line break */
490 while(*t
== ' ' || *t
== '\t') ++t
;
492 /* trailing space is always eliminated */
495 dynstr_append(&d
, c
);
498 dynstr_append(&d
, c
);
502 dynstr_terminate(&d
);
506 /** @brief Match cookie separator characters
508 * This is a subset of the RFC2616 specials, and technically is in breach of
509 * the specification. However rejecting (in particular) slashes is
510 * unreasonably strict and has broken at least one (admittedly somewhat
511 * obscure) browser, so we're more forgiving.
513 static int cookie_separator(int c
) {
530 /** @brief Match cookie value separator characters
532 * Same as cookie_separator() but allows for @c = in cookie values.
534 static int cookie_value_separator(int c
) {
550 /** @brief Parse a RFC2109 Cookie: header
551 * @param s Header field value
552 * @param cd Where to store result
553 * @return 0 on success, non-0 on error
555 * See <a href="http://tools.ietf.org/html/rfc2109">RFC 2109</a>.
557 int parse_cookie(const char *s
,
558 struct cookiedata
*cd
) {
561 memset(cd
, 0, sizeof *cd
);
564 /* Skip separators */
565 if(*s
== ';' || *s
== ',') {
570 if(!(s
= parsetoken(s
, &n
, cookie_separator
))) {
571 disorder_error(0, "parse_cookie: cannot parse attribute name");
576 disorder_error(0, "parse_cookie: did not find expected '='");
580 if(!(s
= mime_parse_word(s
, &v
, cookie_value_separator
))) {
581 disorder_error(0, "parse_cookie: cannot parse value for '%s'", n
);
585 /* Some bit of meta-information */
586 if(!strcmp(n
, "$Version"))
588 else if(!strcmp(n
, "$Path")) {
589 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].path
== 0)
590 cd
->cookies
[cd
->ncookies
-1].path
= v
;
592 disorder_error(0, "redundant $Path in Cookie: header");
595 } else if(!strcmp(n
, "$Domain")) {
596 if(cd
->ncookies
> 0 && cd
->cookies
[cd
->ncookies
-1].domain
== 0)
597 cd
->cookies
[cd
->ncookies
-1].domain
= v
;
599 disorder_error(0, "redundant $Domain in Cookie: header");
604 /* It's a new cookie */
605 cd
->cookies
= xrealloc(cd
->cookies
,
606 (cd
->ncookies
+ 1) * sizeof (struct cookie
));
607 cd
->cookies
[cd
->ncookies
].name
= n
;
608 cd
->cookies
[cd
->ncookies
].value
= v
;
609 cd
->cookies
[cd
->ncookies
].path
= 0;
610 cd
->cookies
[cd
->ncookies
].domain
= 0;
614 if(*s
&& (*s
!= ',' && *s
!= ';')) {
615 disorder_error(0, "missing separator in Cookie: header");
622 /** @brief Find a named cookie
623 * @param cd Parse cookie data
624 * @param name Name of cookie
625 * @return Cookie structure or NULL if not found
627 const struct cookie
*find_cookie(const struct cookiedata
*cd
,
631 for(n
= 0; n
< cd
->ncookies
; ++n
)
632 if(!strcmp(cd
->cookies
[n
].name
, name
))
633 return &cd
->cookies
[n
];
637 /** @brief RFC822 quoting
638 * @param s String to quote
639 * @param force If non-0, always quote
640 * @return Possibly quoted string
642 char *quote822(const char *s
, int force
) {
648 /* See if we need to quote */
649 for(t
= s
; (c
= (unsigned char)*t
); ++t
) {
650 if(mime_tspecial(c
) || mime_http_separator(c
) || whitespace(c
))
661 dynstr_append(d
, '"');
662 for(t
= s
; (c
= (unsigned char)*t
); ++t
) {
663 if(c
== '"' || c
== '\\')
664 dynstr_append(d
, '\\');
667 dynstr_append(d
, '"');
672 /** @brief Return true if @p ptr points at trailing space */
673 static int is_trailing_space(const char *ptr
) {
674 if(*ptr
== ' ' || *ptr
== '\t') {
675 while(*ptr
== ' ' || *ptr
== '\t')
677 return *ptr
== '\n' || *ptr
== 0;
682 /** @brief Encoding text as quoted-printable
683 * @param text String to encode
684 * @return Encoded string
686 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.7">RFC2045
689 char *mime_to_qp(const char *text
) {
691 int linelength
= 0; /* length of current line */
696 * 1. Anything except newline can be replaced with =%02X
697 * 2. Newline, 33-60 and 62-126 stand for themselves (i.e. not '=')
698 * 3. Non-trailing space/tab stand for themselves.
699 * 4. Output lines are limited to 76 chars, with =<newline> being used
700 * as a soft line break
701 * 5. Newlines aren't counted towards the 76 char limit.
704 const int c
= (unsigned char)*text
;
706 /* Newline stands as itself */
707 dynstr_append(d
, '\n');
709 } else if((c
>= 33 && c
<= 126 && c
!= '=')
710 || ((c
== ' ' || c
== '\t')
711 && !is_trailing_space(text
))) {
712 /* Things that can stand for themselves */
716 /* Anything else that needs encoding */
717 byte_snprintf(buffer
, sizeof buffer
, "=%02X", c
);
718 dynstr_append_string(d
, buffer
);
722 if(linelength
> 73 && *text
&& *text
!= '\n') {
723 /* Next character might overflow 76 character limit if encoded, so we
724 * insert a soft break */
725 dynstr_append_string(d
, "=\n");
729 /* Ensure there is a final newline */
731 dynstr_append(d
, '\n');
737 /** @brief Encode text
738 * @param text Underlying UTF-8 text
739 * @param charsetp Where to store charset string
740 * @param encodingp Where to store encoding string
741 * @return Encoded text (might be @p text)
743 const char *mime_encode_text(const char *text
,
744 const char **charsetp
,
745 const char **encodingp
) {
748 /* See if there are in fact any non-ASCII characters */
749 for(ptr
= text
; *ptr
; ++ptr
)
750 if((unsigned char)*ptr
>= 128)
753 /* Plain old ASCII, no encoding required */
754 *charsetp
= "us-ascii";
759 *encodingp
= "quoted-printable";
760 return mime_to_qp(text
);