1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #ifndef _HTTP_UTILS_H_
9 #define _HTTP_UTILS_H_
10 #include <sys/time.h>
11 
12 /**
13  * @brief      Assign new_str to *str pointer, and realloc *str if it not NULL
14  *
15  * @param      str      pointer to string pointer
16  * @param      new_str  assign this tring to str
17  * @param      len      length of string, less than 0 if new_str is zero terminated
18  *
19  * @return
20  *  - new_str pointer
21  *  - NULL
22  */
23 char *http_utils_assign_string(char **str, const char *new_str, int len);
24 
25 /**
26  * @brief      Realloc *str and append new_str to it if new_str is not NULL; return *str pointer if new_str is NULL
27  *
28  * @param      str      pointer to string pointer
29  * @param      new_str  append this string to str
30  * @param      len      length of string, less than 0 if new_str is zero terminated
31  *
32  * @return
33  *  - *str pointer
34  */
35 char *http_utils_append_string(char **str, const char *new_str, int len);
36 
37 /**
38  * @brief      Remove white space at begin and end of string
39  *
40  * @param[in]  str   The string
41  *
42  * @return     New strings have been trimmed
43  */
44 void http_utils_trim_whitespace(char **str);
45 
46 /**
47  * @brief      Gets the string between 2 string.
48  *             It will allocate a new memory space for this string, so you need to free it when no longer use
49  *
50  * @param[in]  str    The source string
51  * @param[in]  begin  The begin string
52  * @param[in]  end    The end string
53  *
54  * @return     The string between begin and end
55  */
56 char *http_utils_get_string_between(const char *str, const char *begin, const char *end);
57 
58 /**
59  * @brief      Join 2 strings to one
60  *             It will allocate a new memory space for this string, so you need to free it when no longer use
61  *
62  * @param[in]  first_str   The first string
63  * @param[in]  len_first   The length first
64  * @param[in]  second_str  The second string
65  * @param[in]  len_second  The length second
66  *
67  * @return
68  * - New string pointer
69  * - NULL: Invalid input
70  */
71 char *http_utils_join_string(const char *first_str, size_t len_first, const char *second_str, size_t len_second);
72 
73 /**
74  * @brief      Check if ``str`` is start with ``start``
75  *
76  * @param[in]  str    The string
77  * @param[in]  start  The start
78  *
79  * @return
80  *     - (-1) if length of ``start`` larger than length of ``str``
81  *     - (1) if ``start`` NOT starts with ``start``
82  *     - (0) if ``str`` starts with ``start``
83  */
84 int http_utils_str_starts_with(const char *str, const char *start);
85 
86 
87 #define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) {                                                  \
88         ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted");                     \
89         action;                                                                                     \
90         }
91 
92 #endif
93