Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "http_utils.h"
#define mem_check
http_utils_join_string(const char *, size_t, const char *, size_t)
http_utils_assign_string(char **, const char *, int)
http_utils_append_string(char **, const char *, int)
http_utils_trim_whitespace(char **)
http_utils_get_string_between(const char *, const char *, const char *)
http_utils_get_string_after(const char *, const char *)
http_utils_str_starts_with(const char *, const char *)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/esp_http_client/lib/http_utils.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <string.h> #include <ctype.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> #include "http_utils.h"6 includes #ifndef mem_check #define mem_check(x) assert(x) #endif char *http_utils_join_string(const char *first_str, size_t len_first, const char *second_str, size_t len_second) { 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); 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; if (l < 0) { 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; }{ ... } char *http_utils_append_string(char **str, const char *new_str, int len) { int l = len; int old_len = 0; char *old_str = *str; 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; }{...} return old_str; }{ ... } void http_utils_trim_whitespace(char **str) { char *end, *start; if (str == NULL) { return; }{...} start = *str; if (start == NULL) { return; }{...} // 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) { char *found = strcasestr(str, begin); char *ret = NULL; if (found) { found += strlen(begin); char *found_end = strcasestr(found, end); if (found_end) { ret = calloc(1, found_end - found + 1); mem_check(ret); memcpy(ret, found, found_end - found); return ret; }{...} }{...} return NULL; }{ ... } 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; }{ ... } 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++) { if (tolower(str[i]) != tolower(start[i])) { return 1; }{...} }{...} return 0; }{ ... }
Details