From 97cdaa18e1bb435ca512e81b70cb4fa3bc13f414 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 16 May 2022 13:03:44 +0200 Subject: [PATCH] refactor(docker): split stream separator code into own function --- CHANGELOG.md | 2 ++ src/docker/docker.v | 23 +---------------------- src/docker/stream.v | 23 ++--------------------- src/util/util.v | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e17cd60..c86761c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Web API for adding & querying build logs * CLI commands to access build logs API * Cron build logs are uploaded to above API +* Proper ASCII table output in CLI ### Changed @@ -20,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Official Arch packages are now split between `vieter` & `vieter-git` * `vieter` is the latest release * `vieter-git` is the latest commit on the dev branch +* Full refactor of Docker socket code ## [0.3.0-alpha.1](https://git.rustybever.be/vieter/vieter/src/tag/0.3.0-alpha.1) diff --git a/src/docker/docker.v b/src/docker/docker.v index 2ee9bff7..f612d1f1 100644 --- a/src/docker/docker.v +++ b/src/docker/docker.v @@ -71,30 +71,9 @@ pub fn (mut d DockerDaemon) send_request_with_json(method string, url urllib. // Importantly, this function never consumes the reader past the HTTP // separator, so the body can be read fully later on. pub fn (mut d DockerDaemon) read_response_head() ?http.Response { - mut c := 0 - mut buf := []u8{len: 4} mut res := []u8{} - for { - c = d.reader.read(mut buf)? - res << buf[..c] - - match_len := util.match_array_in_array(buf[..c], docker.http_separator) - - if match_len == 4 { - break - } - - if match_len > 0 { - mut buf2 := []u8{len: 4 - match_len} - c2 := d.reader.read(mut buf2)? - res << buf2[..c2] - - if buf2 == docker.http_separator[match_len..] { - break - } - } - } + util.read_until_separator(mut d.reader, mut res, http_separator) ? return http.parse_response(res.bytestr()) } diff --git a/src/docker/stream.v b/src/docker/stream.v index f20fe185..ed73098f 100644 --- a/src/docker/stream.v +++ b/src/docker/stream.v @@ -59,7 +59,6 @@ pub fn (mut r ChunkedResponseReader) read(mut buf []u8) ?int { // completely consumed. fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 { mut buf := []u8{len: 2} - mut res := []u8{} if r.started { // Each chunk ends with a `\r\n` which we want to skip first @@ -68,26 +67,8 @@ fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 { r.started = true - for { - c := r.reader.read(mut buf)? - res << buf[..c] - - match_len := util.match_array_in_array(buf[..c], http_chunk_separator) - - if match_len == http_chunk_separator.len { - break - } - - if match_len > 0 { - mut buf2 := []u8{len: 2 - match_len} - c2 := r.reader.read(mut buf2)? - res << buf2[..c2] - - if buf2 == http_chunk_separator[match_len..] { - break - } - } - } + mut res := []u8{} + util.read_until_separator(mut r.reader, mut res, http_chunk_separator) ? // The length of the next chunk is provided as a hexadecimal mut num_data := hex.decode(res#[..-2].bytestr())? diff --git a/src/util/util.v b/src/util/util.v index 324fb3d1..9cf3011c 100644 --- a/src/util/util.v +++ b/src/util/util.v @@ -28,9 +28,14 @@ pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ? { mut buf := []u8{len: 10 * 1024} for { - reader.read(mut buf) or { break } + bytes_read := reader.read(mut buf) or { break } + mut bytes_written := 0 - writer.write(buf) or { break } + for bytes_written < bytes_read { + c := writer.write(buf[bytes_written..bytes_read]) or { break } + + bytes_written += c + } } } @@ -122,3 +127,30 @@ pub fn match_array_in_array(a1 []T, a2 []T) int { return match_len } + +// read_until_separator consumes an io.Reader until it encounters some +// separator array. The data read is stored inside the provided res array. +pub fn read_until_separator(mut reader io.Reader, mut res []u8, sep []u8) ? { + mut buf := []u8{len: sep.len} + + for { + c := reader.read(mut buf)? + res << buf[..c] + + match_len := match_array_in_array(buf[..c], sep) + + if match_len == sep.len { + break + } + + if match_len > 0 { + match_left := sep.len - match_len + c2 := reader.read(mut buf[..match_left])? + res << buf[..c2] + + if buf[..c2] == sep[match_len..] { + break + } + } + } +}