66 lines
2.4 KiB
C
66 lines
2.4 KiB
C
#define DJSTD_BASIC_ENTRY
|
|
#include "core.c"
|
|
|
|
int djstd_entry(Arena *arena, StringList args) {
|
|
Socket sock = socketConnect(arena, (SocketConnectInfo){ .address="::1", .port=8080, .blocking=true });
|
|
|
|
string newLine = s("\r\n");
|
|
string lines[] = {
|
|
s("GET / HTTP/1.1"),
|
|
s("Host: localhost"),
|
|
};
|
|
|
|
for (EachInArray(lines, i)) {
|
|
socketWriteStr(&sock, lines[i]);
|
|
socketWriteStr(&sock, newLine);
|
|
}
|
|
socketWriteStr(&sock, newLine);
|
|
|
|
CharList body = EmptyList();
|
|
bool streamingBody = false;
|
|
|
|
Forever {
|
|
StringResult response = socketReadStr(arena, &sock);
|
|
if (response.valid) {
|
|
if (streamingBody) {
|
|
if (body.capacity - body.length >= response.result.length) {
|
|
memcpy(body.data + body.length, response.result.str, response.result.length);
|
|
body.length += response.result.length;
|
|
}
|
|
} else {
|
|
StringList lines = strSplit(arena, s("\r\n"), response.result);
|
|
for (EachEl(lines, string, line)) {
|
|
if (body.capacity > 0 && strEql(*line, s(""))) {
|
|
streamingBody = true;
|
|
} else if (streamingBody && (body.capacity - body.length) >= line->length) {
|
|
// TODO(dledda): append list to list, string to string, whatever
|
|
// TODO(dledda): `joinStringList`
|
|
memcpy(body.data + body.length, line->str, line->length);
|
|
body.length += line->length;
|
|
} else {
|
|
// TODO(dledda): `strContains`
|
|
// TODO(dledda): `stringContains` -> `strContainsChar`
|
|
StringList split = strSplit(arena, s("Content-Length: "), *line);
|
|
if (split.length > 1) {
|
|
Int32Result lengthResult = parsePositiveInt(split.data[1]);
|
|
if (lengthResult.valid) {
|
|
body = PushList(arena, CharList, lengthResult.result);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (streamingBody == true && body.length == body.capacity) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
// TODO(dledda): string from CharList/ByteList
|
|
// TODO(dledda): `printStr`
|
|
print("%S", (string){.str=body.data, .length=body.length});
|
|
|
|
return 0;
|
|
}
|