http_utils.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  17. * @brief Assign new_str to *str pointer, and realloc *str if it not NULL
  18. *
  19. * @param str pointer to string pointer
  20. * @param new_str assign this tring to str
  21. * @param len length of string, less than 0 if new_str is zero terminated
  22. *
  23. * @return
  24. * - new_str pointer
  25. * - NULL
  26. */
  27. char *http_utils_assign_string(char **str, const char *new_str, int len);
  28. /**
  29. * @brief Realloc *str and append new_str to it if new_str is not NULL; return *str pointer if new_str is NULL
  30. *
  31. * @param str pointer to string pointer
  32. * @param new_str append this string to str
  33. * @param len length of string, less than 0 if new_str is zero terminated
  34. *
  35. * @return
  36. * - *str pointer
  37. */
  38. char *http_utils_append_string(char **str, const char *new_str, int len);
  39. /**
  40. * @brief Remove white space at begin and end of string
  41. *
  42. * @param[in] str The string
  43. *
  44. * @return New strings have been trimmed
  45. */
  46. void http_utils_trim_whitespace(char **str);
  47. /**
  48. * @brief Gets the string between 2 string.
  49. * It will allocate a new memory space for this string, so you need to free it when no longer use
  50. *
  51. * @param[in] str The source string
  52. * @param[in] begin The begin string
  53. * @param[in] end The end string
  54. *
  55. * @return The string between begin and end
  56. */
  57. char *http_utils_get_string_between(const char *str, const char *begin, const char *end);
  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. * @brief Check if ``str`` is start with ``start``
  74. *
  75. * @param[in] str The string
  76. * @param[in] start The start
  77. *
  78. * @return
  79. * - (-1) if length of ``start`` larger than length of ``str``
  80. * - (1) if ``start`` NOT starts with ``start``
  81. * - (0) if ``str`` starts with ``start``
  82. */
  83. int http_utils_str_starts_with(const char *str, const char *start);
  84. #define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) { \
  85. ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \
  86. action; \
  87. }
  88. #endif