天天看点

分析osip的解析报文

static int

_osip_message_parse (osip_message_t * sip, const char *buf, size_t length,

                     int sipfrag)

{

  int i;

  const char *next_header_index;

  char *tmp;

  char *beg;

  tmp = osip_malloc (length + 2);

  if (tmp == NULL)

    {

      OSIP_TRACE (osip_trace

                  (__FILE__, __LINE__, OSIP_ERROR, NULL,

                   "Could not allocate memory.\n"));

      return OSIP_NOMEM;

    }

  beg = tmp;

  memcpy (tmp, buf, length);    /* may contain binary data */

  tmp[length] = '\0';

  osip_util_replace_all_lws (tmp);

  /* parse request or status line */

  i = __osip_message_startline_parse (sip, tmp, &next_header_index);

  if (i != 0 && !sipfrag)

                   "Could not parse start line of message.\n"));

      osip_free (beg);

      return i;

  tmp = (char *) next_header_index;

  /* parse headers */

  i = msg_headers_parse (sip, tmp, &next_header_index);

  if (i != 0)

                   "error in msg_headers_parse()\n"));

  /* this is a *very* simple test... (which handle most cases...) */

  if (tmp[0] == '\0' || tmp[1] == '\0' || tmp[2] == '\0')

      /* this is mantory in the oSIP stack */

      if (sip->content_length == NULL)

        osip_message_set_content_length (sip, "0");

      return OSIP_SUCCESS;                 /* no body found */

  i = msg_osip_body_parse (sip, tmp, &next_header_index, length - (tmp - beg));

  osip_free (beg);

                   "error in msg_osip_body_parse()\n"));

  /* this is mandatory in the oSIP stack */

  if (sip->content_length == NULL)

    osip_message_set_content_length (sip, "0");

  return OSIP_SUCCESS;

}

int osip_message_parse (osip_message_t * sip, const char *buf, size_t length)

  return _osip_message_parse (sip, buf, length, 0);

     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1410792,如需转载请自行联系原作者