1
0

hurl.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "hurl.h"
  2. #include "hdef.h"
  3. #include "hbase.h"
  4. /*
  5. static bool Curl_isunreserved(unsigned char in)
  6. {
  7. switch(in) {
  8. case '0': case '1': case '2': case '3': case '4':
  9. case '5': case '6': case '7': case '8': case '9':
  10. case 'a': case 'b': case 'c': case 'd': case 'e':
  11. case 'f': case 'g': case 'h': case 'i': case 'j':
  12. case 'k': case 'l': case 'm': case 'n': case 'o':
  13. case 'p': case 'q': case 'r': case 's': case 't':
  14. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  15. case 'A': case 'B': case 'C': case 'D': case 'E':
  16. case 'F': case 'G': case 'H': case 'I': case 'J':
  17. case 'K': case 'L': case 'M': case 'N': case 'O':
  18. case 'P': case 'Q': case 'R': case 'S': case 'T':
  19. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  20. case '-': case '.': case '_': case '~':
  21. return TRUE;
  22. default:
  23. break;
  24. }
  25. return FLASE;
  26. }
  27. */
  28. static inline bool is_unambiguous(char c) {
  29. return IS_ALPHANUM(c) ||
  30. c == '-' ||
  31. c == '_' ||
  32. c == '.' ||
  33. c == '~';
  34. }
  35. static inline bool char_in_str(char c, const char* str) {
  36. const char* p = str;
  37. while (*p && *p != c) ++p;
  38. return *p != '\0';
  39. }
  40. static inline unsigned char hex2i(char hex) {
  41. return hex <= '9' ? hex - '0' :
  42. hex <= 'F' ? hex - 'A' + 10 : hex - 'a' + 10;
  43. }
  44. std::string HUrl::escape(const std::string& str, const char* unescaped_chars) {
  45. std::string ostr;
  46. static char tab[] = "0123456789ABCDEF";
  47. const unsigned char* p = reinterpret_cast<const unsigned char*>(str.c_str());
  48. char szHex[4] = "%00";
  49. while (*p != '\0') {
  50. if (is_unambiguous(*p) || char_in_str(*p, unescaped_chars)) {
  51. ostr += *p;
  52. }
  53. else {
  54. szHex[1] = tab[*p >> 4];
  55. szHex[2] = tab[*p & 0xF];
  56. ostr += szHex;
  57. }
  58. ++p;
  59. }
  60. return ostr;
  61. }
  62. std::string HUrl::unescape(const std::string& str) {
  63. std::string ostr;
  64. const char* p = str.c_str();
  65. while (*p != '\0') {
  66. if (*p == '%' &&
  67. IS_HEX(p[1]) &&
  68. IS_HEX(p[2])) {
  69. ostr += ((hex2i(p[1]) << 4) | hex2i(p[2]));
  70. p += 3;
  71. }
  72. else {
  73. if (*p == '+') {
  74. ostr += ' ';
  75. } else {
  76. ostr += *p;
  77. }
  78. ++p;
  79. }
  80. }
  81. return ostr;
  82. }
  83. bool HUrl::parse(const std::string& url) {
  84. reset();
  85. this->url = url;
  86. hurl_t stURL;
  87. if (hv_parse_url(&stURL, url.c_str()) != 0) {
  88. return false;
  89. }
  90. int len = stURL.fields[HV_URL_SCHEME].len;
  91. if (len > 0) {
  92. scheme = url.substr(stURL.fields[HV_URL_SCHEME].off, len);
  93. }
  94. len = stURL.fields[HV_URL_USERNAME].len;
  95. if (len > 0) {
  96. username = url.substr(stURL.fields[HV_URL_USERNAME].off, len);
  97. len = stURL.fields[HV_URL_PASSWORD].len;
  98. if (len > 0) {
  99. password = url.substr(stURL.fields[HV_URL_PASSWORD].off, len);
  100. }
  101. }
  102. len = stURL.fields[HV_URL_HOST].len;
  103. if (len > 0) {
  104. host = url.substr(stURL.fields[HV_URL_HOST].off, len);
  105. }
  106. port = stURL.port;
  107. len = stURL.fields[HV_URL_PATH].len;
  108. if (len > 0) {
  109. path = url.substr(stURL.fields[HV_URL_PATH].off, len);
  110. } else {
  111. path = "/";
  112. }
  113. len = stURL.fields[HV_URL_QUERY].len;
  114. if (len > 0) {
  115. query = url.substr(stURL.fields[HV_URL_QUERY].off, len);
  116. }
  117. len = stURL.fields[HV_URL_FRAGMENT].len;
  118. if (len > 0) {
  119. fragment = url.substr(stURL.fields[HV_URL_FRAGMENT].off, len);
  120. }
  121. return true;
  122. }
  123. const std::string& HUrl::dump() {
  124. url.clear();
  125. // scheme://
  126. if (!scheme.empty()) {
  127. url += scheme;
  128. url += "://";
  129. }
  130. // user:pswd@
  131. if (!username.empty()) {
  132. url += username;
  133. if (!password.empty()) {
  134. url += ":";
  135. url += password;
  136. }
  137. url += "@";
  138. }
  139. // host:port
  140. if (!host.empty()) {
  141. url += host;
  142. if (port != 80 && port != 443) {
  143. char buf[16] = {0};
  144. snprintf(buf, sizeof(buf), ":%d", port);
  145. url += port;
  146. }
  147. }
  148. // /path
  149. if (!path.empty()) {
  150. url += path;
  151. }
  152. // ?query
  153. if (!query.empty()) {
  154. url += '?';
  155. url += query;
  156. }
  157. // #fragment
  158. if (!fragment.empty()) {
  159. url += '#';
  160. url += fragment;
  161. }
  162. return url;
  163. }