http_header.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(void)
  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. return ESP_ERR_NO_MEM;
  86. }
  87. esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value)
  88. {
  89. http_header_item_handle_t item;
  90. if (value == NULL) {
  91. return http_header_delete(header, key);
  92. }
  93. item = http_header_get_item(header, key);
  94. if (item) {
  95. free(item->value);
  96. item->value = strdup(value);
  97. http_utils_trim_whitespace(&item->value);
  98. return ESP_OK;
  99. }
  100. return http_header_new_item(header, key, value);
  101. }
  102. esp_err_t http_header_set_from_string(http_header_handle_t header, const char *key_value_data)
  103. {
  104. char *eq_ch;
  105. char *p_str;
  106. p_str = strdup(key_value_data);
  107. HTTP_MEM_CHECK(TAG, p_str, return ESP_ERR_NO_MEM);
  108. eq_ch = strchr(p_str, ':');
  109. if (eq_ch == NULL) {
  110. free(p_str);
  111. return ESP_ERR_INVALID_ARG;
  112. }
  113. *eq_ch = 0;
  114. http_header_set(header, p_str, eq_ch + 1);
  115. free(p_str);
  116. return ESP_OK;
  117. }
  118. esp_err_t http_header_delete(http_header_handle_t header, const char *key)
  119. {
  120. http_header_item_handle_t item = http_header_get_item(header, key);
  121. if (item) {
  122. STAILQ_REMOVE(header, item, http_header_item, next);
  123. free(item->key);
  124. free(item->value);
  125. free(item);
  126. } else {
  127. return ESP_ERR_NOT_FOUND;
  128. }
  129. return ESP_OK;
  130. }
  131. int http_header_set_format(http_header_handle_t header, const char *key, const char *format, ...)
  132. {
  133. va_list argptr;
  134. int len = 0;
  135. char *buf = NULL;
  136. va_start(argptr, format);
  137. len = vasprintf(&buf, format, argptr);
  138. HTTP_MEM_CHECK(TAG, buf, return 0);
  139. va_end(argptr);
  140. if (buf == NULL) {
  141. return 0;
  142. }
  143. http_header_set(header, key, buf);
  144. free(buf);
  145. return len;
  146. }
  147. int http_header_generate_string(http_header_handle_t header, int index, char *buffer, int *buffer_len)
  148. {
  149. http_header_item_handle_t item;
  150. int siz = 0;
  151. int idx = 0;
  152. int ret_idx = -1;
  153. bool is_end = false;
  154. STAILQ_FOREACH(item, header, next) {
  155. if (item->value && idx >= index) {
  156. siz += strlen(item->key);
  157. siz += strlen(item->value);
  158. siz += 4; //': ' and '\r\n'
  159. }
  160. idx ++;
  161. if (siz + 1 > *buffer_len - 2) {
  162. ret_idx = idx - 1;
  163. }
  164. }
  165. if (siz == 0) {
  166. return 0;
  167. }
  168. if (ret_idx < 0) {
  169. ret_idx = idx;
  170. is_end = true;
  171. }
  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. str_len += snprintf(buffer + str_len, *buffer_len - str_len, "\r\n");
  182. }
  183. *buffer_len = str_len;
  184. return ret_idx;
  185. }
  186. esp_err_t http_header_clean(http_header_handle_t header)
  187. {
  188. http_header_item_handle_t item = STAILQ_FIRST(header), tmp;
  189. while (item != NULL) {
  190. tmp = STAILQ_NEXT(item, next);
  191. free(item->key);
  192. free(item->value);
  193. free(item);
  194. item = tmp;
  195. }
  196. STAILQ_INIT(header);
  197. return ESP_OK;
  198. }
  199. int http_header_count(http_header_handle_t header)
  200. {
  201. http_header_item_handle_t item;
  202. int count = 0;
  203. STAILQ_FOREACH(item, header, next) {
  204. count ++;
  205. }
  206. return count;
  207. }