http_utils.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include <string.h>
  14. #include <ctype.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <assert.h>
  18. #include "http_utils.h"
  19. #ifndef mem_check
  20. #define mem_check(x) assert(x)
  21. #endif
  22. char *http_utils_join_string(const char *first_str, int len_first, const char *second_str, int len_second)
  23. {
  24. int first_str_len = len_first > 0 ? len_first : strlen(first_str);
  25. int second_str_len = len_second > 0 ? len_second : strlen(second_str);
  26. char *ret = NULL;
  27. if (first_str_len + second_str_len > 0) {
  28. ret = calloc(1, first_str_len + second_str_len + 1);
  29. mem_check(ret);
  30. memcpy(ret, first_str, first_str_len);
  31. memcpy(ret + first_str_len, second_str, second_str_len);
  32. }
  33. return ret;
  34. }
  35. char *http_utils_assign_string(char **str, const char *new_str, int len)
  36. {
  37. int l = len;
  38. if (new_str == NULL) {
  39. return NULL;
  40. }
  41. char *old_str = *str;
  42. if (l <= 0) {
  43. l = strlen(new_str);
  44. }
  45. if (old_str) {
  46. old_str = realloc(old_str, l + 1);
  47. mem_check(old_str);
  48. old_str[l] = 0;
  49. } else {
  50. old_str = calloc(1, l + 1);
  51. mem_check(old_str);
  52. }
  53. memcpy(old_str, new_str, l);
  54. *str = old_str;
  55. return old_str;
  56. }
  57. void http_utils_trim_whitespace(char **str)
  58. {
  59. char *end, *start;
  60. if (str == NULL) {
  61. return;
  62. }
  63. start = *str;
  64. if (start == NULL) {
  65. return;
  66. }
  67. // Trim leading space
  68. while (isspace((unsigned char)*start)) start ++;
  69. if (*start == 0) { // All spaces?
  70. **str = 0;
  71. return;
  72. }
  73. // Trim trailing space
  74. end = (char *)(start + strlen(start) - 1);
  75. while (end > start && isspace((unsigned char)*end)) {
  76. end--;
  77. }
  78. // Write new null terminator
  79. *(end + 1) = 0;
  80. memmove(*str, start, strlen(start) + 1);
  81. }
  82. char *http_utils_get_string_between(const char *str, const char *begin, const char *end)
  83. {
  84. char *found = strstr(str, begin);
  85. char *ret = NULL;
  86. if (found) {
  87. found += strlen(begin);
  88. char *found_end = strstr(found, end);
  89. if (found_end) {
  90. ret = calloc(1, found_end - found + 1);
  91. mem_check(ret);
  92. memcpy(ret, found, found_end - found);
  93. return ret;
  94. }
  95. }
  96. return NULL;
  97. }
  98. int http_utils_str_starts_with(const char *str, const char *start)
  99. {
  100. int i;
  101. int match_str_len = strlen(str);
  102. int start_len = strlen(start);
  103. if (start_len > match_str_len) {
  104. return -1;
  105. }
  106. for (i = 0; i < start_len; i++) {
  107. if (str[i] != start[i]) {
  108. return 1;
  109. }
  110. }
  111. return 0;
  112. }