THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.5KB

  1. #include "cache.h"
  2. #include "url.h"
  3. int is_urlschemechar(int first_flag, int ch)
  4. {
  5. /*
  6. * The set of valid URL schemes, as per STD66 (RFC3986) is
  7. * '[A-Za-z][A-Za-z0-9+.-]*'. But use sightly looser check
  8. * of '[A-Za-z0-9][A-Za-z0-9+.-]*' because earlier version
  9. * of check used '[A-Za-z0-9]+' so not to break any remote
  10. * helpers.
  11. */
  12. int alphanumeric, special;
  13. alphanumeric = ch > 0 && isalnum(ch);
  14. special = ch == '+' || ch == '-' || ch == '.';
  15. return alphanumeric || (!first_flag && special);
  16. }
  17. int is_url(const char *url)
  18. {
  19. /* Is "scheme" part reasonable? */
  20. if (!url || !is_urlschemechar(1, *url++))
  21. return 0;
  22. while (*url && *url != ':') {
  23. if (!is_urlschemechar(0, *url++))
  24. return 0;
  25. }
  26. /* We've seen "scheme"; we want colon-slash-slash */
  27. return (url[0] == ':' && url[1] == '/' && url[2] == '/');
  28. }
  29. static char *url_decode_internal(const char **query, int len,
  30. const char *stop_at, struct strbuf *out,
  31. int decode_plus)
  32. {
  33. const char *q = *query;
  34. while (len) {
  35. unsigned char c = *q;
  36. if (!c)
  37. break;
  38. if (stop_at && strchr(stop_at, c)) {
  39. q++;
  40. len--;
  41. break;
  42. }
  43. if (c == '%' && (len < 0 || len >= 3)) {
  44. int val = hex2chr(q + 1);
  45. if (0 < val) {
  46. strbuf_addch(out, val);
  47. q += 3;
  48. len -= 3;
  49. continue;
  50. }
  51. }
  52. if (decode_plus && c == '+')
  53. strbuf_addch(out, ' ');
  54. else
  55. strbuf_addch(out, c);
  56. q++;
  57. len--;
  58. }
  59. *query = q;
  60. return strbuf_detach(out, NULL);
  61. }
  62. char *url_decode(const char *url)
  63. {
  64. return url_decode_mem(url, strlen(url));
  65. }
  66. char *url_decode_mem(const char *url, int len)
  67. {
  68. struct strbuf out = STRBUF_INIT;
  69. const char *colon = memchr(url, ':', len);
  70. /* Skip protocol part if present */
  71. if (colon && url < colon) {
  72. strbuf_add(&out, url, colon - url);
  73. len -= colon - url;
  74. url = colon;
  75. }
  76. return url_decode_internal(&url, len, NULL, &out, 0);
  77. }
  78. char *url_percent_decode(const char *encoded)
  79. {
  80. struct strbuf out = STRBUF_INIT;
  81. return url_decode_internal(&encoded, strlen(encoded), NULL, &out, 0);
  82. }
  83. char *url_decode_parameter_name(const char **query)
  84. {
  85. struct strbuf out = STRBUF_INIT;
  86. return url_decode_internal(query, -1, "&=", &out, 1);
  87. }
  88. char *url_decode_parameter_value(const char **query)
  89. {
  90. struct strbuf out = STRBUF_INIT;
  91. return url_decode_internal(query, -1, "&", &out, 1);
  92. }
  93. void end_url_with_slash(struct strbuf *buf, const char *url)
  94. {
  95. strbuf_addstr(buf, url);
  96. strbuf_complete(buf, '/');
  97. }
  98. void str_end_url_with_slash(const char *url, char **dest)
  99. {
  100. struct strbuf buf = STRBUF_INIT;
  101. end_url_with_slash(&buf, url);
  102. free(*dest);
  103. *dest = strbuf_detach(&buf, NULL);
  104. }