| 386 | // parse URLs: scheme:[//[user:password@]domain[:port]][/]path[?query][#fragment] |
| 387 | |
| 388 | void urlparse::set(const char *newurl) |
| 389 | { |
| 390 | DELSTRING(buf); |
| 391 | char *p, *s = buf = newstring(newurl, strlen(newurl) + 3); |
| 392 | scheme = userpassword = port = path = query = fragment = ""; |
| 393 | |
| 394 | filtertext(s, s, FTXT_NOWHITE); |
| 395 | if((p = strstr(s, "://"))) // domain part is mandatory, which makes the double slash a given - if a scheme is specified |
| 396 | { // scheme found |
| 397 | scheme = s; |
| 398 | *p = '\0'; |
| 399 | s = p + 3; |
| 400 | } |
| 401 | if((p = strchr(s, '@'))) |
| 402 | { // user:password found |
| 403 | userpassword = s; |
| 404 | *p = '\0'; |
| 405 | s = p + 1; |
| 406 | } |
| 407 | domain = s; |
| 408 | if(*s == '[' && (p = strchr(s, ']'))) s = p + 1; // [....] |
| 409 | else while(isalnum(*s) || *s == '.' || *s == '-') s++; // skip domain |
| 410 | if(*s == ':') |
| 411 | { // port found |
| 412 | *s = '\0'; |
| 413 | port = ++s; |
| 414 | while(isdigit(*s)) s++; |
| 415 | } |
| 416 | if(*s) |
| 417 | { |
| 418 | memmove(s + 2, s, strlen(s) + 1); |
| 419 | *s++ = '\0'; // end domain or port string |
| 420 | *s++ = '/'; // add mandatory slash for path |
| 421 | if(*s != '/') s--; |
| 422 | path = s; |
| 423 | if((p = strchr(s, '?'))) |
| 424 | { // query found |
| 425 | *p++ = '\0'; |
| 426 | query = s = p; |
| 427 | } |
| 428 | if((p = strchr(s, '#'))) |
| 429 | { // fragment found |
| 430 | *p++ = '\0'; |
| 431 | fragment = s = p; |
| 432 | } |
| 433 | // fix path, if necessary |
| 434 | size_t pl = strlen(path); |
| 435 | if(pl < 2) path = ""; // ignore single slash |
| 436 | else |
| 437 | { |
| 438 | p = (char *)path; |
| 439 | unixpath(p); |
| 440 | p += pl - 1; |
| 441 | if(*p == '/') *p = '\0'; // remove trailing slash |
| 442 | } |
| 443 | } |
| 444 | } |
no test coverage detected