Set the location in a URL, accepting either a string or a tuple of .
| 351 | |
| 352 | /// Set the location in a URL, accepting either a string or a tuple of <host, port>. |
| 353 | void |
| 354 | URL_Loc_Set(Context &ctx, Expr &expr, ts::URL &url) |
| 355 | { |
| 356 | auto value = ctx.extract(expr); |
| 357 | TextView host_token; |
| 358 | int port = -1; // if still -1 after parsing, the parsing failed. |
| 359 | if (auto loc = std::get_if<IndexFor(STRING)>(&value); nullptr != loc) { |
| 360 | // split the string to get the pieces. |
| 361 | Loc_String_Parse(*loc, host_token, port); |
| 362 | } else if (auto t = std::get_if<IndexFor(TUPLE)>(&value); nullptr != t) { |
| 363 | // Must be host name, then port. |
| 364 | if (t->size() > 0) { |
| 365 | if (auto &f0 = (*t)[0]; f0.index() == IndexFor(STRING)) { |
| 366 | host_token = std::get<IndexFor(STRING)>(f0); |
| 367 | if (t->size() > 1) { |
| 368 | auto &f1 = (*t)[1]; |
| 369 | if (is_empty(f1)) { |
| 370 | port = 0; // clear port. |
| 371 | } else { |
| 372 | port = f1.as_integer(-1); // try as integer, fail if not convertible. |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | if (0 <= port && port < std::numeric_limits<in_port_t>::max()) { |
| 379 | url.host_set(host_token); |
| 380 | url.port_set(port); // if @a port is 0 then it will be removed from the URL. |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /// Set the location in the Host field and URL (if needed). |
| 385 | void |
no test coverage detected