2021-02-19 03:20:41 +00:00
|
|
|
#if 0
|
|
|
|
/*─────────────────────────────────────────────────────────────────╗
|
|
|
|
│ To the extent possible under law, Justine Tunney has waived │
|
|
|
|
│ all copyright and related or neighboring rights to this file, │
|
|
|
|
│ as it is written in the following disclaimers: │
|
|
|
|
│ • http://unlicense.org/ │
|
|
|
|
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
|
|
|
╚─────────────────────────────────────────────────────────────────*/
|
|
|
|
#endif
|
2023-07-07 17:00:49 +00:00
|
|
|
#include "libc/assert.h"
|
2021-07-06 20:39:18 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2021-07-09 04:54:21 +00:00
|
|
|
#include "libc/calls/struct/iovec.h"
|
2023-07-07 17:00:49 +00:00
|
|
|
#include "libc/calls/struct/timeval.h"
|
2021-07-06 20:39:18 +00:00
|
|
|
#include "libc/errno.h"
|
2023-07-07 17:00:49 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
|
|
|
#include "libc/fmt/magnumstrs.internal.h"
|
2024-08-04 19:52:25 +00:00
|
|
|
#include "libc/macros.h"
|
2024-01-08 18:07:35 +00:00
|
|
|
#include "libc/mem/gc.h"
|
2023-07-07 17:00:49 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2021-02-19 03:20:41 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2021-08-14 13:17:56 +00:00
|
|
|
#include "libc/sock/goodsocket.internal.h"
|
2021-02-19 03:20:41 +00:00
|
|
|
#include "libc/sock/sock.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/stdio/append.h"
|
|
|
|
#include "libc/stdio/rand.h"
|
2021-02-19 03:20:41 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
2021-08-14 13:17:56 +00:00
|
|
|
#include "libc/str/slice.h"
|
2021-02-19 03:20:41 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/af.h"
|
|
|
|
#include "libc/sysv/consts/ipproto.h"
|
Make improvements to redbean
The following Lua APIs have been added:
- IsDaemon() → bool
- ProgramPidPath(str)
The following Lua hooks have been added:
- OnClientConnection(ip:int,port:int,serverip:int,serverport:int) → bool
- OnProcessCreate(pid:int,ip:int,port:int,serverip:int,serverport:int)
- OnProcessDestroy(pid:int)
- OnServerStart()
- OnServerStop()
- OnWorkerStart()
- OnWorkerStop()
redbean now does a better job at applying gzip on the fly from the local
filesystem, using a streaming chunked api with constant memory, which is
useful for doing things like serving a 4gb text file off NFS, and having
it start transmitting in milliseconds. redbean will also compute entropy
on the beginnings of files to determine if compression is profitable.
This change pays off technical debts relating to memory, such as relying
on exit() to free() allocations. That's now mostly fixed so it should be
easier now to spot memory leaks in malloc traces.
This change also fixes bugs and makes improvements to our SSL support.
Uniprocess mode failed handshakes are no longer an issue. Token Alpn is
offered so curl -v looks less weird. Hybrid SSL certificate loading is
now smarter about naming conflicts. Self-signed CA root anchors will no
longer be delivered to the client during the handshake.
2021-07-10 22:02:03 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2021-02-19 03:20:41 +00:00
|
|
|
#include "libc/sysv/consts/sock.h"
|
2021-04-21 02:14:21 +00:00
|
|
|
#include "net/http/http.h"
|
|
|
|
#include "net/http/url.h"
|
2021-07-08 04:44:27 +00:00
|
|
|
#include "net/https/https.h"
|
2023-07-03 02:57:43 +00:00
|
|
|
#include "third_party/getopt/getopt.internal.h"
|
2021-07-06 20:39:18 +00:00
|
|
|
#include "third_party/mbedtls/ctr_drbg.h"
|
|
|
|
#include "third_party/mbedtls/debug.h"
|
|
|
|
#include "third_party/mbedtls/error.h"
|
2023-07-07 17:00:49 +00:00
|
|
|
#include "third_party/mbedtls/iana.h"
|
|
|
|
#include "third_party/mbedtls/net_sockets.h"
|
2021-07-06 20:39:18 +00:00
|
|
|
#include "third_party/mbedtls/ssl.h"
|
2023-07-07 17:00:49 +00:00
|
|
|
#include "third_party/mbedtls/x509.h"
|
2023-12-29 06:58:17 +00:00
|
|
|
#include "third_party/musl/netdb.h"
|
2021-07-06 20:39:18 +00:00
|
|
|
|
2021-02-19 03:20:41 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Downloads HTTP URL to stdout.
|
|
|
|
*/
|
|
|
|
|
2021-07-08 04:44:27 +00:00
|
|
|
#define HasHeader(H) (!!msg.headers[H].a)
|
|
|
|
#define HeaderData(H) (p + msg.headers[H].a)
|
|
|
|
#define HeaderLength(H) (msg.headers[H].b - msg.headers[H].a)
|
|
|
|
#define HeaderEqualCase(H, S) \
|
|
|
|
SlicesEqualCase(S, strlen(S), HeaderData(H), HeaderLength(H))
|
|
|
|
|
2023-07-07 17:00:49 +00:00
|
|
|
static int sock;
|
|
|
|
static int outfd;
|
|
|
|
static const char *prog;
|
|
|
|
static const char *outpath;
|
2022-03-16 20:33:13 +00:00
|
|
|
|
2023-07-07 17:00:49 +00:00
|
|
|
static wontreturn void PrintUsage(int fd, int rc) {
|
|
|
|
tinyprint(fd, "usage: ", prog, " [-iksvV] URL\n", NULL);
|
|
|
|
exit(rc);
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 17:00:49 +00:00
|
|
|
static const char *DescribeErrno(void) {
|
|
|
|
const char *reason;
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (!(reason = _strerdoc(errno)))
|
|
|
|
reason = "Unknown error";
|
2023-07-07 17:00:49 +00:00
|
|
|
return reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetSslEntropy(void *c, unsigned char *p, size_t n) {
|
|
|
|
if (getrandom(p, n, 0) != n) {
|
|
|
|
perror("getrandom");
|
2022-03-16 20:33:13 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2023-07-07 17:00:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnSslDebug(void *ctx, int level, const char *file, int line,
|
|
|
|
const char *message) {
|
|
|
|
char sline[12];
|
|
|
|
char slevel[12];
|
|
|
|
FormatInt32(sline, line);
|
|
|
|
FormatInt32(slevel, level);
|
|
|
|
tinyprint(2, file, ":", sline, ": (", slevel, ") ", message, "\n", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WriteOutput(const void *p, size_t n) {
|
|
|
|
if (!outfd) {
|
|
|
|
if (outpath) {
|
|
|
|
if ((outfd = creat(outpath, 0644)) <= 0) {
|
|
|
|
perror(outpath);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
outfd = 1;
|
|
|
|
outpath = "<stdout>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ssize_t rc;
|
|
|
|
for (size_t i = 0; i < n; i += rc) {
|
|
|
|
rc = write(outfd, p, n);
|
|
|
|
if (rc <= 0) {
|
|
|
|
perror(outpath);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 04:44:27 +00:00
|
|
|
static int TlsSend(void *c, const unsigned char *p, size_t n) {
|
2021-07-06 20:39:18 +00:00
|
|
|
int rc;
|
2023-07-07 17:00:49 +00:00
|
|
|
if ((rc = write(*(int *)c, p, n)) == -1) {
|
|
|
|
perror("TlsSend");
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-06 20:39:18 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-07-09 04:54:21 +00:00
|
|
|
static int TlsRecv(void *c, unsigned char *p, size_t n, uint32_t o) {
|
|
|
|
int r;
|
|
|
|
struct iovec v[2];
|
|
|
|
static unsigned a, b;
|
|
|
|
static unsigned char t[4096];
|
|
|
|
if (a < b) {
|
|
|
|
r = MIN(n, b - a);
|
|
|
|
memcpy(p, t + a, r);
|
2023-07-07 17:00:49 +00:00
|
|
|
if ((a += r) == b) {
|
|
|
|
a = b = 0;
|
|
|
|
}
|
2021-07-09 04:54:21 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
v[0].iov_base = p;
|
|
|
|
v[0].iov_len = n;
|
|
|
|
v[1].iov_base = t;
|
|
|
|
v[1].iov_len = sizeof(t);
|
2023-07-07 17:00:49 +00:00
|
|
|
if ((r = readv(*(int *)c, v, 2)) == -1) {
|
|
|
|
perror("TlsRecv");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (r > n) {
|
|
|
|
b = r - n;
|
|
|
|
}
|
2021-07-09 04:54:21 +00:00
|
|
|
return MIN(n, r);
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 00:12:02 +00:00
|
|
|
int _curl(int argc, char *argv[]) {
|
2023-07-07 17:00:49 +00:00
|
|
|
|
|
|
|
if (!NoDebug()) {
|
|
|
|
ShowCrashReports();
|
|
|
|
}
|
|
|
|
|
|
|
|
prog = argv[0];
|
|
|
|
if (!prog) {
|
|
|
|
prog = "curl";
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read flags.
|
|
|
|
*/
|
|
|
|
int opt;
|
|
|
|
struct Headers {
|
|
|
|
size_t n;
|
|
|
|
char **p;
|
|
|
|
} headers = {0};
|
2024-02-22 22:12:18 +00:00
|
|
|
uint64_t method = 0;
|
2023-07-07 17:00:49 +00:00
|
|
|
int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
|
|
|
int ciphersuite = MBEDTLS_SSL_PRESET_SUITEC;
|
2022-12-24 01:06:48 +00:00
|
|
|
bool includeheaders = false;
|
|
|
|
const char *postdata = NULL;
|
2021-07-08 04:44:27 +00:00
|
|
|
const char *agent = "hurl/1.o (https://github.com/jart/cosmopolitan)";
|
2023-07-07 17:00:49 +00:00
|
|
|
while ((opt = getopt(argc, argv, "qiksvBVIX:H:A:d:o:")) != -1) {
|
2021-07-08 04:44:27 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
case 'q':
|
|
|
|
break;
|
2023-07-07 17:00:49 +00:00
|
|
|
case 'o':
|
|
|
|
outpath = optarg;
|
2021-07-08 04:44:27 +00:00
|
|
|
break;
|
2022-12-24 01:06:48 +00:00
|
|
|
case 'i':
|
|
|
|
includeheaders = true;
|
|
|
|
break;
|
2021-07-08 04:44:27 +00:00
|
|
|
case 'I':
|
|
|
|
method = kHttpHead;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
agent = optarg;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
headers.p = realloc(headers.p, ++headers.n * sizeof(*headers.p));
|
|
|
|
headers.p[headers.n - 1] = optarg;
|
|
|
|
break;
|
2022-12-24 01:06:48 +00:00
|
|
|
case 'd':
|
|
|
|
postdata = optarg;
|
|
|
|
break;
|
2021-07-08 04:44:27 +00:00
|
|
|
case 'X':
|
2024-02-22 22:12:18 +00:00
|
|
|
if (!(method = ParseHttpMethod(optarg, -1))) {
|
2023-07-07 17:00:49 +00:00
|
|
|
tinyprint(2, prog, ": bad http method: ", optarg, "\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
++mbedtls_debug_threshold;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
authmode = MBEDTLS_SSL_VERIFY_NONE;
|
|
|
|
break;
|
2023-07-07 17:00:49 +00:00
|
|
|
case 'B':
|
|
|
|
ciphersuite = MBEDTLS_SSL_PRESET_SUITEB;
|
|
|
|
break;
|
2021-07-08 04:44:27 +00:00
|
|
|
case 'h':
|
2023-07-07 17:00:49 +00:00
|
|
|
PrintUsage(1, 0);
|
2021-07-08 04:44:27 +00:00
|
|
|
default:
|
2023-07-07 17:00:49 +00:00
|
|
|
PrintUsage(2, 1);
|
2021-07-08 04:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 02:14:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get argument.
|
|
|
|
*/
|
|
|
|
const char *urlarg;
|
2023-07-07 17:00:49 +00:00
|
|
|
if (optind == argc) {
|
|
|
|
tinyprint(2, prog, ": missing url\n", NULL);
|
|
|
|
PrintUsage(2, 1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
urlarg = argv[optind];
|
2021-04-21 02:14:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse URL.
|
|
|
|
*/
|
|
|
|
struct Url url;
|
|
|
|
char *host, *port;
|
2021-07-06 20:39:18 +00:00
|
|
|
bool usessl = false;
|
2023-07-07 17:00:49 +00:00
|
|
|
gc(ParseUrl(urlarg, -1, &url, kUrlPlus));
|
|
|
|
gc(url.params.p);
|
2021-07-06 20:39:18 +00:00
|
|
|
if (url.scheme.n) {
|
|
|
|
if (url.scheme.n == 5 && !memcasecmp(url.scheme.p, "https", 5)) {
|
|
|
|
usessl = true;
|
|
|
|
} else if (!(url.scheme.n == 4 && !memcasecmp(url.scheme.p, "http", 4))) {
|
2023-07-07 17:00:49 +00:00
|
|
|
tinyprint(2, prog, ": not an http/https url: ", urlarg, "\n", NULL);
|
2021-07-06 20:39:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2021-04-21 02:14:21 +00:00
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
if (url.host.n) {
|
2023-07-07 17:00:49 +00:00
|
|
|
host = gc(strndup(url.host.p, url.host.n));
|
2021-07-08 04:44:27 +00:00
|
|
|
if (url.port.n) {
|
2023-07-07 17:00:49 +00:00
|
|
|
port = gc(strndup(url.port.p, url.port.n));
|
2021-07-08 04:44:27 +00:00
|
|
|
} else {
|
|
|
|
port = usessl ? "443" : "80";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
host = "127.0.0.1";
|
|
|
|
port = usessl ? "443" : "80";
|
|
|
|
}
|
2021-04-21 02:14:21 +00:00
|
|
|
if (!IsAcceptableHost(host, -1)) {
|
2023-07-07 17:00:49 +00:00
|
|
|
tinyprint(2, prog, ": invalid host: ", urlarg, "\n", NULL);
|
2021-04-21 02:14:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
url.fragment.p = 0, url.fragment.n = 0;
|
|
|
|
url.scheme.p = 0, url.scheme.n = 0;
|
|
|
|
url.user.p = 0, url.user.n = 0;
|
|
|
|
url.pass.p = 0, url.pass.n = 0;
|
|
|
|
url.host.p = 0, url.host.n = 0;
|
|
|
|
url.port.p = 0, url.port.n = 0;
|
|
|
|
if (!url.path.n || url.path.p[0] != '/') {
|
2023-07-07 17:00:49 +00:00
|
|
|
char *p = gc(malloc(1 + url.path.n));
|
2021-04-21 02:14:21 +00:00
|
|
|
mempcpy(mempcpy(p, "/", 1), url.path.p, url.path.n);
|
|
|
|
url.path.p = p;
|
|
|
|
++url.path.n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create HTTP message.
|
|
|
|
*/
|
2023-07-07 17:00:49 +00:00
|
|
|
if (!method) {
|
|
|
|
if (postdata) {
|
|
|
|
method = kHttpPost;
|
|
|
|
} else {
|
|
|
|
method = kHttpGet;
|
|
|
|
}
|
|
|
|
}
|
2022-12-24 01:06:48 +00:00
|
|
|
|
2021-07-19 21:55:20 +00:00
|
|
|
char *request = 0;
|
2024-02-22 22:12:18 +00:00
|
|
|
char methodstr[9] = {0};
|
|
|
|
WRITE64LE(methodstr, method);
|
2021-07-19 21:55:20 +00:00
|
|
|
appendf(&request,
|
|
|
|
"%s %s HTTP/1.1\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"User-Agent: %s\r\n",
|
2024-02-22 22:12:18 +00:00
|
|
|
methodstr, gc(EncodeUrl(&url, 0)), agent);
|
2022-12-24 01:06:48 +00:00
|
|
|
|
2023-07-07 17:00:49 +00:00
|
|
|
bool senthost = false;
|
|
|
|
bool sentcontenttype = false;
|
|
|
|
bool sentcontentlength = false;
|
2021-07-08 04:44:27 +00:00
|
|
|
for (int i = 0; i < headers.n; ++i) {
|
2023-07-07 17:00:49 +00:00
|
|
|
appends(&request, headers.p[i]);
|
|
|
|
appends(&request, "\r\n");
|
|
|
|
if (!strncasecmp("Host:", headers.p[i], 5)) {
|
2023-04-27 03:45:01 +00:00
|
|
|
senthost = true;
|
2023-07-07 17:00:49 +00:00
|
|
|
} else if (!strncasecmp("Content-Type:", headers.p[i], 13)) {
|
2023-04-27 03:45:01 +00:00
|
|
|
sentcontenttype = true;
|
2023-07-07 17:00:49 +00:00
|
|
|
} else if (!strncasecmp("Content-Length:", headers.p[i], 15)) {
|
2023-04-27 03:45:01 +00:00
|
|
|
sentcontentlength = true;
|
2023-07-07 17:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!senthost) {
|
|
|
|
appends(&request, "Host: ");
|
|
|
|
appends(&request, host);
|
|
|
|
appendw(&request, ':');
|
|
|
|
appends(&request, port);
|
|
|
|
appends(&request, "\r\n");
|
2022-12-24 01:06:48 +00:00
|
|
|
}
|
|
|
|
if (postdata) {
|
2023-07-07 17:00:49 +00:00
|
|
|
if (!sentcontenttype) {
|
2023-04-27 03:45:01 +00:00
|
|
|
appends(&request, "Content-Type: application/x-www-form-urlencoded\r\n");
|
2023-07-07 17:00:49 +00:00
|
|
|
}
|
|
|
|
if (!sentcontentlength) {
|
|
|
|
char ibuf[21];
|
|
|
|
FormatUint64(ibuf, strlen(postdata));
|
|
|
|
appends(&request, "Content-Length: ");
|
|
|
|
appends(&request, ibuf);
|
|
|
|
appends(&request, "\r\n");
|
|
|
|
}
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
2023-07-07 17:00:49 +00:00
|
|
|
appends(&request, "\r\n");
|
|
|
|
if (postdata) {
|
|
|
|
appends(&request, postdata);
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 02:14:21 +00:00
|
|
|
/*
|
|
|
|
* Perform DNS lookup.
|
|
|
|
*/
|
2021-07-06 20:39:18 +00:00
|
|
|
struct addrinfo *addr;
|
2022-12-24 01:06:48 +00:00
|
|
|
struct addrinfo hints = {.ai_family = AF_UNSPEC,
|
2021-02-19 03:20:41 +00:00
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP,
|
|
|
|
.ai_flags = AI_NUMERICSERV};
|
2023-12-29 06:58:17 +00:00
|
|
|
if (getaddrinfo(host, port, &hints, &addr) != 0) {
|
2023-07-07 17:00:49 +00:00
|
|
|
tinyprint(2, prog, ": could not resolve host: ", host, "\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-06 20:39:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to server.
|
|
|
|
*/
|
2022-03-16 20:33:13 +00:00
|
|
|
int ret;
|
2023-07-07 17:00:49 +00:00
|
|
|
if ((sock = GoodSocket(addr->ai_family, addr->ai_socktype, addr->ai_protocol,
|
|
|
|
false, &(struct timeval){-60})) == -1) {
|
|
|
|
perror("socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (connect(sock, addr->ai_addr, addr->ai_addrlen)) {
|
|
|
|
tinyprint(2, prog, ": failed to connect to ", host, " port ", port, ": ",
|
|
|
|
DescribeErrno(), "\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
freeaddrinfo(addr);
|
2023-07-07 17:00:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup crypto.
|
|
|
|
*/
|
|
|
|
mbedtls_ssl_config conf;
|
|
|
|
mbedtls_ssl_context ssl;
|
|
|
|
mbedtls_ctr_drbg_context drbg;
|
2021-07-06 20:39:18 +00:00
|
|
|
if (usessl) {
|
2023-07-07 17:00:49 +00:00
|
|
|
mbedtls_ssl_init(&ssl);
|
|
|
|
mbedtls_ctr_drbg_init(&drbg);
|
|
|
|
mbedtls_ssl_config_init(&conf);
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(!mbedtls_ctr_drbg_seed(&drbg, GetSslEntropy, 0, "justine", 7));
|
|
|
|
unassert(!mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT,
|
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
ciphersuite));
|
2023-07-07 17:00:49 +00:00
|
|
|
mbedtls_ssl_conf_authmode(&conf, authmode);
|
|
|
|
mbedtls_ssl_conf_ca_chain(&conf, GetSslRoots(), 0);
|
|
|
|
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &drbg);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
mbedtls_ssl_conf_dbg(&conf, OnSslDebug, 0);
|
|
|
|
#endif
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(!mbedtls_ssl_setup(&ssl, &conf));
|
|
|
|
unassert(!mbedtls_ssl_set_hostname(&ssl, host));
|
2021-07-06 20:39:18 +00:00
|
|
|
mbedtls_ssl_set_bio(&ssl, &sock, TlsSend, 0, TlsRecv);
|
|
|
|
if ((ret = mbedtls_ssl_handshake(&ssl))) {
|
2023-07-07 17:00:49 +00:00
|
|
|
tinyprint(2, prog, ": ssl negotiation with ", host,
|
|
|
|
" failed: ", DescribeSslClientHandshakeError(&ssl, ret), "\n",
|
|
|
|
NULL);
|
|
|
|
exit(1);
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send HTTP Message.
|
|
|
|
*/
|
2023-07-07 17:00:49 +00:00
|
|
|
ssize_t rc;
|
|
|
|
size_t i, n;
|
2021-07-19 21:55:20 +00:00
|
|
|
n = appendz(request).i;
|
2023-07-07 17:00:49 +00:00
|
|
|
for (i = 0; i < n; i += rc) {
|
|
|
|
if (usessl) {
|
|
|
|
rc = mbedtls_ssl_write(&ssl, request + i, n - i);
|
|
|
|
if (rc <= 0) {
|
|
|
|
tinyprint(2, prog, ": ssl send failed: ", DescribeMbedtlsErrorCode(rc),
|
|
|
|
"\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc = write(sock, request + i, n - i);
|
|
|
|
if (rc <= 0) {
|
|
|
|
tinyprint(2, prog, ": send failed: ", DescribeErrno(), "\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
2021-04-21 02:14:21 +00:00
|
|
|
|
2021-07-06 20:39:18 +00:00
|
|
|
/*
|
|
|
|
* Handle response.
|
|
|
|
*/
|
2021-07-08 04:44:27 +00:00
|
|
|
int t;
|
|
|
|
char *p;
|
|
|
|
struct HttpMessage msg;
|
|
|
|
struct HttpUnchunker u;
|
2023-07-07 17:00:49 +00:00
|
|
|
size_t g, hdrlen, paylen;
|
2021-07-08 04:44:27 +00:00
|
|
|
InitHttpMessage(&msg, kHttpResponse);
|
|
|
|
for (p = 0, hdrlen = paylen = t = i = n = 0;;) {
|
|
|
|
if (i == n) {
|
|
|
|
n += 1000;
|
|
|
|
n += n >> 1;
|
|
|
|
p = realloc(p, n);
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
if (usessl) {
|
2021-07-08 04:44:27 +00:00
|
|
|
if ((rc = mbedtls_ssl_read(&ssl, p + i, n - i)) < 0) {
|
2021-07-06 20:39:18 +00:00
|
|
|
if (rc == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
2021-07-08 04:44:27 +00:00
|
|
|
rc = 0;
|
2021-07-06 20:39:18 +00:00
|
|
|
} else {
|
2023-07-07 17:00:49 +00:00
|
|
|
tinyprint(2, prog,
|
|
|
|
": ssl recv failed: ", DescribeMbedtlsErrorCode(rc), "\n",
|
|
|
|
NULL);
|
|
|
|
exit(1);
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-07 17:00:49 +00:00
|
|
|
if ((rc = read(sock, p + i, n - i)) == -1) {
|
|
|
|
tinyprint(2, prog, ": recv failed: ", DescribeErrno(), "\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
}
|
|
|
|
g = rc;
|
|
|
|
i += g;
|
|
|
|
switch (t) {
|
|
|
|
case kHttpClientStateHeaders:
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(g);
|
2024-06-04 12:41:53 +00:00
|
|
|
if ((rc = ParseHttpMessage(&msg, p, i, n)) == -1) {
|
2023-07-07 17:00:49 +00:00
|
|
|
tinyprint(2, prog, ": ", host, " sent bad http message\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
if (rc) {
|
|
|
|
hdrlen = rc;
|
|
|
|
if (100 <= msg.status && msg.status <= 199) {
|
|
|
|
DestroyHttpMessage(&msg);
|
|
|
|
InitHttpMessage(&msg, kHttpResponse);
|
|
|
|
memmove(p, p + hdrlen, i - hdrlen);
|
|
|
|
i -= hdrlen;
|
|
|
|
break;
|
|
|
|
}
|
2022-12-24 01:06:48 +00:00
|
|
|
if (method == kHttpHead || includeheaders) {
|
2023-07-07 17:00:49 +00:00
|
|
|
WriteOutput(p, hdrlen);
|
2022-12-24 01:06:48 +00:00
|
|
|
}
|
|
|
|
if (method == kHttpHead || msg.status == 204 || msg.status == 304) {
|
2021-07-08 04:44:27 +00:00
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
if (HasHeader(kHttpTransferEncoding) &&
|
|
|
|
!HeaderEqualCase(kHttpTransferEncoding, "identity")) {
|
2023-07-07 17:00:49 +00:00
|
|
|
if (!HeaderEqualCase(kHttpTransferEncoding, "chunked")) {
|
|
|
|
tinyprint(2, prog, ": ", host,
|
|
|
|
" sent unsupported transfer encoding\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
t = kHttpClientStateBodyChunked;
|
|
|
|
memset(&u, 0, sizeof(u));
|
|
|
|
goto Chunked;
|
|
|
|
} else if (HasHeader(kHttpContentLength)) {
|
2023-07-07 17:00:49 +00:00
|
|
|
if ((rc = ParseContentLength(HeaderData(kHttpContentLength),
|
|
|
|
HeaderLength(kHttpContentLength))) ==
|
|
|
|
-1) {
|
|
|
|
tinyprint(2, prog, ": ", host, " sent bad content length\n",
|
|
|
|
NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
t = kHttpClientStateBodyLengthed;
|
|
|
|
paylen = rc;
|
|
|
|
if (paylen > i - hdrlen) {
|
2023-07-07 17:00:49 +00:00
|
|
|
WriteOutput(p + hdrlen, i - hdrlen);
|
2021-07-08 04:44:27 +00:00
|
|
|
} else {
|
2023-07-07 17:00:49 +00:00
|
|
|
WriteOutput(p + hdrlen, paylen);
|
2021-07-08 04:44:27 +00:00
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t = kHttpClientStateBody;
|
2023-07-07 17:00:49 +00:00
|
|
|
WriteOutput(p + hdrlen, i - hdrlen);
|
2021-07-08 04:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kHttpClientStateBody:
|
2023-07-07 17:00:49 +00:00
|
|
|
WriteOutput(p + i - g, g);
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (!g)
|
|
|
|
goto Finished;
|
2021-07-08 04:44:27 +00:00
|
|
|
break;
|
|
|
|
case kHttpClientStateBodyLengthed:
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(g);
|
2023-07-07 17:00:49 +00:00
|
|
|
if (i - hdrlen > paylen) {
|
|
|
|
g = hdrlen + paylen - (i - g);
|
|
|
|
}
|
|
|
|
WriteOutput(p + i - g, g);
|
|
|
|
if (i - hdrlen >= paylen) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
break;
|
|
|
|
case kHttpClientStateBodyChunked:
|
|
|
|
Chunked:
|
2023-07-07 17:00:49 +00:00
|
|
|
if ((rc = Unchunk(&u, p + hdrlen, i - hdrlen, &paylen)) == -1) {
|
|
|
|
tinyprint(2, prog, ": ", host, " sent bad chunk coding\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
if (rc) {
|
2023-07-07 17:00:49 +00:00
|
|
|
WriteOutput(p + hdrlen, paylen);
|
2021-07-08 04:44:27 +00:00
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
2021-02-19 03:20:41 +00:00
|
|
|
}
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 04:44:27 +00:00
|
|
|
/*
|
|
|
|
* Close connection.
|
|
|
|
*/
|
|
|
|
Finished:
|
2023-07-07 17:00:49 +00:00
|
|
|
if (close(sock)) {
|
|
|
|
tinyprint(2, prog, ": close failed: ", DescribeErrno(), "\n", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-08 04:44:27 +00:00
|
|
|
|
2021-07-06 20:39:18 +00:00
|
|
|
/*
|
2021-07-08 04:44:27 +00:00
|
|
|
* Free memory.
|
2021-07-06 20:39:18 +00:00
|
|
|
*/
|
2021-07-08 04:44:27 +00:00
|
|
|
free(p);
|
|
|
|
free(headers.p);
|
2021-07-06 20:39:18 +00:00
|
|
|
if (usessl) {
|
|
|
|
mbedtls_ssl_free(&ssl);
|
|
|
|
mbedtls_ctr_drbg_free(&drbg);
|
2021-07-08 04:44:27 +00:00
|
|
|
mbedtls_ssl_config_free(&conf);
|
Make improvements to redbean
The following Lua APIs have been added:
- IsDaemon() → bool
- ProgramPidPath(str)
The following Lua hooks have been added:
- OnClientConnection(ip:int,port:int,serverip:int,serverport:int) → bool
- OnProcessCreate(pid:int,ip:int,port:int,serverip:int,serverport:int)
- OnProcessDestroy(pid:int)
- OnServerStart()
- OnServerStop()
- OnWorkerStart()
- OnWorkerStop()
redbean now does a better job at applying gzip on the fly from the local
filesystem, using a streaming chunked api with constant memory, which is
useful for doing things like serving a 4gb text file off NFS, and having
it start transmitting in milliseconds. redbean will also compute entropy
on the beginnings of files to determine if compression is profitable.
This change pays off technical debts relating to memory, such as relying
on exit() to free() allocations. That's now mostly fixed so it should be
easier now to spot memory leaks in malloc traces.
This change also fixes bugs and makes improvements to our SSL support.
Uniprocess mode failed handshakes are no longer an issue. Token Alpn is
offered so curl -v looks less weird. Hybrid SSL certificate loading is
now smarter about naming conflicts. Self-signed CA root anchors will no
longer be delivered to the client during the handshake.
2021-07-10 22:02:03 +00:00
|
|
|
mbedtls_ctr_drbg_free(&drbg);
|
2021-07-06 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 03:20:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|