1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <assert.h>
12
13 #include "http_utils.h"
14
15 #ifndef mem_check
16 #define mem_check(x) assert(x)
17 #endif
18
http_utils_join_string(const char * first_str,size_t len_first,const char * second_str,size_t len_second)19 char *http_utils_join_string(const char *first_str, size_t len_first, const char *second_str, size_t len_second)
20 {
21 size_t first_str_len = len_first > 0 ? len_first : strlen(first_str);
22 size_t second_str_len = len_second > 0 ? len_second : strlen(second_str);
23 char *ret = NULL;
24 if (first_str_len + second_str_len > 0) {
25 ret = calloc(1, first_str_len + second_str_len + 1);
26 mem_check(ret);
27 memcpy(ret, first_str, first_str_len);
28 memcpy(ret + first_str_len, second_str, second_str_len);
29 }
30 return ret;
31 }
32
http_utils_assign_string(char ** str,const char * new_str,int len)33 char *http_utils_assign_string(char **str, const char *new_str, int len)
34 {
35 int l = len;
36 if (new_str == NULL) {
37 return NULL;
38 }
39 char *old_str = *str;
40 if (l < 0) {
41 l = strlen(new_str);
42 }
43 if (old_str) {
44 old_str = realloc(old_str, l + 1);
45 mem_check(old_str);
46 old_str[l] = 0;
47 } else {
48 old_str = calloc(1, l + 1);
49 mem_check(old_str);
50 }
51 memcpy(old_str, new_str, l);
52 *str = old_str;
53 return old_str;
54 }
55
http_utils_append_string(char ** str,const char * new_str,int len)56 char *http_utils_append_string(char **str, const char *new_str, int len)
57 {
58 int l = len;
59 int old_len = 0;
60 char *old_str = *str;
61 if (new_str != NULL) {
62 if (l < 0) {
63 l = strlen(new_str);
64 }
65 if (old_str) {
66 old_len = strlen(old_str);
67 old_str = realloc(old_str, old_len + l + 1);
68 mem_check(old_str);
69 old_str[old_len + l] = 0;
70 } else {
71 old_str = calloc(1, l + 1);
72 mem_check(old_str);
73 }
74 memcpy(old_str + old_len, new_str, l);
75 *str = old_str;
76 }
77 return old_str;
78 }
79
http_utils_trim_whitespace(char ** str)80 void http_utils_trim_whitespace(char **str)
81 {
82 char *end, *start;
83 if (str == NULL) {
84 return;
85 }
86 start = *str;
87 if (start == NULL) {
88 return;
89 }
90 // Trim leading space
91 while (isspace((unsigned char)*start)) start ++;
92
93 if (*start == 0) { // All spaces?
94 **str = 0;
95 return;
96 }
97
98 // Trim trailing space
99 end = (char *)(start + strlen(start) - 1);
100 while (end > start && isspace((unsigned char)*end)) {
101 end--;
102 }
103
104 // Write new null terminator
105 *(end + 1) = 0;
106 memmove(*str, start, strlen(start) + 1);
107 }
108
http_utils_get_string_between(const char * str,const char * begin,const char * end)109 char *http_utils_get_string_between(const char *str, const char *begin, const char *end)
110 {
111 char *found = strcasestr(str, begin);
112 char *ret = NULL;
113 if (found) {
114 found += strlen(begin);
115 char *found_end = strcasestr(found, end);
116 if (found_end) {
117 ret = calloc(1, found_end - found + 1);
118 mem_check(ret);
119 memcpy(ret, found, found_end - found);
120 return ret;
121 }
122 }
123 return NULL;
124 }
125
http_utils_str_starts_with(const char * str,const char * start)126 int http_utils_str_starts_with(const char *str, const char *start)
127 {
128 int i;
129 int match_str_len = strlen(str);
130 int start_len = strlen(start);
131
132 if (start_len > match_str_len) {
133 return -1;
134 }
135 for (i = 0; i < start_len; i++) {
136 if (tolower(str[i]) != tolower(start[i])) {
137 return 1;
138 }
139 }
140 return 0;
141 }
142