message-parser.c revision 5e63deb8afbcbf466abb84acc3fb28a1e33ab17a
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen/* Copyright (C) 2002 Timo Sirainen */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "lib.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "buffer.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "istream.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "str.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "strescape.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "message-content-parser.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "message-parser.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#include "message-size.h"
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstruct message_boundary {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_boundary *next;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen const char *boundary;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen size_t len;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen};
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstruct message_parser_ctx {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen pool_t parser_pool, part_pool;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct istream *input;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *parts, *part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen char *last_boundary;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen char *last_content_type;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_boundary *boundaries;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_header_callback_t *callback;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_body_callback_t *body_callback;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen void *context;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen};
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstruct message_header_parser_ctx {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_header_line line;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct istream *input;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_size *hdr_size;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen string_t *name;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen buffer_t *value_buf;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen size_t skip;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen int has_nuls;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen};
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_part_header(struct message_parser_ctx *parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic struct message_part *
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_part_body(struct message_parser_ctx *parser_ctx);
f6c1297c26b355c4aec2a08978f51ec3efecb351Timo Sirainen
f6c1297c26b355c4aec2a08978f51ec3efecb351Timo Sirainenstatic struct message_part *
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_body(struct message_parser_ctx *parser_ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_boundary *boundaries,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_size *msg_size, int *has_nuls);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic struct message_part *
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_skip_boundary(struct message_parser_ctx *parser_ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_boundary *boundaries,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_size *boundary_size, int *has_nuls);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void message_size_add_part(struct message_size *dest,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *part)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen dest->physical_size +=
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->header_size.physical_size +
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->body_size.physical_size;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen dest->virtual_size +=
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->header_size.virtual_size +
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->body_size.virtual_size;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen dest->lines += part->header_size.lines + part->body_size.lines;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen}
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic struct message_part *
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_part_append(pool_t pool, struct message_part *parent)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *part, **list;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part = p_new(pool, struct message_part, 1);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->parent = parent;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* set child position */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->physical_pos =
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent->physical_pos +
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent->body_size.physical_size +
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent->header_size.physical_size;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen list = &part->parent->children;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen while (*list != NULL)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen list = &(*list)->next;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen *list = part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen}
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void parse_content_type(const unsigned char *value, size_t value_len,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen void *context)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_parser_ctx *parser_ctx = context;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen const char *str;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (parser_ctx->last_content_type != NULL || value_len == 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen str = parser_ctx->last_content_type =
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen p_strndup(parser_ctx->parser_pool, value, value_len);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (strcasecmp(str, "message/rfc822") == 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part->flags |= MESSAGE_PART_FLAG_MESSAGE_RFC822;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else if (strncasecmp(str, "text", 4) == 0 &&
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen (str[4] == '/' || str[4] == '\0'))
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part->flags |= MESSAGE_PART_FLAG_TEXT;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else if (strncasecmp(str, "multipart/", 10) == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part->flags |= MESSAGE_PART_FLAG_MULTIPART;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (strcasecmp(str+10, "digest") == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part->flags |=
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen MESSAGE_PART_FLAG_MULTIPART_DIGEST;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen}
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenparse_content_type_param(const unsigned char *name, size_t name_len,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen const unsigned char *value, size_t value_len,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen int value_quoted, void *context)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_parser_ctx *parser_ctx = context;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((parser_ctx->part->flags & MESSAGE_PART_FLAG_MULTIPART) == 0 ||
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen name_len != 8 || memcasecmp(name, "boundary", 8) != 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (parser_ctx->last_boundary == NULL) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->last_boundary =
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen p_strndup(parser_ctx->parser_pool, value, value_len);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (value_quoted)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen str_unescape(parser_ctx->last_boundary);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen}
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic struct message_part *
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_multipart(struct message_parser_ctx *parser_ctx)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *parent_part, *next_part, *part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_boundary *b;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen int has_nuls;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* multipart message. add new boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen b = p_new(parser_ctx->parser_pool, struct message_boundary, 1);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen b->part = parser_ctx->part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen b->boundary = parser_ctx->last_boundary;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen b->len = strlen(b->boundary);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen b->next = parser_ctx->boundaries;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->boundaries = b;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* reset fields */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->last_boundary = NULL;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->last_content_type = NULL;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* skip the data before the first boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent_part = parser_ctx->part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_skip_boundary(parser_ctx, parser_ctx->boundaries,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen &parent_part->body_size, &has_nuls);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (has_nuls)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent_part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* now, parse the parts */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen while (next_part == parent_part) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* new child */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part = message_part_append(parser_ctx->part_pool, parent_part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((parent_part->flags & MESSAGE_PART_FLAG_IS_MIME) != 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags |= MESSAGE_PART_FLAG_IS_MIME;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part = part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_parse_part_header(parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_parse_part_body(parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((part->flags & MESSAGE_PART_FLAG_HAS_NULS) != 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* it also belongs to parent */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent_part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* update our size */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_size_add_part(&parent_part->body_size, part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (next_part != parent_part)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen break;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* skip the boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_skip_boundary(parser_ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->boundaries,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen &parent_part->body_size,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen &has_nuls);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (has_nuls)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parent_part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* remove boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen i_assert(parser_ctx->boundaries == b);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->boundaries = b->next;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return next_part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen}
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen#define MUTEX_FLAGS \
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen (MESSAGE_PART_FLAG_MESSAGE_RFC822 | MESSAGE_PART_FLAG_MULTIPART)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void message_parse_part_header(struct message_parser_ctx *parser_ctx)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *part = parser_ctx->part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_header_parser_ctx *hdr_ctx;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_header_line *hdr;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen hdr_ctx = message_parse_header_init(parser_ctx->input,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen &part->header_size);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen while ((hdr = message_parse_header_next(hdr_ctx)) != NULL) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* call the user-defined header parser */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (parser_ctx->callback != NULL)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->callback(part, hdr, parser_ctx->context);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (!hdr->eoh && strcasecmp(hdr->name, "Mime-Version") == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* it's MIME. Content-* headers are valid */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags |= MESSAGE_PART_FLAG_IS_MIME;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (!hdr->eoh && strcasecmp(hdr->name, "Content-Type") == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (hdr->continues) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen hdr->use_full_value = TRUE;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen continue;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* we need to know the boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_content_parse_header(hdr->full_value,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen hdr->full_value_len,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parse_content_type,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parse_content_type_param,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((part->flags & MESSAGE_PART_FLAG_IS_MIME) == 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* It's not MIME. Reset everything we found from
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen Content-Type. */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags = 0;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->last_boundary = NULL;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->last_content_type = NULL;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (parser_ctx->callback != NULL)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->callback(part, NULL, parser_ctx->context);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (hdr_ctx->has_nuls)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_parse_header_deinit(hdr_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen i_assert((part->flags & MUTEX_FLAGS) != MUTEX_FLAGS);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen}
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic struct message_part *
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenmessage_parse_part_body(struct message_parser_ctx *parser_ctx)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *part = parser_ctx->part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_part *next_part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen int has_nuls;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (parser_ctx->last_boundary != NULL)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return message_parse_multipart(parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (parser_ctx->last_content_type == NULL) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (part->parent != NULL &&
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen (part->parent->flags &
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen MESSAGE_PART_FLAG_MULTIPART_DIGEST)) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* when there's no content-type specified and we're
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen below multipart/digest, the assume message/rfc822
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen content-type */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags |= MESSAGE_PART_FLAG_MESSAGE_RFC822;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } else {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* otherwise we default to text/plain */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags |= MESSAGE_PART_FLAG_TEXT;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->last_boundary = NULL;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->last_content_type = NULL;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (part->flags & MESSAGE_PART_FLAG_MESSAGE_RFC822) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* message/rfc822 part - the message body begins with
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen headers again, this works pretty much the same as
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen a single multipart/mixed item */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part = message_part_append(parser_ctx->part_pool, part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part = part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_parse_part_header(parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_parse_part_body(parser_ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->part = part->parent;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* our body size is the size of header+body in message/rfc822 */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen message_size_add_part(&part->parent->body_size, part);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen } else {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* normal message, read until the next boundary */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen next_part = message_parse_body(parser_ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen parser_ctx->boundaries,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen &part->body_size, &has_nuls);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (has_nuls)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if ((part->flags & MESSAGE_PART_FLAG_HAS_NULS) != 0 &&
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->parent != NULL) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* it also belongs to parent */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen part->parent->flags |= MESSAGE_PART_FLAG_HAS_NULS;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen return next_part;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen}
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void message_skip_line(struct istream *input,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct message_size *msg_size, int skip_lf,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen int *has_nuls)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen{
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen const unsigned char *msg;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen size_t i, size, startpos;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen startpos = 0;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen *has_nuls = FALSE;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen while (i_stream_read_data(input, &msg, &size, startpos) > 0) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen for (i = startpos; i < size; i++) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (msg[i] == '\0')
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen *has_nuls = TRUE;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else if (msg[i] == '\n') {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (!skip_lf) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (i > 0 && msg[i-1] == '\r')
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen i--;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen startpos = i;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen goto __break;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (msg_size != NULL) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (i == 0 || msg[i-1] != '\r')
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen msg_size->virtual_size++;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen msg_size->lines++;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen startpos = i+1;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen goto __break;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen }
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* leave the last character, it may be \r */
i_stream_skip(input, i - 1);
startpos = 1;
if (msg_size != NULL) {
msg_size->physical_size += i - 1;
msg_size->virtual_size += i - 1;
}
}
__break:
i_stream_skip(input, startpos);
if (msg_size != NULL) {
msg_size->physical_size += startpos;
msg_size->virtual_size += startpos;
}
}
static struct message_boundary *
boundary_find(struct message_boundary *boundaries,
const unsigned char *msg, size_t len)
{
while (boundaries != NULL) {
if (boundaries->len <= len &&
memcmp(boundaries->boundary, msg, boundaries->len) == 0)
return boundaries;
boundaries = boundaries->next;
}
return NULL;
}
/* read until next boundary is found. if skip_over = FALSE, stop at the
[\r]\n before the boundary, otherwise leave it right after the known
boundary so the ending "--" can be checked. */
static struct message_boundary *
message_find_boundary(struct istream *input,
struct message_boundary *boundaries,
struct message_size *msg_size, int skip_over,
int *has_nuls)
{
struct message_boundary *boundary;
const unsigned char *msg;
size_t i, size, startpos, line_start, missing_cr_count;
boundary = NULL;
missing_cr_count = startpos = line_start = 0;
*has_nuls = FALSE;
while (i_stream_read_data(input, &msg, &size, startpos) > 0) {
for (i = startpos; i < size; i++) {
if (msg[i] != '\n') {
if (msg[i] == '\0')
*has_nuls = TRUE;
continue;
}
if (line_start != (size_t)-1 &&
i >= line_start+2 && msg[line_start] == '-' &&
msg[line_start+1] == '-') {
/* possible boundary */
boundary = boundary_find(boundaries,
msg + line_start + 2,
i - line_start - 2);
if (boundary != NULL)
break;
}
if (i == 0 || msg[i-1] != '\r') {
/* missing CR */
missing_cr_count++;
}
msg_size->lines++;
line_start = i+1;
}
if (boundary != NULL)
break;
if (line_start == (size_t)-1) {
/* continued long line, continue skipping over it */
} else if (i - line_start > 128) {
/* long partial line, see if it's a boundary.
RFC-2046 says that the boundaries must be
70 chars without "--" or less. We allow
a bit larger.. */
if (msg[line_start] == '-' &&
msg[line_start+1] == '-') {
boundary = boundary_find(boundaries,
msg + line_start + 2,
i - line_start - 2);
if (boundary != NULL)
break;
}
/* nope, we can skip over the line, just
leave the last char since it may be \r */
i--;
line_start = (size_t)-1;
} else {
/* leave the last line to buffer, it may be
boundary */
i = line_start;
if (i > 0) i--; /* leave the \r\n too */
if (i > 0) i--;
line_start -= i;
}
i_stream_skip(input, i);
msg_size->physical_size += i;
msg_size->virtual_size += i;
startpos = size - i;
}
if (boundary == NULL &&
line_start != (size_t)-1 && line_start+2 <= size &&
msg[line_start] == '-' && msg[line_start+1] == '-') {
/* possible boundary without line feed at end */
boundary = boundary_find(boundaries,
msg + line_start + 2,
size - line_start - 2);
}
if (boundary != NULL) {
i_assert(line_start != (size_t)-1);
if (skip_over) {
/* leave the pointer right after the boundary */
line_start += 2 + boundary->len;
} else if (line_start > 0 && msg[line_start-1] == '\n') {
/* leave the \r\n before the boundary */
line_start--;
msg_size->lines--;
if (line_start > 0 && msg[line_start-1] == '\r')
line_start--;
else
missing_cr_count--;
}
startpos = line_start;
}
i_stream_skip(input, startpos);
msg_size->physical_size += startpos;
msg_size->virtual_size += startpos + missing_cr_count;
i_assert(msg_size->virtual_size >= msg_size->physical_size);
return boundary;
}
static struct message_part *
message_parse_body(struct message_parser_ctx *parser_ctx,
struct message_boundary *boundaries,
struct message_size *msg_size, int *has_nuls)
{
struct message_boundary *boundary;
struct message_size body_size;
if (boundaries == NULL) {
message_get_body_size(parser_ctx->input, &body_size, has_nuls);
message_size_add(msg_size, &body_size);
boundary = NULL;
} else {
boundary = message_find_boundary(parser_ctx->input, boundaries,
msg_size, FALSE, has_nuls);
}
return boundary == NULL ? NULL : boundary->part;
}
/* skip data until next boundary is found. if it's end boundary,
skip the footer as well. */
static struct message_part *
message_skip_boundary(struct message_parser_ctx *parser_ctx,
struct message_boundary *boundaries,
struct message_size *boundary_size, int *has_nuls)
{
struct message_boundary *boundary;
const unsigned char *msg;
size_t size;
int end_boundary;
boundary = message_find_boundary(parser_ctx->input, boundaries,
boundary_size, TRUE, has_nuls);
if (boundary == NULL)
return NULL;
/* now, see if it's end boundary */
end_boundary = FALSE;
if (i_stream_read_data(parser_ctx->input, &msg, &size, 1) > 0)
end_boundary = msg[0] == '-' && msg[1] == '-';
/* skip the rest of the line */
message_skip_line(parser_ctx->input, boundary_size,
!end_boundary, has_nuls);
if (end_boundary) {
/* skip the footer */
return message_parse_body(parser_ctx, boundary->next,
boundary_size, has_nuls);
}
return boundary == NULL ? NULL : boundary->part;
}
struct message_parser_ctx *
message_parser_init(pool_t part_pool, struct istream *input)
{
struct message_parser_ctx *ctx;
pool_t pool;
pool = pool_alloconly_create("Message Parser", 1024);
ctx = p_new(pool, struct message_parser_ctx, 1);
ctx->parser_pool = pool;
ctx->part_pool = part_pool;
ctx->input = input;
ctx->parts = ctx->part = p_new(part_pool, struct message_part, 1);
return ctx;
}
struct message_part *message_parser_deinit(struct message_parser_ctx *ctx)
{
struct message_part *parts = ctx->parts;
pool_unref(ctx->parser_pool);
return parts;
}
void message_parser_parse_header(struct message_parser_ctx *ctx,
struct message_size *hdr_size,
message_header_callback_t *callback,
void *context)
{
ctx->callback = callback;
ctx->context = context;
message_parse_part_header(ctx);
*hdr_size = ctx->part->header_size;
}
void message_parser_parse_body(struct message_parser_ctx *ctx,
message_header_callback_t *hdr_callback,
message_body_callback_t *body_callback,
void *context)
{
ctx->callback = hdr_callback;
ctx->body_callback = body_callback;
ctx->context = context;
message_parse_part_body(ctx);
}
static void part_parse_headers(struct message_part *part, struct istream *input,
message_header_callback_t *callback,
void *context)
{
while (part != NULL) {
/* note that we want to parse the header of all
the message parts, multiparts too. */
i_assert(part->physical_pos >= input->v_offset);
i_stream_skip(input, part->physical_pos - input->v_offset);
message_parse_header(part, input, NULL, callback, context);
if (part->children != NULL) {
part_parse_headers(part->children, input,
callback, context);
}
part = part->next;
}
}
void message_parse_from_parts(struct message_part *part, struct istream *input,
message_header_callback_t *callback,
void *context)
{
part_parse_headers(part, input, callback, context);
}
void message_parse_header(struct message_part *part, struct istream *input,
struct message_size *hdr_size,
message_header_callback_t *callback, void *context)
{
struct message_header_parser_ctx *hdr_ctx;
struct message_header_line *hdr;
hdr_ctx = message_parse_header_init(input, hdr_size);
while ((hdr = message_parse_header_next(hdr_ctx)) != NULL)
callback(part, hdr, context);
message_parse_header_deinit(hdr_ctx);
/* call after the final skipping */
callback(part, NULL, context);
}
struct message_header_parser_ctx *
message_parse_header_init(struct istream *input, struct message_size *hdr_size)
{
struct message_header_parser_ctx *ctx;
ctx = i_new(struct message_header_parser_ctx, 1);
ctx->input = input;
ctx->hdr_size = hdr_size;
ctx->name = str_new(default_pool, 128);
if (hdr_size != NULL)
memset(hdr_size, 0, sizeof(*hdr_size));
return ctx;
}
void message_parse_header_deinit(struct message_header_parser_ctx *ctx)
{
i_stream_skip(ctx->input, ctx->skip);
if (ctx->value_buf != NULL)
buffer_free(ctx->value_buf);
str_free(ctx->name);
i_free(ctx);
}
struct message_header_line *
message_parse_header_next(struct message_header_parser_ctx *ctx)
{
struct message_header_line *line = &ctx->line;
const unsigned char *msg;
size_t i, size, startpos, colon_pos, parse_size;
int ret;
if (line->eoh)
return NULL;
if (ctx->skip > 0) {
i_stream_skip(ctx->input, ctx->skip);
ctx->skip = 0;
}
startpos = 0; colon_pos = UINT_MAX;
line->no_newline = FALSE;
if (line->continues) {
if (line->use_full_value && !line->continued) {
/* save the first line */
if (ctx->value_buf != NULL)
buffer_set_used_size(ctx->value_buf, 0);
else {
ctx->value_buf =
buffer_create_dynamic(default_pool,
4096, (size_t)-1);
}
buffer_append(ctx->value_buf,
line->value, line->value_len);
}
line->continued = TRUE;
line->continues = FALSE;
colon_pos = 0;
} else {
/* new header line */
line->continued = FALSE;
}
for (;;) {
ret = i_stream_read_data(ctx->input, &msg, &size, startpos+1);
if (ret != 0) {
/* we want to know one byte in advance to find out
if it's multiline header */
parse_size = size-1;
} else {
parse_size = size;
}
if (ret <= 0 && (ret != 0 || startpos == size)) {
if (ret == -1) {
/* error / EOF with no bytes */
return NULL;
}
if (msg[0] == '\n' ||
(msg[0] == '\r' && size > 1 && msg[1] == '\n')) {
/* end of headers - this mostly happens just
with mbox where headers are read separately
from body */
size = 0;
if (ctx->hdr_size != NULL)
ctx->hdr_size->lines++;
if (msg[0] == '\r')
ctx->skip = 2;
else {
ctx->skip = 1;
if (ctx->hdr_size != NULL)
ctx->hdr_size->virtual_size++;
}
break;
}
/* a) line is larger than input buffer
b) header ended unexpectedly */
if (colon_pos == UINT_MAX) {
/* header name is huge. just skip it. */
message_skip_line(ctx->input, ctx->hdr_size,
TRUE, &ctx->has_nuls);
continue;
}
/* go back to last LWSP if found. */
for (i = size-1; i > colon_pos; i--) {
if (IS_LWSP(msg[i])) {
size = i;
break;
}
}
line->no_newline = TRUE;
line->continues = TRUE;
ctx->skip = size;
break;
}
/* find ':' */
if (colon_pos == UINT_MAX) {
for (i = startpos; i < parse_size; i++) {
if (msg[i] <= ':') {
if (msg[i] == ':') {
colon_pos = i;
break;
}
if (msg[i] == '\n') {
/* end of headers, or error */
break;
}
if (msg[i] == '\0')
ctx->has_nuls = TRUE;
}
}
}
/* find '\n' */
for (i = startpos; i < parse_size; i++) {
if (msg[i] <= '\n') {
if (msg[i] == '\n')
break;
if (msg[i] == '\0')
ctx->has_nuls = TRUE;
}
}
if (i < parse_size) {
/* got a line */
line->continues = i+1 < size && IS_LWSP(msg[i+1]);
if (ctx->hdr_size != NULL)
ctx->hdr_size->lines++;
if (i == 0 || msg[i-1] != '\r') {
/* missing CR */
if (ctx->hdr_size != NULL)
ctx->hdr_size->virtual_size++;
size = i;
} else {
size = i-1;
}
ctx->skip = i+1;
break;
}
startpos = i;
}
if (size == 0) {
/* end of headers */
line->eoh = TRUE;
line->name_len = line->value_len = line->full_value_len = 0;
line->name = ""; line->value = line->full_value = NULL;
} else if (line->continued) {
line->value = msg;
line->value_len = size;
} else if (colon_pos == UINT_MAX) {
/* missing ':', assume the whole line is name */
line->value = NULL;
line->value_len = 0;
str_truncate(ctx->name, 0);
str_append_n(ctx->name, msg, size);
line->name = str_c(ctx->name);
line->name_len = str_len(ctx->name);
} else {
/* get value. skip all LWSP after ':'. Note that RFC2822
doesn't say we should, but history behind it.. */
line->value = msg + colon_pos+1;
line->value_len = size - colon_pos - 1;
while (line->value_len > 0 &&
IS_LWSP(line->value[0])) {
line->value++;
line->value_len--;
}
/* get name, skip LWSP before ':' */
while (colon_pos > 0 && IS_LWSP(msg[colon_pos-1]))
colon_pos--;
str_truncate(ctx->name, 0);
str_append_n(ctx->name, msg, colon_pos);
line->name = str_c(ctx->name);
line->name_len = str_len(ctx->name);
}
if (!line->continued) {
/* first header line, set full_value = value */
line->full_value = line->value;
line->full_value_len = line->value_len;
} else if (line->use_full_value) {
/* continue saving the full value */
buffer_append(ctx->value_buf, line->value, line->value_len);
line->full_value = buffer_get_data(ctx->value_buf,
&line->full_value_len);
} else {
/* we didn't want full_value, and this is a continued line. */
line->full_value = NULL;
line->full_value_len = 0;
}
/* always reset it */
line->use_full_value = FALSE;
if (ctx->hdr_size != NULL) {
ctx->hdr_size->physical_size += ctx->skip;
ctx->hdr_size->virtual_size += ctx->skip;
}
return line;
}