分析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)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "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)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "error in msg_headers_parse()\n"));
      osip_free (beg);
      return i;
    }
  tmp = (char *) next_header_index;

  /* 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");
      osip_free (beg);
      return OSIP_SUCCESS;                 /* no body found */
    }

  i = msg_osip_body_parse (sip, tmp, &next_header_index, length - (tmp - beg));
  osip_free (beg);
  if (i != 0)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "error in msg_osip_body_parse()\n"));
      return i;
    }

  /* 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,如需转载请自行联系原作者



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值