1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _HTTP_HEADER_H_
8 #define _HTTP_HEADER_H_
9 
10 #include "sys/queue.h"
11 #include "esp_err.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 typedef struct http_header *http_header_handle_t;
18 typedef struct http_header_item *http_header_item_handle_t;
19 
20 /**
21  * @brief      initialize and allocate the memory for the header object
22  *
23  * @return
24  *     - http_header_handle_t
25  *     - NULL if any errors
26  */
27 http_header_handle_t http_header_init(void);
28 
29 /**
30  * @brief      Cleanup and free all http header pairs
31  *
32  * @param[in]  header  The header
33  *
34  * @return
35  *     - ESP_OK
36  *     - ESP_FAIL
37  */
38 esp_err_t http_header_clean(http_header_handle_t header);
39 
40 /**
41  * @brief      Cleanup with http_header_clean and destroy http header handle object
42  *
43  * @param[in]  header  The header
44  *
45  * @return
46  *     - ESP_OK
47  *     - ESP_FAIL
48  */
49 esp_err_t http_header_destroy(http_header_handle_t header);
50 
51 /**
52  * @brief      Add a key-value pair of http header to the list,
53  *             note that with value = NULL, this function will remove the header with `key` already exists in the list.
54  *
55  * @param[in]  header  The header
56  * @param[in]  key     The key
57  * @param[in]  value   The value
58  *
59  * @return
60  *     - ESP_OK
61  *     - ESP_FAIL
62  */
63 esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value);
64 
65 /**
66  * @brief      Sample as `http_header_set` but the value can be formated
67  *
68  * @param[in]  header     The header
69  * @param[in]  key        The key
70  * @param[in]  format     The format
71  * @param[in]  ...        format parameters
72  *
73  * @return     Total length of value
74  */
75 int http_header_set_format(http_header_handle_t header, const char *key, const char *format, ...);
76 
77 /**
78  * @brief      Get a value of header in header list
79  *             The address of the value will be assign set to `value` parameter or NULL if no header with the key exists in the list
80  *
81  * @param[in]  header  The header
82  * @param[in]  key     The key
83  * @param[out] value   The value
84  *
85  * @return
86  *     - ESP_OK
87  *     - ESP_FAIL
88  */
89 esp_err_t http_header_get(http_header_handle_t header, const char *key, char **value);
90 
91 /**
92  * @brief      Create HTTP header string from the header with index, output string to buffer with buffer_len
93  *             Also return the last index of header was generated
94  *
95  * @param[in]  header      The header
96  * @param[in]  index       The index
97  * @param      buffer      The buffer
98  * @param      buffer_len  The buffer length
99  *
100  * @return     The last index of header was generated
101  */
102 int http_header_generate_string(http_header_handle_t header, int index, char *buffer, int *buffer_len);
103 
104 /**
105  * @brief      Remove the header with key from the headers list
106  *
107  * @param[in]  header  The header
108  * @param[in]  key     The key
109  *
110  * @return
111  *     - ESP_OK
112  *     - ESP_FAIL
113  */
114 esp_err_t http_header_delete(http_header_handle_t header, const char *key);
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif
121