istream-header-filter.c revision 76a71915c6452a4bee9a8ae89ddbdf58fa941dee
/* Copyright (c) 2003-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "message-parser.h"
#include "istream-private.h"
#include "istream-header-filter.h"
struct header_filter_istream {
struct istream_private istream;
struct message_header_parser_ctx *hdr_ctx;
const char **headers;
unsigned int headers_count;
void *context;
struct message_size header_size;
unsigned int cur_line, parsed_lines;
ARRAY(unsigned int) match_change_lines;
unsigned int header_read:1;
unsigned int seen_eoh:1;
unsigned int header_parsed:1;
unsigned int headers_edited:1;
unsigned int exclude:1;
unsigned int crlf:1;
unsigned int crlf_preserve:1;
unsigned int hide_body:1;
unsigned int add_missing_eoh:1;
unsigned int end_body_with_lf:1;
unsigned int last_lf_added:1;
unsigned int eoh_not_matched:1;
};
{
struct header_filter_istream *mstream =
(struct header_filter_istream *)stream;
}
static ssize_t
{
const unsigned char *data;
return -1;
}
if (pos <= body_highwater_size) {
(mstream->end_body_with_lf &&
if (ret <= 0) {
/* add missing trailing LF to body */
}
return ret;
}
}
return ret;
}
{
}
{
return FALSE;
}
{
else
}
{
return -2;
return ret;
}
{
struct message_header_line *hdr;
bool matched;
int hdr_ret;
NULL, 0);
}
/* remove skipped data from hdr_buf */
if (mstream->header_read) {
/* we want to return mixed headers and body */
}
}
return -2;
&hdr)) > 0) {
if (mstream->header_parsed) {
if (mstream->eoh_not_matched)
}
if (!matched) {
continue;
}
continue;
}
bsearch_strcasecmp) != NULL;
/* nothing gets excluded */
/* first time in this line or we have actually modified
the header so we always want to call the callbacks */
bool orig_matched = matched;
if (matched != orig_matched &&
!mstream->headers_edited) {
}
} else {
/* second time in this line. was it excluded by the
callback the first time? */
if (match_line_changed(mstream))
}
/* ignore */
} else {
}
if (!hdr->no_newline)
/* we need more */
} else {
if (mstream->skip_count > 0) {
mstream->skip_count = 0;
}
break;
}
}
break;
}
if (hdr_ret < 0) {
return -1;
}
}
}
/* don't copy eof here because we're only returning headers here.
the body will be returned in separate read() call. */
if (hdr_ret == 0) {
/* need more data to finish parsing headers. we may have some
data already available though. */
return ret;
}
/* finished */
/* check if the callback added more headers.
this is allowed only of EOH wasn't added yet. */
else {
}
}
}
if (ret == 0) {
/* we're at the end of headers. */
}
return ret;
}
static ssize_t
{
const unsigned char *data;
bool last_lf;
else if (size > 0)
else
/* missing LF, need to add it */
} else {
}
return ret;
}
{
struct header_filter_istream *mstream =
(struct header_filter_istream *)stream;
if (mstream->last_lf_added) {
return -1;
}
if (!mstream->header_read ||
return ret;
}
return -1;
}
if (mstream->end_body_with_lf)
return ret;
}
static void
{
}
{
if (mstream->header_read)
return;
/* need to re-parse headers */
}
while (!mstream->header_read &&
}
}
static void
{
}
{
struct header_filter_istream *mstream =
(struct header_filter_istream *)stream;
/* just reset the input buffer */
return;
}
/* if last_lf_added=TRUE, we're currently at EOF. So reset it only if
we're seeking backwards, otherwise we would just add a duplicate */
if (v_offset == 0) {
/* seeking to beginning of headers. */
stream_reset_to(mstream, 0);
return;
}
/* if we haven't parsed the whole header yet, we don't know if we
want to seek inside header or body. so make sure we've parsed the
header. */
/* seek into headers. we'll have to re-parse them, use
skip_count to set the wanted position */
} else {
/* body */
}
}
static void ATTR_NORETURN
{
i_panic("istream-header-filter sync() not implemented");
}
static int
{
struct header_filter_istream *mstream =
(struct header_filter_istream *)stream;
return -1;
}
return 0;
/* fix the filtered header size */
return 0;
}
struct istream *
enum header_filter_flags flags,
const char *const *headers,
unsigned int headers_count,
{
struct header_filter_istream *mstream;
unsigned int i, j;
int ret;
"header filter stream", 4096);
for (i = j = 0; i < headers_count; i++) {
ret = j == 0 ? -1 :
if (ret == 0) {
/* drop duplicate */
continue;
}
}
mstream->headers_count = j;
if ((flags & HEADER_FILTER_CRLF_PRESERVE) != 0)
else if ((flags & HEADER_FILTER_NO_CR) != 0)
else
(flags & HEADER_FILTER_END_BODY_WITH_LF) != 0;
}
{
}