Refactor and improve CTL and other code

This commit is contained in:
Justine Tunney 2024-06-04 05:41:53 -07:00
parent 1d8f37a2f0
commit 9906f299bb
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
25 changed files with 5768 additions and 5350 deletions

View file

@ -447,7 +447,7 @@ int _curl(int argc, char *argv[]) {
switch (t) {
case kHttpClientStateHeaders:
unassert(g);
if ((rc = ParseHttpMessage(&msg, p, i)) == -1) {
if ((rc = ParseHttpMessage(&msg, p, i, n)) == -1) {
tinyprint(2, prog, ": ", host, " sent bad http message\n", NULL);
exit(1);
}

View file

@ -64,7 +64,8 @@ static int LuaFetch(lua_State *L) {
return LuaNilError(L, "bad method");
}
lua_getfield(L, 2, "followredirect");
if (lua_isboolean(L, -1)) followredirect = lua_toboolean(L, -1);
if (lua_isboolean(L, -1))
followredirect = lua_toboolean(L, -1);
lua_getfield(L, 2, "maxredirects");
maxredirects = luaL_optinteger(L, -1, maxredirects);
lua_getfield(L, 2, "numredirects");
@ -162,8 +163,10 @@ static int LuaFetch(lua_State *L) {
}
#ifndef UNSECURE
if (usingssl) keepalive = kaNONE;
if (usingssl && !sslinitialized) TlsInit();
if (usingssl)
keepalive = kaNONE;
if (usingssl && !sslinitialized)
TlsInit();
#endif
if (url.host.n) {
@ -189,7 +192,8 @@ static int LuaFetch(lua_State *L) {
if (!IsAcceptablePort(port, -1)) {
return LuaNilError(L, "invalid port");
}
if (!hosthdr) hosthdr = gc(xasprintf("%s:%s", host, port));
if (!hosthdr)
hosthdr = gc(xasprintf("%s:%s", host, port));
// check if hosthdr is in keepalive table
if (keepalive && lua_istable(L, 2)) {
@ -317,7 +321,8 @@ static int LuaFetch(lua_State *L) {
if (usingssl) {
rc = mbedtls_ssl_write(&sslcli, request + i, requestlen - i);
if (rc <= 0) {
if (rc == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) goto VerifyFailed;
if (rc == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
goto VerifyFailed;
close(sock);
return LuaNilTlsError(L, "write", rc);
}
@ -373,7 +378,7 @@ static int LuaFetch(lua_State *L) {
WARNF("(ftch) HTTP client %s error", "EOF headers");
goto TransportError;
}
rc = ParseHttpMessage(&msg, inbuf.p, inbuf.n);
rc = ParseHttpMessage(&msg, inbuf.p, inbuf.n, inbuf.c);
if (rc == -1) {
WARNF("(ftch) HTTP client %s error", "ParseHttpMessage");
goto TransportError;
@ -454,7 +459,8 @@ static int LuaFetch(lua_State *L) {
WARNF("(ftch) HTTP client %s error", "Unchunk");
goto TransportError;
}
if (rc) goto Finished;
if (rc)
goto Finished;
break;
default:
__builtin_unreachable();
@ -462,7 +468,8 @@ static int LuaFetch(lua_State *L) {
}
Finished:
if (paylen && logbodies) LogBody("received", inbuf.p + hdrsize, paylen);
if (paylen && logbodies)
LogBody("received", inbuf.p + hdrsize, paylen);
VERBOSEF("(ftch) completed %s HTTP%02d %d %s %`'.*s", method, msg.version,
msg.status, urlarg, FetchHeaderLength(kHttpServer),
FetchHeaderData(kHttpServer));
@ -551,7 +558,8 @@ Finished:
DestroyHttpMessage(&msg);
free(inbuf.p);
if (!keepalive || keepalive == kaCLOSE) close(sock);
if (!keepalive || keepalive == kaCLOSE)
close(sock);
return LuaFetch(L);
} else {
lua_pushinteger(L, msg.status);
@ -559,7 +567,8 @@ Finished:
lua_pushlstring(L, inbuf.p + hdrsize, paylen);
DestroyHttpMessage(&msg);
free(inbuf.p);
if (!keepalive || keepalive == kaCLOSE) close(sock);
if (!keepalive || keepalive == kaCLOSE)
close(sock);
return 3;
}
TransportError:

View file

@ -6458,7 +6458,7 @@ static bool HandleMessageActual(void) {
long reqtime, contime;
char *p;
struct timespec now;
if ((rc = ParseHttpMessage(&cpm.msg, inbuf.p, amtread)) != -1) {
if ((rc = ParseHttpMessage(&cpm.msg, inbuf.p, amtread, inbuf.n)) != -1) {
if (!rc)
return false;
hdrsize = rc;

View file

@ -7,11 +7,8 @@ import sys
def GetDeps(path):
visited = set()
def Dive(path, inside, depth, that_isnt=None):
sys.stdout.write('%s%s' % ('\t' * depth, path))
sys.stdout.write('\n')
if path in visited:
return
visited.add(path)
included = path
if not os.path.exists(path):
if inside:
samedir = os.path.join(os.path.dirname(inside), path)
@ -24,6 +21,15 @@ def GetDeps(path):
else:
# sys.stderr.write('not found: %s\n' % (path))
return
sys.stdout.write('\t' * depth)
sys.stdout.write(path)
sys.stdout.write('\n')
if path in visited:
return
visited.add(path)
with open(path) as f:
code = f.read()
for dep in re.findall(r'[.#]\s*include\s+[<"]([^">]+)[">]', code):