http_utils.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, 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 Remove white space at begin and end of string
  30. *
  31. * @param[in] str The string
  32. *
  33. * @return New strings have been trimmed
  34. */
  35. void http_utils_trim_whitespace(char **str);
  36. /**
  37. * @brief Gets the string between 2 string.
  38. * It will allocate a new memory space for this string, so you need to free it when no longer use
  39. *
  40. * @param[in] str The source string
  41. * @param[in] begin The begin string
  42. * @param[in] end The end string
  43. *
  44. * @return The string between begin and end
  45. */
  46. char *http_utils_get_string_between(const char *str, const char *begin, const char *end);
  47. /**
  48. * @brief Join 2 strings to one
  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] first_str The first string
  52. * @param[in] len_first The length first
  53. * @param[in] second_str The second string
  54. * @param[in] len_second The length second
  55. *
  56. * @return
  57. * - New string pointer
  58. * - NULL: Invalid input
  59. */
  60. char *http_utils_join_string(const char *first_str, int len_first, const char *second_str, int len_second);
  61. /**
  62. * @brief Check if ``str`` is start with ``start``
  63. *
  64. * @param[in] str The string
  65. * @param[in] start The start
  66. *
  67. * @return
  68. * - (-1) if length of ``start`` larger than length of ``str``
  69. * - (1) if ``start`` NOT starts with ``start``
  70. * - (0) if ``str`` starts with ``start``
  71. */
  72. int http_utils_str_starts_with(const char *str, const char *start);
  73. #define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) { \
  74. ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
  75. action; \
  76. }
  77. #endif