http_parser.c 14 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
 * 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
 *
 */

/**@CFILE http_parser.c
 *
 * HTTP parser.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Thu Oct  5 14:01:24 2000 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>

#include <stdarg.h>
44
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
45

46
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
47

48 49 50 51 52
#include "sofia-sip/http_parser.h"
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/http_header.h>
#include <sofia-sip/http_status.h>
#include <sofia-sip/msg_mclass.h>
Pekka Pessi's avatar
Pekka Pessi committed
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 102 103 104 105 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 226 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 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 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 338 339 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 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 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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 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

/** HTTP version 1.1. */
char const http_version_1_1[] = "HTTP/1.1";
/** HTTP version 1.0. */
char const http_version_1_0[] = "HTTP/1.0";
/** HTTP version 0.9 is an empty string. */
char const http_version_0_9[] = "";

msg_mclass_t *http_default_mclass(void)
{
  extern msg_mclass_t http_mclass[];

  return http_mclass;
}

static int 
http_extract_chunk(msg_t *msg, http_t *http, char b[], int bsiz, int eos);

/** Calculate length of line ending (0, 1 or 2) */
#define CRLF_TEST(s) \
  (((s)[0]) == '\r' ? (((s)[1]) == '\n') + 1 : ((s)[0])=='\n')

/** Extract the HTTP message body, including separator line. 
 *
 * @retval -1    error
 * @retval 0     cannot proceed
 * @retval other number of bytes extracted
 */
int http_extract_body(msg_t *msg, http_t *http, char b[], int bsiz, int eos)
{
  int m = 0;
  unsigned body_len;

  int flags = http->http_flags;

  if (eos && bsiz == 0) {
    msg_mark_as_complete(msg, MSG_FLG_COMPLETE);
    return 0;
  }

  if (flags & MSG_FLG_TRAILERS) {
    /* The empty line after trailers */
    if (!eos && (bsiz == 0 || (bsiz == 1 && b[0] == '\r')))
      return 0;

    m = CRLF_TEST(b); 

    assert(m > 0 || eos); /* We should be looking at an empty line */

    /* We have completed trailers */
    msg_mark_as_complete(msg, MSG_FLG_COMPLETE);

    return m;
  }

  if (flags & MSG_FLG_CHUNKS)
    return http_extract_chunk(msg, http, b, bsiz, eos);

  if (!(flags & MSG_FLG_BODY)) {
    /* We are looking at a potential empty line */
    m = msg_extract_separator(msg, http, b, bsiz, eos);

    if (m == 0)			/* Not yet */
      return 0;

    http->http_flags |= MSG_FLG_BODY;
    b += m, bsiz -= m;
  }

  /* body_len is determined by rules in RFC2616 sections 4.3 and 4.4 */

  /* 1XX, 204, 304 do not have message-body, ever */
  if (http->http_status) {
    int status = http->http_status->st_status;

    if (status < 200 || status == 204 || status == 304)
      flags |= HTTP_FLG_NO_BODY;
  }

  if (flags & HTTP_FLG_NO_BODY) {
    msg_mark_as_complete(msg, MSG_FLG_COMPLETE);
    return m;
  }

  if (http->http_transfer_encoding) {
    if (/* NOTE - there is really no Transfer-Encoding: identity in RFC 2616
	 * but it was used in drafts...
	 */
	http->http_transfer_encoding->k_items &&
	http->http_transfer_encoding->k_items[0] &&
	strcasecmp(http->http_transfer_encoding->k_items[0], 
		   "identity") != 0) {
      http->http_flags |= MSG_FLG_CHUNKS;

      if (http->http_flags & MSG_FLG_STREAMING)
	msg_set_streaming(msg, msg_start_streaming);

      if (m)
	return m;

      return http_extract_chunk(msg, http, b, bsiz, eos);
    }
  }


  if (http->http_content_length)
    body_len = http->http_content_length->l_length;
  /* We cannot parse multipart/byteranges ... */
  else if (http->http_content_type && http->http_content_type->c_type &&
	   strcasecmp(http->http_content_type->c_type, "multipart/byteranges") 
	   == 0)
    return -1;
  else if (MSG_IS_MAILBOX(flags)) /* message fragments */
    body_len = 0;
  else if (http->http_request) 
    body_len = 0;
  else if (eos)
    body_len = bsiz;
  else 
    return 0;			/* XXX */

  if (body_len == 0) {
    msg_mark_as_complete(msg, MSG_FLG_COMPLETE);
    return m;
  }

  if (http->http_flags & MSG_FLG_STREAMING)
    msg_set_streaming(msg, msg_start_streaming);

  if (m)
    return m;

  m = msg_extract_payload(msg, http, NULL, body_len, b, bsiz, eos);
  if (m == -1)
    return -1;

  /* We have now all message fragments in place */
  http->http_flags |= MSG_FLG_FRAGS;
  if (bsiz >= body_len) {
    msg_mark_as_complete(msg, MSG_FLG_COMPLETE);
  }

  return m;
}

/** Extract a chunk. 
 *
 * @retval -1    error
 * @retval 0     cannot proceed
 * @retval other number of bytes extracted
 */
int http_extract_chunk(msg_t *msg, http_t *http, char b[], int bsiz, int eos)
{
  int crlf, n;
  unsigned chunk_len;
  char *b0 = b, *s;
  int bsiz0 = bsiz;
  union {
    msg_header_t *header;  
    msg_payload_t *chunk;
  } h = { NULL };

  if (bsiz == 0)
    return 0;

  /* We should be looking at an empty line followed by the chunk header */
  while ((crlf = CRLF_TEST(b))) {
    if (bsiz == 1 && crlf == 1 && b[0] == '\r' && !eos)
      return 0;

    if (crlf == bsiz) {
      if (eos) {
	msg_mark_as_complete(msg, MSG_FLG_COMPLETE | MSG_FLG_FRAGS);
	return (b - b0) + crlf;
      }
      else
	return 0;
    }
    assert(crlf < bsiz);

    /* Skip crlf */
    b += crlf; bsiz -= crlf;  
  }

  /* Now, looking at the chunk header */
  n = strcspn(b, CRLF); 
  if (!eos && n == bsiz) 
    return 0;
  crlf = CRLF_TEST(b + n);
  
  if (n == 0) {
    if (crlf == bsiz && eos) {
      msg_mark_as_complete(msg, MSG_FLG_COMPLETE | MSG_FLG_FRAGS);
      return crlf;
    }
    else
      return -1;		/* XXX - should we be more liberal? */
  }

  if (!eos && n + crlf == bsiz && (crlf == 0 || (crlf == 1 && b[n] == '\r')))
    return 0;

  chunk_len = strtoul(b, &s, 16);
  if (s == b)
    return -1;
  skip_ws(&s);
  if (s != b + n && s[0] != ';') /* Extra stuff that is not parameter */
    return -1;

  if (chunk_len == 0) {  /* We found last-chunk */
    b += n + crlf, bsiz -= n + crlf;

    crlf = bsiz > 0 ? CRLF_TEST(b) : 0;

    if ((eos && bsiz == 0) || crlf == 2 || 
	(crlf == 1 && (bsiz > 1 || b[0] == '\n'))) {
      /* Shortcut - We got empty trailers */
      b += crlf;
      msg_mark_as_complete(msg, MSG_FLG_COMPLETE | MSG_FLG_FRAGS);
    } else {
      /* We have to parse trailers */
      http->http_flags |= MSG_FLG_TRAILERS;
    }

    return b - b0;
  }

  //  if (bsiz == n + crlf)
  // return 0;

  b += n + crlf, bsiz -= n + crlf;

  /* Extract chunk */
  bsiz = msg_extract_payload(msg, http, 
			     &h.header, chunk_len + (b - b0),
			     b0, bsiz0, eos);

  if (bsiz != -1 && h.header) {
    assert(h.chunk->pl_data);
    h.chunk->pl_data += (b - b0);
    h.chunk->pl_len -= (b - b0);
  }
  
  return bsiz;
}

/** Parse HTTP version.
 *
 *  The function http_version_d() parses a HTTP method.
 *
 * @retval 0 when successful,
 * @retval -1 upon an error.
 */
int http_version_d(char **ss, char const **ver)
{
  char *s = *ss;
  char const *result;
  int const version_size = sizeof(http_version_1_1) - 1;

  if (strncasecmp(s, http_version_1_1, version_size) == 0 && 
      !IS_TOKEN(s[version_size])) {
    result = http_version_1_1;
    s += version_size;
  }
  else if (strncasecmp(s, http_version_1_0, version_size) == 0 && 
	   !IS_TOKEN(s[version_size])) {
    result = http_version_1_0;
    s += version_size;
  }
  else if (s[0] == '\0') {
    result = http_version_0_9;
  } else {
    /* Version consists of one or two tokens, separated by / */
    int l1 = 0, l2 = 0, n;

    result = s;

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

    if (l1 == 0)
      return -1;

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

      /* Compare again with compacted version */ 
      if (strcasecmp(s, http_version_1_1) == 0)
	result = http_version_1_1;
      else if (strcasecmp(s, http_version_1_0) == 0)
	result = http_version_1_0;
    }

    s += n;
  }

  while (IS_LWS(*s)) *s++ = '\0';
  
  *ss = s;

  if (ver) 
    *ver = result;

  return 0;
}

/** Calculate extra space required by version string */
int http_version_xtra(char const *version)
{
  if (version == http_version_1_1)
    return 0;
  else if (version == http_version_1_0)
    return 0;
  else
    return MSG_STRING_SIZE(version);
}

/** Duplicate a transport string */
void http_version_dup(char **pp, char const **dd, char const *s)
{
  if (s == http_version_1_1)
    *dd = s;
  else if (s == http_version_1_0)
    *dd = s;
  else
    MSG_STRING_DUP(*pp, *dd, s);
}

/** Well-known HTTP method names. */
static char const * const methods[] = {
  "<UNKNOWN>",
  http_method_name_get,
  http_method_name_post,
  http_method_name_head,
  http_method_name_options,
  http_method_name_put,
  http_method_name_delete,
  http_method_name_trace,
  http_method_name_connect,
  NULL,
  /* If you add something here, add also them to http_method_d! */
};

char const http_method_name_get[]     = "GET";
char const http_method_name_post[]    = "POST";
char const http_method_name_head[]    = "HEAD";
char const http_method_name_options[] = "OPTIONS";
char const http_method_name_put[]     = "PUT";
char const http_method_name_delete[]  = "DELETE";
char const http_method_name_trace[]   = "TRACE";
char const http_method_name_connect[] = "CONNECT";

char const *http_method_name(http_method_t method, char const *name)
{
  if (method > 0 && method < sizeof(methods)/sizeof(methods[0]))
    return methods[method];
  else if (method == 0)
    return name;
  else
    return NULL;
}

/**Parse a HTTP method name.
 *
 * The function @c http_method_d() parses a HTTP method, and returns a code
 * corresponding to the method.  It stores the address of the first non-LWS
 * character after method name in @c *ss.
 *
 * @param ss    pointer to pointer to string to be parsed
 * @param nname pointer to value-result parameter formethod name
 *
 * @note
 * If there is no whitespace after method name, the value in @a *nname
 * may not be NUL-terminated.  The calling function @b must NUL terminate
 * the value by setting the @a **ss to NUL after first examining its value.
 *
 * @return The function @c http_method_d returns the method code if method
 * was identified, 0 (@c http_method_unknown) if method is not known, or @c -1
 * (@c http_method_invalid) if an error occurred.
 *
 * If the value-result argument @a nname is not @c NULL, http_method_d()
 * stores a pointer to the method name to it.
 */
http_method_t http_method_d(char **ss, char const **nname)
{
  char *s = *ss, c = *s;
  char const *name;
  int code = http_method_unknown;
  int n = 0;

#define MATCH(s, m) (strncasecmp(s, m, n = sizeof(m) - 1) == 0)

  if (c >= 'a' && c <= 'z') 
    c += 'A' - 'a';

  switch (c) {
  case 'C': if (MATCH(s, "CONNECT")) code = http_method_connect; break;
  case 'D': if (MATCH(s, "DELETE")) code = http_method_delete; break;
  case 'G': if (MATCH(s, "GET")) code = http_method_get; break;
  case 'H': if (MATCH(s, "HEAD")) code = http_method_head; break;
  case 'O': if (MATCH(s, "OPTIONS")) code = http_method_options; break;
  case 'P': if (MATCH(s, "POST")) code = http_method_post; 
            else
            if (MATCH(s, "PUT")) code = http_method_put; break;
  case 'T': if (MATCH(s, "TRACE")) code = http_method_trace; break;
  }

#undef MATCH

  if (!code || IS_NON_WS(s[n])) {
    /* Unknown method */
    code = http_method_unknown;
    name = s;
    for (n = 0; IS_UNRESERVED(s[n]); n++)
      ;
    if (s[n]) {
      if (!IS_LWS(s[n]))
	return http_method_invalid;
      if (nname)
	s[n++] = '\0';
    }
  }
  else {
    name = methods[code];
  }

  while (IS_LWS(s[n]))
    n++;

  *ss = (s + n);
  if (nname) *nname = name;

  return code;
}

/** Get method enum corresponding to method name */
http_method_t http_method_code(char const *name)
{
  /* Note that http_method_d() does not change string if nname is NULL */
  return http_method_d((char **)&name, NULL);
}

/**Parse HTTP query string.
 *
 * The function http_query_parse() searches for the given keys in HTTP @a
 * query. For each key, a query element (in the form name=value) is searched
 * from the query string. If a query element has a beginning matching with
 * the key, a copy of the rest of the element is returned in corresponding
 * return_value argument. 
 *
 * @note The @a query string will be modified.
 *
 * @return
 * The function http_query_parse() returns number keys that matched within
 * the @a query string.
 */
int http_query_parse(char *query,
		     /* char const *key, char **return_value, */
		     ...)
{
  va_list ap;
  char *q, *q_next;
  char *name, *value, **return_value;
  char const *key;
  size_t namelen, valuelen, keylen, N;
  int has_value;

  if (!query)
    return -1;

  for (q = query, N = 0; *q; q = q_next) {
    namelen = strcspn(q, "=&");
    valuelen = namelen + strcspn(q + namelen, "&");

    q_next = q + valuelen;
    if (*q_next) 
      *q_next++ = '\0';

    value = q + namelen; 
    has_value = (*value) != '\0'; /* is the part in form of name=value? */
    if (has_value)
      *value++ = '\0';

    name = url_unescape(q, q); 

    if (has_value) {
      namelen = strlen(name);
      name[namelen] = '=';
      url_unescape(name + namelen + 1, value);
    }

    va_start(ap, query);

    while ((key = va_arg(ap, char const *))) {
      return_value = va_arg(ap, char **);
      keylen = strlen(key);

      if (strncmp(key, name, keylen) == 0) {
	*return_value = name + keylen;
	N++;
      }
    }

    va_end(ap);
  }

  return N;
}