http_header.c 6.2 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 "esp_log.h"
  12. #include "http_header.h"
  13. #include "http_utils.h"
  14. static const char *TAG = "HTTP_HEADER";
  15. #define HEADER_BUFFER (1024)
  16. /**
  17. * dictionary item struct, with key-value pair
  18. */
  19. typedef struct http_header_item {
  20. char *key; /*!< key */
  21. char *value; /*!< value */
  22. STAILQ_ENTRY(http_header_item) next; /*!< Point to next entry */
  23. } http_header_item_t;
  24. STAILQ_HEAD(http_header, http_header_item);
  25. http_header_handle_t http_header_init(void)
  26. {
  27. http_header_handle_t header = calloc(1, sizeof(struct http_header));
  28. HTTP_MEM_CHECK(TAG, header, return NULL);
  29. STAILQ_INIT(header);
  30. return header;
  31. }
  32. esp_err_t http_header_destroy(http_header_handle_t header)
  33. {
  34. esp_err_t err = http_header_clean(header);
  35. free(header);
  36. return err;
  37. }
  38. http_header_item_handle_t http_header_get_item(http_header_handle_t header, const char *key)
  39. {
  40. http_header_item_handle_t item;
  41. if (header == NULL || key == NULL) {
  42. return NULL;
  43. }
  44. STAILQ_FOREACH(item, header, next) {
  45. if (strcasecmp(item->key, key) == 0) {
  46. return item;
  47. }
  48. }
  49. return NULL;
  50. }
  51. esp_err_t http_header_get(http_header_handle_t header, const char *key, char **value)
  52. {
  53. http_header_item_handle_t item;
  54. item = http_header_get_item(header, key);
  55. if (item) {
  56. *value = item->value;
  57. } else {
  58. *value = NULL;
  59. }
  60. return ESP_OK;
  61. }
  62. static esp_err_t http_header_new_item(http_header_handle_t header, const char *key, const char *value)
  63. {
  64. http_header_item_handle_t item;
  65. item = calloc(1, sizeof(http_header_item_t));
  66. HTTP_MEM_CHECK(TAG, item, return ESP_ERR_NO_MEM);
  67. http_utils_assign_string(&item->key, key, -1);
  68. HTTP_MEM_CHECK(TAG, item->key, goto _header_new_item_exit);
  69. http_utils_trim_whitespace(&item->key);
  70. http_utils_assign_string(&item->value, value, -1);
  71. HTTP_MEM_CHECK(TAG, item->value, goto _header_new_item_exit);
  72. http_utils_trim_whitespace(&item->value);
  73. STAILQ_INSERT_TAIL(header, item, next);
  74. return ESP_OK;
  75. _header_new_item_exit:
  76. free(item->key);
  77. free(item->value);
  78. free(item);
  79. return ESP_ERR_NO_MEM;
  80. }
  81. esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value)
  82. {
  83. http_header_item_handle_t item;
  84. if (value == NULL) {
  85. return http_header_delete(header, key);
  86. }
  87. item = http_header_get_item(header, key);
  88. if (item) {
  89. free(item->value);
  90. item->value = strdup(value);
  91. http_utils_trim_whitespace(&item->value);
  92. return ESP_OK;
  93. }
  94. return http_header_new_item(header, key, value);
  95. }
  96. esp_err_t http_header_set_from_string(http_header_handle_t header, const char *key_value_data)
  97. {
  98. char *eq_ch;
  99. char *p_str;
  100. p_str = strdup(key_value_data);
  101. HTTP_MEM_CHECK(TAG, p_str, return ESP_ERR_NO_MEM);
  102. eq_ch = strchr(p_str, ':');
  103. if (eq_ch == NULL) {
  104. free(p_str);
  105. return ESP_ERR_INVALID_ARG;
  106. }
  107. *eq_ch = 0;
  108. http_header_set(header, p_str, eq_ch + 1);
  109. free(p_str);
  110. return ESP_OK;
  111. }
  112. esp_err_t http_header_delete(http_header_handle_t header, const char *key)
  113. {
  114. http_header_item_handle_t item = http_header_get_item(header, key);
  115. if (item) {
  116. STAILQ_REMOVE(header, item, http_header_item, next);
  117. free(item->key);
  118. free(item->value);
  119. free(item);
  120. } else {
  121. return ESP_ERR_NOT_FOUND;
  122. }
  123. return ESP_OK;
  124. }
  125. int http_header_set_format(http_header_handle_t header, const char *key, const char *format, ...)
  126. {
  127. va_list argptr;
  128. int len = 0;
  129. char *buf = NULL;
  130. va_start(argptr, format);
  131. len = vasprintf(&buf, format, argptr);
  132. va_end(argptr);
  133. HTTP_MEM_CHECK(TAG, buf, return 0);
  134. if (buf == NULL) {
  135. return 0;
  136. }
  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. }