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.
 
 
 
 
 
 

132 lines
2.7 KiB

  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 int url_decode_char(const char *q)
  30. {
  31. int i;
  32. unsigned char val = 0;
  33. for (i = 0; i < 2; i++) {
  34. unsigned char c = *q++;
  35. val <<= 4;
  36. if (c >= '0' && c <= '9')
  37. val += c - '0';
  38. else if (c >= 'a' && c <= 'f')
  39. val += c - 'a' + 10;
  40. else if (c >= 'A' && c <= 'F')
  41. val += c - 'A' + 10;
  42. else
  43. return -1;
  44. }
  45. return val;
  46. }
  47. static char *url_decode_internal(const char **query, int len,
  48. const char *stop_at, struct strbuf *out,
  49. int decode_plus)
  50. {
  51. const char *q = *query;
  52. while (len) {
  53. unsigned char c = *q;
  54. if (!c)
  55. break;
  56. if (stop_at && strchr(stop_at, c)) {
  57. q++;
  58. len--;
  59. break;
  60. }
  61. if (c == '%') {
  62. int val = url_decode_char(q + 1);
  63. if (0 <= val) {
  64. strbuf_addch(out, val);
  65. q += 3;
  66. len -= 3;
  67. continue;
  68. }
  69. }
  70. if (decode_plus && c == '+')
  71. strbuf_addch(out, ' ');
  72. else
  73. strbuf_addch(out, c);
  74. q++;
  75. len--;
  76. }
  77. *query = q;
  78. return strbuf_detach(out, NULL);
  79. }
  80. char *url_decode(const char *url)
  81. {
  82. return url_decode_mem(url, strlen(url));
  83. }
  84. char *url_decode_mem(const char *url, int len)
  85. {
  86. struct strbuf out = STRBUF_INIT;
  87. const char *colon = memchr(url, ':', len);
  88. /* Skip protocol part if present */
  89. if (colon && url < colon) {
  90. strbuf_add(&out, url, colon - url);
  91. len -= colon - url;
  92. url = colon;
  93. }
  94. return url_decode_internal(&url, len, NULL, &out, 0);
  95. }
  96. char *url_decode_parameter_name(const char **query)
  97. {
  98. struct strbuf out = STRBUF_INIT;
  99. return url_decode_internal(query, -1, "&=", &out, 1);
  100. }
  101. char *url_decode_parameter_value(const char **query)
  102. {
  103. struct strbuf out = STRBUF_INIT;
  104. return url_decode_internal(query, -1, "&", &out, 1);
  105. }
  106. void end_url_with_slash(struct strbuf *buf, const char *url)
  107. {
  108. strbuf_addstr(buf, url);
  109. strbuf_complete(buf, '/');
  110. }
  111. void str_end_url_with_slash(const char *url, char **dest) {
  112. struct strbuf buf = STRBUF_INIT;
  113. end_url_with_slash(&buf, url);
  114. free(*dest);
  115. *dest = strbuf_detach(&buf, NULL);
  116. }