url.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. // ----------------------------------------------------------------------------
  4. // URL encode / decode
  5. // code from: http://www.geekhideout.com/urlcode.shtml
  6. /* Converts a hex character to its integer value */
  7. char from_hex(char ch) {
  8. return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10);
  9. }
  10. /* Converts an integer value to its hex character*/
  11. char to_hex(char code) {
  12. static char hex[] = "0123456789abcdef";
  13. return hex[code & 15];
  14. }
  15. /* Returns an url-encoded version of str */
  16. /* IMPORTANT: be sure to free() the returned string after use */
  17. char *url_encode(char *str) {
  18. char *buf, *pbuf;
  19. pbuf = buf = mallocz(strlen(str) * 3 + 1);
  20. while (*str) {
  21. if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
  22. *pbuf++ = *str;
  23. else if (*str == ' ')
  24. *pbuf++ = '+';
  25. else{
  26. *pbuf++ = '%';
  27. *pbuf++ = to_hex((char)(*str >> 4));
  28. *pbuf++ = to_hex((char)(*str & 15));
  29. }
  30. str++;
  31. }
  32. *pbuf = '\0';
  33. pbuf = strdupz(buf);
  34. freez(buf);
  35. return pbuf;
  36. }
  37. /**
  38. * Percentage escape decode
  39. *
  40. * Decode %XX character or return 0 if cannot
  41. *
  42. * @param s the string to decode
  43. *
  44. * @return The character decoded on success and 0 otherwise
  45. */
  46. char url_percent_escape_decode(const char *s) {
  47. if(likely(s[1] && s[2]))
  48. return (char)(from_hex(s[1]) << 4 | from_hex(s[2]));
  49. return 0;
  50. }
  51. /**
  52. * Get byte length
  53. *
  54. * This (utf8 string related) should be moved in separate file in future
  55. *
  56. * @param c is the utf8 character
  57. * *
  58. * @return It returns the length of the specific character.
  59. */
  60. char url_utf8_get_byte_length(char c) {
  61. if(!IS_UTF8_BYTE(c))
  62. return 1;
  63. char length = 0;
  64. while(likely(c & 0x80)) {
  65. length++;
  66. c <<= 1;
  67. }
  68. //4 byte is max size for UTF-8 char
  69. //10XX XXXX is not valid character -> check length == 1
  70. if(length > 4 || length == 1)
  71. return -1;
  72. return length;
  73. }
  74. /**
  75. * Decode Multibyte UTF8
  76. *
  77. * Decode % encoded UTF-8 characters and copy them to *d
  78. *
  79. * @param s first address
  80. * @param d
  81. * @param d_end last address
  82. *
  83. * @return count of bytes written to *d
  84. */
  85. char url_decode_multibyte_utf8(const char *s, char *d, const char *d_end) {
  86. char first_byte = url_percent_escape_decode(s);
  87. if(unlikely(!first_byte || !IS_UTF8_STARTBYTE(first_byte)))
  88. return 0;
  89. char byte_length = url_utf8_get_byte_length(first_byte);
  90. if(unlikely(byte_length <= 0 || d+byte_length >= d_end))
  91. return 0;
  92. char to_read = byte_length;
  93. while(to_read > 0) {
  94. char c = url_percent_escape_decode(s);
  95. if(unlikely( !IS_UTF8_BYTE(c) ))
  96. return 0;
  97. if((to_read != byte_length) && IS_UTF8_STARTBYTE(c))
  98. return 0;
  99. *d++ = c;
  100. s+=3;
  101. to_read--;
  102. }
  103. return byte_length;
  104. }
  105. /*
  106. * The utf8_check() function scans the '\0'-terminated string starting
  107. * at s. It returns a pointer to the first byte of the first malformed
  108. * or overlong UTF-8 sequence found, or NULL if the string contains
  109. * only correct UTF-8. It also spots UTF-8 sequences that could cause
  110. * trouble if converted to UTF-16, namely surrogate characters
  111. * (U+D800..U+DFFF) and non-Unicode positions (U+FFFE..U+FFFF). This
  112. * routine is very likely to find a malformed sequence if the input
  113. * uses any other encoding than UTF-8. It therefore can be used as a
  114. * very effective heuristic for distinguishing between UTF-8 and other
  115. * encodings.
  116. *
  117. * Markus Kuhn <http://www.cl.cam.ac.uk/~mgk25/> -- 2005-03-30
  118. * License: http://www.cl.cam.ac.uk/~mgk25/short-license.html
  119. */
  120. unsigned char *utf8_check(unsigned char *s)
  121. {
  122. while (*s)
  123. {
  124. if (*s < 0x80)
  125. /* 0xxxxxxx */
  126. s++;
  127. else if ((s[0] & 0xe0) == 0xc0)
  128. {
  129. /* 110XXXXx 10xxxxxx */
  130. if ((s[1] & 0xc0) != 0x80 ||
  131. (s[0] & 0xfe) == 0xc0) /* overlong? */
  132. return s;
  133. else
  134. s += 2;
  135. }
  136. else if ((s[0] & 0xf0) == 0xe0)
  137. {
  138. /* 1110XXXX 10Xxxxxx 10xxxxxx */
  139. if ((s[1] & 0xc0) != 0x80 ||
  140. (s[2] & 0xc0) != 0x80 ||
  141. (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) || /* overlong? */
  142. (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) || /* surrogate? */
  143. (s[0] == 0xef && s[1] == 0xbf &&
  144. (s[2] & 0xfe) == 0xbe)) /* U+FFFE or U+FFFF? */
  145. return s;
  146. else
  147. s += 3;
  148. }
  149. else if ((s[0] & 0xf8) == 0xf0)
  150. {
  151. /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
  152. if ((s[1] & 0xc0) != 0x80 ||
  153. (s[2] & 0xc0) != 0x80 ||
  154. (s[3] & 0xc0) != 0x80 ||
  155. (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || /* overlong? */
  156. (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4) /* > U+10FFFF? */
  157. return s;
  158. else
  159. s += 4;
  160. }
  161. else
  162. return s;
  163. }
  164. return NULL;
  165. }
  166. char *url_decode_r(char *to, const char *url, size_t size) {
  167. const char *s = url; // source
  168. char *d = to, // destination
  169. *e = &to[size - 1]; // destination end
  170. while(*s && d < e) {
  171. if(unlikely(*s == '%')) {
  172. char t = url_percent_escape_decode(s);
  173. if(IS_UTF8_BYTE(t)) {
  174. char bytes_written = url_decode_multibyte_utf8(s, d, e);
  175. if(likely(bytes_written)){
  176. d += bytes_written;
  177. s += (bytes_written * 3)-1;
  178. }
  179. else {
  180. goto fail_cleanup;
  181. }
  182. }
  183. else if(likely(t) && isprint(t)) {
  184. // avoid HTTP header injection
  185. *d++ = t;
  186. s += 2;
  187. }
  188. else
  189. goto fail_cleanup;
  190. }
  191. else if(unlikely(*s == '+'))
  192. *d++ = ' ';
  193. else
  194. *d++ = *s;
  195. s++;
  196. }
  197. *d = '\0';
  198. if(unlikely( utf8_check((unsigned char *)to) )) //NULL means success here
  199. return NULL;
  200. return to;
  201. fail_cleanup:
  202. *d = '\0';
  203. return NULL;
  204. }
  205. inline bool url_is_request_complete(char *begin, char *end, size_t length, char **post_payload, size_t *post_payload_size) {
  206. if (begin == end || length < 4)
  207. return false;
  208. if(likely(strncmp(begin, "GET ", 4)) == 0) {
  209. return strstr(end - 4, "\r\n\r\n");
  210. }
  211. else if(unlikely(strncmp(begin, "POST ", 5) == 0)) {
  212. char *cl = strstr(begin, "Content-Length: ");
  213. if(!cl) return false;
  214. cl = &cl[16];
  215. size_t content_length = str2ul(cl);
  216. char *payload = strstr(cl, "\r\n\r\n");
  217. if(!payload) return false;
  218. payload += 4;
  219. size_t payload_length = length - (payload - begin);
  220. if(payload_length == content_length) {
  221. if(post_payload && post_payload_size) {
  222. if (*post_payload)
  223. freez(*post_payload);
  224. *post_payload = mallocz(payload_length + 1);
  225. memcpy(*post_payload, payload, payload_length);
  226. (*post_payload)[payload_length] = '\0';
  227. *post_payload_size = payload_length;
  228. }
  229. return true;
  230. }
  231. return false;
  232. }
  233. else {
  234. return strstr(end - 4, "\r\n\r\n");
  235. }
  236. }
  237. /**
  238. * Find protocol
  239. *
  240. * Search for the string ' HTTP/' in the message given.
  241. *
  242. * @param s is the start of the user request.
  243. * @return
  244. */
  245. inline char *url_find_protocol(char *s) {
  246. while(*s) {
  247. // find the next space
  248. while (*s && *s != ' ') s++;
  249. // is it SPACE + "HTTP/" ?
  250. if(*s && !strncmp(s, " HTTP/", 6)) break;
  251. else s++;
  252. }
  253. return s;
  254. }