http_extra.c 11.2 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
 * 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
 *
 */

25
/**@CFILE http_extra.c
Pekka Pessi's avatar
Pekka Pessi committed
26 27 28 29 30 31 32 33 34 35
 *
 * Extra HTTP headers
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Jun 13 02:57:51 2000 ppessi
 */

#include "config.h"

36
#include <sofia-sip/su_string.h>
Pekka Pessi's avatar
Pekka Pessi committed
37

38 39 40 41
/* Avoid casting http_t to msg_pub_t and http_header_t to msg_header_t  */
#define MSG_PUB_T struct http_s
#define MSG_HDR_T union http_header_u

42
#include "sofia-sip/http_parser.h"
Pekka Pessi's avatar
Pekka Pessi committed
43

44 45 46 47 48 49
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>

Pekka Pessi's avatar
Pekka Pessi committed
50 51 52 53 54 55 56 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 100 101
/* ========================================================================== */

/**@HTTP_HEADER http_proxy_connection Proxy-Connection extension header. */

#define http_proxy_connection_d msg_list_d
#define http_proxy_connection_e msg_list_e
msg_hclass_t http_proxy_connection_class[] =
HTTP_HEADER_CLASS_LIST(proxy_connection, "Proxy-Connection", list);

/* ====================================================================== */
/**@HTTP_HEADER http_cookie Cookie extension header.
 *
 * The Cookie header is used to transmit state information from server
 * back to the http client. Its syntax is defined in RFC 2109 section 4.3.4
 * as follows:
 *
 * @code
 *   cookie         = "Cookie:" cookie-version
 *                    1*((";" | ",") cookie-value)
 *   cookie-value   = NAME "=" VALUE [";" path] [";" domain]
 *   cookie-version = "$Version" "=" value
 *   NAME           = attr
 *   VALUE          = value
 *   path           = "$Path" "=" value
 *   domain         = "$Domain" "=" value
 * @endcode
 *
 */

/**@ingroup http_cookie
 *
 * @typedef typedef struct http_cookie_s http_cookie_t;
 *
 * The structure http_cookie_t contains representation of @b Cookie
 * header. Please note that a single http_cookie_t can contain many
 * cookies.
 *
 * The http_cookie_t is defined as follows:
 * @code
 * typedef struct http_cookie_s
 * {
 * } http_cookie_t;
 * @endcode
 */

/**Update Cookie parameters.
 *
 * The function http_cookie_update() updates a @b Cookie parameter
 * shortcuts.
 *
 * @param sc pointer to a @c http_cookie_t object
 */
102
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
103 104
void http_cookie_update(http_cookie_t *c)
{
105
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  c->c_name = NULL;
  c->c_version = NULL, c->c_domain = NULL, c->c_path = NULL;

  if (!c->c_params)
    return;

  if (!(MSG_PARAM_MATCH(c->c_version, c->c_params[0], "$Version")))
    return;
  if (!c->c_params[1] || c->c_params[1][0] == '$')
    return;

  c->c_name = c->c_params[1];

  for (i = 2; ; i++) {
    msg_param_t p = c->c_params[i];
    if (!p || *p++ != '$')
      break;
    switch (p[0]) {
    case 'd': case 'D':
      MSG_PARAM_MATCH(c->c_domain, p, "Domain");
      break;
    case 'p': case 'P':
      MSG_PARAM_MATCH(c->c_path, p, "Path");
      break;
    }
  }
}

/* Scan a cookie parameter */
136
static issize_t cookie_scanner(char *s)
Pekka Pessi's avatar
Pekka Pessi committed
137 138
{
  char *p = s;
139
  size_t tlen;
Pekka Pessi's avatar
Pekka Pessi committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

  skip_token(&s);

  if (s == p)		/* invalid parameter name */
    return -1;

  tlen = s - p;

  if (IS_LWS(*s)) { *s++ = '\0'; skip_lws(&s); }

  if (*s == '=') {
    char *v;
    s++;
    skip_lws(&s);

    v = s;

    /* get value */
    if (*s == '"') {
159
      size_t qlen = span_quoted(s);
Pekka Pessi's avatar
Pekka Pessi committed
160 161 162 163 164 165
      if (!qlen)
	return -1;
      s += qlen;
    }
    else {
      s += strcspn(s, ",;" LWS);
166
      if (s == v)
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	return -1;
    }

    if (p + tlen + 1 != v) {
      memmove(p + tlen + 1, v, s - v);
      p[tlen] = '=';
      p[tlen + 1 + (s - v)] = '\0';
    }
  }

  if (IS_LWS(*s)) { *s++ = '\0'; skip_lws(&s); }

  return s - p;
}

/** Decode (parse) a Cookie header */
183
issize_t http_cookie_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
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
{
  http_cookie_t *c = (http_cookie_t *)h;

  assert(h); assert(sizeof(*h));

  for (;*s;) {
    /* Ignore empty entries (comma-whitespace) */
    if (*s == ',') { *s++ = '\0'; skip_lws(&s); continue; }

    if (msg_any_list_d(home, &s, (msg_param_t **)&c->c_params,
		       cookie_scanner, ';') == -1)
      return -1;

    if (*s != '\0' && *s != ',')
      return -1;

    if (!c->c_params)
      return -1;
  }

  http_cookie_update(c);

  return 0;
}

/** Encode (print) a Cookie header */
210
issize_t http_cookie_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
211 212 213
{
  char *b0 = b, *end = b + bsiz;
  http_cookie_t const *c = (http_cookie_t *)h;
214
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228

  if (c->c_params) {
    for (i = 0; c->c_params[i]; i++) {
      if (i > 0) MSG_CHAR_E(b, end, ';');
      MSG_STRING_E(b, end, c->c_params[i]);
    }
  }

  MSG_TERM_E(b, end);

  return b - b0;
}

/** Calculate extra storage used by Cookie header field */
229
isize_t http_cookie_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
230 231 232
{
  http_cookie_t const *c = (http_cookie_t *)h;

233
  MSG_PARAMS_SIZE(offset, c->c_params);
Pekka Pessi's avatar
Pekka Pessi committed
234

235
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
236 237 238 239
}

/** Duplicate a Cookie header field */
char *http_cookie_dup_one(msg_header_t *dst, msg_header_t const *src,
240
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244 245 246 247 248
{
  http_cookie_t *c = (http_cookie_t *)dst;
  http_cookie_t const *o = (http_cookie_t const *)src;
  char *end = b + xtra;

  b = msg_params_dup(&c->c_params, o->c_params, b, xtra);
  http_cookie_update(c);

249
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

  return b;
}

msg_hclass_t http_cookie_class[] =
HTTP_HEADER_CLASS(cookie, "Cookie", c_params, append, cookie);

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

/**@HTTP_HEADER http_set_cookie Set-Cookie extension header.
 *
 * The Set-Cookie header is used to transmit state information from server
 * back to the http client. Its syntax is defined in RFC 2109 section 4.2.2
 * as follows:
 *
 * @code
 * set-cookie      =       "Set-Cookie:" cookies
 * cookies         =       1#cookie
 * cookie          =       NAME "=" VALUE *(";" cookie-av)
 * NAME            =       attr
 * VALUE           =       value
 * cookie-av       =       "Comment" "=" value
 *                 |       "Domain" "=" value
 *                 |       "Max-Age" "=" value
 *                 |       "Path" "=" value
 *                 |       "Secure"
 *                 |       "Version" "=" 1*DIGIT
 *
 * @endcode
 *
 */

/**@ingroup http_set_cookie
 *
 * @typedef typedef struct http_set_cookie_s http_set_cookie_t;
 *
 * The structure http_set_cookie_t contains representation of @b Set-Cookie
 * header.
 *
 * The http_set_cookie_t is defined as follows:
 * @code
 * typedef struct http_set_cookie_s
 * {
 * } http_set_cookie_t;
 * @endcode
 */

/**Update Set-Cookie parameters.
 *
 * The function http_set_cookie_update() updates a @b Set-Cookie parameter
 * shortcuts.
 *
 * @param sc pointer to a @c http_set_cookie_t object
 */
304
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
305 306
void http_set_cookie_update(http_set_cookie_t *sc)
{
307
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
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 338 339 340 341 342 343

  sc->sc_name = NULL;
  sc->sc_version = NULL, sc->sc_domain = NULL, sc->sc_path = NULL;
  sc->sc_comment = NULL, sc->sc_max_age = NULL, sc->sc_secure = 0;

  if (!sc->sc_params)
    return;

  sc->sc_name = sc->sc_params[0];

  for (i = 1; sc->sc_params[i]; i++) {
    msg_param_t p = sc->sc_params[i];
    switch (p[0]) {
    case 'c': case 'C':
      MSG_PARAM_MATCH(sc->sc_comment, p, "Comment");
      break;
    case 'd': case 'D':
      MSG_PARAM_MATCH(sc->sc_domain, p, "Domain");
      break;
    case 'm': case 'M':
      MSG_PARAM_MATCH(sc->sc_max_age, p, "Max-Age");
      break;
    case 'p': case 'P':
      MSG_PARAM_MATCH(sc->sc_path, p, "Path");
      break;
    case 's': case 'S':
      MSG_PARAM_MATCH_P(sc->sc_secure, p, "Secure");
      break;
    case 'v': case 'V':
      MSG_PARAM_MATCH(sc->sc_version, p, "Version");
      break;
    }
  }

}

344
#include <sofia-sip/msg_date.h>
Pekka Pessi's avatar
Pekka Pessi committed
345 346

/* Scan a cookie parameter */
347
static issize_t set_cookie_scanner(char *s)
Pekka Pessi's avatar
Pekka Pessi committed
348 349 350 351
{
  char *rest;

#define LOOKING_AT(s, what) \
352
  (su_casenmatch((s), what, strlen(what)) && (rest = s + strlen(what)))
Pekka Pessi's avatar
Pekka Pessi committed
353 354 355

  /* Special cases from Netscape spec */
  if (LOOKING_AT(s, "expires=")) {
356
    msg_time_t value;
Pekka Pessi's avatar
Pekka Pessi committed
357 358 359 360 361 362 363 364 365 366 367 368 369
    msg_date_d((char const **)&rest, &value);
  } else if (LOOKING_AT(s, "path=/")) {
    for (;;) {
      rest += span_unreserved(rest);
      if (*rest != '/')
	break;
      rest++;
    }
  } else {
    return msg_attribute_value_scanner(s);
  }
#undef LOOKING_AT

370 371
  if (IS_LWS(*rest)) {
    *rest++ = '\0'; skip_lws(&rest);
Pekka Pessi's avatar
Pekka Pessi committed
372 373 374 375 376 377
  }

  return rest - s;
}

/** Decode (parse) Set-Cookie header */
378
issize_t http_set_cookie_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
{
  msg_header_t **hh = &h->sh_succ, *h0 = h;
  http_set_cookie_t *sc = (http_set_cookie_t *)h;
  msg_param_t *params;

  assert(h); assert(sizeof(*h));

  for (;*s;) {
    /* Ignore empty entries (comma-whitespace) */
    if (*s == ',') { *s++ = '\0'; skip_lws(&s); continue; }

    if (!h) {      /* Allocate next header structure */
      if (!(h = msg_header_alloc(home, h0->sh_class, 0)))
	return -1;
      *hh = h; h->sh_prev = hh; hh = &h->sh_succ;
      sc = sc->sc_next = (http_set_cookie_t *)h;
    }

    /* "Set-Cookie:" 1#(NAME "=" VALUE *(";" cookie-av))) */
    params = su_zalloc(home, MSG_PARAMS_NUM(1) * sizeof(msg_param_t));
    if (!params)
      return -1;

    params[0] = s, sc->sc_params = params;
    s += strcspn(s, ",;" LWS);

    if (*s) {
      *s++ = '\0';
      skip_lws(&s);
      if (*s && msg_any_list_d(home, &s, (msg_param_t **)&sc->sc_params,
			       set_cookie_scanner, ';') == -1)
	return -1;
    }

    if (*s != '\0' && *s != ',')
      return -1;

    if (sc->sc_params)
      http_set_cookie_update(sc);

    h = NULL;
  }

  return 0;
}

/** Encode (print) Set-Cookie header */
426
issize_t http_set_cookie_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
427 428 429
{
  char *b0 = b, *end = b + bsiz;
  http_set_cookie_t const *sc = (http_set_cookie_t *)h;
430
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444

  if (sc->sc_params) {
    for (i = 0; sc->sc_params[i]; i++) {
      if (i > 0) MSG_CHAR_E(b, end, ';');
      MSG_STRING_E(b, end, sc->sc_params[i]);
    }
  }

  MSG_TERM_E(b, end);

  return b - b0;
}

/** Calculate extra storage used by Set-Cookie header field */
445
isize_t http_set_cookie_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
446 447 448
{
  http_set_cookie_t const *sc = (http_set_cookie_t *)h;

449
  MSG_PARAMS_SIZE(offset, sc->sc_params);
Pekka Pessi's avatar
Pekka Pessi committed
450

451
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
452 453 454 455
}

/** Duplicate a Set-Cookie header field */
char *http_set_cookie_dup_one(msg_header_t *dst, msg_header_t const *src,
456
			      char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
457 458 459 460 461 462 463 464
{
  http_set_cookie_t *sc = (http_set_cookie_t *)dst;
  http_set_cookie_t const *o = (http_set_cookie_t const *)src;
  char *end = b + xtra;

  b = msg_params_dup(&sc->sc_params, o->sc_params, b, xtra);
  http_set_cookie_update(sc);

465
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
466 467 468 469 470 471

  return b;
}

msg_hclass_t http_set_cookie_class[] =
HTTP_HEADER_CLASS(set_cookie, "Set-Cookie", sc_params, append, set_cookie);