Docker wrapper now waits for chunked responses

main^2
Jef Roosens 2022-02-20 13:03:00 +01:00
parent 275227400f
commit fbba66caa5
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
2 changed files with 22 additions and 5 deletions

View File

@ -3,6 +3,6 @@ module main
import docker import docker
fn build() { fn build() {
println(docker.pull('archlinux', 'latest') or { panic('yeetus') }) println(docker.pull('nginx', 'latest') or { panic('yeetus') })
// println(docker.containers() or { panic('heet') }) // println(docker.containers() or { panic('heet') })
} }

View File

@ -23,12 +23,31 @@ fn send(req &string) ?http.Response {
// Write the request to the socket // Write the request to the socket
s.write_string(req) ? s.write_string(req) ?
s.wait_for_write() ?
mut c := 0
mut buf := []byte{len: docker.buf_len} mut buf := []byte{len: docker.buf_len}
mut res := []byte{} mut res := []byte{}
mut c := 0 for {
c = s.read(mut buf) or { return error('Failed to read data from socket.') }
res << buf[..c]
for res.len < 5 && res#[-4..] != [0, '\r', `\n`, `\r`, `\n`] { if c < docker.buf_len {
break
}
}
// If the response isn't a chunked reply, we return early
parsed := http.parse_response(res.bytestr()) ?
if parsed.header.get(http.CommonHeader.transfer_encoding) or { '' } != 'chunked' {
return parsed
}
// We loop until we've encountered the end of the chunked response
for res.len < 5 || res#[-5..] != [byte(`0`), `\r`, `\n`, `\r`, `\n`] {
// Wait for the server to respond // Wait for the server to respond
s.wait_for_write() ? s.wait_for_write() ?
@ -42,8 +61,6 @@ fn send(req &string) ?http.Response {
} }
} }
println(res)
// Decode chunked response // Decode chunked response
return http.parse_response(res.bytestr()) return http.parse_response(res.bytestr())