⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osip_message_parse.c

📁 嵌入式产品中的osip的源代码.
💻 C
📖 第 1 页 / 共 3 页
字号:
  /* unknownheader */  if (osip_message_set_header (sip, hname, hvalue) == -1)    {      OSIP_TRACE (osip_trace		  (__FILE__, __LINE__, OSIP_ERROR, NULL,		   "Could not set unknown header\n"));      return -1;    }  return 0;}static intmsg_handle_multiple_values (osip_message_t * sip, char *hname, char *hvalue){  int i;  char *ptr;			/* current location of the search */  char *comma;			/* This is the separator we are elooking for */  char *beg;			/* beg of a header */  char *end;			/* end of a header */  char *quote1;			/* first quote of a pair of quotes   */  char *quote2;			/* second quuote of a pair of quotes */  beg = hvalue;  end = NULL;  ptr = hvalue;  if (hvalue == NULL)    {      i = osip_message_set__header (sip, hname, hvalue);      if (i == -1)	return -1;      return 0;    }  comma = strchr (ptr, ',');  osip_tolower (hname);  if (comma == NULL || (strncmp (hname, "date", 4) == 0 && strlen (hname) == 4) || strncmp (hname, "organization", 12) == 0 || (strncmp (hname, "to", 2) == 0 && strlen (hname) == 2) || (strncmp (hname, "from", 4) == 0 && strlen (hname) == 4)	/* AMD: BUG fix */      || strncmp (hname, "call-id", 7) == 0 || (strncmp (hname, "cseq", 4) == 0 && strlen (hname) == 4)	/* AMD: BUG fix */      || strncmp (hname, "subject", 7) == 0 || strncmp (hname, "user-agent", 10) == 0 || strncmp (hname, "server", 6) == 0 || strncmp (hname, "www-authenticate", 16) == 0	/* AMD: BUG fix */      || strncmp (hname, "authentication-info", 19) == 0 || strncmp (hname, "proxy-authenticate", 20) == 0 || strncmp (hname, "proxy-authorization", 19) == 0 || strncmp (hname, "proxy-authentication-info", 25) == 0	/* AMD: BUG fix */      || strncmp (hname, "expires", 7) == 0      || strncmp (hname, "authorization", 13) == 0)    /* there is no multiple header! likely      */    /* to happen most of the time...            */    /* or hname is a TEXT-UTF8-TRIM and may     */    /* contain a comma. this is not a separator */    /* THIS DOES NOT WORK FOR UNKNOWN HEADER!!!! */    {      i = osip_message_set__header (sip, hname, hvalue);      if (i == -1)	return -1;      return 0;    }  quote2 = NULL;  while (comma != NULL)    {      quote1 = __osip_quote_find (ptr);      if (quote1 != NULL)	{	  quote2 = __osip_quote_find (quote1 + 1);	  if (quote2 == NULL)	    return -1;		/* quotes comes by pair */	  ptr = quote2 + 1;	}      if ((quote1 == NULL) || (quote1 > comma))	{	  end = comma;	  comma = strchr (comma + 1, ',');	  ptr = comma + 1;	}      else if ((quote1 < comma) && (quote2 < comma))	{			/* quotes are located before the comma, */	  /* continue the search for next quotes  */	  ptr = quote2 + 1;	}      else if ((quote1 < comma) && (comma < quote2))	{			/* if comma is inside the quotes... */	  /* continue with the next comma.    */	  ptr = quote2 + 1;	  comma = strchr (ptr, ',');	  if (comma == NULL)	    /* this header last at the end of the line! */	    {			/* this one does not need an allocation... */	      if (strlen (beg) < 2)		return 0;	/* empty header */	      osip_clrspace (beg);	      i = osip_message_set__header (sip, hname, beg);	      if (i == -1)		return -1;	      return 0;	    }	}      if (end != NULL)	{	  char *avalue;	  if (end - beg + 1 < 2)	    return -1;	  avalue = (char *) osip_malloc (end - beg + 1);	  osip_strncpy (avalue, beg, end - beg);	  osip_clrspace (avalue);	  /* really store the header in the sip structure */	  i = osip_message_set__header (sip, hname, avalue);	  osip_free (avalue);	  if (i == -1)	    return -1;	  beg = end + 1;	  end = NULL;	  if (comma == NULL)	    /* this header last at the end of the line! */	    {			/* this one does not need an allocation... */	      if (strlen (beg) < 2)		return 0;	/* empty header */	      osip_clrspace (beg);	      i = osip_message_set__header (sip, hname, beg);	      if (i == -1)		return -1;	      return 0;	    }	}    }  return -1;			/* if comma is NULL, we should have already return 0 */}/* set all headers */static intmsg_headers_parse (osip_message_t * sip, const char *start_of_header,		   const char **body){  const char *colon_index;	/* index of ':' */  char *hname;  char *hvalue;  const char *end_of_header;  int i;  for (;;)    {      i = __osip_find_next_crlf (start_of_header, &end_of_header);      if (i == -1)	{	  OSIP_TRACE (osip_trace		      (__FILE__, __LINE__, OSIP_ERROR, NULL,		       "End of header Not found\n"));	  return -1;		/* this is an error case!     */	}      if (end_of_header[0] == '\0')	{			/* final CRLF is missing */	  OSIP_TRACE (osip_trace		      (__FILE__, __LINE__, OSIP_ERROR, NULL,		       "SIP message does not end with CRLFCRLF\n"));	  return -1;	}      /* find the header name */      colon_index = strchr (start_of_header, ':');      if (colon_index == NULL)	{	  OSIP_TRACE (osip_trace		      (__FILE__, __LINE__, OSIP_ERROR, NULL,		       "End of header Not found\n"));	  return -1;		/* this is also an error case */	}      if (colon_index - start_of_header + 1 < 2)	return -1;      if (end_of_header <= colon_index)	{	  OSIP_TRACE (osip_trace		      (__FILE__, __LINE__, OSIP_ERROR, NULL,		       "Malformed message\n"));	  return -1;	}      hname = (char *) osip_malloc (colon_index - start_of_header + 1);      osip_strncpy (hname, start_of_header, colon_index - start_of_header);      osip_clrspace (hname);      {	const char *end;	/* END of header is (end_of_header-2) if header separation is CRLF */	/* END of header is (end_of_header-1) if header separation is CR or LF */	if ((end_of_header[-2] == '\r') || (end_of_header[-2] == '\n'))	  end = end_of_header - 2;	else	  end = end_of_header - 1;	if ((end) - colon_index < 2)	  hvalue = NULL;	/* some headers (subject) can be empty */	else	  {	    hvalue = (char *) osip_malloc ((end) - colon_index);	    osip_strncpy (hvalue, colon_index + 1, (end) - colon_index - 1);	    osip_clrspace (hvalue);	  }      }      /* hvalue MAY contains multiple value. In this case, they   */      /* are separated by commas. But, a comma may be part of a   */      /* quoted-string ("here, and there" is an example where the */      /* comma is not a separator!) */      i = msg_handle_multiple_values (sip, hname, hvalue);      osip_free (hname);      osip_free (hvalue);      if (i == -1)	{	  OSIP_TRACE (osip_trace		      (__FILE__, __LINE__, OSIP_ERROR, NULL,		       "End of header Not found\n"));	  return -1;	}      /* the list of headers MUST always end with  */      /* CRLFCRLF (also CRCR and LFLF are allowed) */      if ((end_of_header[0] == '\r') || (end_of_header[0] == '\n'))	{	  *body = end_of_header;	  return 0;		/* end of header found        */	}      /* continue on the next header */      start_of_header = end_of_header;    }/* Unreachable code OSIP_TRACE (osip_trace	      (__FILE__, __LINE__, OSIP_BUG, NULL,	       "This code cannot be reached\n")); */  return -1;}static intmsg_osip_body_parse2 (osip_message_t * sip, const char *start_of_buf,		      const char **next_body, size_t length){  const char *start_of_body;  const char *end_of_body;  char *tmp;  int i;  char *sep_boundary;  osip_generic_param_t *ct_param;  if (sip->content_type == NULL      ||sip->content_type->type==NULL      ||sip->content_type->subtype==NULL)    return 0;		/* no body is attached */    if (0!=osip_strcasecmp(sip->content_type->type, "multipart"))    {      size_t osip_body_len;            if (start_of_buf[0] == '\0')	return -1;		/* final CRLF is missing */      /* get rid of the first CRLF */      if ('\r' == start_of_buf[0])	{	  if ('\n' == start_of_buf[1])	    start_of_body = start_of_buf + 2;	  else	    start_of_body = start_of_buf + 1;	}      else if ('\n' == start_of_buf[0])	start_of_body = start_of_buf + 1;      else	return -1;	/* message does not end with CRLFCRLF, CRCR or LFLF */            /* update length (without CRLFCRLF */      length = length-(start_of_buf-start_of_body);      if (length<=2)	return -1;            if (sip->content_length != NULL)	osip_body_len = osip_atoi (sip->content_length->value);      else	{	  /* if content_length does not exist, set it. */	  char tmp[16];	  	  /* case where content-length is missing but the		 body only contains non-binary data */	  if (0==osip_strcasecmp(sip->content_type->type, "application")	      && 0==osip_strcasecmp(sip->content_type->subtype,"sdp"))	    {	      osip_body_len = strlen (start_of_body);	      sprintf (tmp, "%i", osip_body_len);	      i = osip_message_set_content_length (sip, tmp);	      if (i != 0)		return -1;	    }	  else return -1; /* Content-type may be non binary data */	}            if (length<osip_body_len)	{	  OSIP_TRACE (osip_trace		      (__FILE__, __LINE__, OSIP_ERROR, NULL,		       "Message was not receieved enterely. lenngth=%i osip_body_len=%i\n",length, osip_body_len));	  return -1;	}      end_of_body = start_of_body + osip_body_len;      tmp = osip_malloc (end_of_body - start_of_body + 2);      if (tmp == NULL)	return -1;      memcpy (tmp, start_of_body, end_of_body - start_of_body);            i = osip_message_set_body (sip, tmp);      osip_free (tmp);      if (i != 0)	return -1;      return 0;    }  /* find the boundary */  i = osip_generic_param_get_byname (sip->content_type->gen_params,				     "boundary", &ct_param);  if (i != 0)    return -1;  if (ct_param == NULL)    return -1;  if (ct_param->gvalue == NULL)    return -1;			/* No boundary but multiple headers??? */  sep_boundary = (char *) osip_malloc (strlen (ct_param->gvalue) + 3);  sprintf (sep_boundary, "--%s", ct_param->gvalue);  *next_body = NULL;  start_of_body = start_of_buf;  for (;;)    {      i =	__osip_find_next_occurence (sep_boundary, start_of_body,				    &start_of_body);      if (i == -1)	{	  osip_free (sep_boundary);	  return -1;	}      i =	__osip_find_next_occurence (sep_boundary,				    start_of_body + strlen (sep_boundary),				    &end_of_body);      if (i == -1)	{	  osip_free (sep_boundary);	  return -1;	}      /* this is the real beginning of body */      start_of_body = start_of_body + strlen (sep_boundary) + 2;      tmp = osip_malloc (end_of_body - start_of_body + 1);      memcpy (tmp, start_of_body, end_of_body - start_of_body);      tmp[end_of_body - start_of_body]='\0';      i = osip_message_set_body_mime (sip, tmp);      osip_free (tmp);      if (i == -1)	{	  osip_free (sep_boundary);	  return -1;	}      if (strncmp (end_of_body + strlen (sep_boundary), "--", 2) == 0)	{			/* end of all bodies */	  *next_body = end_of_body;	  osip_free (sep_boundary);	  return 0;	}      /* continue on the next body */      start_of_body = end_of_body;    }  /* Unreachable code */  /* osip_free (sep_boundary); */  return -1;}/* internal method to parse the body */static intmsg_osip_body_parse (osip_message_t * sip, const char *start_of_buf,		     const char **next_body){  const char *start_of_body;  const char *end_of_body;  char *tmp;  int i;  char *sep_boundary;  osip_generic_param_t *ct_param;  if (sip->content_type == NULL      ||sip->content_type->type==NULL      ||sip->content_type->subtype==NULL)    return 0;		/* no body is attached */  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -