/*
 * lws-minimal-http-client-multi
 *
 * Written in 2010-2021 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 *
 * This demonstrates the a minimal http client using lws, which makes
 * 8 downloads simultaneously from warmcat.com.
 *
 * Currently that takes the form of 8 individual simultaneous tcp and
 * tls connections, which happen concurrently.  Notice that the ordering
 * of the returned payload may be intermingled for the various connections.
 *
 * By default the connections happen all together at the beginning and operate
 * concurrently, which is fast.  However this is resource-intenstive, there are
 * 8 tcp connections, 8 tls tunnels on both the client and server.  You can
 * instead opt to have the connections happen one after the other inside a
 * single tcp connection and tls tunnel, using HTTP/1.1 pipelining.  To be
 * eligible to be pipelined on another existing connection to the same server,
 * the client connection must have the LCCSCF_PIPELINE flag on its
 * info.ssl_connection member (this is independent of whether the connection
 * is in ssl mode or not).
 *
 * HTTP/1.0: Pipelining only possible if Keep-Alive: yes sent by server
 * HTTP/1.1: always possible... serializes requests
 * HTTP/2:   always possible... all requests sent as individual streams in parallel
 *
 * Note: stats are kept on tls session reuse and checked depending on mode
 *
 *  - default: no reuse expected (connections made too quickly at once)
 *  - staggered, no pipeline: n - 1 reuse expected
 *  - staggered, pipelined: no reuse expected
 */

#include <libwebsockets.h>

enum {
	LWS_SW_CONMON,
	LWS_SW_EV,
	LWS_SW_EVENT,
	LWS_SW_GLIB,
	LWS_SW_LIMIT,
	LWS_SW_NO_TLS,
	LWS_SW_NO_TLS_SESSION_REUSE,
	LWS_SW_PATH,
	LWS_SW_PORT,
	LWS_SW_POST,
	LWS_SW_SERVER,
	LWS_SW_SSL_HANDSHAKE_SERIALIZE,
	LWS_SW_UV,
	LWS_SW_C,
	LWS_SW_L,
	LWS_SW_N,
	LWS_SW_P,
	LWS_SW_S,
	LWS_SW_OUTDIR,
	LWS_SW_SAVE_TICKET,
	LWS_SW_LOAD_TICKET,
	LWS_SW_SEQ,
	LWS_SW_QUIC_INITIAL_CWND,
	LWS_SW_HELP,
};

static const struct lws_switches switches[] = {
	[LWS_SW_CONMON]	= { "--conmon",        "Enable --conmon feature" },
	[LWS_SW_EV]	= { "--ev",            "Enable --ev feature" },
	[LWS_SW_EVENT]	= { "--event",         "Enable --event feature" },
	[LWS_SW_GLIB]	= { "--glib",          "Enable --glib feature" },
	[LWS_SW_LIMIT]	= { "--limit",         "Enable --limit feature" },
	[LWS_SW_NO_TLS]	= { "--no-tls",        "Enable --no-tls feature" },
	[LWS_SW_NO_TLS_SESSION_REUSE]	= { "--no-tls-session-reuse", "Enable --no-tls-session-reuse feature" },
	[LWS_SW_PATH]	= { "--path",          "Enable --path feature" },
	[LWS_SW_PORT]	= { "--port",          "Port to connect or listen on" },
	[LWS_SW_POST]	= { "--post",          "Enable --post feature" },
	[LWS_SW_SERVER]	= { "--server",        "Server address to connect to" },
	[LWS_SW_SSL_HANDSHAKE_SERIALIZE]	= { "--ssl-handshake-serialize", "Enable --ssl-handshake-serialize feature" },
	[LWS_SW_UV]	= { "--uv",            "Enable --uv feature" },
	[LWS_SW_C]	= { "-c",              "Client connections" },
	[LWS_SW_L]	= { "-l",              "Enable -l feature" },
	[LWS_SW_N]	= { "-n",              "Enable -n feature" },
	[LWS_SW_P]	= { "-p",              "Port number to listen or connect on" },
	[LWS_SW_S]	= { "-s",              "Use TLS / https" },
	[LWS_SW_OUTDIR]	= { "-O",              "Output directory for downloads" },
	[LWS_SW_SAVE_TICKET] = { "--save-ticket", "Path to save TLS session ticket" },
	[LWS_SW_LOAD_TICKET] = { "--load-ticket", "Path to load TLS session ticket" },
	[LWS_SW_SEQ]         = { "--seq", "Run sequentially" },
	[LWS_SW_QUIC_INITIAL_CWND] = { "--quic-initial-cwnd", "Initial congestion window in bytes" },
	[LWS_SW_HELP]	= { "--help", 		"Show this help information" },
};

#include <string.h>
#include <signal.h>
#include <assert.h>
#include <time.h>
#if !defined(WIN32)
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif

#define COUNT 4096

struct cliuser {
	int index;
};

static int completed, failed, stagger_idx, posting, count = COUNT,
#if defined(LWS_WITH_TLS_SESSIONS)
	   reuse,
#endif
	   staggered, sequential;
static lws_sorted_usec_list_t sul_stagger;
static struct lws_client_connect_info i;
static struct lws *client_wsi[COUNT];
static int conn_state[COUNT];

static void
lws_try_client_connection(struct lws_client_connect_info *ii, int m);
static char intr;
static struct lws_context *context;

/* we only need this for tracking POST emit state */

static char out_dir[256];
static char save_ticket[256];
static char load_ticket[256];

struct req {
	char path[512];
	char address[256];
	int port;
};
static struct req reqs[COUNT];

struct pss {
	char body_part;
	int fd;
};

#if defined(LWS_WITH_TLS_SESSIONS) && !defined(LWS_WITH_MBEDTLS) && !defined(WIN32)

/* this should work OK on win32, but not adapted for non-posix file apis */

static int
sess_save_cb(struct lws_context *cx, struct lws_tls_session_dump *info)
{
	int fd, n;

	fd = open(save_ticket, LWS_O_WRONLY | O_CREAT | O_TRUNC, 0600);
	if (fd < 0) {
		lwsl_warn("%s: cannot open %s\n", __func__, save_ticket);
		return 1;
	}

	n = (int)write(fd, info->blob, info->blob_len);
	close(fd);

	return n != (int)info->blob_len;
}

static int
sess_load_cb(struct lws_context *cx, struct lws_tls_session_dump *info)
{
	struct stat sta;
	int fd, n;

	fd = open(load_ticket, LWS_O_RDONLY);
	if (fd < 0)
		return 1;

	if (fstat(fd, &sta) || !sta.st_size)
		goto bail;

	info->blob = malloc((size_t)sta.st_size);
	if (!info->blob)
		goto bail;

	info->blob_len = (size_t)sta.st_size;
	n = (int)read(fd, info->blob, info->blob_len);
	close(fd);

	return n != (int)info->blob_len;

bail:
	close(fd);
	return 1;
}
#endif

#if defined(LWS_WITH_CONMON)
void
dump_conmon_data(struct lws *wsi)
{
	const struct addrinfo *ai;
	struct lws_conmon cm;
	char ads[48];

	lws_conmon_wsi_take(wsi, &cm);

	lws_sa46_write_numeric_address(&cm.peer46, ads, sizeof(ads));
	lwsl_notice("%s: peer %s, dns: %uus, sockconn: %uus, tls: %uus, txn_resp: %uus\n",
		    __func__, ads,
		    (unsigned int)cm.ciu_dns,
		    (unsigned int)cm.ciu_sockconn,
		    (unsigned int)cm.ciu_tls,
		    (unsigned int)cm.ciu_txn_resp);

	ai = cm.dns_results_copy;
	while (ai) {
		lws_sa46_write_numeric_address((lws_sockaddr46 *)ai->ai_addr, ads, sizeof(ads));
		lwsl_notice("%s: DNS %s\n", __func__, ads);
		ai = ai->ai_next;
	}

	/*
	 * This destroys the DNS list in the lws_conmon that we took
	 * responsibility for when we used lws_conmon_wsi_take()
	 */

	lws_conmon_release(&cm);
}
#endif

static int
callback_http(struct lws *wsi, enum lws_callback_reasons reason,
	      void *user, void *in, size_t len)
{
	char buf[LWS_PRE + 1024], *start = &buf[LWS_PRE], *p = start,
	     *end = &buf[sizeof(buf) - 1];
	int n, idx = (int)(intptr_t)lws_get_opaque_user_data(wsi);
	struct pss *pss = (struct pss *)user;

	switch (reason) {

	case LWS_CALLBACK_CLIENT_ESTABLISHED_EARLY:
		lwsl_user("LWS_CALLBACK_CLIENT_ESTABLISHED_EARLY: idx: %d, opting into 0-RTT\n", idx);
		return 1;

	case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
		lwsl_user("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: idx: %d, resp %u\n",
				idx, lws_http_client_http_response(wsi));
		
		if (out_dir[0] && lws_http_client_http_response(wsi) == 200) {
			char path[512];
			const char *basename = reqs[idx].path;
			const char *p = strrchr(basename, '/');
			if (p) basename = p + 1;
			if (!basename[0]) basename = "index.html";
			
			lws_snprintf(path, sizeof(path), "%s/%s", out_dir, basename);
			pss->fd = open(path, LWS_O_WRONLY | O_CREAT | O_TRUNC, 0644); /* NOSONAR */
			if (pss->fd < 0) {
				lwsl_err("Failed to open %s for writing\n", path);
			} else {
				lwsl_user("Opened %s for writing\n", path);
			}
		} else {
			pss->fd = -1;
		}

#if defined(LWS_WITH_TLS_SESSIONS) && !defined(LWS_WITH_MBEDTLS) && !defined(WIN32)
		if (lws_tls_session_is_reused(wsi))
			reuse++;
		/*
		 * Don't attempt to save the session here: the NewSessionTicket
		 * arrives asynchronously AFTER this callback fires, so the
		 * vhost session cache is empty or has only a stub at this
		 * point.  The deferred save happens after the event loop exits.
		 */
#endif
		break;

	/* because we are protocols[0] ... */
	case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
		lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
			 in ? (char *)in : "(null)");
		client_wsi[idx] = NULL;
		if (conn_state[idx] == 2)
			break;
		conn_state[idx] = 2;
		failed++;

#if defined(LWS_WITH_CONMON)
		dump_conmon_data(wsi);
#endif

		goto finished;

	/* chunks of chunked content, with header removed */
	case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
		lwsl_user("RECEIVE_CLIENT_HTTP_READ: conn %d: read %d\n", idx, (int)len);
		if (!pss)
			break;
		if (pss->fd >= 0) {
			if (write(pss->fd, in, (unsigned int)len) != (ssize_t)len)
				lwsl_err("Write failed\n");
		}
		return 0; /* don't passthru */

	case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:

		/*
		 * Tell lws we are going to send the body next...
		 */
		if (posting && !lws_http_is_redirected_to_get(wsi)) {
			lwsl_user("%s: conn %d, doing POST flow\n", __func__, idx);
			lws_client_http_body_pending(wsi, 1);
			lws_callback_on_writable(wsi);
		} else
			lwsl_user("%s: conn %d, doing GET flow\n", __func__, idx);
		break;

	/* uninterpreted http content */
	case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
		{
			char buffer[1024 + LWS_PRE];
			char *px = buffer + LWS_PRE;
			int lenx = sizeof(buffer) - LWS_PRE;

			if (lws_http_client_read(wsi, &px, &lenx) < 0)
				return -1;
		}
		return 0; /* don't passthru */

	case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
		lwsl_user("LWS_CALLBACK_COMPLETED_CLIENT_HTTP %s: idx %d\n",
			  lws_wsi_tag(wsi), idx);
		if (!pss)
			break;
		if (pss->fd >= 0) {
			close(pss->fd);
			pss->fd = -1;
		}
		client_wsi[idx] = NULL;
		if (conn_state[idx] == 2)
			break;
		conn_state[idx] = 2;
		goto finished;

	case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
		lwsl_info("%s: closed: %s\n", __func__, lws_wsi_tag(wsi));

		if (!pss)
			break;

		if (pss->fd >= 0) {
			close(pss->fd);
			pss->fd = -1;
		}

#if defined(LWS_WITH_CONMON)
		dump_conmon_data(wsi);
#endif

		if (conn_state[idx] != 2) {
			/*
			 * If it completed normally, it will have been set to
			 * 2 then already.  So we are dealing with an
			 * abnormal, failing, close
			 */
			client_wsi[idx] = NULL;
			conn_state[idx] = 2;
			failed++;
			goto finished;
		}
		break;

	case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE:
		if (!posting)
			break;
		if (lws_http_is_redirected_to_get(wsi))
			break;
		lwsl_info("LWS_CALLBACK_CLIENT_HTTP_WRITEABLE: %s, idx %d,"
				" part %d\n", lws_wsi_tag(wsi), idx, pss->body_part);

		n = LWS_WRITE_HTTP;

		/*
		 * For a small body like this, we could prepare it in memory and
		 * send it all at once.  But to show how to handle, eg,
		 * arbitrary-sized file payloads, or huge form-data fields, the
		 * sending is done in multiple passes through the event loop.
		 */

		switch (pss->body_part++) {
		case 0:
			if (lws_client_http_multipart(wsi, "text", NULL, NULL,
						      &p, end))
				return -1;
			/* notice every usage of the boundary starts with -- */
			p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "my text field\xd\xa");
			break;
		case 1:
			if (lws_client_http_multipart(wsi, "file", "myfile.txt",
						      "text/plain", &p, end))
				return -1;
			p += lws_snprintf(p, lws_ptr_diff_size_t(end, p),
					"This is the contents of the "
					"uploaded file.\xd\xa"
					"\xd\xa");
			break;
		case 2:
			if (lws_client_http_multipart(wsi, NULL, NULL, NULL,
						      &p, end))
				return -1;
			lws_client_http_body_pending(wsi, 0);
			 /* necessary to support H2, it means we will write no
			  * more on this stream */
			n = LWS_WRITE_HTTP_FINAL;
			break;

		default:
			/*
			 * We can get extra callbacks here, if nothing to do,
			 * then do nothing.
			 */
			return 0;
		}

		if (lws_write(wsi, (uint8_t *)start, lws_ptr_diff_size_t(p, start), (enum lws_write_protocol)n)
				!= lws_ptr_diff(p, start))
			return 1;

		if (n != LWS_WRITE_HTTP_FINAL)
			lws_callback_on_writable(wsi);

		break;

	default:
		break;
	}

	return lws_callback_http_dummy(wsi, reason, user, in, len);

finished:
	if (++completed == count) {
		if (!failed)
			lwsl_user("Done: all OK\n");
		else
			lwsl_err("Done: failed: %d\n", failed);
		intr = 1;
		/*
		 * Exit the loop by cancelling service so we can save TLS
		 * sessions in main() before actually destroying the context.
		 */
		lws_cancel_service(lws_get_context(wsi));
	} else if (sequential) {
		lws_try_client_connection(&i, completed);
	}

	return 0;
}

static const struct lws_protocols protocols[] = {
	{ "http", callback_http, sizeof(struct pss), 0, 0, NULL, 0 },
	LWS_PROTOCOL_LIST_TERM
};

#if defined(LWS_WITH_SYS_METRICS)

static int
my_metric_report(lws_metric_pub_t *mp)
{
	lws_metric_bucket_t *sub = mp->u.hist.head;
	char buf[192];

	do {
		if (lws_metrics_format(mp, &sub, buf, sizeof(buf)))
			lwsl_user("%s: %s\n", __func__, buf);
	} while ((mp->flags & LWSMTFL_REPORT_HIST) && sub);

	/* 0 = leave metric to accumulate, 1 = reset the metric */

	return 1;
}

static lws_system_ops_t system_ops = {
	.metric_report = my_metric_report,
};

#endif

static void
stagger_cb(lws_sorted_usec_list_t *sul);

static void
lws_try_client_connection(struct lws_client_connect_info *ii, int m)
{
	ii->address = reqs[m].address;
	ii->host = reqs[m].address;
	ii->origin = reqs[m].address;
	ii->port = reqs[m].port;
	ii->path = reqs[m].path;

	ii->pwsi = &client_wsi[m];
	ii->opaque_user_data = (void *)(intptr_t)m;

	if (!lws_client_connect_via_info(ii)) {
		if (conn_state[m] != 2) {
			conn_state[m] = 2;
			failed++;
			lwsl_user("%s: failed: conn idx %d\n", __func__, m);
			if (++completed == count) {
				lwsl_user("Done: failed: %d\n", failed);
				intr = 1;
				lws_cancel_service(context);
			}
		}
	} else {
		if (conn_state[m] != 2)
			conn_state[m] = 1;
		lwsl_user("started connection %s: idx %d (%s)\n",
			  lws_wsi_tag(client_wsi[m]), m, ii->path);
	}
}


static int
system_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
		   int current, int target)
{
	struct lws_context *cx = mgr->parent;
	int m;

	if (current != LWS_SYSTATE_OPERATIONAL || target != LWS_SYSTATE_OPERATIONAL)
		return 0;

	/* all the system prerequisites are ready */

	if (sequential)
		lws_try_client_connection(&i, 0);
	else if (!staggered)
		/*
		 * just pile on all the connections at once, testing the
		 * pipeline queuing before the first is connected
		 */
		for (m = 0; m < count; m++)
			lws_try_client_connection(&i, m);
	else
		/*
		 * delay the connections slightly
		 */
		lws_sul_schedule(cx, 0, &sul_stagger, stagger_cb,
				 50 * LWS_US_PER_MS);

	return 0;
}

static void
signal_cb(void *handle, int signum)
{
	switch (signum) {
	case SIGTERM:
	case SIGINT:
		break;
	default:
		lwsl_err("%s: signal %d\n", __func__, signum);
		break;
	}
	intr = 1;
	lws_cancel_service(context);
}

static void
sigint_handler(int sig)
{
	signal_cb(NULL, sig);
}

#if defined(WIN32)
int gettimeofday(struct timeval * tp, struct timezone * tzp)
{
    // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
    // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
    // until 00:00:00 January 1, 1970 
    static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);

    SYSTEMTIME  system_time;
    FILETIME    file_time;
    uint64_t    time;

    GetSystemTime( &system_time );
    SystemTimeToFileTime( &system_time, &file_time );
    time =  ((uint64_t)file_time.dwLowDateTime )      ;
    time += ((uint64_t)file_time.dwHighDateTime) << 32;

    tp->tv_sec  = (long) ((time - EPOCH) / 10000000L);
    tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
    return 0;
}
#endif

unsigned long long us(void)
{
	struct timeval t;

	gettimeofday(&t, NULL);

	return ((unsigned long long)t.tv_sec * 1000000ull) + (unsigned long long)t.tv_usec;
}

static void
stagger_cb(lws_sorted_usec_list_t *sul)
{
	lws_usec_t next;

	/*
	 * open the connections at 100ms intervals, with the
	 * last one being after 1s, testing both queuing, and
	 * direct H2 stream addition stability
	 */
	lws_try_client_connection(&i, stagger_idx++);

	if (stagger_idx == count)
		return;

	if (count > 100) next = 1 * LWS_US_PER_MS; else next = 150 * LWS_US_PER_MS;
	if (stagger_idx == count - 1)
		next += 400 * LWS_US_PER_MS;

#if defined(LWS_WITH_TLS_SESSIONS)
	if (stagger_idx == 1)
		next += 600 * LWS_US_PER_MS;
#endif

	lws_sul_schedule(context, 0, &sul_stagger, stagger_cb, next);
}

static int custom_cpd_bypass(struct lws_context *cx) {
        lws_system_cpd_set(cx, LWS_CPD_INTERNET_OK);
        return 0;
}

int main(int argc, const char **argv)
{
	lws_state_notify_link_t notifier = { { NULL, NULL, NULL },
						system_notify_cb, "app" };
	lws_state_notify_link_t *na[] = { &notifier, NULL };
	struct lws_context_creation_info info;
	unsigned long long start;
	const char *p;
#if defined(LWS_WITH_TLS_SESSIONS)
	int pl = 0;
#endif

	lws_context_info_defaults(&info, NULL);

        lws_cmdline_option_handle_builtin(argc, argv, &info);

	info.timeout_secs = 120;

	info.signal_cb = signal_cb;
	info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;

	if (lws_cmdline_option(argc, argv, switches[LWS_SW_UV].sw))
		info.options |= LWS_SERVER_OPTION_LIBUV;
	else
		if (lws_cmdline_option(argc, argv, switches[LWS_SW_EVENT].sw))
			info.options |= LWS_SERVER_OPTION_LIBEVENT;
		else
			if (lws_cmdline_option(argc, argv, switches[LWS_SW_EV].sw))
				info.options |= LWS_SERVER_OPTION_LIBEV;
			else
				if (lws_cmdline_option(argc, argv, switches[LWS_SW_GLIB].sw))
					info.options |= LWS_SERVER_OPTION_GLIB;
				else
					signal(SIGINT, sigint_handler);

	staggered = !!lws_cmdline_option(argc, argv, switches[LWS_SW_S].sw);
	sequential = !!lws_cmdline_option(argc, argv, switches[LWS_SW_SEQ].sw);

	lwsl_user("LWS minimal http client [-s (staggered)] [-p (pipeline)]\n");
	lwsl_user("   [-l (localhost)] [-d <logs>]\n");
	lwsl_user("   [-n (numbered)] [--post]\n");

	info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
	info.protocols = protocols;
	/*
	 * since we know this lws context is only ever going to be used with
	 * COUNT client wsis / fds / sockets at a time, let lws know it doesn't
	 * have to use the default allocations for fd tables up to ulimit -n.
	 * It will just allocate for 1 internal and COUNT + 1 (allowing for h2
	 * network wsi) that we will use.
	 */
	info.fd_limit_per_thread = 1 + COUNT + 8 + 1;
	info.register_notifier_list = na;
	info.pcontext = &context;

#if defined(LWS_WITH_SYS_METRICS)
        info.system_ops = &system_ops;
#endif
        if (lws_cmdline_option(argc, argv, "--cpd-bypass")) {
#if defined(LWS_WITH_SYS_METRICS)
                system_ops.captive_portal_detect_request = custom_cpd_bypass;
#else
                static lws_system_ops_t sops;
                sops.captive_portal_detect_request = custom_cpd_bypass;
                info.system_ops = &sops;
#endif
        }

#if defined(LWS_WITH_MBEDTLS) || defined(USE_WOLFSSL) || defined(LWS_WITH_OPENHITLS)
	/*
	 * OpenSSL uses the system trust store.  mbedTLS has to be told which
	 * CA to trust explicitly.
	 */
	info.client_ssl_ca_filepath = "./warmcat.com.cer";
#endif

	/* vhost option allowing tls session reuse, requires
	 * LWS_WITH_TLS_SESSIONS build option */
	if (lws_cmdline_option(argc, argv, switches[LWS_SW_NO_TLS_SESSION_REUSE].sw))
		info.options |= LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE;

	if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_LIMIT].sw)))
		info.simultaneous_ssl_restriction = atoi(p);

	if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_SSL_HANDSHAKE_SERIALIZE].sw)))
		/* We only consider simultaneous_ssl_restriction > 1 use cases.
		 * If ssl isn't limited or only 1 is allowed, we don't care.
		 */
		info.simultaneous_ssl_handshake_restriction = atoi(p);

        if ((p = lws_cmdline_option(argc, argv, "--tls13-ciphers"))) {
                info.client_tls_1_3_plus_cipher_list = p;
                info.client_ssl_cipher_list = p;
        }
        
        // lwsl_notice("AGY-DEBUG: minimal: before TESTCASE check, client_ssl_cipher_list is '%s'\n", info.client_ssl_cipher_list ? info.client_ssl_cipher_list : "NULL");

        const char *testcase = getenv("TESTCASE");
        if (testcase && !strcmp(testcase, "chacha20")) {
#if defined(LWS_WITH_GNUTLS)
                info.client_tls_1_3_plus_cipher_list = "NORMAL:-CIPHER-ALL:-AES-256-GCM:-AES-128-GCM:-AES-128-CCM:-AES-128-CCM-8:+CHACHA20-POLY1305";
                info.client_ssl_cipher_list = "NORMAL:-CIPHER-ALL:-AES-256-GCM:-AES-128-GCM:-AES-128-CCM:-AES-128-CCM-8:+CHACHA20-POLY1305";
#else
                info.client_tls_1_3_plus_cipher_list = "TLS_CHACHA20_POLY1305_SHA256";
                info.client_ssl_cipher_list = "TLS_CHACHA20_POLY1305_SHA256";
#endif
        }

        if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_QUIC_INITIAL_CWND].sw))) {
                int val = atoi(p);
                if (val > 0)
                        info.quic_initial_cwnd = (uint32_t)val;
        }

#if defined(LWS_WITH_TLS_SESSIONS) && !defined(LWS_WITH_MBEDTLS) && !defined(WIN32)
        /*
         * When --0rtt or the QIR harness (TESTCASE=zerortt) is used,
         * auto-configure the ticket persistence path so that phase 1
         * saves the ticket and phase 2 can load it and attempt 0-RTT,
         * provided the caller hasn't already set an explicit path.
         */
        if ((info.options & LWS_SERVER_OPTION_ALLOW_EARLY_DATA) ||
	    (testcase && !strcmp(testcase, "zerortt"))) {
                if (!save_ticket[0])
                        lws_strncpy(save_ticket, "./lws_session_ticket",
                                    sizeof(save_ticket));
                if (!load_ticket[0])
                        lws_strncpy(load_ticket, "./lws_session_ticket",
                                    sizeof(load_ticket));
                lwsl_notice("%s: early-data/zerortt: ticket path '%s'\n",
                            __func__, save_ticket);
                if (!info.quic_initial_cwnd)
                        info.quic_initial_cwnd = 250000;
        }
#endif

        if (lws_cmdline_option(argc, argv, "--quicv2"))
                info.options |= LWS_SERVER_OPTION_QUIC_LATEST_VERSION;

        if (lws_cmdline_option(argc, argv, "--quic-early-key-update"))
                info.options |= LWS_SERVER_OPTION_QUIC_EARLY_KEY_UPDATE;

        /* lws_context_info_defaults sets LWS_SERVER_OPTION_EXPLICIT_VHOSTS by default now,
         * which skips creating the default vhost using our info struct. Clear it so
         * our TLS cipher overrides are actually used to create the default vhost! */
        info.options &= ~(uint64_t)LWS_SERVER_OPTION_EXPLICIT_VHOSTS;

        context = lws_create_context(&info);
	if (!context) {
		lwsl_err("lws init failed\n");
		return 1;
	}

#if defined(LWS_ROLE_H2) && defined(LWS_ROLE_H1)
	i.alpn = "h2,http/1.1";
#elif defined(LWS_ROLE_H2)
	i.alpn = "h2";
#elif defined(LWS_ROLE_H1)
	i.alpn = "http/1.1";
#endif

	i.context = context;
	i.ssl_connection = LCCSCF_USE_SSL | LCCSCF_H2_QUIRK_OVERFLOWS_TXCR | LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM;

	if (lws_cmdline_option(argc, argv, "-j"))
		i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
	if (lws_cmdline_option(argc, argv, "-k"))
		i.ssl_connection |= LCCSCF_ALLOW_INSECURE;
	if (lws_cmdline_option(argc, argv, "-m"))
		i.ssl_connection |= LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
	if (lws_cmdline_option(argc, argv, "-e"))
		i.ssl_connection |= LCCSCF_ALLOW_EXPIRED;


	if (lws_cmdline_option(argc, argv, switches[LWS_SW_POST].sw)) {
		posting = 1;
		i.method = "POST";
		i.ssl_connection |= LCCSCF_HTTP_MULTIPART_MIME;
	} else
		i.method = "GET";

	/* enables h1 or h2 connection sharing */
	if (lws_cmdline_option(argc, argv, switches[LWS_SW_P].sw)) {
		i.ssl_connection |= LCCSCF_PIPELINE;
#if defined(LWS_WITH_TLS_SESSIONS)
		pl = 1;
#endif
	}

#if defined(LWS_WITH_CONMON)
	if (lws_cmdline_option(argc, argv, "--conmon"))
		i.ssl_connection |= LCCSCF_CONMON;
#endif

	/* force h1 even if h2 available */
	if (lws_cmdline_option(argc, argv, "--h3"))
		i.alpn = "h3";

	if (lws_cmdline_option(argc, argv, "--quicv2"))
		info.options |= LWS_SERVER_OPTION_QUIC_LATEST_VERSION;
	
	if (lws_cmdline_option(argc, argv, "--quic-early-key-update"))
		info.options |= LWS_SERVER_OPTION_QUIC_EARLY_KEY_UPDATE;



	if (lws_cmdline_option(argc, argv, "--h1"))
		i.alpn = "http/1.1";

	if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_OUTDIR].sw)))
		lws_strncpy(out_dir, p, sizeof(out_dir));

	if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_SAVE_TICKET].sw)))
		lws_strncpy(save_ticket, p, sizeof(save_ticket));

	if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_LOAD_TICKET].sw)))
		lws_strncpy(load_ticket, p, sizeof(load_ticket));

	/* Parse URLs */
	count = 0;

	for (int j = 1; j < argc; j++) {
		if (!strncmp(argv[j], "https://", 8)) {
			const char *url = argv[j] + 8;
			const char *slash = strchr(url, '/');
			const char *colon = strchr(url, ':');
			
			if (slash) {
				lws_strncpy(reqs[count].path, slash, sizeof(reqs[count].path));
			} else {
				lws_strncpy(reqs[count].path, "/", sizeof(reqs[count].path));
			}
			
			if (colon && (!slash || colon < slash)) {
				reqs[count].port = atoi(colon + 1);
				lws_strncpy(reqs[count].address, url, lws_ptr_diff_size_t(colon, url) + 1);
			} else {
				reqs[count].port = 443;
				if (slash)
					lws_strncpy(reqs[count].address, url, lws_ptr_diff_size_t(slash, url) + 1);
				else
					lws_strncpy(reqs[count].address, url, sizeof(reqs[count].address));
			}
			count++;
			if (count == COUNT) break;
		}
	}
	int default_urls = 0;
	if (count == 0) {
		/* Fallback to default */
		lws_strncpy(reqs[0].address, "libwebsockets.org", sizeof(reqs[0].address));
		reqs[0].port = 443;
		if (posting)
			lws_strncpy(reqs[0].path, "/testserver/formtest", sizeof(reqs[0].path));
		else
			lws_strncpy(reqs[0].path, "/", sizeof(reqs[0].path));
		count = 8;
		default_urls = 1;

		if (lws_cmdline_option(argc, argv, switches[LWS_SW_L].sw)) {
			reqs[0].port = 7681;
			lws_strncpy(reqs[0].address, "localhost", sizeof(reqs[0].address));
			i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
			if (posting)
				lws_strncpy(reqs[0].path, "/formtest", sizeof(reqs[0].path));
		} else if (lws_cmdline_option(argc, argv, "--h3")) {
			reqs[0].port = 443;
			lws_strncpy(reqs[0].address, "cloudflare-quic.com", sizeof(reqs[0].address));
			lws_strncpy(reqs[0].path, "/", sizeof(reqs[0].path));
		}

		if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_SERVER].sw)))
			lws_strncpy(reqs[0].address, p, sizeof(reqs[0].address));

		if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_PORT].sw)))
			reqs[0].port = atoi(p);

		if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_PATH].sw)))
			lws_strncpy(reqs[0].path, p, sizeof(reqs[0].path));
	}

	if (lws_cmdline_option(argc, argv, switches[LWS_SW_NO_TLS].sw))
		i.ssl_connection &= ~(LCCSCF_USE_SSL);

	if (lws_cmdline_option(argc, argv, "--0rtt") ||
	    (testcase && !strcmp(testcase, "zerortt")))
		i.ssl_connection |= LCCSCF_ALLOW_EARLY_DATA;

	int c_opt = count;
	if ((p = lws_cmdline_option(argc, argv, switches[LWS_SW_C].sw))) {
		c_opt = atoi(p);
		if (c_opt > COUNT || c_opt <= 0)
			c_opt = count;
	}

	if (count <= 1 || default_urls) {
		for (int j = 1; j < c_opt; j++) {
			reqs[j] = reqs[0];
		}
	} else {
		/* If explicit URLs were provided, don't overwrite them. 
		 * If -c was larger than the provided URLs, limit it. */
		c_opt = count;
	}
	count = c_opt;

	if (lws_cmdline_option(argc, argv, switches[LWS_SW_N].sw)) {
		for (int j = 0; j < count; j++) {
			lws_snprintf(reqs[j].path, sizeof(reqs[j].path), "/%d.png", j + 1);
		}
	}

	i.host = reqs[0].address;
	i.origin = reqs[0].address;
	i.protocol = protocols[0].name;

#if defined(LWS_WITH_TLS_SESSIONS) && !defined(LWS_WITH_MBEDTLS) && !defined(WIN32)
	/*
	 * Attempt to preload a session from external storage
	 */
	if (load_ticket[0]) {
		if (lws_tls_session_dump_load(lws_get_vhost_by_name(context, "default"),
					  i.host, (uint16_t)reqs[0].port, sess_load_cb, load_ticket))
			lwsl_warn("%s: session load failed\n", __func__);
	}
#endif

	start = us();
	while (!intr && !lws_service(context, 0))
		;

#if defined(LWS_WITH_TLS_SESSIONS)
	lwsl_user("%s: session reuse count %d\n", __func__, reuse);

	if (staggered && !pl && !reuse) {
		lwsl_err("%s: failing, expected 1 .. %d reused\n", __func__, count - 1);
		// too difficult to reproduce in CI
		// failed = 1;
	}
#endif

	lwsl_user("Duration: %lldms\n", (us() - start) / 1000);

#if defined(LWS_WITH_TLS_SESSIONS) && !defined(LWS_WITH_MBEDTLS) && !defined(WIN32)
	/*
	 * Save TLS session AFTER the event loop, not at ESTABLISHED time.
	 * The TLS 1.3 NewSessionTicket arrives from the server asynchronously
	 * after the HTTP response begins.  By the time we reach here the
	 * session is fully cached in-memory and ready for disk persistence.
	 * This is especially important for zerortt: phase 2 is a separate
	 * process and needs the ticket on disk to attempt early data.
	 */
	lwsl_user("save_ticket = %s\n", save_ticket);
	if (save_ticket[0]) {
		struct lws_vhost *save_vh = lws_get_vhost_by_name(context, "default");
		if (save_vh) {
			if (lws_tls_session_dump_save(save_vh, i.host,
						      (uint16_t)reqs[0].port,
						      sess_save_cb, save_ticket))
				lwsl_warn("%s: deferred session save failed\n",
					  __func__);
			else
				lwsl_notice("%s: session saved to %s\n",
					    __func__, save_ticket);
		}
	}
#endif

	lws_context_destroy(context);

	lwsl_user("Exiting with %d\n", failed || completed != count);

	return failed || completed != count;
}
