Commit b2c1b1ff b2c1b1ff400a604fa41c168351630bac3238177e by Sergey Poznyakoff

Improve compatibility with RFC 2231 and RFC 2047.

* mailbox/mimehdr.c: New file.
* mailbox/Makefile.am (libmailutils_la_SOURCES): Add
mailbox/mimehdr.c.
* mailbox/attachment.c (_header_get_param)
(_get_attachment_name, mu_message_aget_attachment_name)
(mu_message_get_attachment_name: Move to mailbox/mimehdr.c (with
edits).
(mu_message_save_attachment): Add a FIXME comment.
* include/mailutils/message.h (MU_MIMEHDR_MULTILINE)
(MU_MIMEHDR_CSINFO): New defines.
(mu_mimehdr_get_disp,mu_mimehdr_aget_disp)
(mu_mimehdr_get_param,mu_mimehdr_aget_param)
(mu_mimehdr_decode_param)
(mu_mimehdr_aget_decoded_param): New prototypes.
(mu_message_aget_attachment_name): Change signature.
(mu_message_aget_decoded_attachment_name): New prototype.

* mailbox/mutil.c (mu_hex2ul): Fix a silly bug (have
anybody ever tried to use that function?!?)
* mailbox/testsuite/Urls: Update.

* examples/mimetest.c (message_display_parts): Add a FIXME comment.
* libmu_cpp/message.cc (Message::get_attachment_name): Likewise.
* mh/mhn.c (store_handler): Likewise.
* python/libmu_py/message.c (api_message_get_attachment_name): Likewise.
1 parent d5ae81e5
......@@ -242,7 +242,8 @@ message_display_parts (mu_message_t msg, int indent)
{
/* Save the attachements. */
char *fname = NULL;
mu_message_aget_attachment_name (part, &fname);
/* FIXME: CS/Lang info is ignored */
mu_message_aget_attachment_name (part, &fname, NULL);
if (fname == NULL)
fname = mu_tempname (NULL);
......
......@@ -119,9 +119,36 @@ extern int mu_message_encapsulate (mu_message_t msg, mu_message_t *newmsg,
extern int mu_message_unencapsulate (mu_message_t msg, mu_message_t *newmsg,
void **data);
/* Bit values for *pflags in functions below */
#define MU_MIMEHDR_MULTILINE 0x01 /* Parameter was multiline */
#define MU_MIMEHDR_CSINFO 0x02 /* Parameter contains charset/language
info */
extern int mu_mimehdr_get_disp (const char *str, const char *param,
char *buf, size_t bufsz, size_t *retsz);
extern int mu_mimehdr_aget_disp (const char *str, const char *param,
char **pvalue);
extern int mu_mimehdr_get_param (const char *str, const char *param,
char *buf, size_t bufsz, size_t *retsz,
int *pflags);
extern int mu_mimehdr_aget_param (const char *str, const char *param,
char **pval, int *pflags);
extern int mu_mimehdr_decode_param (const char *value, int csinfo,
const char *charset,
char **pval, char **plang);
extern int mu_mimehdr_aget_decoded_param (const char *str, const char *param,
const char *charset,
char **pval, char **plang);
extern int mu_message_get_attachment_name (mu_message_t, char *name,
size_t bufsz, size_t* sz);
extern int mu_message_aget_attachment_name (mu_message_t, char **name);
size_t bufsz, size_t* sz,
int *pflags);
extern int mu_message_aget_attachment_name (mu_message_t, char **name,
int *pflags);
extern int mu_message_aget_decoded_attachment_name (mu_message_t msg,
const char *charset,
char **name,
char **plang);
extern int mu_message_save_to_mailbox (mu_message_t msg,
mu_debug_t debug, const char *toname,
......
......@@ -224,7 +224,8 @@ Message :: get_attachment_name ()
char *c_name;
std::string name;
int status = mu_message_aget_attachment_name (msg, &c_name);
/* FIXME: CS/Lang info is ignored */
int status = mu_message_aget_attachment_name (msg, &c_name, NULL);
if (status)
throw Exception ("Message::get_attachment_name", status);
if (c_name) {
......
......@@ -101,6 +101,7 @@ libmailutils_la_SOURCES = \
memory_stream.c\
message_stream.c\
mime.c\
mimehdr.c\
mkfilename.c\
monitor.c\
msrv.c\
......
......@@ -185,297 +185,6 @@ _attachment_free (struct _msg_info *info, int free_message)
free (info);
}
/* See RFC 2045, 5.1. Syntax of the Content-Type Header Field */
#define _ISSPECIAL(c) !!strchr ("()<>@,;:\\\"/[]?=", c)
/* _header_get_param - an auxiliary function to extract values from
Content-Type, Content-Disposition and similar headers.
Arguments:
FIELD_BODY Header value, complying to RFCs 2045, 2183, 2231.3;
DISP Disposition. Unless it is NULL, the disposition part
of FIELD_BODY is compared with it. If they differ,
the function returns MU_ERR_NOENT.
PARAM Name of the parameter to extract from FIELD_BODY;
BUF Where to extract the value to;
BUFSZ Size of BUF;
PRET Pointer to the memory location for the return buffer (see
below).
PLEN Pointer to the return size.
The function parses FIELD_BODY and extracts the value of the parameter
PARAM.
If BUF is not NULL and BUFSZ is not 0, the extracted value is stored into
BUF. At most BUFSZ-1 bytes are copied.
Otherwise, if PRET is not NULL, the function allocates enough memory to
hold the extracted value, copies there the result, and stores the
pointer to the allocated memory into the location pointed to by PRET.
If PLEN is not NULL, the size of the extracted value (without terminating
NUL character) is stored there.
If BUF==NULL *and* PRET==NULL, no memory is allocated, but PLEN is
honored anyway, i.e. unless it is NULL it receives size of the result.
This can be used to estimate the needed buffer size.
Return values:
0 on success.
MU_ERR_NOENT, requested parameter not found, or disposition does
not match DISP.
MU_ERR_PARSE, if FIELD_BODY does not comply to any of the abovemntioned
RFCs.
ENOMEM , if unable to allocate memory.
*/
int
_header_get_param (char *field_body,
const char *disp,
const char *param,
char *buf, size_t bufsz,
char **pret, size_t *plen)
{
int res = MU_ERR_NOENT; /* Return value, pessimistic default */
size_t param_len = strlen (param);
char *p;
char *mem = NULL; /* Allocated memory storage */
size_t retlen = 0; /* Total number of bytes copied */
unsigned long cind = 0; /* Expected continued parameter index.
See RFC 2231, Section 3,
"Parameter Value Continuations" */
if (field_body == NULL)
return EINVAL;
if (bufsz == 0) /* Make sure buf value is meaningful */
buf = NULL;
p = strchr (field_body, ';');
if (!p)
return MU_ERR_NOENT;
if (disp && mu_c_strncasecmp (field_body, disp, p - field_body))
return MU_ERR_NOENT;
while (p && *p)
{
char *v, *e;
size_t len, escaped_chars = 0;
if (*p != ';')
{
res = MU_ERR_PARSE;
break;
}
/* walk upto start of param */
p = mu_str_skip_class (p + 1, MU_CTYPE_SPACE);
if ((v = strchr (p, '=')) == NULL)
break;
v++;
/* Find end of the parameter */
if (*v == '"')
{
/* Quoted string */
for (e = ++v; *e != '"'; e++)
{
if (*e == 0) /* Malformed header */
{
res = MU_ERR_PARSE;
break;
}
if (*e == '\\')
{
if (*++e == 0)
{
res = MU_ERR_PARSE;
break;
}
escaped_chars++;
}
}
if (res == MU_ERR_PARSE)
break;
len = e - v;
e++;
}
else
{
for (e = v + 1; !(_ISSPECIAL (*e) || mu_isspace (*e)); e++)
;
len = e - v;
}
/* Is it our parameter? */
if (mu_c_strncasecmp (p, param, param_len))
{ /* nope, jump to next */
p = strchr (e, ';');
continue;
}
res = 0; /* Indicate success */
if (p[param_len] == '*')
{
/* Parameter value continuation (RFC 2231, Section 3).
See if the index is OK */
char *end;
unsigned long n = strtoul (p + param_len + 1, &end, 10);
if (*end != '=' || n != cind)
{
res = MU_ERR_PARSE;
break;
}
/* Everything OK, increase the estimation */
cind++;
}
/* Prepare P for the next iteration */
p = e;
/* Escape characters that appear in quoted-pairs are
semantically "invisible" (RFC 2822, Section 3.2.2,
"Quoted characters") */
len -= escaped_chars;
/* Adjust len if nearing end of the buffer */
if (bufsz && len >= bufsz)
len = bufsz - 1;
if (pret)
{
/* The caller wants us to allocate the memory */
if (!buf && !mem)
{
mem = malloc (len + 1);
if (!mem)
{
res = ENOMEM;
break;
}
buf = mem;
}
else if (mem)
{
/* If we got here, it means we are iterating over
a parameter value continuation, and cind=0 has
already been passed. Reallocate the memory to
accomodate next chunk of data. */
char *newmem = realloc (mem, retlen + len + 1);
if (!newmem)
{
res = ENOMEM;
break;
}
mem = newmem;
}
}
if (buf)
{
/* Actually copy the data. Buf is not NULL either because
the user passed it as an argument, or because we allocated
memory for it. */
if (escaped_chars)
{
int i;
for (i = 0; i < len; i++)
{
if (*v == '\\')
++v;
buf[retlen + i] = *v++;
}
}
else
memcpy (buf + retlen, v, len);
}
/* Adjust total result size ... */
retlen += len;
/* ... and remaining buffer size, if necessary */
if (bufsz)
{
bufsz -= len;
if (bufsz == 0)
break;
}
}
if (res == 0)
{
/* Everything OK, prepare the returned data. */
if (buf)
buf[retlen] = 0;
if (plen)
*plen = retlen;
if (pret)
*pret = mem;
}
else if (mem)
free (mem);
return res;
}
/* Get the attachment name from MSG. See _header_get_param, for a
description of the rest of arguments. */
static int
_get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
char **pbuf, size_t *sz)
{
int ret = EINVAL;
mu_header_t hdr;
char *value = NULL;
if (!msg)
return ret;
if ((ret = mu_message_get_header (msg, &hdr)) != 0)
return ret;
ret = mu_header_aget_value (hdr, "Content-Disposition", &value);
/* If the header wasn't there, we'll fall back to Content-Type, but
other errors are fatal. */
if (ret != 0 && ret != MU_ERR_NOENT)
return ret;
if (ret == 0 && value != NULL)
{
ret = _header_get_param (value, "attachment",
"filename", buf, bufsz, pbuf, sz);
free (value);
value = NULL;
if (ret == 0 || ret != MU_ERR_NOENT)
return ret;
}
/* If we didn't get the name, we fall back on the Content-Type name
parameter. */
free (value);
ret = mu_header_aget_value (hdr, "Content-Type", &value);
if (ret == 0)
ret = _header_get_param (value, NULL, "name", buf, bufsz, pbuf, sz);
free (value);
return ret;
}
int
mu_message_aget_attachment_name (mu_message_t msg, char **name)
{
if (name == NULL)
return MU_ERR_OUT_PTR_NULL;
return _get_attachment_name (msg, NULL, 0, name, NULL);
}
int
mu_message_get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
size_t *sz)
{
return _get_attachment_name (msg, buf, bufsz, NULL, sz);
}
int
mu_message_save_attachment (mu_message_t msg, const char *filename,
void **data)
......@@ -499,7 +208,8 @@ mu_message_save_attachment (mu_message_t msg, const char *filename,
{
if (filename == NULL)
{
ret = mu_message_aget_attachment_name (msg, &partname);
/* FIXME: Charset info is ignored */
ret = mu_message_aget_attachment_name (msg, &partname, NULL);
if (partname)
fname = partname;
}
......
......@@ -75,10 +75,10 @@ mu_hex2ul (char hex)
return hex - '0';
if (hex >= 'a' && hex <= 'z')
return hex - 'a';
return hex - 'a' + 10;
if (hex >= 'A' && hex <= 'Z')
return hex - 'A';
return hex - 'A' + 10;
return -1;
}
......
......@@ -58,7 +58,7 @@ scheme://%75%73%65%72:%70%61%73%73@%68%6f%73%74 => SUCCESS
user <user>
passwd <pass>
auth <>
host <hest>
host <host>
port 0
path <>
......
......@@ -1590,7 +1590,9 @@ store_handler (mu_message_t msg, msg_part_t part, char *type, char *encoding,
{
char *val;
if (mu_message_aget_attachment_name (msg, &val) == 0)
/* FIXME: Take into account CS/Lang info and recode the value
if necessary */
if (mu_message_aget_attachment_name (msg, &val, NULL) == 0)
{
name = normalize_path (dir, val);
free (val);
......
......@@ -287,7 +287,8 @@ api_message_get_attachment_name (PyObject *self, PyObject *args)
if (!PyArg_ParseTuple (args, "O!", &PyMessageType, &py_msg))
return NULL;
status = mu_message_aget_attachment_name (py_msg->msg, &name);
/* FIXME: CS/Lang info is ignored */
status = mu_message_aget_attachment_name (py_msg->msg, &name, NULL);
return status_object (status, PyString_FromString (name ? name : ""));
}
......