msg_mime.c 60.9 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup msg_mime
 * @CFILE msg_mime.c
 *
 * MIME-related headers and MIME multipart bodies for SIP/HTTP/RTSP.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Jun 13 02:57:51 2000 ppessi
 *
 *
 */

#include "config.h"

#define _GNU_SOURCE 1

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
45
#include <errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
46 47
#include <assert.h>

48
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
49 50

#include "msg_internal.h"
51 52
#include "sofia-sip/msg.h"
#include "sofia-sip/msg_mime.h"
Pekka Pessi's avatar
Pekka Pessi committed
53

54
#include <sofia-sip/su_uniqueid.h>
55
#include <sofia-sip/su_errno.h>
Martti Mela's avatar
Martti Mela committed
56

Pekka Pessi's avatar
Pekka Pessi committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#if !HAVE_MEMMEM
void *memmem(const void *haystack, size_t haystacklen,
	     const void *needle, size_t needlelen);
#endif

size_t memspn(const void *mem, size_t memlen,
	      const void *accept, size_t acceptlen);
size_t memcspn(const void *mem, size_t memlen,
	       const void *reject, size_t rejectlen);

/** Protocol version of MIME */
char const msg_mime_version_1_0[] = "MIME/1.0";

/* Internally used version of msg_header_t */
union msg_mime_u
{
  msg_common_t    sh_common[1];
  struct {
    msg_common_t  shn_common;
    msg_header_t *shn_next;
  }               sh_header_next[1];

  msg_multipart_t           sh_multipart[1];

  msg_accept_t              sh_accept[1];
  msg_accept_any_t          sh_accept_any[1];
  msg_accept_charset_t      sh_accept_charset[1];
  msg_accept_encoding_t     sh_accept_encoding[1];
  msg_accept_language_t     sh_accept_language[1];
  msg_content_disposition_t sh_content_disposition[1];
  msg_content_encoding_t    sh_content_encoding[1];
  msg_content_id_t          sh_content_id[1];
  msg_content_language_t    sh_content_language[1];
  msg_content_length_t      sh_content_length[1];
  msg_content_location_t    sh_content_location[1];
  msg_content_type_t        sh_content_type[1];
  msg_mime_version_t        sh_mime_version[1];
  msg_warning_t             sh_warning[1];
  msg_unknown_t             sh_unknown[1];
  msg_separator_t           sh_separator[1];
  msg_payload_t             sh_payload[1];
};

100 101
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_mime_protos.h>
Pekka Pessi's avatar
Pekka Pessi committed
102 103 104

/** Define a header class for headers without any extra data to copy */
#define MSG_HEADER_CLASS_G(c, l, s, kind) \
105 106 107
  MSG_HEADER_CLASS(msg_, c, l, s, g_common, kind, msg_generic, msg_generic)

#define msg_generic_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
108 109 110

/** Define a header class for a msg_list_t kind of header */
#define MSG_HEADER_CLASS_LIST(c, l, s, kind) \
111
  MSG_HEADER_CLASS(msg_, c, l, s, k_items, kind, msg_list, msg_list)
Pekka Pessi's avatar
Pekka Pessi committed
112

113
#define msg_list_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
114 115 116

/* ====================================================================== */

117
/** Calculate length of line ending (0, 1 or 2). @internal */
Pekka Pessi's avatar
Pekka Pessi committed
118 119 120 121 122 123 124 125 126
#define CRLF_TEST(b) ((b)[0] == '\r' ? ((b)[1] == '\n') + 1 : (b)[0] =='\n')

/**@ingroup msg_mime
 * @defgroup msg_multipart MIME Multipart Body
 *
 * Representing MIME multipart bodies and their manipulation.
 *
 * The #msg_multipart_t is an object for storing MIME multipart message
 * bodies. It includes message components used for framing and identifying
127
 * message parts. Its syntax is defined in  @RFC2046 as follows:
Pekka Pessi's avatar
Pekka Pessi committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
 *
 * @code
 *
 *   multipart-body := [preamble CRLF]
 *                     dash-boundary transport-padding CRLF
 *                     body-part *encapsulation
 *                     close-delimiter transport-padding
 *                     [CRLF epilogue]
 *
 *   preamble := discard-text
 *
 *   discard-text := *(*text CRLF)
 *                   ; May be ignored or discarded.
 *
 *   dash-boundary := "--" boundary
 *                    ; boundary taken from the value of boundary parameter
 *                    ; of the Content-Type field.
 *
 *   boundary := 0*69<bchars> bcharsnospace
 *
 *   bchars := bcharsnospace / " "
 *
 *   bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
 *                    "+" / "_" / "," / "-" / "." /
 *                    "/" / ":" / "=" / "?"
 *
 *   transport-padding := *LWSP-char
 *                        ; Composers MUST NOT generate non-zero length
 *                        ; transport padding, but receivers MUST be able to
 *                        ; handle padding added by message transports.
 *
159
 *   body-part := <"message" as defined in @RFC822, with all header fields
Pekka Pessi's avatar
Pekka Pessi committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
 *                 optional, not starting with the specified dash-boundary,
 *                 and with the delimiter not occurring anywhere in the body
 *                 part. Note that the semantics of a part differ from the
 *                 semantics of a message, as described in the text.>
 *
 *   encapsulation := delimiter transport-padding CRLF 
 *                    body-part
 *
 *   close-delimiter := delimiter "--"
 *
 *   delimiter := CRLF dash-boundary
 *
 *   epilogue := discard-text
 *
 * @endcode
 *
 * @par Parsing a Multipart Message
 * 
 * When a message body contains a multipart entity (in other words, it has a
 * MIME media type of "multipart"), the application can split the multipart
 * entity into body parts
 *
 * The parsing is relatively simple, the application just gives a memory
 * home object, a Content-Type header object and message body object as an 
 * argument to msg_multipart_parse() function:
 * @code
 *    if (sip->sip_content_type && 
 *        strncasecmp(sip->sip_content_type, "multipart/", 10) == 0) {
 *      msg_multipart_t *mp;
 *
 *      if (sip->sip_multipart)
 *        mp = sip->sip_multipart;
 *      else
 *        mp = msg_multipart_parse(msg_home(msg), 
 *                                 sip->sip_content_type,
 *                                 (sip_payload_t *)sip->sip_payload);
 * 
 *      if (mp)
 *        ... processing multipart ...
 *      else
 *        ... error handling ...
 *    }
 * @endcode
 *
 * The resulting list of msg_multipart_t structures contain the parts of the
 * multipart entity, each part represented by a separate #msg_multipart_t
 * structure. Please note that in order to make error recovery possible, the
 * parsing is not recursive - if multipart contains another multipart, the
 * application is responsible for scanning for it and parsing it.
 *
 * @par Constructing a Multipart Message
 *
 * Constructing a multipart body is a bit more hairy. The application needs
 * a message object (#msg_t), which is used to buffer the encoding of
 * multipart components.
 *
 * As an example, let us create a "multipart/mixed" multipart entity with a
 * HTML and GIF contents, and convert it into a #sip_payload_t structure:
 * @code
 *   msg_t *msg = msg_create(sip_default_mclass, 0);
 *   su_home_t *home = msg_home(msg);
 *   sip_t *sip = sip_object(msg);
 *   sip_content_type_t *c;
 *   msg_multipart_t *mp = NULL;
 *   msg_header_t *h = NULL;
 *   char *b;
226
 *   size_t len, offset;
Pekka Pessi's avatar
Pekka Pessi committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
 * 
 *   mp = msg_multipart_create(home, "text/html;level=3", html, strlen(html));
 *   mp->mp_next = msg_multipart_create(home, "image/gif", gif, giflen);
 *
 *   c = sip_content_type_make(home, "multipart/mixed");
 * 
 *   // Add delimiters to multipart, and boundary parameter to content-type
 *   if (msg_multipart_complete(home, c, mp) < 0)
 *     return -1;		// Error
 *
 *   // Combine multipart components into the chain
 *   h = NULL;
 *   if (msg_multipart_serialize(&h, mp) < 0)
 *     return -1;		// Error
 *
 *   // Encode all multipart components 
 *   len = msg_multipart_prepare(msg, mp, 0);
 *   if (len < 0)
 *     return -1;		// Error
 *
 *   pl = sip_payload_create(home, NULL, len);
 *
 *   // Copy each element from multipart to pl_data 
 *   b = pl->pl_data;
 *   for (offset = 0, h = mp; offset < len; h = h->sh_succ) {
 *     memcpy(b + offset, h->sh_data, h->sh_len);
 *     offset += h->sh_len;
 *   }
 * @endcode
 *
 */

/**Create a part for MIME multipart entity.
 *
 * The function msg_multipart_create() allocates a new #msg_multipart_t
 * object from memory home @a home. If @a content_type is non-NULL, it makes
 * a #msg_content_type_t header object and adds the header to the
 * #msg_multipart_t object. If @a dlen is nonzero, it allocates a
 * msg_payload_t structure of @a dlen bytes for the payload of the newly
 * created #msg_multipart_t object. If @a data is non-NULL, it copies the @a
 * dlen bytes of of data to the payload of the newly created
 * #msg_multipart_t object.
 *
 * @return A pointer to the newly created #msg_multipart_t object, or NULL
 * upon an error.
 */
msg_multipart_t *msg_multipart_create(su_home_t *home,
				      char const *content_type,
				      void const *data,
276
				      isize_t dlen)
Pekka Pessi's avatar
Pekka Pessi committed
277 278 279
{
  msg_multipart_t *mp;

280
  mp = (msg_multipart_t *)msg_header_alloc(home, msg_multipart_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

  if (mp) {
    if (content_type)
      mp->mp_content_type = msg_content_type_make(home, content_type);
    if (dlen)
      mp->mp_payload = msg_payload_create(home, data, dlen);

    if ((!mp->mp_content_type && content_type) ||
	(!mp->mp_payload && dlen)) {
      su_free(home, mp->mp_content_type);
      su_free(home, mp->mp_payload);
      su_free(home, mp);
      mp = NULL;
    }
  }

  return mp;
}

/** Convert boundary parameter to a search string. */
static char *
msg_multipart_boundary(su_home_t *home, char const *b)
{
  char *boundary;

  if (!b || !(boundary = su_alloc(home, 2 + 2 + strlen(b) + 2 + 1)))
    return NULL;

  strcpy(boundary, CR LF "--");

  if (b[0] == '"') /* " See http://bugzilla.gnome.org/show_bug.cgi?id=134216 */

    msg_unquote(boundary + 4, b);
  else
    strcpy(boundary + 4, b);


  strcat(boundary + 4, CR LF);

  return boundary;
}


/** Boundary chars. */
static char const bchars[] = 
"'()+_,-./:=?"
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
" ";

#define bchars_len (sizeof(bchars) - 1)

/** Search for a suitable boundary from MIME. */
static char *
msg_multipart_search_boundary(su_home_t *home, char const *p, size_t len)
{
338 339
  size_t m;
  unsigned crlf;
Pekka Pessi's avatar
Pekka Pessi committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  char const *end = p + len;
  char *boundary;

  if (len < 2)
    return NULL;

  /* Boundary looks like LF -- string SP* [CR] LF */
  if (memcmp("--", p, 2) == 0) {
    /* We can be at boundary beginning, there is no CR LF */
    m = 2 + memspn(p + 2, len - 2, bchars, bchars_len);
    if (m + 2 >= len)
      return NULL;
    crlf = p[m] == '\r' ? 1 + (p[m + 1] == '\n') : (p[m] == '\n');
    while (p[m - 1] == ' ' || p[m - 1] == '\t')
      m--;
    if (m > 2 && crlf) {
      boundary = su_alloc(home, 2 + m + 2 + 1);
      if (boundary) {
	memcpy(boundary, CR LF, 2);
	memcpy(boundary + 2, p, m);
	strcpy(boundary + m + 2, CR LF);
      }
      return boundary;
    }
  } 

  /* Look for LF -- */
  for (;(p = memmem(p, end - p, LF "--", 3)); p += 3) {
    len = end - p;
    m = 3 + memspn(p + 3, len - 3, bchars, bchars_len);
    if (m + 2 >= len)
      return NULL;
    crlf = p[m] == '\r' ? 1 + (p[m + 1] == '\n') : (p[m] == '\n');
    while (p[m - 1] == ' ' || p[m - 1] == '\t')
      m--;
    m--;
    if (m > 2 && crlf) {
      boundary = su_alloc(home, 2 + m + 2 + 1);
      if (boundary) {
	memcpy(boundary, CR LF, 2);
	memcpy(boundary + 2, p + 1, m);
	strcpy(boundary + 2 + m, CR LF);
      }
      return boundary;
    }
  }

  return NULL;
}

/** Parse a MIME multipart.
 *
 * The function msg_multipart_parse() parses a MIME multipart message. The
393
 * common syntax of multiparts is described in @RFC2046 (section 7).
Pekka Pessi's avatar
Pekka Pessi committed
394
 *
395 396 397
 * @param[in,out] home home for allocating structures 
 * @param[in]     c    content-type header for multipart 
 * @param[in]     pl   payload structure for multipart 
Pekka Pessi's avatar
Pekka Pessi committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
 *
 * After parsing, the @a pl will contain the plain-text preamble (if any).
 *
 * @note If no @b Content-Type header is given, the msg_multipart_parse()
 * tries to look for a suitable boundary. Currently, it takes first
 * boundary-looking string and uses that, so it can be fooled with, for
 * instance, signature @c "--Pekka".
 */
msg_multipart_t *msg_multipart_parse(su_home_t *home,
				     msg_content_type_t const *c,
				     msg_payload_t *pl)
{
  msg_multipart_t *mp = NULL, *all = NULL, **mmp = &all;
  /* Dummy msg object */
  msg_t msg[1] = {{{ SU_HOME_INIT(msg) }}};
413
  size_t len, m, blen;
Pekka Pessi's avatar
Pekka Pessi committed
414 415 416 417 418 419 420 421 422 423 424
  char *boundary, *p, *next, save;
  char const *b, *end;
  msg_param_t param;

  p = pl->pl_data; len = pl->pl_len; end = p + len;

  su_home_init(msg_home(msg));
  msg->m_class = msg_multipart_mclass;
  msg->m_tail = &msg->m_chain;

  /* Get boundary from Content-Type */
425
  if (c && (param = msg_header_find_param(c->c_common, "boundary=")))
Pekka Pessi's avatar
Pekka Pessi committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    boundary = msg_multipart_boundary(msg_home(msg), param);
  else
    boundary = msg_multipart_search_boundary(msg_home(msg), p, len);

  if (!boundary)
    return NULL;

  m = strlen(boundary) - 2, blen = m - 1;

  /* Find first delimiter */
  if (memcmp(boundary + 2, p, m - 2) == 0)
    b = p, p = p + m - 2, len -= m - 2;
  else if ((p = memmem(p, len, boundary + 1, m - 1))) {
    if (p != pl->pl_data && p[-1] == '\r')
      b = --p, p = p + m, len -= m;
    else
      b = p, p = p + m - 1, len -= m - 1;
  }
  else {
    su_home_deinit(msg_home(msg));
    return NULL;
  }

  /* Split multipart into parts */
  for (;;) {
    while (p[0] == ' ') 
      p++;

    p += p[0] == '\r' ? 1 + (p[1] == '\n') : (p[0] == '\n');

    len = end - p;

    if (len < blen)
      break;

    next = memmem(p, len, boundary + 1, m = blen);

    if (!next)
      break;			/* error */

    if (next != p && next[-1] == '\r')
      next--, m++;

469
    mp = (msg_multipart_t *)msg_header_alloc(msg_home(msg), msg_multipart_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    if (mp == NULL)
      break;			/* error */
    *mmp = mp; mmp = &mp->mp_next;

    /* Put delimiter transport-padding CRLF here */
    mp->mp_common->h_data = b;
    mp->mp_common->h_len = p - b;

    /* .. and body-part here */
    mp->mp_data = p;
    mp->mp_len = next - p;

    if (next[m] == '-' && next[m + 1] == '-') {
      /* We found close-delimiter */
      assert(mp);
      if (!mp)
	break;			/* error */
487 488
      mp->mp_close_delim = (msg_payload_t *)
	msg_header_alloc(msg_home(msg), msg_payload_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
      if (!mp->mp_close_delim)
	break;			/* error */
      /* Include also transport-padding and epilogue in the close-delimiter */
      mp->mp_close_delim->pl_data = next;
      mp->mp_close_delim->pl_len = p + len - next;
      break;
    }

    b = next; p = next + m; 
  }

  if (!mp || !mp->mp_close_delim) {
    su_home_deinit(msg_home(msg));
    /* Delimiter error */
    return NULL;
  }

  /* Parse each part */
  for (mp = all; mp; mp = mp->mp_next) {
508
    msg->m_object = (msg_pub_t *)mp; p = mp->mp_data; next = p + mp->mp_len;
Pekka Pessi's avatar
Pekka Pessi committed
509 510 511 512 513 514 515 516 517 518 519 520

    if (msg->m_tail)
      mp->mp_common->h_prev = msg->m_tail,
	*msg->m_tail = (msg_header_t *)mp;

    msg->m_chain = (msg_header_t *)mp;
    msg->m_tail = &mp->mp_common->h_succ;

    save = *next; *next = '\0';	/* NUL-terminate this part */

    for (len = next - p; len > 0; len -= m, p += m) {
      if (IS_CRLF(p[0])) {
521
	m = msg_extract_separator(msg, (msg_pub_t*)mp, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
522 523 524 525 526
	assert(m > 0);

	p += m; len -= m;

	if (len > 0) {
527
	  m = msg_extract_payload(msg, (msg_pub_t*)mp, NULL, len, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
528 529 530 531 532 533
	  assert(m > 0);
	  assert(len == m);
	}
	break;
      }

534
      m = msg_extract_header(msg, (msg_pub_t*)mp, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

      if (m <= 0) {
	assert(m > 0);
	/* Xyzzy */
      }
    }

    *next = save; /* XXX - Should we leave the payload NUL-terminated? */
  }

  /* Postprocess */
  blen = strlen(boundary);

  for (mp = all; mp; mp = mp->mp_next) {
    mp->mp_data = boundary; 
    mp->mp_len = blen;

    assert(mp->mp_payload || mp->mp_separator);

    if (mp->mp_close_delim) {
      msg_header_t **tail;

      if (mp->mp_payload)
	tail = &mp->mp_payload->pl_common->h_succ;
      else
	tail = &mp->mp_separator->sep_common->h_succ;

      assert(msg->m_chain == (msg_header_t *)mp);
      assert(*tail == NULL);

      mp->mp_close_delim->pl_common->h_prev = tail;
      *tail = (msg_header_t *)mp->mp_close_delim;
    }
  }

  msg_fragment_clear(pl->pl_common);
  pl->pl_len = all->mp_data - (char *)pl->pl_data;

  su_home_move(home, msg_home(msg)); su_home_deinit(msg_home(msg));

  return all;
}

/**Add all missing parts to the multipart.
 * 
580 581 582
 * Add missing components such as boundaries between body parts, separators
 * between body-part headers and data, and close-delimiter after last
 * body-part to the multipart message.
Pekka Pessi's avatar
Pekka Pessi committed
583
 * 
584 585 586
 * @param[in,out] home home for allocating structures 
 * @param[in,out] c    content-type header for multipart 
 * @param[in,out] mp   pointer to first multipart structure 
Pekka Pessi's avatar
Pekka Pessi committed
587
 *
588 589
 * @retval 0 when successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
 *
 * @ERRORS 
 * @ERROR EBADMSG
 * The @b Content-Type header @a c is malformed, or multipart message
 * contains a malformed @b Content-Type header.
 * @ERROR ENOMEM
 * A memory allocation failed.
 * @ERROR EINVAL
 * The function msg_multipart_complete() was given invalid arguments.
 */
int msg_multipart_complete(su_home_t *home,
			   msg_content_type_t *c,
			   msg_multipart_t *mp)
{
  char *boundary;
  char const *b;
606
  size_t blen, m;
Pekka Pessi's avatar
Pekka Pessi committed
607 608 609 610

  if (c == NULL || mp == NULL)
    return (errno = EINVAL), -1;

611
  if (!(b = msg_header_find_param(c->c_common, "boundary="))) {
Pekka Pessi's avatar
Pekka Pessi committed
612
    /* Generate boundary */
613
    enum { tlen = 16 * 4 / 3 };
Pekka Pessi's avatar
Pekka Pessi committed
614 615 616 617 618 619 620 621 622 623 624 625 626
    char token[sizeof("boundary=") + tlen + 1];

    if (mp->mp_data) {
      b = mp->mp_data;
      m = mp->mp_len;

      if (strncmp(b, CR LF "--", 4) == 0)
	b += 4, m -= 4;
      else if (strncmp(b, "--", 2) == 0)
	b += 2, m -= 2;
      else
	return (errno = EBADMSG), -1;
      /* XXX - quoting? */
627
      b = su_sprintf(home, "boundary=\"%.*s\"", (int)m, b);
Pekka Pessi's avatar
Pekka Pessi committed
628 629 630
    }
    else {
      strcpy(token, "boundary=");
631
      msg_random_token(token + strlen("boundary="), (size_t)tlen, NULL, 0);
Pekka Pessi's avatar
Pekka Pessi committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
      b = su_strdup(home, token);
    }

    if (!b)
      return -1;

    msg_params_replace(home, (msg_param_t **)&c->c_params, b);

    b += strlen("boundary=");
  }

  if (!(boundary = msg_multipart_boundary(home, b)))
    return -1;

  blen = strlen(boundary); m = blen - 2;

  for (; mp; mp = mp->mp_next) {
    if (mp->mp_data == NULL) {
      mp->mp_data = boundary, mp->mp_len = blen;
    } else {
      if (mp->mp_len < 3)
	return -1;
      if (mp->mp_data[0] == '\r' && mp->mp_data[1] == '\n') {
	if (mp->mp_len < m || memcmp(mp->mp_data + 2, boundary + 2, m - 2))
	  return -1;
      } else if (mp->mp_data[0] == '\n') {
	if (mp->mp_len < m - 1 || memcmp(mp->mp_data + 1, boundary + 2, m - 2))
	  return -1;
      } else {
	if (mp->mp_len < m - 2 || memcmp(mp->mp_data, boundary + 2, m - 2))
	  return -1;
      }
    }

    if (mp->mp_next == NULL) {
      if (!mp->mp_close_delim)
	mp->mp_close_delim = msg_payload_format(home, "%.*s--" CR LF, 
669
						(int)m, boundary);
Pekka Pessi's avatar
Pekka Pessi committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
      if (!mp->mp_close_delim)
	return -1;
    }
    else if (mp->mp_close_delim) {
      msg_payload_t *e = mp->mp_close_delim;

      mp->mp_close_delim = NULL;

      if (e->pl_common->h_prev)
	*e->pl_common->h_prev = e->pl_common->h_succ;
      if (e->pl_common->h_succ)
	e->pl_common->h_succ->sh_prev = e->pl_common->h_prev;
    }

    mp->mp_common->h_data = mp->mp_data;
    mp->mp_common->h_len = mp->mp_len;

    if (!mp->mp_separator)
      if (!(mp->mp_separator = msg_separator_make(home, CR LF)))
	return -1;

    if (mp->mp_multipart) {
      c = mp->mp_content_type;
      if (c == NULL)
	return (errno = EBADMSG), -1;

      if (msg_multipart_complete(home, c, mp->mp_multipart) < 0)
	return -1;
    }

    if (!mp->mp_payload)
      if (!(mp->mp_payload = msg_payload_create(home, NULL, 0)))
	return -1;
  }

  return 0;
}

/** Serialize a multipart message.
 *
 */
msg_header_t *msg_multipart_serialize(msg_header_t **head0,
				      msg_multipart_t *mp)
{
  msg_header_t *h_succ_all = NULL;
  msg_header_t *h, **head, **hh, *h0, *h_succ;
  void *hend;

#define is_in_chain(h) ((h) && ((msg_frg_t*)(h))->h_prev != NULL)
#define insert(head, h) \
  ((h)->sh_succ = *(head), *(head) = (h), \
   (h)->sh_prev = (head), (head) = &(h)->sh_succ)

  if (mp == NULL || head0 == NULL)
    return NULL;

  h_succ_all = *head0; head = head0;

  for (; mp; mp = mp->mp_next) {
    h0 = (msg_header_t *)mp;

    assert(mp->mp_separator); assert(mp->mp_payload);
    assert(mp->mp_next || mp->mp_close_delim);

    if (!mp->mp_separator || !mp->mp_payload ||
	(!mp->mp_next && !mp->mp_close_delim))
      return NULL;

    if ((void *)mp == h_succ_all)
      h_succ_all = NULL;

    *head0 = h0; h0->sh_prev = head;

    if (is_in_chain(mp->mp_separator))
      hend = mp->mp_separator;
    else if (is_in_chain(mp->mp_payload))
      hend = mp->mp_payload;
    else if (is_in_chain(mp->mp_multipart))
      hend = mp->mp_multipart;
    else if (is_in_chain(mp->mp_close_delim))
      hend = mp->mp_close_delim;
    else if (is_in_chain(mp->mp_next))
      hend = mp->mp_next;
    else
      hend = NULL;

    /* Search latest header in chain */
    for (head = &mp->mp_common->h_succ;
	 *head && *head != hend;
	 head = &(*head)->sh_succ)
      ;

    h_succ = *head;

    /* Serialize headers */
765
    for (hh = &((msg_pub_t*)mp)->msg_request;
766
	 (char *)hh < (char *)&mp->mp_separator;
Pekka Pessi's avatar
Pekka Pessi committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
	 hh++) {
      h = *hh; if (!h) continue;
      for (h = *hh; h; h = h->sh_next) {
	if (h == h_succ || !is_in_chain(h)) {
	  *head = h; h->sh_prev = head; head = &h->sh_succ;
	  while (*head && *head != hend)
	    head = &(*head)->sh_succ;
	  if (h == h_succ)
	    h_succ = *head;
	}
	else {
	  /* XXX Check that h is between head and hend */
	}
      }
    }

    if (!is_in_chain(mp->mp_separator)) {
      insert(head, (msg_header_t *)mp->mp_separator);
    } else {
      assert(h_succ == (msg_header_t *)mp->mp_separator);
      mp->mp_separator->sep_common->h_prev = head;
      *head = (msg_header_t *)mp->mp_separator;
      head = &mp->mp_separator->sep_common->h_succ;
      h_succ = *head;
    }

    if (!is_in_chain(mp->mp_payload)) {
      insert(head, (msg_header_t *)mp->mp_payload);
    } else {
      assert(h_succ == (msg_header_t *)mp->mp_payload);
      mp->mp_payload->pl_common->h_prev = head;
      *head = (msg_header_t *)mp->mp_payload;
      head = &mp->mp_payload->pl_common->h_succ;
      h_succ = *head;
    }

    if (mp->mp_multipart) {
      if ((*head = h_succ))
	h_succ->sh_prev = head;
      if (!(h = msg_multipart_serialize(head, mp->mp_multipart)))
	return NULL;
      head = &h->sh_succ; h_succ = *head;
    }

    if (mp->mp_close_delim) {
      if (!is_in_chain(mp->mp_close_delim)) {
	insert(head, (msg_header_t*)mp->mp_close_delim);
      } else {
	assert(h_succ == (msg_header_t *)mp->mp_close_delim);
	mp->mp_close_delim->pl_common->h_prev = head;
	*head = (msg_header_t *)mp->mp_close_delim;
	head = &mp->mp_close_delim->pl_common->h_succ;
      }

      if (h_succ_all)
	*head = h_succ_all, h_succ_all->sh_prev = head;

      return (msg_header_t *)mp->mp_close_delim;
    }

    *head = h_succ;

    head0 = head;
  }

  assert(!mp);

  return NULL;
}

/** Encode a multipart.
 *
 * @return The size of multipart in bytes, or -1 upon an error.
 */
841
issize_t msg_multipart_prepare(msg_t *msg, msg_multipart_t *mp, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
{
  if (!mp || !mp->mp_data)
    return -1;

  if (!mp->mp_common->h_data || 
      mp->mp_common->h_len != mp->mp_len - 2 ||
      memcmp(mp->mp_common->h_data, mp->mp_data + 2, mp->mp_len - 2)) {
    mp->mp_common->h_data = mp->mp_data + 2;
    mp->mp_common->h_len = mp->mp_len - 2;
  }

  return msg_headers_prepare(msg, (msg_header_t *)mp, flags);
}

/** Decode a multipart. */
857
issize_t msg_multipart_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
858 859 860 861 862 863 864 865 866
{
  su_home_t tmphome[1] = { SU_HOME_INIT(tmphome) };
  msg_payload_t pl[1];
  msg_multipart_t *mp, *result;

  assert(h && msg_is_multipart(h));

  msg_payload_init(pl);
  
867
  result = (msg_multipart_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

  pl->pl_data = s; 
  pl->pl_len = slen;

  mp = msg_multipart_parse(tmphome, NULL, pl);

  if (mp) {
    *result = *mp;

    if (result->mp_common->h_succ->sh_prev)
      result->mp_common->h_succ->sh_prev = 
	&result->mp_common->h_succ;

    su_free(tmphome, mp);
    
    su_home_move(home, tmphome);
  }
   
  su_home_deinit(tmphome);

  return mp ? 0 : -1;
}

/** Encode a multipart.
 *
 * Please note that here we just encode a element, the msg_multipart_t
 * itself.
 */
896
issize_t msg_multipart_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
897 898 899 900 901
{
  return msg_payload_e(b, bsiz, h, flags);
}

/** Calculate extra size of a multipart */
902
isize_t msg_multipart_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
903
{
904
  msg_multipart_t const *mp = (msg_multipart_t *)h;
905
  msg_header_t const * const *hh;
Pekka Pessi's avatar
Pekka Pessi committed
906 907 908

  offset = msg_payload_dup_xtra(h, offset);

909
  for (hh = (msg_header_t const **)&((msg_pub_t *)mp)->msg_request;
910
       (char *)hh <= (char *)&mp->mp_close_delim; 
Pekka Pessi's avatar
Pekka Pessi committed
911 912 913 914 915 916 917 918 919 920 921 922
       hh++) {
    for (h = *hh; h; h = h->sh_next) {
      MSG_STRUCT_SIZE_ALIGN(offset);
      offset = h->sh_class->hc_dxtra(h, offset + h->sh_class->hc_size);
    }
  }

  return offset;
}

/** Duplicate one msg_multipart_t object */
char *msg_multipart_dup_one(msg_header_t *dst, msg_header_t const *src,
923
			    char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
924
{
925
  msg_multipart_t const *mp = (msg_multipart_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
926 927 928 929 930
  msg_header_t *h, **hh;
  char *end = b + xtra;

  b = msg_payload_dup_one(dst, src, b, xtra);

931
  for (hh = &((msg_pub_t*)mp)->msg_request;
932
       (char *)hh <= (char *)&mp->mp_close_delim; 
Pekka Pessi's avatar
Pekka Pessi committed
933 934 935 936 937 938 939
       hh++) {
    for (h = *hh; h; h = h->sh_next) {
      MSG_STRUCT_ALIGN(b);
      dst = (msg_header_t *)b;
      memset(dst, 0, sizeof dst->sh_common);
      dst->sh_class = h->sh_class;
      b = h->sh_class->hc_dup_one(dst, h, b + h->sh_class->hc_size, end - b);
940 941
      if (h->sh_class->hc_update)
	msg_header_update_params(h->sh_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
      assert(b <= end);
    }
  }

  return b;
}

#if 0
msg_hclass_t msg_multipart_class[] =
MSG_HEADER_CLASS(msg_, multipart, NULL, "", mp_common, append, msg_multipart);
#endif

/**Calculate Q value.
 *
 * The function msg_q_value() converts q-value string @a q to numeric value
 * in range (0..1000).  Q values are used, for instance, to describe
 * relative priorities of registered contacts.
 *
 * @param q q-value string ("1" | "." 1,3DIGIT)
 *
 * @return
 * The function msg_q_value() returns an integer in range 0 .. 1000.
 */
unsigned msg_q_value(char const *q)
{
  unsigned value = 0;

  if (!q)
    return 500;
  if (q[0] != '0' && q[0] != '.' && q[0] != '1')
    return 500;
  while (q[0] == '0')
    q++;
  if (q[0] >= '1' && q[0] <= '9')
    return 1000;
  if (q[0] == '\0')
    return 0;
  if (q[0] != '.')
    /* Garbage... */
    return 500;

  if (q[1] >= '0' && q[1] <= '9') {
    value = (q[1] - '0') * 100;
    if (q[2] >= '0' && q[2] <= '9') {
      value += (q[2] - '0') * 10;
      if (q[3] >= '0' && q[3] <= '9') {
	value += (q[3] - '0');
	if (q[4] > '5' && q[4] <= '9')
	  /* Round upwards */
	  value += 1;
	else if (q[4] == '5')
	  value += value & 1; /* Round to even */
      }
    }
  }

  return value;
}

/** Parse media type (type/subtype).
 *
 * The function msg_mediatype_d() parses a mediatype string.
 *
1005 1006
 * @param[in,out] ss    string to be parsed 
 * @param[out]    type  value result for media type 
Pekka Pessi's avatar
Pekka Pessi committed
1007 1008 1009 1010
 *
 * @retval 0 when successful,
 * @retval -1 upon an error.
 */
1011
issize_t msg_mediatype_d(char **ss, char const **type)
Pekka Pessi's avatar
Pekka Pessi committed
1012 1013 1014
{
  char *s = *ss;
  char const *result = s;
1015
  size_t l1 = 0, l2 = 0, n;
Pekka Pessi's avatar
Pekka Pessi committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

  /* Media type consists of two tokens, separated by / */

  l1 = span_token(s);
  for (n = l1; IS_LWS(s[n]); n++);
  if (s[n] == '/') {
    for (n++; IS_LWS(s[n]); n++);
    l2 = span_token(s + n);
    n += l2;
  }

  if (l1 == 0 || l2 == 0)
    return -1;

  /* If there is extra ws between tokens, compact version */
  if (n > l1 + 1 + l2) {
    s[l1] = '/';
    memmove(s + l1 + 1, s + n - l2, l2);
    s[l1 + 1 + l2] = 0;
  }

  s += n;

  while (IS_WS(*s)) *s++ = '\0';

  *ss = s;

  if (type)
    *type = result;

  return 0;
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept Accept Header
 *
 * The @b Accept request-header field can be used to specify certain media
 * types which are acceptable for the response. Its syntax is defined in
 * [H14.1, S20.1] as follows:
 *
 * @code
 *    Accept         = "Accept" ":" #( media-range [ accept-params ] )
 *
 *    media-range    = ( "*" "/" "*"
 *                     | ( type "/" "*" )
 *                     | ( type "/" subtype ) ) *( ";" parameter )
 *
 *    accept-params  = ";" "q" "=" qvalue *( accept-extension )
 *
 *    accept-extension = ";" token [ "=" ( token | quoted-string ) ]
 * @endcode
 *
 */

/**@ingroup msg_accept
 * @typedef typedef struct msg_accept_s msg_accept_t;
 *
 * The structure msg_accept_t contains representation of an @b Accept
 * header.
 *
 * The msg_accept_t is defined as follows:
 * @code
 * typedef struct msg_accept_s {
 *   msg_common_t        ac_common[1]; // Common fragment info
 *   msg_accept_t       *ac_next;      // Pointer to next Accept header
 *   char const         *ac_type;      // Pointer to type/subtype
 *   char const         *ac_subtype;   // Points after first slash in type
 *   msg_param_t const  *ac_params;    // List of parameters
 *   msg_param_t         ac_q;         // Value of q parameter
 * } msg_accept_t;
 * @endcode
 */

msg_hclass_t msg_accept_class[] =
1092 1093
MSG_HEADER_CLASS(msg_, accept, "Accept", "", ac_params, apndlist, 
		 msg_accept, msg_accept);
Pekka Pessi's avatar
Pekka Pessi committed
1094

1095
issize_t msg_accept_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1096
{
1097
  msg_accept_t *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1098

1099 1100
  while (*s == ',')   /* Ignore empty entries (comma-whitespace) */
    *s = '\0', s += span_lws(s + 1) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1101

1102 1103
  if (*s == '\0') {
    /* Empty Accept list is not an error */
1104
    ac->ac_type = ac->ac_subtype = "";
1105
    return 0;
1106
  }
Pekka Pessi's avatar
Pekka Pessi committed
1107

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
  /* "Accept:" #(type/subtyp ; *(parameters))) */
  if (msg_mediatype_d(&s, &ac->ac_type) == -1)
    return -1;
  if (!(ac->ac_subtype = strchr(ac->ac_type, '/')))
    return -1;
  ac->ac_subtype++;

  if (*s == ';' && msg_params_d(home, &s, &ac->ac_params) == -1)
    return -1;

  return msg_parse_next_field(home, h, s, slen);
Pekka Pessi's avatar
Pekka Pessi committed
1119 1120
}

1121
issize_t msg_accept_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1122 1123
{
  char *b0 = b, *end = b + bsiz;
1124
  msg_accept_t const *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

  assert(msg_is_accept(h));

  if (ac->ac_type) {
    MSG_STRING_E(b, end, ac->ac_type);
    MSG_PARAMS_E(b, end, ac->ac_params, flags);
  }
  MSG_TERM_E(b, end);

  return b - b0;
}

1137
isize_t msg_accept_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1138
{
1139
  msg_accept_t const *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1140 1141

  if (ac->ac_type) {
1142 1143
    MSG_PARAMS_SIZE(offset, ac->ac_params);
    offset += MSG_STRING_SIZE(ac->ac_type);
Pekka Pessi's avatar
Pekka Pessi committed
1144 1145
  }

1146
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1147 1148 1149 1150
}

/** Duplicate one msg_accept_t object */
char *msg_accept_dup_one(msg_header_t *dst, msg_header_t const *src,
1151
		      char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1152
{
1153 1154
  msg_accept_t *ac = (msg_accept_t *)dst;
  msg_accept_t const *o = (msg_accept_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1155 1156 1157 1158 1159 1160 1161 1162 1163
  char *end = b + xtra;

  if (o->ac_type) {
    b = msg_params_dup(&ac->ac_params, o->ac_params, b, xtra);
    MSG_STRING_DUP(b, ac->ac_type, o->ac_type);
    if ((ac->ac_subtype = strchr(ac->ac_type, '/')))
      ac->ac_subtype++;
  }

1164
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1165 1166 1167 1168

  return b;
}

1169 1170
/** Update parameter(s) for Accept header. */ 
int msg_accept_update(msg_common_t *h, 
1171
		      char const *name, isize_t namelen,
1172
		      char const *value)
Pekka Pessi's avatar
Pekka Pessi committed
1173
{
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
  msg_accept_t *ac = (msg_accept_t *)h;

  if (name == NULL) {
    ac->ac_q = NULL;
  }
  else if (namelen == 1 && strncasecmp(name, "q", 1) == 0) {
    /* XXX - check for invalid value? */
    ac->ac_q = value;
  }

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1185 1186 1187 1188 1189
}

/* ====================================================================== */

/** Decode an Accept-* header. */
1190 1191 1192
issize_t msg_accept_any_d(su_home_t *home,
			  msg_header_t *h,
			  char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1193
{
1194
  /** @relatesalso msg_accept_any_s */
1195
  msg_accept_any_t *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1196

1197 1198
  while (*s == ',')   /* Ignore empty entries (comma-whitespace) */
    *s = '\0', s += span_lws(s + 1) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1199

1200 1201
  if (*s == '\0')
    return -2;			/* Empty list */
1202

1203 1204 1205
  /* "Accept-*:" 1#(token *(SEMI accept-param)) */
  if (msg_token_d(&s, &aa->aa_value) == -1)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1206

1207 1208
  if (*s == ';' && msg_params_d(home, &s, &aa->aa_params) == -1)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1209

1210
  return msg_parse_next_field(home, h, s, slen);
Pekka Pessi's avatar
Pekka Pessi committed
1211 1212
}

1213
/** Encode an Accept-* header field. */
1214
issize_t msg_accept_any_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1215
{
1216
  /** @relatesalso msg_accept_any_s */
Pekka Pessi's avatar
Pekka Pessi committed
1217
  char *b0 = b, *end = b + bsiz;
1218
  msg_accept_any_t const *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1219 1220

  MSG_STRING_E(b, end, aa->aa_value);
1221
  MSG_PARAMS_E(b, end, aa->aa_params, flags);
Pekka Pessi's avatar
Pekka Pessi committed
1222 1223 1224 1225 1226 1227
  MSG_TERM_E(b, end);

  return b - b0;
}

/** Calculate extra memory used by accept-* headers. */
1228
isize_t msg_accept_any_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1229
{
1230
  /** @relatesalso msg_accept_any_s */
1231
  msg_accept_any_t const *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1232

1233 1234
  MSG_PARAMS_SIZE(offset, aa->aa_params);
  offset += MSG_STRING_SIZE(aa->aa_value);
Pekka Pessi's avatar
Pekka Pessi committed
1235

1236
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1237 1238 1239 1240
}

/** Duplicate one msg_accept_any_t object. */
char *msg_accept_any_dup_one(msg_header_t *dst, msg_header_t const *src,
1241
			     char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1242
{
1243
  /** @relatesalso msg_accept_any_s */
1244 1245
  msg_accept_any_t *aa = (msg_accept_any_t *)dst;
  msg_accept_any_t const *o = (msg_accept_any_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1246 1247
  char *end = b + xtra;

1248
  b = msg_params_dup(&aa->aa_params, o->aa_params, b, xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1249 1250
  MSG_STRING_DUP(b, aa->aa_value, o->aa_value);

1251
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1252 1253 1254 1255

  return b;
}

1256 1257
/** Update parameter(s) for Accept-* header. */ 
int msg_accept_any_update(msg_common_t *h, 
1258
			  char const *name, isize_t namelen,
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
			  char const *value)
{
  msg_accept_any_t *aa = (msg_accept_any_t *)h;

  if (name == NULL) {
    aa->aa_q = NULL;
  }
  else if (namelen == 1 && strncasecmp(name, "q", 1) == 0) {
    aa->aa_q = value;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept_charset Accept-Charset Header
 *
 * The Accept-Charset header is similar to Accept, but restricts the
 * character set that are acceptable in the response.  Its syntax is
 * defined in [H14.2] as follows:
 *
 * @code
 *    Accept-Charset = "Accept-Charset" ":"
 *            1#( ( charset | "*" )[ ";" "q" "=" qvalue ] )
 * @endcode
 *
 */

/**@ingroup msg_accept_charset
 * @typedef typedef struct msg_accept_charset_s msg_accept_charset_t;
 *
 * The structure msg_accept_encoding_t contains representation of @b
 * Accept-Charset header.
 *
 * The msg_accept_charset_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t        aa_common[1]; // Common fragment info
 *   msg_accept_any_t   *aa_next;      // Pointer to next Accept-Charset
 *   char const         *aa_value;     // Charset
1301
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1302 1303 1304 1305 1306 1307 1308
 *   char const         *aa_q;	       // Q-value
 * } msg_accept_charset_t;
 * @endcode
 */

msg_hclass_t msg_accept_charset_class[1] =
 MSG_HEADER_CLASS(msg_, accept_charset, "Accept-Charset", "",
1309
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1310

1311
issize_t msg_accept_charset_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1312 1313 1314 1315
{
  return msg_accept_any_d(home, h, s, slen);
}

1316
issize_t msg_accept_charset_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
{
  assert(msg_is_accept_charset(h));
  return msg_accept_any_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept_encoding Accept-Encoding Header
 *
 * The Accept-Encoding header is similar to Accept, but restricts the
 * content-codings that are acceptable in the response.  Its syntax is
1329
 * defined in [H14.3, S20.2] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
 *
 * @code
 *    Accept-Encoding  = "Accept-Encoding" ":"
 *                       1#( codings [ ";" "q" "=" qvalue ] )
 *    codings          = ( content-coding | "*" )
 *    content-coding   = token
 * @endcode
 *
 */

/**@ingroup msg_accept_encoding
 * @typedef typedef struct msg_accept_encoding_s msg_accept_encoding_t;
 *
 * The structure msg_accept_encoding_t contains representation of @b
 * Accept-Encoding header.
 *
 * The msg_accept_encoding_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t        aa_common[1]; // Common fragment info
 *   msg_accept_any_t   *aa_next;      // Pointer to next Accept-Encoding
 *   char const         *aa_value;     // Content-coding
1352
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1353 1354 1355 1356 1357 1358 1359
 *   char const         *aa_q;	       // Q-value
 * } msg_accept_encoding_t;
 * @endcode
 */

msg_hclass_t msg_accept_encoding_class[1] =
 MSG_HEADER_CLASS(msg_, accept_encoding, "Accept-Encoding", "",
1360
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1361

1362
issize_t msg_accept_encoding_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1363 1364 1365 1366
{
  return msg_accept_any_d(home, h, s, slen);
}

1367
issize_t msg_accept_encoding_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
{
  return msg_accept_any_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept_language Accept-Language Header
 *
 * The Accept-Language header allows the client to indicate to the server in
 * which language it would prefer to receive reason phrases, session
 * descriptions or status responses carried as message bodies. Its syntax is
1380
 * defined in [H14.4, S20.3] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
 *
 * @code
 *    Accept-Language = "Accept-Language" ":"
 *                      1#( language-range [ ";" "q" "=" qvalue ] )
 *
 *    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
 * @endcode
 *
 */

/**@ingroup msg_accept_language
 * @typedef typedef struct msg_accept_language_s msg_accept_language_t;
 *
 * The structure msg_accept_language_t contains representation of @b
 * Accept-Language header.
 *
 * The msg_accept_language_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t        aa_common[1]; // Common fragment info
 *   msg_accept_any_t   *aa_next;      // Pointer to next Accept-Encoding
 *   char const         *aa_value;     // Language-range
1403
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1404 1405 1406 1407 1408 1409 1410
 *   char const         *aa_q;	       // Q-value
 * } msg_accept_language_t;
 * @endcode
 */

msg_hclass_t msg_accept_language_class[1] =
 MSG_HEADER_CLASS(msg_, accept_language, "Accept-Language", "",
1411
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1412

1413
issize_t msg_accept_language_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1414 1415 1416 1417
{
  return msg_accept_any_d(home, h, s, slen);
}

1418
issize_t msg_accept_language_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
{
  assert(msg_is_accept_language(h));
  return msg_accept_any_e(b, bsiz, h, f);
}


/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_disposition Content-Disposition Header
 *
 * The Content-Disposition header field describes how the message body or,
 * in the case of multipart messages, a message body part is to be
1432 1433
 * interpreted by the UAC or UAS.  Its syntax is defined in [S20.11]
 * as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
 *
 * @code
 *    Content-Disposition   =  "Content-Disposition" ":"
 *                             disposition-type *( ";" disposition-param )
 *    disposition-type      =  "render" | "session" | "icon" | "alert"
 *                         |   disp-extension-token
 *    disposition-param     =  "handling" "="
 *                             ( "optional" | "required" | other-handling )
 *                         |   generic-param
 *    other-handling        =  token
 *    disp-extension-token  =  token
 * @endcode
 *
 * The Content-Disposition header was extended by
 * draft-lennox-sip-reg-payload-01.txt section 3.1 as follows:
 *
 * @code
 *    Content-Disposition      =  "Content-Disposition" ":"
 *                                disposition-type *( ";" disposition-param )
1453 1454 1455
 *    disposition-type        /=  "script" | "sip-cgi" | token
 *    disposition-param       /=  action-param
 *                             /  modification-date-param
Pekka Pessi's avatar
Pekka Pessi committed
1456 1457 1458 1459 1460 1461 1462
 *    action-param             =  "action" "=" action-value
 *    action-value             =  "store" | "remove" | token
 *    modification-date-param  =  "modification-date" "=" quoted-date-time
 *    quoted-date-time         =  <"> SIP-date <">
 * @endcode
 */

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
/**@ingroup msg_content_disposition
 * @typedef struct msg_content_disposition_s msg_content_disposition_t; 
 *
 * The structure msg_content_disposition_t contains representation of an @b
 * Content-Disposition header.
 *
 * The msg_content_disposition_t is defined as follows:
 * @code
 * typedef struct msg_content_disposition_s
 * {
 *   msg_common_t       cd_common[1];  // Common fragment info
 *   msg_error_t       *cd_next;       // Link to next (dummy)
 *   char const        *cd_type;       // Disposition type
 *   msg_param_t const *cd_params;     // List of parameters
 *   msg_param_t        cd_handling;   // Value of @b handling parameter
 *   unsigned           cd_required:1; // True if handling=required
 *   unsigned           cd_optional:1; // True if handling=optional
 * } msg_content_disposition_t;
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1484 1485
msg_hclass_t msg_content_disposition_class[] =
MSG_HEADER_CLASS(msg_, content_disposition, "Content-Disposition", "",
1486 1487
		 cd_params, single, msg_content_disposition,
		 msg_content_disposition);
Pekka Pessi's avatar
Pekka Pessi committed
1488

1489
issize_t msg_content_disposition_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1490
{
1491
  msg_content_disposition_t *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1492 1493 1494 1495 1496 1497

  if (msg_token_d(&s, &cd->cd_type) < 0 ||
      (*s == ';' && msg_params_d(home, &s, &cd->cd_params) < 0))
      return -1;

  if (cd->cd_params)
1498
    msg_header_update_params(cd->cd_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1499 1500 1501 1502

  return 0;
}

1503
issize_t msg_content_disposition_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1504 1505
{
  char *b0 = b, *end = b + bsiz;
1506
  msg_content_disposition_t const *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

  assert(msg_is_content_disposition(h));

  MSG_STRING_E(b, end, cd->cd_type);
  MSG_PARAMS_E(b, end, cd->cd_params, f);

  MSG_TERM_E(b, end);

  return b - b0;
}

1518
isize_t msg_content_disposition_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1519
{
1520
  msg_content_disposition_t const *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1521

1522 1523
  MSG_PARAMS_SIZE(offset, cd->cd_params);
  offset += MSG_STRING_SIZE(cd->cd_type);
Pekka Pessi's avatar
Pekka Pessi committed
1524

1525
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1526 1527 1528 1529 1530
}

/** Duplicate one msg_content_disposition_t object */
char *msg_content_disposition_dup_one(msg_header_t *dst,
				     msg_header_t const *src,
1531
				     char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1532
{
1533 1534
  msg_content_disposition_t *cd = (msg_content_disposition_t *)dst;
  msg_content_disposition_t const *o = (msg_content_disposition_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1535 1536 1537 1538 1539
  char *end = b + xtra;

  b = msg_params_dup(&cd->cd_params, o->cd_params, b, xtra);
  MSG_STRING_DUP(b, cd->cd_type, o->cd_type);

1540
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1541 1542 1543 1544

  return b;
}

1545 1546
/** Update Content-Disposition parameters */
int msg_content_disposition_update(msg_common_t *h,
1547
				   char const *name, isize_t namelen,
1548
				   char const *value)
Pekka Pessi's avatar
Pekka Pessi committed
1549
{
1550
  msg_content_disposition_t *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1551

1552 1553 1554 1555 1556 1557 1558 1559 1560
  if (name == NULL) {
    cd->cd_handling = NULL, cd->cd_required = 0, cd->cd_optional = 0;
  }
  else if (namelen == strlen("handling") &&
	   strncasecmp(name, "handling", namelen) == 0) {
    cd->cd_handling = value;
    cd->cd_required = strcasecmp(value, "required") == 0;
    cd->cd_optional = strcasecmp(value, "optional") == 0;
  }
Pekka Pessi's avatar
Pekka Pessi committed
1561

1562
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1563 1564 1565 1566 1567 1568 1569 1570
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_encoding Content-Encoding Header
 *
 * The Content-Encoding header indicates what additional content codings
1571 1572
 * have been applied to the entity-body. Its syntax is defined in [H14.11]
 * and [S20.12] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1573 1574
 *
 * @code
1575
 *    Content-Encoding = ( "Content-Encoding" / "e" ) ":" 1#content-coding
Pekka Pessi's avatar
Pekka Pessi committed
1576 1577 1578 1579
 *    content-coding   = token
 * @endcode
 */

1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
/**@ingroup msg_content_encoding
 * @typedef struct msg_list_s msg_content_encoding_t; 
 *
 * The structure msg_content_encoding_t contains representation of an @b
 * Content-Encoding header.
 *
 * The msg_content_encoding_t is defined as follows:
 * @code
 * typedef struct msg_list_s
 * {
 *   msg_common_t       k_common[1];  // Common fragment info
 *   msg_list_t        *k_next;	      // Link to next header
 *   msg_param_t       *k_items;      // List of items
 * } msg_content_encoding_t;
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1597
msg_hclass_t msg_content_encoding_class[] =
1598
  MSG_HEADER_CLASS_LIST(content_encoding, "Content-Encoding", "e", list);
Pekka Pessi's avatar
Pekka Pessi committed
1599

1600
issize_t msg_content_encoding_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1601
{
1602
  msg_content_encoding_t *e = (msg_content_encoding_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1603 1604 1605
  return msg_commalist_d(home, &s, &e->k_items, msg_token_scan);
}

1606
issize_t msg_content_encoding_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
{
  assert(msg_is_content_encoding(h));
  return msg_list_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_language Content-Language Header
 *
 * The Content-Language header describes the natural language(s) of the
 * intended audience for the enclosed message body. Note that this might not
 * be equivalent to all the languages used within the message-body. Its
 * syntax is defined in [H14.12, S20.13] as follows:
 *
 * @code
 *    Content-Language  = "Content-Language" ":" 1#language-tag
 * @endcode
 * or
 * @code
 *    Content-Language  =  "Content-Language" HCOLON
 *                         language-tag *(COMMA language-tag)
 *    language-tag      =  primary-tag *( "-" subtag )
 *    primary-tag       =  1*8ALPHA
 *    subtag            =  1*8ALPHA
 * @endcode
 *
 */

/**@ingroup msg_content_language
 * @typedef typedef struct msg_content_language_s msg_content_language_t;
 *
 * The structure msg_content_language_t contains representation of @b
 * Content-Language header.
 *
 * The msg_content_language_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t            k_common[1]; // Common fragment info
 *   msg_content_language_t *k_next;      // (Content-Encoding header)
 *   msg_param_t            *k_items;     // List of languages
 * } msg_content_language_t;
 * @endcode
 */

msg_hclass_t msg_content_language_class[] =
MSG_HEADER_CLASS_LIST(content_language, "Content-Language", "", list);

1655
issize_t msg_content_language_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1656
{
1657
  msg_content_language_t *k = (msg_content_language_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1658 1659 1660
  return msg_commalist_d(home, &s, &k->k_items, msg_token_scan);
}

1661
issize_t msg_content_language_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
{
  assert(msg_is_content_language(h));
  return msg_list_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_length Content-Length Header
 *
 * The Content-Length header indicates the size of the message-body in
 * decimal number of octets.  Its syntax is defined in [S10.18] as
 * follows:
 *
 * @code
 *    Content-Length  =  ( "Content-Length" / "l" ) HCOLON 1*DIGIT
 * @endcode
 *
 */

/**@ingroup msg_content_length
 * @typedef typedef struct msg_content_length_s msg_content_length_t;
 *
 * The structure msg_content_length_t contains representation of a
 * Content-Length header.
 *
 * The msg_content_length_t is defined as follows:
 * @code
 * typedef struct msg_content_length_s {
 *   msg_common_t   l_common[1];        // Common fragment info
 *   msg_error_t   *l_next;             // Link to next (dummy)
 *   unsigned long  l_length;           // Numeric value
 * } msg_content_length_t;
 * @endcode
 */

#define msg_content_length_d msg_numeric_d
#define msg_content_length_e msg_numeric_e

msg_hclass_t msg_content_length_class[] =
MSG_HEADER_CLASS(msg_, content_length, "Content-Length", "l",
1703
		 l_common, single_critical, msg_default, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721

/**@ingroup msg_content_length
 *Create a @b Content-Length header object.
 *
 * The function msg_content_length_create() creates a Content-Length
 * header object with the value @a n.  The memory for the header is
 * allocated from the memory home @a home.
 *
 * @param home  memory home
 * @param n     payload size in bytes
 *
 * @return
 * The function msg_content_length_create() returns a pointer to newly
 * created @b Content-Length header object when successful or NULL upon
 * an error.
 */
msg_content_length_t *msg_content_length_create(su_home_t *home, uint32_t n)
{
1722 1723
  msg_content_length_t *l = (msg_content_length_t *)
    msg_header_alloc(home, msg_content_length_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

  if (l)
    l->l_length = n;

  return l;
}


/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_md5 Content-MD5 Header
 *
 * The Content-MD5 header is an MD5 digest of the entity-body for the
 * purpose of providing an end-to-end message integrity check (MIC) of the
1739
 * message-body. Its syntax is defined in [@RFC1864, H14.15] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1740 1741 1742
 *
 * @code
 *      Content-MD5   = "Content-MD5" ":" md5-digest
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
 *      md5-digest   = <base64 of 128 bit MD5 digest as per @RFC1864>
 * @endcode
 */

/**@ingroup msg_content_md5
 * @typedef struct msg_generic_s msg_content_md5_t; 
 *
 * The structure msg_content_md5_t contains representation of an @b
 * Content-MD5 header.
 *
 * The msg_content_md5_t is defined as follows:
 * @code
 * typedef struct msg_generic_s
 * {
 *   msg_common_t   g_common[1];    // Common fragment info
 *   msg_generic_t *g_next;	    // Link to next header
 *   char const    *g_string;	    // Header value
 * } msg_content_md5_t;
Pekka Pessi's avatar
Pekka Pessi committed
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
 * @endcode
 */

#define msg_content_md5_d msg_generic_d
#define msg_content_md5_e msg_generic_e
msg_hclass_t msg_content_md5_class[] =
MSG_HEADER_CLASS_G(content_md5, "Content-MD5", "", single);

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_id Content-ID Header
 *
 * The Content-ID header is an unique identifier of an entity-body. The
 * Content-ID value may be used for uniquely identifying MIME entities in
 * several contexts, particularly for caching data referenced by the
 * message/external-body mechanism. Its syntax is defined in [RFC2045] as
 * follows:
 *
 * @code
 *      Content-ID   = "Content-ID" ":" msg-id
 *      msg-id       = [CFWS] "<" id-left "@" id-right ">" [CFWS]
 *      id-left      = dot-atom-text / no-fold-quote / obs-id-left
 *      id-right     = dot-atom-text / no-fold-literal / obs-id-right
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
/**@ingroup msg_content_id
 * @typedef msg_generic_t msg_content_id_t;
 * Content-ID Header Structure.
 * @code
 * typedef struct
 * {
 *   msg_common_t      g_common[1];    // Common fragment info
 *   msg_content_id_t *g_next;	       // Link to next header
 *   char const       *g_string;       // Header value
 * }
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
#define msg_content_id_d msg_generic_d
#define msg_content_id_e msg_generic_e
msg_hclass_t msg_content_id_class[] =
MSG_HEADER_CLASS_G(content_id, "Content-ID", "", single);

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_type Content-Type Header
 *
 * The @b Content-Type header indicates the media type of the message-body
1812
 * sent to the recipient. Its syntax is defined in [H3.7, S20.15]
Pekka Pessi's avatar
Pekka Pessi committed
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
 * as follows:
 *
 * @code
 *    Content-Type  = ( "Content-Type" | "c" ) ":" media-type
 *    media-type    = type "/" subtype *( ";" parameter )
 *    type          = token
 *    subtype       = token
 * @endcode
 */

/**@ingroup msg_content_type
 * @typedef typedef struct msg_content_type_s msg_content_type_t;
 *
 * The structure msg_content_type_t contains representation of @b
 * Content-Type header.
 *
 * The msg_content_type_t is defined as follows:
 * @code
 * typedef struct msg_content_type_s {
 *   msg_common_t        c_common[1];  // Common fragment info
 *   msg_unknown_t      *c_next;       // Dummy link to next
 *   char const         *c_type;       // Pointer to type/subtype
 *   char const         *c_subtype;    // Points after first slash in type
 *   msg_param_t const  *c_params;     // List of parameters
 * } msg_content_type_t;
 * @endcode
 *
 * The @a c_type is always void of whitespace, that is, there is no
 * whitespace around the slash.
 */

1844 1845
#define msg_content_type_update NULL

Pekka Pessi's avatar
Pekka Pessi committed
1846 1847
msg_hclass_t msg_content_type_class[] =
MSG_HEADER_CLASS(msg_, content_type, "Content-Type", "c", c_params,
1848
		 single, msg_content_type, msg_content_type);
Pekka Pessi's avatar
Pekka Pessi committed
1849

1850
issize_t msg_content_type_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1851 1852 1853 1854 1855
{
  msg_content_type_t *c;

  assert(h);

1856
  c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869

  /* "Content-type:" type/subtyp *(; parameter))) */
  if (msg_mediatype_d(&s, &c->c_type) == -1 || /* compacts token / token */
      (c->c_subtype = strchr(c->c_type, '/')) == NULL ||
      (*s == ';' && msg_params_d(home, &s, &c->c_params) == -1) ||
      (*s != '\0'))
    return -1;

  c->c_subtype++;

  return 0;
}

1870
issize_t msg_content_type_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1871 1872
{
  char *b0 = b, *end = b + bsiz;
1873
  msg_content_type_t const *c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883

  assert(msg_is_content_type(h));

  MSG_STRING_E(b, end, c->c_type);
  MSG_PARAMS_E(b, end, c->c_params, flags);
  MSG_TERM_E(b, end);

  return b - b0;
}

1884
isize_t msg_content_type_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1885
{
1886
  msg_content_type_t const *c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1887

1888 1889
  MSG_PARAMS_SIZE(offset, c->c_params);
  offset += MSG_STRING_SIZE(c->c_type);
Pekka Pessi's avatar
Pekka Pessi committed
1890

1891
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1892 1893 1894 1895
}

/** Duplicate one msg_content_type_t object */
char *msg_content_type_dup_one(msg_header_t *dst, msg_header_t const *src,
1896
			       char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1897
{
1898 1899
  msg_content_type_t *c = (msg_content_type_t *)dst;
  msg_content_type_t const *o = (msg_content_type_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1900 1901 1902 1903 1904 1905 1906
  char *end = b + xtra;

  b = msg_params_dup(&c->c_params, o->c_params, b, xtra);
  MSG_STRING_DUP(b, c->c_type, o->c_type);
  c->c_subtype = strchr(c->c_type, '/');
  c->c_subtype++;

1907
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917

  return b;
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_mime_version MIME-Version Header
 *
 * MIME-Version header indicates what version of the protocol was used
1918 1919
 * to construct the message.  Its syntax is defined in [H19.4.1, S20.24]
 * as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1920 1921 1922 1923 1924 1925
 *
 * @code
 *    MIME-Version   = "MIME-Version" ":" 1*DIGIT "." 1*DIGIT
 * @endcode
 */

1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
/**@ingroup msg_mime_version
 * @typedef struct msg_generic_s msg_mime_version_t; 
 *
 * The structure msg_mime_version_t contains representation of an @b
 * MIME-Version header.
 *
 * The msg_mime_version_t is defined as follows:
 * @code
 * typedef struct msg_generic_s
 * {
 *   msg_common_t   g_common[1];    // Common fragment info
 *   msg_generic_t *g_next;	    // Link to next header
 *   char const    *g_string;	    // Header value
 * } msg_mime_version_t;
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1943 1944 1945
msg_hclass_t msg_mime_version_class[] =
MSG_HEADER_CLASS_G(mime_version, "MIME-Version", "", single);

1946
issize_t msg_mime_version_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1947 1948 1949 1950
{
  return msg_generic_d(home, h, s, slen);
}

1951
issize_t msg_mime_version_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
{
  assert(msg_is_mime_version(h));
  return msg_generic_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_location Content-Location Header
 *
 *
 */

1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
/**@ingroup msg_content_location
 * @typedef struct msg_generic_s msg_content_location_t; 
 *
 * The structure msg_content_location_t contains representation of an @b
 * Content-Location header.
 *
 * The msg_content_location_t is defined as follows:
 * @code
 * typedef struct msg_generic_s
 * {
 *   msg_common_t   g_common[1];    // Common fragment info
 *   msg_generic_t *g_next;	    // Link to next header
 *   char const    *g_string;	    // Header value
 * } msg_content_location_t;
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1982 1983 1984 1985 1986 1987
#define msg_content_location_d msg_generic_d
#define msg_content_location_e msg_generic_e
msg_hclass_t msg_content_location_class[] =
MSG_HEADER_CLASS_G(content_location, "Content-Location", "", single);


1988 1989
/* ====================================================================== */
#if 0
Pekka Pessi's avatar
Pekka Pessi committed
1990 1991 1992
/**@ingroup msg_mime
 * @defgroup msg_content_base Content-Base Header
 *
1993
 * @RFC2617:
1994 1995 1996 1997
 * Content-Base was deleted from the specification: it was not
 * implemented widely, and there is no simple, safe way to introduce it
 * without a robust extension mechanism. In addition, it is used in a
 * similar, but not identical fashion in MHTML [45].
Pekka Pessi's avatar
Pekka Pessi committed
1998 1999 2000
 *
 */

2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014

/**@ingroup msg_content_base
 * @typedef msg_generic_t msg_content_base_t;
 * Content-Base Header Structure.
 * @code
 * typedef struct
 * {
 *   msg_common_t        g_common[1];    // Common fragment info
 *   msg_content_base_t *g_next;	 // Link to next header
 *   char const         *g_string;       // Header value
 * }
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
2015 2016 2017 2018 2019
#define msg_content_base_d msg_generic_d
#define msg_content_base_e msg_generic_e
msg_hclass_t msg_content_base_class[] =
MSG_HEADER_CLASS_G(content_base, "Content-Base", "", single);

2020 2021
#endif

Pekka Pessi's avatar
Pekka Pessi committed
2022 2023 2024 2025 2026 2027 2028 2029
/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_transfer_encoding Content-Transfer-Encoding Header
 *
 *
 */

2030 2031 2032 2033 2034 2035 2036 2037