http_header.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <stdbool.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. #include <stdarg.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. web_err_t http_header_destroy(http_header_handle_t header)
  40. {
  41. web_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. web_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 WEB_OK;
  68. }
  69. static web_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 WEB_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 WEB_OK;
  82. _header_new_item_exit:
  83. if (item->key)
  84. free(item->key);
  85. if (item->value)
  86. free(item->value);
  87. free(item);
  88. return WEB_ERR_NO_MEM;
  89. }
  90. web_err_t http_header_set(http_header_handle_t header, const char *key, const char *value)
  91. {
  92. http_header_item_handle_t item;
  93. if (value == NULL) {
  94. return http_header_delete(header, key);
  95. }
  96. item = http_header_get_item(header, key);
  97. if (item) {
  98. free(item->value);
  99. item->value = strdup(value);
  100. http_utils_trim_whitespace(&item->value);
  101. return WEB_OK;
  102. }
  103. return http_header_new_item(header, key, value);
  104. }
  105. web_err_t http_header_set_from_string(http_header_handle_t header, const char *key_value_data)
  106. {
  107. char *eq_ch;
  108. char *p_str;
  109. p_str = strdup(key_value_data);
  110. HTTP_MEM_CHECK(TAG, p_str, return WEB_ERR_NO_MEM);
  111. eq_ch = strchr(p_str, ':');
  112. if (eq_ch == NULL) {
  113. free(p_str);
  114. return WEB_ERR_INVALID_ARG;
  115. }
  116. *eq_ch = 0;
  117. http_header_set(header, p_str, eq_ch + 1);
  118. free(p_str);
  119. return WEB_OK;
  120. }
  121. web_err_t http_header_delete(http_header_handle_t header, const char *key)
  122. {
  123. http_header_item_handle_t item = http_header_get_item(header, key);
  124. if (item) {
  125. STAILQ_REMOVE(header, item, http_header_item, next);
  126. free(item->key);
  127. free(item->value);
  128. free(item);
  129. } else {
  130. return WEB_ERR_NOT_FOUND;
  131. }
  132. return WEB_OK;
  133. }
  134. int http_header_set_format(http_header_handle_t header, const char *key, const char *format, ...)
  135. {
  136. va_list argptr;
  137. int len = 0;
  138. char *buf = NULL;
  139. va_start(argptr, format);
  140. len = vasprintf(&buf, format, argptr);
  141. va_end(argptr);
  142. if (buf == NULL) {
  143. return 0;
  144. }
  145. http_header_set(header, key, buf);
  146. free(buf);
  147. return len;
  148. }
  149. int http_header_generate_string(http_header_handle_t header, int index, char *buffer, int *buffer_len)
  150. {
  151. http_header_item_handle_t item;
  152. int siz = 0;
  153. int idx = 0;
  154. int ret_idx = -1;
  155. bool is_end = false;
  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. ret_idx = idx - 1;
  165. }
  166. }
  167. if (siz == 0) {
  168. return 0;
  169. }
  170. if (ret_idx < 0) {
  171. ret_idx = idx;
  172. is_end = true;
  173. }
  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. str_len += snprintf(buffer + str_len, *buffer_len - str_len, "\r\n");
  184. }
  185. *buffer_len = str_len;
  186. return ret_idx;
  187. }
  188. web_err_t http_header_clean(http_header_handle_t header)
  189. {
  190. http_header_item_handle_t item = STAILQ_FIRST(header), tmp;
  191. while (item != NULL) {
  192. tmp = STAILQ_NEXT(item, next);
  193. free(item->key);
  194. free(item->value);
  195. free(item);
  196. item = tmp;
  197. }
  198. STAILQ_INIT(header);
  199. return WEB_OK;
  200. }
  201. int http_header_count(http_header_handle_t header)
  202. {
  203. http_header_item_handle_t item;
  204. int count = 0;
  205. STAILQ_FOREACH(item, header, next) {
  206. count ++;
  207. }
  208. return count;
  209. }