This implementation is faster than http_parser_parse_url() and allows ignoring of scheme("http://")
| 156 | // This implementation is faster than http_parser_parse_url() and allows |
| 157 | // ignoring of scheme("http://") |
| 158 | int URI::SetHttpURL(const char* url) { |
| 159 | Clear(); |
| 160 | |
| 161 | const char* p = url; |
| 162 | // skip heading blanks |
| 163 | if (*p == ' ') { |
| 164 | for (++p; *p == ' '; ++p) {} |
| 165 | } |
| 166 | const char* start = p; |
| 167 | // Find end of host, locate scheme and user_info during the searching |
| 168 | bool need_scheme = true; |
| 169 | bool need_user_info = true; |
| 170 | for (; true; ++p) { |
| 171 | const char action = g_url_parsing_fast_action_map[(int)*p]; |
| 172 | if (action == URI_PARSE_CONTINUE) { |
| 173 | continue; |
| 174 | } |
| 175 | if (action == URI_PARSE_BREAK) { |
| 176 | break; |
| 177 | } |
| 178 | if (!is_valid_char(*p)) { |
| 179 | _st.set_error(EINVAL, "invalid character in url"); |
| 180 | return -1; |
| 181 | } else if (*p == ':') { |
| 182 | if (p[1] == '/' && p[2] == '/' && need_scheme) { |
| 183 | need_scheme = false; |
| 184 | _scheme.assign(start, p - start); |
| 185 | p += 2; |
| 186 | start = p + 1; |
| 187 | } |
| 188 | } else if (*p == '@') { |
| 189 | if (need_user_info) { |
| 190 | need_user_info = false; |
| 191 | _user_info.assign(start, p - start); |
| 192 | start = p + 1; |
| 193 | } |
| 194 | } else if (*p == ' ') { |
| 195 | if (!is_all_spaces(p + 1)) { |
| 196 | _st.set_error(EINVAL, "Invalid space in url"); |
| 197 | return -1; |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | const char* host_end = SplitHostAndPort(start, p, &_port); |
| 203 | _host.assign(start, host_end - start); |
| 204 | if (*p == '/') { |
| 205 | start = p; //slash pointed by p is counted into _path |
| 206 | ++p; |
| 207 | for (; *p && *p != '?' && *p != '#'; ++p) { |
| 208 | if (*p == ' ') { |
| 209 | if (!is_all_spaces(p + 1)) { |
| 210 | _st.set_error(EINVAL, "Invalid space in path"); |
| 211 | return -1; |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | } |