http_header.c 6.3 KB

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