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

📄 osip_message_to_str.c

📁 libosip2-3.0.3最新版本
💻 C
📖 第 1 页 / 共 2 页
字号:
  char *content_length_to_modify = NULL;  char *message;  char *next;  char *tmp;  int pos;  int i;  char *boundary = NULL;  malloc_size = SIP_MESSAGE_MAX_LENGTH;  *dest = NULL;  if ((sip == NULL))    return -1;  {    if (1 == osip_message_get__property (sip))      {                         /* message is already available in "message" */        *dest = osip_malloc (sip->message_length + 1);        if (*dest == NULL)          return -1;        memcpy (*dest, sip->message, sip->message_length);        (*dest)[sip->message_length] = '\0';        if (message_length != NULL)          *message_length = sip->message_length;        return 0;    } else      {        /* message should be rebuilt: delete the old one if exists. */        osip_free (sip->message);        sip->message = NULL;      }  }  message = (char *) osip_malloc (SIP_MESSAGE_MAX_LENGTH);      /* ???? message could be > 4000  */  if (message == NULL)    return -1;  *dest = message;  /* add the first line of message */  i = __osip_message_startline_to_str (sip, &tmp);  if (i == -1)    {      if (!sipfrag)        {          osip_free (*dest);          *dest = NULL;          return -1;        }      /* A start-line isn't required for message/sipfrag parts. */  } else    {      message = osip_str_append (message, tmp);      osip_free (tmp);      message = osip_strn_append (message, CRLF, 2);    }  {    struct to_str_table {      char header_name[30];      int  header_length;      osip_list_t *header_list;      void *header_data;      int (*to_str) (void *, char **);    }#ifndef MINISIZE    table[25] = #else    table[15] = #endif	{	  { "Via: ", 5, NULL, NULL,	    (int (*)(void *, char **)) &osip_via_to_str },	  { "Record-Route: ", 14, NULL, NULL,	    (int (*)(void *, char **)) &osip_record_route_to_str },	  { "Route: ", 7, NULL, NULL,	    (int (*)(void *, char **)) &osip_route_to_str },	  { "From: ", 6, NULL, NULL,	    (int (*)(void *, char **)) &osip_from_to_str },	  { "To: ", 4, NULL, NULL,	    (int (*)(void *, char **)) &osip_to_to_str },	  { "Call-ID: ", 9, NULL, NULL,	    (int (*)(void *, char **)) &osip_call_id_to_str },	  { "CSeq: ", 6, NULL, NULL,	    (int (*)(void *, char **)) &osip_cseq_to_str },	  { "Contact: ", 9, NULL, NULL,	    (int (*)(void *, char **)) &osip_contact_to_str },	  { "Authorization: ", 15, NULL, NULL,	    (int (*)(void *, char **)) &osip_authorization_to_str },	  { "WWW-Authenticate: ", 18, NULL, NULL,	    (int (*)(void *, char **)) &osip_www_authenticate_to_str },	  { "Proxy-Authenticate: ", 20, NULL, NULL,	    (int (*)(void *, char **)) &osip_www_authenticate_to_str },	  { "Proxy-Authorization: ", 21, NULL, NULL,	    (int (*)(void *, char **)) &osip_authorization_to_str },	  { "Content-Type: ", 14, NULL, NULL,	    (int (*)(void *, char **)) &osip_content_type_to_str },	  { "Mime-Version: ", 14, NULL, NULL,	    (int (*)(void *, char **)) &osip_content_length_to_str },#ifndef MINISIZE	  { "Allow: ", 7, NULL, NULL,	    (int (*)(void *, char **)) &osip_allow_to_str },	  { "Content-Encoding: ", 18, NULL, NULL,	    (int (*)(void *, char **)) &osip_content_encoding_to_str },	  { "Call-Info: ", 11, NULL, NULL,	    (int (*)(void *, char **)) &osip_call_info_to_str },	  { "Alert-Info: ", 12, NULL, NULL,	    (int (*)(void *, char **)) &osip_call_info_to_str },	  { "Error-Info: ", 12, NULL, NULL,	    (int (*)(void *, char **)) &osip_call_info_to_str },	  { "Accept: ", 8, NULL, NULL,	    (int (*)(void *, char **)) &osip_accept_to_str },	  { "Accept-Encoding: ", 17, NULL, NULL,	    (int (*)(void *, char **)) &osip_accept_encoding_to_str },	  { "Accept-Language: ", 17, NULL, NULL,	    (int (*)(void *, char **)) &osip_accept_language_to_str },	  { "Authentication-Info: ", 21, NULL, NULL,	    (int (*)(void *, char **)) &osip_authentication_info_to_str },	  { "Proxy-Authentication-Info: ", 27, NULL, NULL,	    (int (*)(void *, char **)) &osip_authentication_info_to_str },#endif	  { { '\0' }, 0, NULL, NULL, NULL }	};    table[0].header_list = &sip->vias;    table[1].header_list = &sip->record_routes;    table[2].header_list = &sip->routes;    table[3].header_data = sip->from;    table[4].header_data = sip->to;    table[5].header_data = sip->call_id;    table[6].header_data = sip->cseq;    table[7].header_list = &sip->contacts;    table[8].header_list = &sip->authorizations;    table[9].header_list = &sip->www_authenticates;    table[10].header_list = &sip->proxy_authenticates;    table[11].header_list = &sip->proxy_authorizations;    table[12].header_data = sip->content_type;    table[13].header_data = sip->mime_version;#ifndef MINISIZE    table[14].header_list = &sip->allows;    table[15].header_list = &sip->content_encodings;    table[16].header_list = &sip->call_infos;    table[17].header_list = &sip->alert_infos;    table[18].header_list = &sip->error_infos;    table[19].header_list = &sip->accepts;    table[20].header_list = &sip->accept_encodings;    table[21].header_list = &sip->accept_languages;    table[22].header_list = &sip->authentication_infos;    table[23].header_list = &sip->proxy_authentication_infos;#endif    pos = 0;    while (table[pos].header_name[0]!='\0')      {	if (table[13].header_list==NULL)	  i = strcat_simple_header (dest, &malloc_size, &message,				    table[pos].header_data,				    table[pos].header_name,				    table[pos].header_length,				    ((int (*)(void *, char **))				     table[pos].to_str), &next);	i =	  strcat_headers_one_per_line (dest, &malloc_size, &message,				       table[pos].header_list,				       table[pos].header_name,				       table[pos].header_length,				       ((int (*)(void *, char **))					table[pos].to_str), &next);	if (i != 0)	  {	    osip_free (*dest);	    *dest = NULL;	    return -1;	  }	message = next;		pos++;      }  }  pos = 0;  while (!osip_list_eol (&sip->headers, pos))    {      osip_header_t *header;      size_t header_len = 0;      header = (osip_header_t *) osip_list_get (&sip->headers, pos);      i = osip_header_to_str (header, &tmp);      if (i == -1)        {          osip_free (*dest);          *dest = NULL;          return -1;        }      header_len = strlen (tmp);      if (_osip_message_realloc (&message, dest, header_len + 3, &malloc_size) < 0)	{	  osip_free (tmp);          *dest = NULL;	  return -1;	}      message = osip_str_append (message, tmp);      osip_free (tmp);      message = osip_strn_append (message, CRLF, 2);      pos++;    }  /* we have to create the body before adding the contentlength */  /* add enough lenght for "Content-Length: " */  if (_osip_message_realloc (&message, dest, 16, &malloc_size) < 0)    return -1;  if (sipfrag && osip_list_eol (&sip->bodies, 0))    {      /* end of headers */      osip_strncpy (message, CRLF, 2);      message = message + 2;      /* same remark as at the beginning of the method */      sip->message_property = 1;      sip->message = osip_strdup (*dest);      sip->message_length = message - *dest;      if (message_length != NULL)        *message_length = message - *dest;      return 0;                 /* it's all done */    }  osip_strncpy (message, "Content-Length: ", 16);  message = message + 16;  /* SIPit Day1     ALWAYS RECALCULATE?     if (sip->contentlength!=NULL)     {     i = osip_content_length_to_str(sip->contentlength, &tmp);     if (i==-1) {     osip_free(*dest);     *dest = NULL;     return -1;     }     osip_strncpy(message,tmp,strlen(tmp));     osip_free(tmp);     }     else     { */  if (osip_list_eol (&sip->bodies, 0))   /* no body */    message = osip_strn_append (message, "0", 1);  else    {      /* BUG: p130 (rfc2543bis-04)         "No SP after last token or quoted string"         In fact, if extra spaces exist: the stack can't be used         to make user-agent that wants to make authentication...         This should be changed...       */      content_length_to_modify = message;      message = osip_str_append (message, "     ");    }  /*  } */  message = osip_strn_append (message, CRLF, 2);  /* end of headers */  message = osip_strn_append (message, CRLF, 2);  start_of_bodies = message;  total_length = start_of_bodies - *dest;  if (osip_list_eol (&sip->bodies, 0))    {      /* same remark as at the beginning of the method */      sip->message_property = 1;      sip->message = osip_strdup (*dest);      sip->message_length = total_length;      if (message_length != NULL)        *message_length = total_length;      return 0;                 /* it's all done */    }  if (sip->mime_version != NULL && sip->content_type      && sip->content_type->type      && !osip_strcasecmp (sip->content_type->type, "multipart"))    {      osip_generic_param_t *ct_param = NULL;      /* find the boundary */      i = osip_generic_param_get_byname (&sip->content_type->gen_params,                                         "boundary", &ct_param);      if ((i >= 0) && ct_param && ct_param->gvalue)        {          size_t len = strlen (ct_param->gvalue);          if (len > MIME_MAX_BOUNDARY_LEN)            {              osip_free (*dest);              *dest = NULL;              return -1;            }          boundary = osip_malloc (len + 5);          osip_strncpy (boundary, CRLF, 2);          osip_strncpy (boundary + 2, "--", 2);          if (ct_param->gvalue[0] == '"' && ct_param->gvalue[len - 1] == '"')            osip_strncpy (boundary + 4, ct_param->gvalue + 1, len - 2);          else            osip_strncpy (boundary + 4, ct_param->gvalue, len);        }    }  pos = 0;  while (!osip_list_eol (&sip->bodies, pos))    {      osip_body_t *body;      size_t body_length;      body = (osip_body_t *) osip_list_get (&sip->bodies, pos);      if (boundary)        {          /* Needs at most 77 bytes,             last realloc allocate at least 100 bytes extra */          message = osip_str_append (message, boundary);          message = osip_strn_append (message, CRLF, 2);        }      i = osip_body_to_str (body, &tmp, &body_length);      if (i != 0)        {          osip_free (*dest);          *dest = NULL;          if (boundary)            osip_free (boundary);          return -1;        }      if (malloc_size < message - *dest + 100 + body_length)        {          size_t size = message - *dest;          int offset_of_body;          int offset_content_length_to_modify = 0;          offset_of_body = (int) (start_of_bodies - *dest);          if (content_length_to_modify != NULL)            offset_content_length_to_modify =              (int) (content_length_to_modify - *dest);          malloc_size = message - *dest + body_length + 100;          *dest = osip_realloc (*dest, malloc_size);          if (*dest == NULL)            {              osip_free (tmp);  /* fixed 09/Jun/2005 */              if (boundary)                osip_free (boundary);              return -1;            }          start_of_bodies = *dest + offset_of_body;          if (content_length_to_modify != NULL)            content_length_to_modify = *dest + offset_content_length_to_modify;          message = *dest + size;        }      memcpy (message, tmp, body_length);      message[body_length] = '\0';      osip_free (tmp);      message = message + body_length;      pos++;    }  if (boundary)    {      /* Needs at most 79 bytes,         last realloc allocate at least 100 bytes extra */      message = osip_str_append (message, boundary);      message = osip_strn_append (message, "--", 2);      message = osip_strn_append (message, CRLF, 2);      osip_free (boundary);      boundary = NULL;    }  if (content_length_to_modify == NULL)    {      osip_free (*dest);      *dest = NULL;      return -1;    }  /* we NOW have the length of bodies: */  {    size_t size = message - start_of_bodies;    char tmp2[15];    total_length += size;    sprintf (tmp2, "%i", size);    /* do not use osip_strncpy here! */    strncpy (content_length_to_modify + 5 - strlen (tmp2), tmp2, strlen (tmp2));  }  /* same remark as at the beginning of the method */  sip->message_property = 1;  sip->message = osip_malloc (total_length + 1);  if (sip->message != NULL)    {      memcpy (sip->message, *dest, total_length);      sip->message[total_length] = '\0';      sip->message_length = total_length;      if (message_length != NULL)        *message_length = total_length;    }  return 0;}intosip_message_to_str (osip_message_t * sip, char **dest, size_t * message_length){  return _osip_message_to_str (sip, dest, message_length, 0);}intosip_message_to_str_sipfrag (osip_message_t * sip, char **dest,                             size_t * message_length){  return _osip_message_to_str (sip, dest, message_length, 1);}

⌨️ 快捷键说明

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