2021-05-31 08:06:09 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "http_utils.h"
|
|
|
|
|
|
|
|
#ifndef mem_check
|
|
|
|
#define mem_check(x) assert(x)
|
|
|
|
#endif
|
|
|
|
|
2020-11-16 23:48:35 -05:00
|
|
|
char *http_utils_join_string(const char *first_str, size_t len_first, const char *second_str, size_t len_second)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2020-11-16 23:48:35 -05:00
|
|
|
size_t first_str_len = len_first > 0 ? len_first : strlen(first_str);
|
|
|
|
size_t second_str_len = len_second > 0 ? len_second : strlen(second_str);
|
2017-11-13 22:16:20 -05:00
|
|
|
char *ret = NULL;
|
|
|
|
if (first_str_len + second_str_len > 0) {
|
|
|
|
ret = calloc(1, first_str_len + second_str_len + 1);
|
|
|
|
mem_check(ret);
|
|
|
|
memcpy(ret, first_str, first_str_len);
|
|
|
|
memcpy(ret + first_str_len, second_str, second_str_len);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *http_utils_assign_string(char **str, const char *new_str, int len)
|
|
|
|
{
|
|
|
|
int l = len;
|
|
|
|
if (new_str == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *old_str = *str;
|
2020-08-13 04:56:07 -04:00
|
|
|
if (l < 0) {
|
2017-11-13 22:16:20 -05:00
|
|
|
l = strlen(new_str);
|
|
|
|
}
|
|
|
|
if (old_str) {
|
|
|
|
old_str = realloc(old_str, l + 1);
|
|
|
|
mem_check(old_str);
|
|
|
|
old_str[l] = 0;
|
|
|
|
} else {
|
|
|
|
old_str = calloc(1, l + 1);
|
|
|
|
mem_check(old_str);
|
|
|
|
}
|
|
|
|
memcpy(old_str, new_str, l);
|
|
|
|
*str = old_str;
|
|
|
|
return old_str;
|
|
|
|
}
|
|
|
|
|
2021-01-07 06:47:22 -05:00
|
|
|
char *http_utils_append_string(char **str, const char *new_str, int len)
|
|
|
|
{
|
2021-02-06 22:37:49 -05:00
|
|
|
int l = len;
|
|
|
|
int old_len = 0;
|
2021-01-07 06:47:22 -05:00
|
|
|
char *old_str = *str;
|
2021-02-06 22:37:49 -05:00
|
|
|
if (new_str != NULL) {
|
|
|
|
if (l < 0) {
|
|
|
|
l = strlen(new_str);
|
|
|
|
}
|
|
|
|
if (old_str) {
|
|
|
|
old_len = strlen(old_str);
|
|
|
|
old_str = realloc(old_str, old_len + l + 1);
|
|
|
|
mem_check(old_str);
|
|
|
|
old_str[old_len + l] = 0;
|
|
|
|
} else {
|
|
|
|
old_str = calloc(1, l + 1);
|
|
|
|
mem_check(old_str);
|
|
|
|
}
|
|
|
|
memcpy(old_str + old_len, new_str, l);
|
|
|
|
*str = old_str;
|
2021-01-07 06:47:22 -05:00
|
|
|
}
|
|
|
|
return old_str;
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:16:20 -05:00
|
|
|
void http_utils_trim_whitespace(char **str)
|
|
|
|
{
|
2018-07-31 12:25:11 -04:00
|
|
|
char *end, *start;
|
|
|
|
if (str == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
start = *str;
|
|
|
|
if (start == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-13 22:16:20 -05:00
|
|
|
// Trim leading space
|
|
|
|
while (isspace((unsigned char)*start)) start ++;
|
|
|
|
|
|
|
|
if (*start == 0) { // All spaces?
|
|
|
|
**str = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim trailing space
|
|
|
|
end = (char *)(start + strlen(start) - 1);
|
|
|
|
while (end > start && isspace((unsigned char)*end)) {
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write new null terminator
|
|
|
|
*(end + 1) = 0;
|
|
|
|
memmove(*str, start, strlen(start) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *http_utils_get_string_between(const char *str, const char *begin, const char *end)
|
|
|
|
{
|
2021-04-08 08:41:21 -04:00
|
|
|
char *found = strcasestr(str, begin);
|
2017-11-13 22:16:20 -05:00
|
|
|
char *ret = NULL;
|
|
|
|
if (found) {
|
|
|
|
found += strlen(begin);
|
2021-04-08 08:41:21 -04:00
|
|
|
char *found_end = strcasestr(found, end);
|
2017-11-13 22:16:20 -05:00
|
|
|
if (found_end) {
|
|
|
|
ret = calloc(1, found_end - found + 1);
|
|
|
|
mem_check(ret);
|
|
|
|
memcpy(ret, found, found_end - found);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-10-19 08:57:08 -04:00
|
|
|
char *http_utils_get_string_after(const char *str, const char *begin)
|
|
|
|
{
|
|
|
|
char *found = strcasestr(str, begin);
|
|
|
|
char *ret = NULL;
|
|
|
|
if (found) {
|
|
|
|
found += strlen(begin);
|
|
|
|
char *found_end = (char *)str + strlen(str);
|
|
|
|
if (found_end) {
|
|
|
|
ret = calloc(1, found_end - found + 1);
|
|
|
|
mem_check(ret);
|
|
|
|
memcpy(ret, found, found_end - found);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:16:20 -05:00
|
|
|
int http_utils_str_starts_with(const char *str, const char *start)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int match_str_len = strlen(str);
|
|
|
|
int start_len = strlen(start);
|
|
|
|
|
|
|
|
if (start_len > match_str_len) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < start_len; i++) {
|
2021-04-08 08:41:21 -04:00
|
|
|
if (tolower(str[i]) != tolower(start[i])) {
|
2017-11-13 22:16:20 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|