Fix leak in http.

This commit is contained in:
Jordan Bancino 2023-05-23 20:19:27 +00:00
parent 441599b088
commit b1049a9a70

View file

@ -46,10 +46,10 @@ usage(char *prog)
int int
Main(Array * args) Main(Array * args)
{ {
HttpClientContext *cx; HttpClientContext *cx = NULL;
HttpStatus res; HttpStatus res;
HttpRequestMethod method = HTTP_GET; HttpRequestMethod method = HTTP_GET;
Uri *uri; Uri *uri = NULL;
char *data = NULL; char *data = NULL;
HashMap *requestHeaders = HashMapCreate(); HashMap *requestHeaders = HashMapCreate();
@ -61,6 +61,7 @@ Main(Array * args)
int requestFlags = HTTP_FLAG_NONE; int requestFlags = HTTP_FLAG_NONE;
int ch; int ch;
int ret = 1;
ArgParseStateInit(&arg); ArgParseStateInit(&arg);
while ((ch = ArgParse(&arg, args, "iH:X:d:")) != -1) while ((ch = ArgParse(&arg, args, "iH:X:d:")) != -1)
@ -102,21 +103,21 @@ Main(Array * args)
break; break;
default: default:
usage(ArrayGet(args, 0)); usage(ArrayGet(args, 0));
return 1; goto finish;
} }
} }
if (ArraySize(args) - arg.optInd < 1) if (ArraySize(args) - arg.optInd < 1)
{ {
usage(ArrayGet(args, 0)); usage(ArrayGet(args, 0));
return 1; goto finish;
} }
uri = UriParse(ArrayGet(args, arg.optInd)); uri = UriParse(ArrayGet(args, arg.optInd));
if (!uri) if (!uri)
{ {
StreamPrintf(StreamStderr(), "Failed to parse URI: %s\n", ArrayGet(args, arg.optInd)); StreamPrintf(StreamStderr(), "Failed to parse URI: %s\n", ArrayGet(args, arg.optInd));
return 1; goto finish;
} }
if (!uri->port) if (!uri->port)
@ -134,8 +135,7 @@ Main(Array * args)
if (!uri->port) if (!uri->port)
{ {
StreamPrintf(StreamStderr(), "Unknown protocol: %s\n", uri->proto); StreamPrintf(StreamStderr(), "Unknown protocol: %s\n", uri->proto);
UriFree(uri); goto finish;
return 1;
} }
if (StrEquals(uri->proto, "https")) if (StrEquals(uri->proto, "https"))
@ -148,8 +148,7 @@ Main(Array * args)
if (!cx) if (!cx)
{ {
StreamPuts(StreamStderr(), "Failed to connect.\n"); StreamPuts(StreamStderr(), "Failed to connect.\n");
UriFree(uri); goto finish;
return 1;
} }
while (HashMapIterate(requestHeaders, &key, (void **) &val)) while (HashMapIterate(requestHeaders, &key, (void **) &val))
@ -158,8 +157,6 @@ Main(Array * args)
Free(val); Free(val);
} }
HashMapFree(requestHeaders);
if (data) if (data)
{ {
if (*data == '@') if (*data == '@')
@ -181,7 +178,7 @@ Main(Array * args)
if (!in) if (!in)
{ {
StreamPrintf(StreamStderr(), "%s: %s\n", data, strerror(errno)); StreamPrintf(StreamStderr(), "%s: %s\n", data, strerror(errno));
return 1; goto finish;
} }
len = StreamSeek(in, 0, SEEK_END); len = StreamSeek(in, 0, SEEK_END);
@ -229,9 +226,7 @@ Main(Array * args)
if (!res) if (!res)
{ {
StreamPuts(StreamStderr(), "Failed to send request.\n"); StreamPuts(StreamStderr(), "Failed to send request.\n");
HttpClientContextFree(cx); goto finish;
UriFree(uri);
return 1;
} }
if (flags & FLAG_HEADERS) if (flags & FLAG_HEADERS)
@ -250,8 +245,13 @@ Main(Array * args)
StreamCopy(HttpClientStream(cx), StreamStdout()); StreamCopy(HttpClientStream(cx), StreamStdout());
ret = !(res == HTTP_OK);
finish:
HashMapFree(requestHeaders);
HttpClientContextFree(cx); HttpClientContextFree(cx);
UriFree(uri); UriFree(uri);
return !(res == HTTP_OK); return ret;
} }