📄 osip_message_parse.c
字号:
if (i == -2) {} else if (i != 0) { /* error case??? no end of mesage found */ OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Final CRLF is missing\n")); return i; } if ('\0' == end_of_line[0]) { /* error case??? no end of message found */ OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Final CRLF is missing\n")); return OSIP_SYNTAXERROR; } else if ('\r' == end_of_line[0]) { if ('\n' == end_of_line[1]) end_of_line++; *end_of_part = end_of_line + 1; return OSIP_SUCCESS; } else if ('\n' == end_of_line[0]) { *end_of_part = end_of_line + 1; return OSIP_SUCCESS; } start_of_line = end_of_line; }}static intosip_message_set__header (osip_message_t * sip, const char *hname, const char *hvalue){ int my_index; if (hname == NULL) return OSIP_SYNTAXERROR; /* some headers are analysed completely */ /* this method is used for selective parsing */ my_index = __osip_message_is_known_header (hname); if (my_index >= 0) /* ok */ { int ret; ret = __osip_message_call_method (my_index, sip, hvalue); if (ret != 0) return ret; return OSIP_SUCCESS; } /* unknownheader */ if (osip_message_set_header (sip, hname, hvalue) != 0) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "Could not set unknown header\n")); return OSIP_SUCCESS; } return OSIP_SUCCESS;}intosip_message_set_multiple_header (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 */ size_t hname_len; /* Find header based upon lowercase comparison */ osip_tolower (hname); if (hvalue == NULL) { i = osip_message_set__header (sip, hname, hvalue); if (i != 0) return i; return OSIP_SUCCESS; } ptr = hvalue; comma = strchr (ptr, ','); hname_len = strlen (hname); if (comma == NULL || (hname_len == 4 && strncmp (hname, "date", 4) == 0) || (hname_len == 2 && strncmp (hname, "to", 2) == 0) || (hname_len == 4 && strncmp (hname, "from", 4) == 0) || (hname_len == 7 && strncmp (hname, "call-id", 7) == 0) || (hname_len == 4 && strncmp (hname, "cseq", 4) == 0) || (hname_len == 7 && strncmp (hname, "subject", 7) == 0) || (hname_len == 7 && strncmp (hname, "expires", 7) == 0) || (hname_len == 6 && strncmp (hname, "server", 6) == 0) || (hname_len == 10 && strncmp (hname, "user-agent", 10) == 0) || (hname_len == 16 && strncmp (hname, "www-authenticate", 16) == 0) || (hname_len == 19 && strncmp (hname, "authentication-info", 19) == 0) || (hname_len == 18 && strncmp (hname, "proxy-authenticate", 18) == 0) || (hname_len == 19 && strncmp (hname, "proxy-authorization", 19) == 0) || (hname_len == 25 && strncmp (hname, "proxy-authentication-info", 25) == 0) || (hname_len == 12 && strncmp (hname, "organization", 12) == 0) || (hname_len == 13 && 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 != 0) return i; return OSIP_SUCCESS; } beg = hvalue; end = NULL; quote2 = NULL; while (comma != NULL) { quote1 = __osip_quote_find (ptr); if (quote1 != NULL) { quote2 = __osip_quote_find (quote1 + 1); if (quote2 == NULL) return OSIP_SYNTAXERROR; /* quotes comes by pair */ ptr = quote2 + 1; } if ((quote1 == NULL) || (quote1 > comma)) { /* We must search for the next comma which is not within quotes! */ end = comma; if (quote1 != NULL && quote1 > comma) { /* comma may be within the quotes */ /* ,<sip:usera@host.example.com>;methods=\"INVITE,BYE,OPTIONS,ACK,CANCEL\",<sip:userb@host.blah.com> */ /* we want the next comma after the quotes */ char *tmp_comma; char *tmp_quote1; char *tmp_quote2; tmp_quote1 = quote1; tmp_quote2 = quote2; tmp_comma = strchr (comma + 1, ','); while (1) { if (tmp_comma < tmp_quote1) break; /* ok (before to quotes) */ if (tmp_comma < tmp_quote2) { tmp_comma = strchr (tmp_quote2 + 1, ','); } tmp_quote1 = __osip_quote_find (tmp_quote2 + 1); if (tmp_quote1 == NULL) break; tmp_quote2 = __osip_quote_find (tmp_quote1 + 1); if (tmp_quote2 == NULL) break; /* probably a malformed message? */ } comma = tmp_comma; /* this one is not enclosed within quotes */ } else comma = strchr (comma + 1, ','); if (comma != NULL) 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 0 if (strlen (beg) < 2) return OSIP_SUCCESS; /* empty header */#else if (beg[0] == '\0' || beg[1] == '\0') return OSIP_SUCCESS; /* empty header */#endif osip_clrspace (beg); i = osip_message_set__header (sip, hname, beg); if (i != 0) return i; return OSIP_SUCCESS; } } if (end != NULL) { char *avalue; if (end - beg + 1 < 2) return OSIP_SYNTAXERROR; avalue = (char *) osip_malloc (end - beg + 1); if (avalue==NULL) return OSIP_NOMEM; osip_clrncpy (avalue, beg, end - beg); /* really store the header in the sip structure */ i = osip_message_set__header (sip, hname, avalue); osip_free (avalue); if (i != 0) return i; 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 0 if (strlen (beg) < 2) return OSIP_SUCCESS; /* empty header */#else if (beg[0] == '\0' || beg[1] == '\0') return OSIP_SUCCESS; /* empty header */#endif osip_clrspace (beg); i = osip_message_set__header (sip, hname, beg); if (i != 0) return i; return OSIP_SUCCESS; } } } return OSIP_SYNTAXERROR; /* 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 (;;) { if (start_of_header[0] == '\0') { /* final CRLF is missing */ OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL, "SIP message does not end with CRLFCRLF\n")); return OSIP_SUCCESS; } i = __osip_find_next_crlf (start_of_header, &end_of_header); if (i == -2) {} else if (i != 0) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "End of header Not found\n")); return i; /* this is an error case! */ } /* the list of headers MUST always end with */ /* CRLFCRLF (also CRCR and LFLF are allowed) */ if ((start_of_header[0] == '\r') || (start_of_header[0] == '\n')) { *body = start_of_header; return OSIP_SUCCESS; /* end of header found */ } /* 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 OSIP_SYNTAXERROR; /* this is also an error case */ } if (colon_index - start_of_header + 1 < 2) return OSIP_SYNTAXERROR; if (end_of_header <= colon_index) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Malformed message\n")); return OSIP_SYNTAXERROR; } hname = (char *) osip_malloc (colon_index - start_of_header + 1); if (hname==NULL) return OSIP_NOMEM; osip_clrncpy (hname, start_of_header, colon_index - start_of_header); { 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 + 1); if (hvalue==NULL) { osip_free (hname); return OSIP_NOMEM; } osip_clrncpy (hvalue, colon_index + 1, (end) - colon_index - 1); } } /* 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 = osip_message_set_multiple_header (sip, hname, hvalue); osip_free (hname); if (hvalue != NULL) osip_free (hvalue); if (i != 0) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "End of header Not found\n")); return OSIP_SYNTAXERROR; } /* 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 OSIP_SYNTAXERROR;}static intmsg_osip_body_parse (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; const char *end_of_buf; char *tmp; int i; char *sep_boundary; size_t len_sep_boundary; osip_generic_param_t *ct_param; if (sip->content_type == NULL || sip->content_type->type == NULL || sip->content_type->subtype == NULL) return OSIP_SUCCESS; /* 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 OSIP_SYNTAXERROR; /* 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 OSIP_SYNTAXERROR; /* message does not end with CRLFCRLF, CRCR or LFLF */ /* update length (without CRLFCRLF */ length = length - (start_of_body - start_of_buf); /* fixed 24 08 2004 */ if (length <= 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -