1
0

hurl.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. ostr += *p;
  74. ++p;
  75. }
  76. }
  77. return ostr;
  78. }
  79. bool HUrl::parse(const std::string& url) {
  80. reset();
  81. this->url = url;
  82. hurl_t stURL;
  83. if (hv_parse_url(&stURL, url.c_str()) != 0) {
  84. return false;
  85. }
  86. int len = stURL.fields[HV_URL_SCHEME].len;
  87. if (len > 0) {
  88. scheme = url.substr(stURL.fields[HV_URL_SCHEME].off, len);
  89. }
  90. len = stURL.fields[HV_URL_USERNAME].len;
  91. if (len > 0) {
  92. username = url.substr(stURL.fields[HV_URL_USERNAME].off, len);
  93. len = stURL.fields[HV_URL_PASSWORD].len;
  94. if (len > 0) {
  95. password = url.substr(stURL.fields[HV_URL_PASSWORD].off, len);
  96. }
  97. }
  98. len = stURL.fields[HV_URL_HOST].len;
  99. if (len > 0) {
  100. host = url.substr(stURL.fields[HV_URL_HOST].off, len);
  101. }
  102. port = stURL.port;
  103. len = stURL.fields[HV_URL_PATH].len;
  104. if (len > 0) {
  105. path = url.substr(stURL.fields[HV_URL_PATH].off, len);
  106. } else {
  107. path = "/";
  108. }
  109. len = stURL.fields[HV_URL_QUERY].len;
  110. if (len > 0) {
  111. query = url.substr(stURL.fields[HV_URL_QUERY].off, len);
  112. }
  113. len = stURL.fields[HV_URL_FRAGMENT].len;
  114. if (len > 0) {
  115. fragment = url.substr(stURL.fields[HV_URL_FRAGMENT].off, len);
  116. }
  117. return true;
  118. }
  119. const std::string& HUrl::dump() {
  120. url.clear();
  121. // scheme://
  122. if (!scheme.empty()) {
  123. url += scheme;
  124. url += "://";
  125. }
  126. // user:pswd@
  127. if (!username.empty()) {
  128. url += username;
  129. if (!password.empty()) {
  130. url += ":";
  131. url += password;
  132. }
  133. url += "@";
  134. }
  135. // host:port
  136. if (!host.empty()) {
  137. url += host;
  138. if (port != 80 && port != 443) {
  139. char buf[16] = {0};
  140. snprintf(buf, sizeof(buf), ":%d", port);
  141. url += port;
  142. }
  143. }
  144. // /path
  145. if (!path.empty()) {
  146. url += path;
  147. }
  148. // ?query
  149. if (!query.empty()) {
  150. url += '?';
  151. url += query;
  152. }
  153. // #fragment
  154. if (!fragment.empty()) {
  155. url += '#';
  156. url += fragment;
  157. }
  158. return url;
  159. }