refactor(docker): split stream separator code into own function

Jef Roosens 2022-05-16 13:03:44 +02:00
parent ce67208fbd
commit 97cdaa18e1
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 39 additions and 45 deletions

View File

@ -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)

View File

@ -71,30 +71,9 @@ pub fn (mut d DockerDaemon) send_request_with_json<T>(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())
}

View File

@ -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())?

View File

@ -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<T>(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
}
}
}
}