update
This commit is contained in:
62
examples/http1-1.c
Normal file
62
examples/http1-1.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#define DJSTD_BASIC_ENTRY
|
||||
#include "../core.c"
|
||||
|
||||
int djstd_entry(Arena *arena, StringList args) {
|
||||
Socket sock = socketConnect(arena, (SocketConnectInfo){
|
||||
.address="dlh.mediasuite.zii.aero",
|
||||
.port=443,
|
||||
.blocking=true
|
||||
});
|
||||
|
||||
println("%d", sock.closed);
|
||||
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);
|
||||
|
||||
StringList body = EmptyList();
|
||||
bool streamingBody = false;
|
||||
|
||||
Forever {
|
||||
StringResult response = socketReadStr(arena, &sock);
|
||||
if (response.valid) {
|
||||
if (streamingBody) {
|
||||
if (body.capacity - body.length >= response.result.length) {
|
||||
ListAppend(body, response.result);
|
||||
}
|
||||
} 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) {
|
||||
ListAppend(body, *line);
|
||||
} else {
|
||||
StringList split = strSplit(arena, s("Content-Length: "), *line);
|
||||
if (split.length > 1) {
|
||||
Int32Result lengthResult = parsePositiveInt(split.data[1]);
|
||||
if (lengthResult.valid) {
|
||||
body = PushList(arena, StringList, lengthResult.result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (streamingBody == true && body.length == body.capacity) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printStr(strListJoin(arena, body));
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user