http_header.c 6.6 KB

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