blob: ec0981488352cbd46f3664f9d88c9e0143435f62 [file] [log] [blame]
Jerome Forissier359d4ed2024-10-16 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0+
2/* Copyright (C) 2024 Linaro Ltd. */
3
4#include <command.h>
5#include <console.h>
6#include <display_options.h>
7#include <efi_loader.h>
8#include <image.h>
9#include <lwip/apps/http_client.h>
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020010#include "lwip/altcp_tls.h"
Jerome Forissier359d4ed2024-10-16 12:04:09 +020011#include <lwip/timeouts.h>
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020012#include <rng.h>
Jerome Forissier359d4ed2024-10-16 12:04:09 +020013#include <mapmem.h>
14#include <net.h>
15#include <time.h>
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020016#include <dm/uclass.h>
Jerome Forissier359d4ed2024-10-16 12:04:09 +020017
Heinrich Schuchardtd064fc92024-11-08 18:45:26 +010018#define SERVER_NAME_SIZE 254
Jerome Forissier359d4ed2024-10-16 12:04:09 +020019#define HTTP_PORT_DEFAULT 80
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020020#define HTTPS_PORT_DEFAULT 443
Jerome Forissier359d4ed2024-10-16 12:04:09 +020021#define PROGRESS_PRINT_STEP_BYTES (100 * 1024)
22
23enum done_state {
Jerome Forissier97083502024-11-07 12:27:57 +010024 NOT_DONE = 0,
25 SUCCESS = 1,
26 FAILURE = 2
Jerome Forissier359d4ed2024-10-16 12:04:09 +020027};
28
29struct wget_ctx {
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -030030 char server_name[SERVER_NAME_SIZE];
31 u16 port;
Jerome Forissier359d4ed2024-10-16 12:04:09 +020032 char *path;
33 ulong daddr;
34 ulong saved_daddr;
35 ulong size;
36 ulong prevsize;
37 ulong start_time;
38 enum done_state done;
39};
40
Adriano Cordova25e88412024-11-11 18:09:01 -030041static void wget_lwip_fill_info(struct pbuf *hdr, u16_t hdr_len, u32_t hdr_cont_len)
42{
Adriano Cordova50a2a0b2024-11-26 13:19:21 -030043 if (wget_info->headers) {
44 if (hdr_len < MAX_HTTP_HEADERS_SIZE)
45 pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0);
46 else
47 hdr_len = 0;
48 wget_info->headers[hdr_len] = 0;
49 }
Adriano Cordova25e88412024-11-11 18:09:01 -030050 wget_info->hdr_cont_len = (u32)hdr_cont_len;
51}
52
53static void wget_lwip_set_file_size(u32_t rx_content_len)
54{
55 wget_info->file_size = (ulong)rx_content_len;
56}
57
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020058bool wget_validate_uri(char *uri);
59
60int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len,
61 size_t *olen)
62{
63 struct udevice *dev;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020064 int ret;
65
66 *olen = 0;
67
68 ret = uclass_get_device(UCLASS_RNG, 0, &dev);
69 if (ret) {
70 log_err("Failed to get an rng: %d\n", ret);
71 return ret;
72 }
Ilias Apalodimas4276a3c2024-11-14 16:29:15 +020073 ret = dm_rng_read(dev, output, len);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020074 if (ret)
75 return ret;
76
Ilias Apalodimas4276a3c2024-11-14 16:29:15 +020077 *olen = len;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020078
79 return 0;
80}
81
82static int parse_url(char *url, char *host, u16 *port, char **path,
83 bool *is_https)
Jerome Forissier359d4ed2024-10-16 12:04:09 +020084{
85 char *p, *pp;
86 long lport;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020087 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +020088
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020089 if (!wget_validate_uri(url)) {
90 log_err("Invalid URL. Use http(s)://\n");
91 return -EINVAL;
92 }
93
94 *is_https = false;
95 *port = HTTP_PORT_DEFAULT;
96 prefix_len = strlen("http://");
Jerome Forissier359d4ed2024-10-16 12:04:09 +020097 p = strstr(url, "http://");
98 if (!p) {
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020099 p = strstr(url, "https://");
100 prefix_len = strlen("https://");
101 *port = HTTPS_PORT_DEFAULT;
102 *is_https = true;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200103 }
104
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200105 p += prefix_len;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200106
107 /* Parse hostname */
108 pp = strchr(p, ':');
109 if (!pp)
110 pp = strchr(p, '/');
111 if (!pp)
112 return -EINVAL;
113
114 if (p + SERVER_NAME_SIZE <= pp)
115 return -EINVAL;
116
117 memcpy(host, p, pp - p);
118 host[pp - p] = '\0';
119
120 if (*pp == ':') {
121 /* Parse port number */
122 p = pp + 1;
123 lport = simple_strtol(p, &pp, 10);
124 if (pp && *pp != '/')
125 return -EINVAL;
126 if (lport > 65535)
127 return -EINVAL;
128 *port = (u16)lport;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200129 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200130
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200131 if (*pp != '/')
132 return -EINVAL;
133 *path = pp;
134
135 return 0;
136}
137
138/*
139 * Legacy syntax support
140 * Convert [<server_name_or_ip>:]filename into a URL if needed
141 */
142static int parse_legacy_arg(char *arg, char *nurl, size_t rem)
143{
144 char *p = nurl;
145 size_t n;
146 char *col = strchr(arg, ':');
147 char *env;
148 char *server;
149 char *path;
150
151 if (strstr(arg, "http") == arg) {
152 n = snprintf(nurl, rem, "%s", arg);
153 if (n < 0 || n > rem)
154 return -1;
155 return 0;
156 }
157
158 n = snprintf(p, rem, "%s", "http://");
159 if (n < 0 || n > rem)
160 return -1;
161 p += n;
162 rem -= n;
163
164 if (col) {
165 n = col - arg;
166 server = arg;
167 path = col + 1;
168 } else {
169 env = env_get("httpserverip");
170 if (!env)
171 env = env_get("serverip");
172 if (!env) {
173 log_err("error: httpserver/serverip has to be set\n");
174 return -1;
175 }
176 n = strlen(env);
177 server = env;
178 path = arg;
179 }
180
181 if (rem < n)
182 return -1;
Jerome Forissierc7157322024-11-26 15:45:06 +0100183 strncpy(p, server, n);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200184 p += n;
185 rem -= n;
186 if (rem < 1)
187 return -1;
188 *p = '/';
189 p++;
190 rem--;
191 n = strlen(path);
192 if (rem < n)
193 return -1;
Jerome Forissierc7157322024-11-26 15:45:06 +0100194 strncpy(p, path, n);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200195 p += n;
196 rem -= n;
197 if (rem < 1)
198 return -1;
199 *p = '\0';
200
201 return 0;
202}
203
204static err_t httpc_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf,
205 err_t err)
206{
207 struct wget_ctx *ctx = arg;
208 struct pbuf *buf;
209
210 if (!pbuf)
211 return ERR_BUF;
212
213 if (!ctx->start_time)
214 ctx->start_time = get_timer(0);
215
216 for (buf = pbuf; buf; buf = buf->next) {
217 memcpy((void *)ctx->daddr, buf->payload, buf->len);
218 ctx->daddr += buf->len;
219 ctx->size += buf->len;
220 if (ctx->size - ctx->prevsize > PROGRESS_PRINT_STEP_BYTES) {
221 printf("#");
222 ctx->prevsize = ctx->size;
223 }
224 }
225
226 altcp_recved(pcb, pbuf->tot_len);
227 pbuf_free(pbuf);
228 return ERR_OK;
229}
230
231static void httpc_result_cb(void *arg, httpc_result_t httpc_result,
232 u32_t rx_content_len, u32_t srv_res, err_t err)
233{
234 struct wget_ctx *ctx = arg;
235 ulong elapsed;
236
Adriano Cordova25e88412024-11-11 18:09:01 -0300237 wget_info->status_code = (u32)srv_res;
238
239 if (err == ERR_BUF) {
240 ctx->done = FAILURE;
241 return;
242 }
243
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200244 if (httpc_result != HTTPC_RESULT_OK) {
245 log_err("\nHTTP client error %d\n", httpc_result);
246 ctx->done = FAILURE;
247 return;
248 }
249 if (srv_res != 200) {
250 log_err("\nHTTP server error %d\n", srv_res);
251 ctx->done = FAILURE;
252 return;
253 }
254
255 elapsed = get_timer(ctx->start_time);
256 if (!elapsed)
257 elapsed = 1;
258 if (rx_content_len > PROGRESS_PRINT_STEP_BYTES)
259 printf("\n");
260 printf("%u bytes transferred in %lu ms (", rx_content_len, elapsed);
261 print_size(rx_content_len / elapsed * 1000, "/s)\n");
262 printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size);
Adriano Cordova93cba0f2024-12-04 00:05:23 -0300263 if (wget_info->set_bootdev)
264 efi_set_bootdev("Http", ctx->server_name, ctx->path, map_sysmem(ctx->saved_daddr, 0),
Adriano Cordova25e88412024-11-11 18:09:01 -0300265 rx_content_len);
Adriano Cordova25e88412024-11-11 18:09:01 -0300266 wget_lwip_set_file_size(rx_content_len);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200267 if (env_set_hex("filesize", rx_content_len) ||
268 env_set_hex("fileaddr", ctx->saved_daddr)) {
269 log_err("Could not set filesize or fileaddr\n");
270 ctx->done = FAILURE;
271 return;
272 }
273
274 ctx->done = SUCCESS;
275}
276
Adriano Cordova25e88412024-11-11 18:09:01 -0300277static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct pbuf *hdr,
278 u16_t hdr_len, u32_t content_len)
279{
280 wget_lwip_fill_info(hdr, hdr_len, content_len);
281
282 if (wget_info->check_buffer_size && (ulong)content_len > wget_info->buffer_size)
283 return ERR_BUF;
284
285 return ERR_OK;
286}
287
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100288#if CONFIG_IS_ENABLED(WGET_HTTPS)
289enum auth_mode {
290 AUTH_NONE,
291 AUTH_OPTIONAL,
292 AUTH_REQUIRED,
293};
294
295static char *cacert;
296static size_t cacert_size;
297static enum auth_mode cacert_auth_mode = AUTH_OPTIONAL;
298#endif
299
300#if CONFIG_IS_ENABLED(WGET_CACERT)
301static int set_auth(enum auth_mode auth)
302{
303 cacert_auth_mode = auth;
304
305 return CMD_RET_SUCCESS;
306}
Jerome Forissier7231a692025-03-05 15:26:45 +0100307#endif
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100308
Jerome Forissier7231a692025-03-05 15:26:45 +0100309#if CONFIG_IS_ENABLED(WGET_BUILTIN_CACERT)
310extern const char builtin_cacert[];
311extern const size_t builtin_cacert_size;
312static bool cacert_initialized;
313#endif
314
315#if CONFIG_IS_ENABLED(WGET_CACERT) || CONFIG_IS_ENABLED(WGET_BUILTIN_CACERT)
316static int _set_cacert(const void *addr, size_t sz)
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100317{
318 mbedtls_x509_crt crt;
Jerome Forissier7231a692025-03-05 15:26:45 +0100319 void *p;
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100320 int ret;
321
322 if (cacert)
323 free(cacert);
324
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100325 if (!addr) {
326 cacert = NULL;
327 cacert_size = 0;
328 return CMD_RET_SUCCESS;
329 }
330
Jerome Forissier7231a692025-03-05 15:26:45 +0100331 p = malloc(sz);
332 if (!p)
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100333 return CMD_RET_FAILURE;
Jerome Forissier7231a692025-03-05 15:26:45 +0100334 cacert = p;
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100335 cacert_size = sz;
336
337 memcpy(cacert, (void *)addr, sz);
338
339 mbedtls_x509_crt_init(&crt);
340 ret = mbedtls_x509_crt_parse(&crt, cacert, cacert_size);
341 if (ret) {
342 printf("Could not parse certificates (%d)\n", ret);
343 free(cacert);
344 cacert = NULL;
345 cacert_size = 0;
346 return CMD_RET_FAILURE;
347 }
348
Jerome Forissier7231a692025-03-05 15:26:45 +0100349#if CONFIG_IS_ENABLED(WGET_BUILTIN_CACERT)
350 cacert_initialized = true;
351#endif
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100352 return CMD_RET_SUCCESS;
353}
Jerome Forissier7231a692025-03-05 15:26:45 +0100354
355#if CONFIG_IS_ENABLED(WGET_BUILTIN_CACERT)
356static int set_cacert_builtin(void)
357{
358 return _set_cacert(builtin_cacert, builtin_cacert_size);
359}
360#endif
361
362#if CONFIG_IS_ENABLED(WGET_CACERT)
363static int set_cacert(char * const saddr, char * const ssz)
364{
365 ulong addr, sz;
366
367 addr = hextoul(saddr, NULL);
368 sz = hextoul(ssz, NULL);
369
370 return _set_cacert((void *)addr, sz);
371}
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100372#endif
Jerome Forissier7231a692025-03-05 15:26:45 +0100373#endif /* CONFIG_WGET_CACERT || CONFIG_WGET_BUILTIN_CACERT */
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100374
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200375static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
376{
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100377#if CONFIG_IS_ENABLED(WGET_HTTPS)
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200378 altcp_allocator_t tls_allocator;
379#endif
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200380 httpc_connection_t conn;
381 httpc_state_t *state;
382 struct netif *netif;
383 struct wget_ctx ctx;
384 char *path;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200385 bool is_https;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200386
387 ctx.daddr = dst_addr;
388 ctx.saved_daddr = dst_addr;
389 ctx.done = NOT_DONE;
390 ctx.size = 0;
391 ctx.prevsize = 0;
392 ctx.start_time = 0;
393
Adriano Cordova484ade32024-12-03 09:55:34 -0300394 if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200395 return CMD_RET_USAGE;
396
397 netif = net_lwip_new_netif(udev);
398 if (!netif)
399 return -1;
400
401 memset(&conn, 0, sizeof(conn));
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100402#if CONFIG_IS_ENABLED(WGET_HTTPS)
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200403 if (is_https) {
Jerome Forissier7231a692025-03-05 15:26:45 +0100404 char *ca;
405 size_t ca_sz;
406
407#if CONFIG_IS_ENABLED(WGET_BUILTIN_CACERT)
408 if (!cacert_initialized)
409 set_cacert_builtin();
410#endif
411 ca = cacert;
412 ca_sz = cacert_size;
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100413
414 if (cacert_auth_mode == AUTH_REQUIRED) {
415 if (!ca || !ca_sz) {
416 printf("Error: cacert authentication mode is "
417 "'required' but no CA certificates "
418 "given\n");
419 return CMD_RET_FAILURE;
420 }
421 } else if (cacert_auth_mode == AUTH_NONE) {
422 ca = NULL;
423 ca_sz = 0;
424 } else if (cacert_auth_mode == AUTH_OPTIONAL) {
425 /*
426 * Nothing to do, this is the default behavior of
427 * altcp_tls to check server certificates against CA
428 * certificates when the latter are provided and proceed
429 * with no verification if not.
430 */
431 }
432
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200433 tls_allocator.alloc = &altcp_tls_alloc;
434 tls_allocator.arg =
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100435 altcp_tls_create_config_client(ca, ca_sz,
436 ctx.server_name);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200437
438 if (!tls_allocator.arg) {
439 log_err("error: Cannot create a TLS connection\n");
440 net_lwip_remove_netif(netif);
441 return -1;
442 }
443
444 conn.altcp_allocator = &tls_allocator;
445 }
446#endif
447
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200448 conn.result_fn = httpc_result_cb;
Adriano Cordova25e88412024-11-11 18:09:01 -0300449 conn.headers_done_fn = httpc_headers_done_cb;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200450 ctx.path = path;
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300451 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200452 &ctx, &state)) {
453 net_lwip_remove_netif(netif);
454 return CMD_RET_FAILURE;
455 }
456
457 while (!ctx.done) {
458 net_lwip_rx(udev, netif);
459 sys_check_timeouts();
460 if (ctrlc())
461 break;
462 }
463
464 net_lwip_remove_netif(netif);
465
466 if (ctx.done == SUCCESS)
467 return 0;
468
469 return -1;
470}
471
Adriano Cordovab479fc42024-12-04 00:05:16 -0300472int wget_do_request(ulong dst_addr, char *uri)
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200473{
Jerome Forissier37829282025-01-30 09:22:20 +0100474 net_lwip_set_current();
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200475
Adriano Cordova25e88412024-11-11 18:09:01 -0300476 if (!wget_info)
477 wget_info = &default_wget_info;
478
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200479 return wget_loop(eth_get_dev(), dst_addr, uri);
480}
481
482int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
483{
484 char *end;
485 char *url;
486 ulong dst_addr;
487 char nurl[1024];
488
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100489#if CONFIG_IS_ENABLED(WGET_CACERT)
490 if (argc == 4 && !strncmp(argv[1], "cacert", strlen("cacert")))
491 return set_cacert(argv[2], argv[3]);
492 if (argc == 3 && !strncmp(argv[1], "cacert", strlen("cacert"))) {
Jerome Forissier7231a692025-03-05 15:26:45 +0100493#if CONFIG_IS_ENABLED(WGET_BUILTIN_CACERT)
494 if (!strncmp(argv[2], "builtin", strlen("builtin")))
495 return set_cacert_builtin();
496#endif
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100497 if (!strncmp(argv[2], "none", strlen("none")))
498 return set_auth(AUTH_NONE);
499 if (!strncmp(argv[2], "optional", strlen("optional")))
500 return set_auth(AUTH_OPTIONAL);
501 if (!strncmp(argv[2], "required", strlen("required")))
502 return set_auth(AUTH_REQUIRED);
503 return CMD_RET_USAGE;
504 }
505#endif
506
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200507 if (argc < 2 || argc > 3)
508 return CMD_RET_USAGE;
509
510 dst_addr = hextoul(argv[1], &end);
Jerome Forissier97083502024-11-07 12:27:57 +0100511 if (end == (argv[1] + strlen(argv[1]))) {
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200512 if (argc < 3)
513 return CMD_RET_USAGE;
514 url = argv[2];
515 } else {
516 dst_addr = image_load_addr;
517 url = argv[1];
518 }
519
520 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
Jerome Forissier97083502024-11-07 12:27:57 +0100521 return CMD_RET_FAILURE;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200522
Adriano Cordova25e88412024-11-11 18:09:01 -0300523 wget_info = &default_wget_info;
Adriano Cordovab479fc42024-12-04 00:05:16 -0300524 if (wget_do_request(dst_addr, nurl))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200525 return CMD_RET_FAILURE;
526
527 return CMD_RET_SUCCESS;
528}
529
530/**
531 * wget_validate_uri() - validate the uri for wget
532 *
533 * @uri: uri string
534 *
535 * This function follows the current U-Boot wget implementation.
536 * scheme: only "http:" is supported
537 * authority:
538 * - user information: not supported
539 * - host: supported
540 * - port: not supported(always use the default port)
541 *
542 * Uri is expected to be correctly percent encoded.
543 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
544 * and space character(0x20) are not allowed.
545 *
546 * TODO: stricter uri conformance check
547 *
548 * Return: true on success, false on failure
549 */
550bool wget_validate_uri(char *uri)
551{
552 char c;
553 bool ret = true;
554 char *str_copy, *s, *authority;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200555 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200556
557 for (c = 0x1; c < 0x21; c++) {
558 if (strchr(uri, c)) {
559 log_err("invalid character is used\n");
560 return false;
561 }
562 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200563
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200564 if (strchr(uri, 0x7f)) {
565 log_err("invalid character is used\n");
566 return false;
567 }
568
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200569 if (!strncmp(uri, "http://", strlen("http://"))) {
570 prefix_len = strlen("http://");
Jerome Forissier42129542025-02-04 17:00:49 +0100571 } else if (CONFIG_IS_ENABLED(WGET_HTTPS)) {
572 if (!strncmp(uri, "https://", strlen("https://"))) {
573 prefix_len = strlen("https://");
574 } else {
575 log_err("only http(s):// is supported\n");
576 return false;
577 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200578 } else {
Jerome Forissier42129542025-02-04 17:00:49 +0100579 log_err("only http:// is supported\n");
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200580 return false;
581 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200582
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200583 str_copy = strdup(uri);
584 if (!str_copy)
585 return false;
586
587 s = str_copy + strlen("http://");
588 authority = strsep(&s, "/");
589 if (!s) {
590 log_err("invalid uri, no file path\n");
591 ret = false;
592 goto out;
593 }
594 s = strchr(authority, '@');
595 if (s) {
596 log_err("user information is not supported\n");
597 ret = false;
598 goto out;
599 }
600
601out:
602 free(str_copy);
603
604 return ret;
605}