http_utils.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef _HTTP_UTILS_H_
  14. #define _HTTP_UTILS_H_
  15. #include <sys/time.h>
  16. #include <ulog/ulog.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * @brief Assign new_str to *str pointer, and realloc *str if it not NULL
  22. *
  23. * @param str pointer to string pointer
  24. * @param new_str assign this tring to str
  25. * @param len length of string, 0 if new_str is zero terminated
  26. *
  27. * @return
  28. * - new_str pointer
  29. * - NULL
  30. */
  31. char *http_utils_assign_string(char **str, const char *new_str, int len);
  32. /**
  33. * @brief Remove white space at begin and end of string
  34. *
  35. * @param[in] str The string
  36. *
  37. * @return New strings have been trimmed
  38. */
  39. void http_utils_trim_whitespace(char **str);
  40. /**
  41. * @brief Gets the string between 2 string.
  42. * It will allocate a new memory space for this string, so you need to free it when no longer use
  43. *
  44. * @param[in] str The source string
  45. * @param[in] begin The begin string
  46. * @param[in] end The end string
  47. *
  48. * @return The string between begin and end
  49. */
  50. char *http_utils_get_string_between(const char *str, const char *begin, const char *end);
  51. /**
  52. * @brief Join 2 strings to one
  53. * It will allocate a new memory space for this string, so you need to free it when no longer use
  54. *
  55. * @param[in] first_str The first string
  56. * @param[in] len_first The length first
  57. * @param[in] second_str The second string
  58. * @param[in] len_second The length second
  59. *
  60. * @return
  61. * - New string pointer
  62. * - NULL: Invalid input
  63. */
  64. char *http_utils_join_string(const char *first_str, int len_first, const char *second_str, int len_second);
  65. /**
  66. * @brief Check if ``str`` is start with ``start``
  67. *
  68. * @param[in] str The string
  69. * @param[in] start The start
  70. *
  71. * @return
  72. * - (-1) if length of ``start`` larger than length of ``str``
  73. * - (1) if ``start`` NOT starts with ``start``
  74. * - (0) if ``str`` starts with ``start``
  75. */
  76. int http_utils_str_starts_with(const char *str, const char *start);
  77. // #define HTTP_MEM_CHECK(TAG, a, action) //TRANSPORT_MEM_CHECK(TAG, a, action)
  78. #define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) { \
  79. LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
  80. action; \
  81. }
  82. #ifdef __cplusplus
  83. }
  84. #endif
  85. #endif