Select one of the symbols to view example projects that use it.
 
Outline
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include "esp_log.h"
#include "esp_check.h"
#include "http_header.h"
#include "http_utils.h"
TAG
#define HEADER_BUFFER
http_header_item
http_header
http_header_init()
http_header_destroy(http_header_handle_t)
http_header_get_item(http_header_handle_t, const char *)
http_header_get(http_header_handle_t, const char *, char **)
http_header_new_item(http_header_handle_t, const char *, const char *)
http_header_set(http_header_handle_t, const char *, const char *)
http_header_set_from_string(http_header_handle_t, const char *)
http_header_delete(http_header_handle_t, const char *)
http_header_set_format(http_header_handle_t, const char *, const char *, ...)
http_header_generate_string(http_header_handle_t, int, char *, int *)
http_header_clean(http_header_handle_t)
http_header_count(http_header_handle_t)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/esp_http_client/lib/http_header.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <stdlib.h> #include <string.h> #include <ctype.h> #include <stdio.h> #include <stdarg.h> #include <stdbool.h> #include "esp_log.h" #include "esp_check.h" #include "http_header.h" #include "http_utils.h"10 includes static const char *TAG = "HTTP_HEADER"; #define HEADER_BUFFER (1024) /** * dictionary item struct, with key-value pair *//* ... */ typedef struct http_header_item { char *key; /*!< key */ char *value; /*!< value */ STAILQ_ENTRY(http_header_item) next; /*!< Point to next entry */ }{ ... } http_header_item_t; STAILQ_HEAD(http_header, http_header_item); http_header_handle_t http_header_init(void) { http_header_handle_t header = calloc(1, sizeof(struct http_header)); ESP_RETURN_ON_FALSE(header, NULL, TAG, "Memory exhausted"); STAILQ_INIT(header); return header; }{ ... } esp_err_t http_header_destroy(http_header_handle_t header) { esp_err_t err = http_header_clean(header); free(header); return err; }{ ... } http_header_item_handle_t http_header_get_item(http_header_handle_t header, const char *key) { http_header_item_handle_t item; if (header == NULL || key == NULL) { return NULL; }{...} STAILQ_FOREACH(item, header, next) { if (strcasecmp(item->key, key) == 0) { return item; }{...} }{...} return NULL; }{ ... } esp_err_t http_header_get(http_header_handle_t header, const char *key, char **value) { http_header_item_handle_t item; item = http_header_get_item(header, key); if (item) { *value = item->value; }{...} else { *value = NULL; }{...} return ESP_OK; }{ ... } static esp_err_t http_header_new_item(http_header_handle_t header, const char *key, const char *value) { esp_err_t ret = ESP_OK; http_header_item_handle_t item; item = calloc(1, sizeof(http_header_item_t)); ESP_RETURN_ON_FALSE(item, ESP_ERR_NO_MEM, TAG, "Memory exhausted"); http_utils_assign_string(&item->key, key, -1); ESP_GOTO_ON_FALSE(item->key, ESP_ERR_NO_MEM, _header_new_item_exit, TAG, "Memory exhausted"); http_utils_trim_whitespace(&item->key); http_utils_assign_string(&item->value, value, -1); ESP_GOTO_ON_FALSE(item->value, ESP_ERR_NO_MEM, _header_new_item_exit, TAG, "Memory exhausted"); http_utils_trim_whitespace(&item->value); STAILQ_INSERT_TAIL(header, item, next); return ret; _header_new_item_exit: free(item->key); free(item->value); free(item); return ret; }{ ... } esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value) { http_header_item_handle_t item; if (value == NULL) { return http_header_delete(header, key); }{...} item = http_header_get_item(header, key); if (item) { free(item->value); item->value = strdup(value); http_utils_trim_whitespace(&item->value); return ESP_OK; }{...} return http_header_new_item(header, key, value); }{ ... } esp_err_t http_header_set_from_string(http_header_handle_t header, const char *key_value_data) { char *eq_ch; char *p_str; p_str = strdup(key_value_data); ESP_RETURN_ON_FALSE(p_str, ESP_ERR_NO_MEM, TAG, "Memory exhausted"); eq_ch = strchr(p_str, ':'); if (eq_ch == NULL) { free(p_str); return ESP_ERR_INVALID_ARG; }{...} *eq_ch = 0; http_header_set(header, p_str, eq_ch + 1); free(p_str); return ESP_OK; }{ ... } esp_err_t http_header_delete(http_header_handle_t header, const char *key) { http_header_item_handle_t item = http_header_get_item(header, key); if (item) { STAILQ_REMOVE(header, item, http_header_item, next); free(item->key); free(item->value); free(item); }{...} else { return ESP_ERR_NOT_FOUND; }{...} return ESP_OK; }{ ... } int http_header_set_format(http_header_handle_t header, const char *key, const char *format, ...) { va_list argptr; int len = 0; char *buf = NULL; va_start(argptr, format); len = vasprintf(&buf, format, argptr); va_end(argptr); ESP_RETURN_ON_FALSE(buf, 0, TAG, "Memory exhausted"); http_header_set(header, key, buf); free(buf); return len; }{ ... } int http_header_generate_string(http_header_handle_t header, int index, char *buffer, int *buffer_len) { http_header_item_handle_t item; int siz = 0; int idx = 0; int ret_idx = -1; bool is_end = false; // iterate over the header entries to calculate buffer size and determine last item STAILQ_FOREACH(item, header, next) { if (item->value && idx >= index) { siz += strlen(item->key); siz += strlen(item->value); siz += 4; //': ' and '\r\n' }{...} idx ++; if (siz + 1 > *buffer_len - 2) { // if this item would not fit to the buffer, return the index of the last fitting one ret_idx = idx - 1; ESP_LOGE(TAG, "Buffer length is small to fit all the headers"); break; }{...} }{...} if (siz == 0) { return 0; }{...} if (ret_idx < 0) { // all items would fit, mark this as the end of http header string ret_idx = idx; is_end = true; }{...} // iterate again over the header entries to write only the fitting indeces int str_len = 0; idx = 0; STAILQ_FOREACH(item, header, next) { if (item->value && idx >= index && idx < ret_idx) { str_len += snprintf(buffer + str_len, *buffer_len - str_len, "%s: %s\r\n", item->key, item->value); }{...} idx ++; }{...} if (is_end) { // write the http header terminator if all header entries have been written in this function call str_len += snprintf(buffer + str_len, *buffer_len - str_len, "\r\n"); }{...} *buffer_len = str_len; return ret_idx; }{ ... } esp_err_t http_header_clean(http_header_handle_t header) { http_header_item_handle_t item = STAILQ_FIRST(header), tmp; while (item != NULL) { tmp = STAILQ_NEXT(item, next); free(item->key); free(item->value); free(item); item = tmp; }{...} STAILQ_INIT(header); return ESP_OK; }{ ... } int http_header_count(http_header_handle_t header) { http_header_item_handle_t item; int count = 0; STAILQ_FOREACH(item, header, next) { count ++; }{...} return count; }{ ... }
Details
Show:
from
Types: Columns: