If content is true, we only return the header's contents. */
| 119 | |
| 120 | /* If content is true, we only return the header's contents. */ |
| 121 | char* get_header(const char* message, const char* name, bool content) |
| 122 | { |
| 123 | /* non reentrant. consider accepting char buffer as param */ |
| 124 | static char last_header[MAX_HEADER_LEN * 10]; |
| 125 | const char *cptr; |
| 126 | char *src, *src_copy, *dest, *start, *ptr; |
| 127 | bool first_time = true; |
| 128 | char header_with_newline[MAX_HEADER_LEN + 1]; |
| 129 | |
| 130 | const char* compact_header_with_newline; |
| 131 | |
| 132 | /* returns empty string in case of error */ |
| 133 | last_header[0] = '\0'; |
| 134 | |
| 135 | if (!message || !*message) { |
| 136 | return last_header; |
| 137 | } |
| 138 | |
| 139 | /* for safety's sake */ |
| 140 | if (!name || !strrchr(name, ':')) { |
| 141 | WARNING("Can not search for header (no colon): %s", name ? name : "(null)"); |
| 142 | return last_header; |
| 143 | } |
| 144 | |
| 145 | snprintf(header_with_newline, MAX_HEADER_LEN, "\n%s", name); |
| 146 | compact_header_with_newline = internal_compact_header_name(name); |
| 147 | |
| 148 | /* find end of SIP headers - perform search only until that */ |
| 149 | cptr = strstr(message, "\r\n\r\n"); |
| 150 | if (!cptr) { |
| 151 | src_copy = strdup(message); |
| 152 | } else if ((src_copy = (char*)malloc(cptr - message + 1))) { |
| 153 | src_copy[cptr - message] = '\0'; |
| 154 | memcpy(src_copy, message, cptr - message); |
| 155 | } |
| 156 | if (!src_copy) { |
| 157 | ERROR("Out of memory"); |
| 158 | return last_header; |
| 159 | } |
| 160 | |
| 161 | src = src_copy; |
| 162 | dest = last_header; |
| 163 | |
| 164 | while ((src = internal_match_header( |
| 165 | src, header_with_newline, compact_header_with_newline))) { |
| 166 | if (!content && first_time) { |
| 167 | // Add the name to the string; |
| 168 | dest += sprintf(dest, "%s", name); |
| 169 | first_time = false; |
| 170 | } |
| 171 | |
| 172 | if (content || !first_time) { |
| 173 | /* Just want the header's content, so skip over the header |
| 174 | * and newline */ |
| 175 | |
| 176 | /* Skip over header */ |
| 177 | while (*src != ':') { |
| 178 | src++; |
no test coverage detected