Exemplo n.º 1
0
int make_request(MSocket & ws, TinyTLSContext * ctx, TTlsLink * link)
{
	ttlsReset(ctx);

	int res = ws.connect(hostname, 443);

	printf("Connect returned %d\n", res);
	if (res != 0) {
		return -1;
	}

	ttlsSetLink(ctx, link);

	do {
		int result = ttlsHandshake(ctx);
		if (result > 0) break;
		if (result < 0) {
			printf("Handshake failed with error %d\n", result);
			return -1;
		}
	} while (true);

	printf("\nREQUEST:\n==================\n%s", request);
	ttlsSend(ctx, (const uint8_t*)request, strlen(request));

	printf("\nRESPONSE:\n==================\n");

	for (;;) {
		uint8_t buf[60];
		int res = ttlsRecv(ctx, buf, 60);

		if (res <= 0) {
			printf("\n\n::res = %d\n", res);
			break;
		}

		fwrite(buf, 1, res, stdout);
	}

	ws.disconnect();
	return 0;
}