-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_get.c
More file actions
78 lines (66 loc) · 1.88 KB
/
https_get.c
File metadata and controls
78 lines (66 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// examples/https_get.c — Fetch an HTTPS page (Windows only, freestanding)
//
// Usage: bin/https_get [hostname] [path]
// Default: fetches https://example.com/
//
// Demonstrates l_tls.h for HTTPS GET requests with zero external dependencies.
#define L_MAINFILE
#include "l_tls.h"
int main(int argc, char *argv[]) {
l_getenv_init(argc, argv);
const char *host = argc > 1 ? argv[1] : "example.com";
const char *path = argc > 2 ? argv[2] : "/";
#if !L_TLS_AVAILABLE
puts("TLS not available on this platform.\n");
(void)host; (void)path;
return 0;
#else
if (l_tls_init() != 0) {
puts("TLS init failed\n");
return 1;
}
puts("Connecting to ");
puts(host);
puts(":443...\n");
int h = l_tls_connect(host, 443);
if (h < 0) {
puts("TLS connect failed\n");
l_tls_cleanup();
return 1;
}
puts("Connected. Sending GET request...\n");
// Build HTTP request manually (no snprintf in freestanding)
char req[512];
int off = 0;
const char *p;
// "GET "
for (p = "GET "; *p; p++) req[off++] = *p;
// path
for (p = path; *p && off < 400; p++) req[off++] = *p;
// " HTTP/1.1\r\n"
for (p = " HTTP/1.1\r\n"; *p; p++) req[off++] = *p;
// "Host: "
for (p = "Host: "; *p; p++) req[off++] = *p;
// hostname
for (p = host; *p && off < 480; p++) req[off++] = *p;
// "\r\nConnection: close\r\n\r\n"
for (p = "\r\nConnection: close\r\n\r\n"; *p; p++) req[off++] = *p;
req[off] = '\0';
if (l_tls_send(h, req, off) < 0) {
puts("TLS send failed\n");
l_tls_close(h);
l_tls_cleanup();
return 1;
}
// Read and display response
char buf[4096];
int n;
while ((n = l_tls_recv(h, buf, (int)sizeof(buf) - 1)) > 0) {
buf[n] = '\0';
puts(buf);
}
l_tls_close(h);
l_tls_cleanup();
return 0;
#endif
}