http_header.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <stdbool.h>
  12. #include "esp_log.h"
  13. #include "esp_check.h"
  14. #include "http_header.h"
  15. #include "http_utils.h"
  16. static const char *TAG = "HTTP_HEADER";
  17. #define HEADER_BUFFER (1024)
  18. /**
  19. * dictionary item struct, with key-value pair
  20. */
  21. typedef struct http_header_item {
  22. char *key; /*!< key */
  23. char *value; /*!< value */
  24. STAILQ_ENTRY(http_header_item) next; /*!< Point to next entry */
  25. } http_header_item_t;
  26. STAILQ_HEAD(http_header, http_header_item);
  27. http_header_handle_t http_header_init(void)
  28. {
  29. http_header_handle_t header = calloc(1, sizeof(struct http_header));
  30. ESP_RETURN_ON_FALSE(header, NULL, TAG, "Memory exhausted");
  31. STAILQ_INIT(header);
  32. return header;
  33. }
  34. esp_err_t http_header_destroy(http_header_handle_t header)
  35. {
  36. esp_err_t err = http_header_clean(header);
  37. free(header);
  38. return err;
  39. }
  40. http_header_item_handle_t http_header_get_item(http_header_handle_t header, const char *key)
  41. {
  42. http_header_item_handle_t item;
  43. if (header == NULL || key == NULL) {
  44. return NULL;
  45. }
  46. STAILQ_FOREACH(item, header, next) {
  47. if (strcasecmp(item->key, key) == 0) {
  48. return item;
  49. }
  50. }
  51. return NULL;
  52. }
  53. esp_err_t http_header_get(http_header_handle_t header, const char *key, char **value)
  54. {
  55. http_header_item_handle_t item;
  56. item = http_header_get_item(header, key);
  57. if (item) {
  58. *value = item->value;
  59. } else {
  60. *value = NULL;
  61. }
  62. return ESP_OK;
  63. }
  64. static esp_err_t http_header_new_item(http_header_handle_t header, const char *key, const char *value)
  65. {
  66. esp_err_t ret = ESP_OK;
  67. http_header_item_handle_t item;
  68. item = calloc(1, sizeof(http_header_item_t));
  69. ESP_RETURN_ON_FALSE(item, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  70. http_utils_assign_string(&item->key, key, -1);
  71. ESP_GOTO_ON_FALSE(item->key, ESP_ERR_NO_MEM, _header_new_item_exit, TAG, "Memory exhausted");
  72. http_utils_trim_whitespace(&item->key);
  73. http_utils_assign_string(&item->value, value, -1);
  74. ESP_GOTO_ON_FALSE(item->value, ESP_ERR_NO_MEM, _header_new_item_exit, TAG, "Memory exhausted");
  75. http_utils_trim_whitespace(&item->value);
  76. STAILQ_INSERT_TAIL(header, item, next);
  77. return ret;
  78. _header_new_item_exit:
  79. free(item->key);
  80. free(item->value);
  81. free(item);
  82. return ret;
  83. }
  84. esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value)
  85. {
  86. http_header_item_handle_t item;
  87. if (value == NULL) {
  88. return http_header_delete(header, key);
  89. }
  90. item = http_header_get_item(header, key);
  91. if (item) {
  92. free(item->value);
  93. item->value = strdup(value);
  94. http_utils_trim_whitespace(&item->value);
  95. return ESP_OK;
  96. }
  97. return http_header_new_item(header, key, value);
  98. }
  99. esp_err_t http_header_set_from_string(http_header_handle_t header, const char *key_value_data)
  100. {
  101. char *eq_ch;
  102. char *p_str;
  103. p_str = strdup(key_value_data);
  104. ESP_RETURN_ON_FALSE(p_str, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  105. eq_ch = strchr(p_str, ':');
  106. if (eq_ch == NULL) {
  107. free(p_str);
  108. return ESP_ERR_INVALID_ARG;
  109. }
  110. *eq_ch = 0;
  111. http_header_set(header, p_str, eq_ch + 1);
  112. free(p_str);
  113. return ESP_OK;
  114. }
  115. esp_err_t http_header_delete(http_header_handle_t header, const char *key)
  116. {
  117. http_header_item_handle_t item = http_header_get_item(header, key);
  118. if (item) {
  119. STAILQ_REMOVE(header, item, http_header_item, next);
  120. free(item->key);
  121. free(item->value);
  122. free(item);
  123. } else {
  124. return ESP_ERR_NOT_FOUND;
  125. }
  126. return ESP_OK;
  127. }
  128. int http_header_set_format(http_header_handle_t header, const char *key, const char *format, ...)
  129. {
  130. va_list argptr;
  131. int len = 0;
  132. char *buf = NULL;
  133. va_start(argptr, format);
  134. len = vasprintf(&buf, format, argptr);
  135. va_end(argptr);
  136. ESP_RETURN_ON_FALSE(buf, 0, TAG, "Memory exhausted");
  137. http_header_set(header, key, buf);
  138. free(buf);
  139. return len;
  140. }
  141. int http_header_generate_string(http_header_handle_t header, int index, char *buffer, int *buffer_len)
  142. {
  143. http_header_item_handle_t item;
  144. int siz = 0;
  145. int idx = 0;
  146. int ret_idx = -1;
  147. bool is_end = false;
  148. // iterate over the header entries to calculate buffer size and determine last item
  149. STAILQ_FOREACH(item, header, next) {
  150. if (item->value && idx >= index) {
  151. siz += strlen(item->key);
  152. siz += strlen(item->value);
  153. siz += 4; //': ' and '\r\n'
  154. }
  155. idx ++;
  156. if (siz + 1 > *buffer_len - 2) {
  157. // if this item would not fit to the buffer, return the index of the last fitting one
  158. ret_idx = idx - 1;
  159. ESP_LOGE(TAG, "Buffer length is small to fit all the headers");
  160. break;
  161. }
  162. }
  163. if (siz == 0) {
  164. return 0;
  165. }
  166. if (ret_idx < 0) {
  167. // all items would fit, mark this as the end of http header string
  168. ret_idx = idx;
  169. is_end = true;
  170. }
  171. // iterate again over the header entries to write only the fitting indeces
  172. int str_len = 0;
  173. idx = 0;
  174. STAILQ_FOREACH(item, header, next) {
  175. if (item->value && idx >= index && idx < ret_idx) {
  176. str_len += snprintf(buffer + str_len, *buffer_len - str_len, "%s: %s\r\n", item->key, item->value);
  177. }
  178. idx ++;
  179. }
  180. if (is_end) {
  181. // write the http header terminator if all header entries have been written in this function call
  182. str_len += snprintf(buffer + str_len, *buffer_len - str_len, "\r\n");
  183. }
  184. *buffer_len = str_len;
  185. return ret_idx;
  186. }
  187. esp_err_t http_header_clean(http_header_handle_t header)
  188. {
  189. http_header_item_handle_t item = STAILQ_FIRST(header), tmp;
  190. while (item != NULL) {
  191. tmp = STAILQ_NEXT(item, next);
  192. free(item->key);
  193. free(item->value);
  194. free(item);
  195. item = tmp;
  196. }
  197. STAILQ_INIT(header);
  198. return ESP_OK;
  199. }
  200. int http_header_count(http_header_handle_t header)
  201. {
  202. http_header_item_handle_t item;
  203. int count = 0;
  204. STAILQ_FOREACH(item, header, next) {
  205. count ++;
  206. }
  207. return count;
  208. }