http_utils.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. char *http_utils_append_string(char **str, const char *new_str, int len)
  58. {
  59. int l = len;
  60. int old_len = 0;
  61. char *old_str = *str;
  62. if (new_str != NULL) {
  63. if (l < 0) {
  64. l = strlen(new_str);
  65. }
  66. if (old_str) {
  67. old_len = strlen(old_str);
  68. old_str = realloc(old_str, old_len + l + 1);
  69. mem_check(old_str);
  70. old_str[old_len + l] = 0;
  71. } else {
  72. old_str = calloc(1, l + 1);
  73. mem_check(old_str);
  74. }
  75. memcpy(old_str + old_len, new_str, l);
  76. *str = old_str;
  77. }
  78. return old_str;
  79. }
  80. void http_utils_trim_whitespace(char **str)
  81. {
  82. char *end, *start;
  83. if (str == NULL) {
  84. return;
  85. }
  86. start = *str;
  87. if (start == NULL) {
  88. return;
  89. }
  90. // Trim leading space
  91. while (isspace((unsigned char)*start)) start ++;
  92. if (*start == 0) { // All spaces?
  93. **str = 0;
  94. return;
  95. }
  96. // Trim trailing space
  97. end = (char *)(start + strlen(start) - 1);
  98. while (end > start && isspace((unsigned char)*end)) {
  99. end--;
  100. }
  101. // Write new null terminator
  102. *(end + 1) = 0;
  103. memmove(*str, start, strlen(start) + 1);
  104. }
  105. char *http_utils_get_string_between(const char *str, const char *begin, const char *end)
  106. {
  107. char *found = strstr(str, begin);
  108. char *ret = NULL;
  109. if (found) {
  110. found += strlen(begin);
  111. char *found_end = strstr(found, end);
  112. if (found_end) {
  113. ret = calloc(1, found_end - found + 1);
  114. mem_check(ret);
  115. memcpy(ret, found, found_end - found);
  116. return ret;
  117. }
  118. }
  119. return NULL;
  120. }
  121. int http_utils_str_starts_with(const char *str, const char *start)
  122. {
  123. int i;
  124. int match_str_len = strlen(str);
  125. int start_len = strlen(start);
  126. if (start_len > match_str_len) {
  127. return -1;
  128. }
  129. for (i = 0; i < start_len; i++) {
  130. if (str[i] != start[i]) {
  131. return 1;
  132. }
  133. }
  134. return 0;
  135. }