docker/docker.v

162 lines
4.3 KiB
Coq
Raw Normal View History

2022-06-22 09:15:00 +02:00
module docker
2022-06-18 17:59:22 +02:00
import net.unix
import io
import net.http
import strings
import json
import util
const (
socket = '/var/run/docker.sock'
buf_len = 10 * 1024
http_separator = [u8(`\r`), `\n`, `\r`, `\n`]
http_chunk_separator = [u8(`\r`), `\n`]
2022-06-21 20:28:25 +02:00
timestamp_attr = 'timestamp'
api_version = 'v1.41'
2022-06-18 17:59:22 +02:00
)
[heap]
2022-06-18 17:59:22 +02:00
pub struct DockerConn {
mut:
socket &unix.StreamConn
reader &io.BufferedReader
// Data for the request that's currently being constructed.
method http.Method
url string
params map[string]string
content_type string
// Before send: body of the request
// After send: body of response
body string
// HTTP head of the response
head http.Response
2022-06-18 17:59:22 +02:00
}
// new_conn creates a new connection to the Docker daemon.
2022-11-01 19:14:25 +01:00
pub fn new_conn() !&DockerConn {
s := unix.connect_stream(docker.socket)!
2022-06-18 17:59:22 +02:00
d := &DockerConn{
socket: s
reader: io.new_buffered_reader(reader: s)
}
return d
}
// close closes the underlying socket connection.
2022-11-01 19:14:25 +01:00
pub fn (mut d DockerConn) close() ! {
d.socket.close()!
2022-06-18 17:59:22 +02:00
}
// read_response_head consumes the socket's contents until it encounters
// '\r\n\r\n', after which it parses the response as an HTTP response.
// Importantly, this function never consumes the reader past the HTTP
// separator, so the body can be read fully later on.
fn (mut d DockerConn) read_response_head() ! {
2022-06-18 17:59:22 +02:00
mut res := []u8{}
2022-11-01 19:14:25 +01:00
util.read_until_separator(mut d.reader, mut res, docker.http_separator)!
2022-06-18 17:59:22 +02:00
d.head = http.parse_response(res.bytestr())!
2022-06-18 17:59:22 +02:00
}
2023-01-05 16:48:17 +01:00
// read_response_body consumes the rest of the HTTP response and stores it as
// the response body. This function should only be called after
// read_response_head. This function always reads the entire response into
// memory, even if it's chunked.
fn (mut d DockerConn) read_response_body() ! {
if d.head.status() == .no_content {
return
}
mut builder := strings.new_builder(docker.buf_len)
if d.head.header.get(.transfer_encoding) or { '' } == 'chunked' {
mut body_stream := d.get_chunked_response_reader()
util.reader_to_writer(mut body_stream, mut builder)!
} else {
content_length := d.head.header.get(.content_length)!.int()
if content_length == 0 {
2023-01-05 16:57:55 +01:00
d.body = ''
return
}
mut buf := []u8{len: docker.buf_len}
mut c := 0
2022-06-18 17:59:22 +02:00
for builder.len < content_length {
c = d.reader.read(mut buf)!
2022-06-18 17:59:22 +02:00
builder.write(buf[..c])!
}
2022-06-18 17:59:22 +02:00
}
d.body = builder.str()
2022-06-18 17:59:22 +02:00
}
2023-01-05 16:48:17 +01:00
// read_response is a convenience function that always consumes the entire
// response and loads it into memory. It should only be used when we're certain
// that the result isn't too large, as even chunked responses will get fully
// loaded into memory.
fn (mut d DockerConn) read_response() ! {
d.read_response_head()!
d.check_error()!
d.read_response_body()!
2022-06-18 17:59:22 +02:00
}
// read_json_response<T> is a convenience function that runs read_response
// before parsing its contents, which is assumed to be JSON, into a struct.
2022-11-01 19:14:25 +01:00
fn (mut d DockerConn) read_json_response<T>() !T {
d.read_response()!
data := json.decode(T, d.body)!
2022-06-21 20:28:25 +02:00
//$for field in T.fields {
//$if field.typ is time.Time {
// data.$(field.name) = time.parse_rfc3339(data.$(field.name + '_str'))?
//}
//}
2022-06-21 20:28:25 +02:00
return data
}
2022-06-18 17:59:22 +02:00
// get_chunked_response_reader returns a ChunkedResponseReader using the socket
2023-01-05 16:48:17 +01:00
// as reader. This function should only be called after check_error.
fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
2022-06-18 17:59:22 +02:00
r := new_chunked_response_reader(d.reader)
return r
}
// get_stream_format_reader returns a StreamFormatReader using the socket as
2023-01-05 16:48:17 +01:00
// reader. This function should only be called after check_error.
fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
2022-06-18 17:59:22 +02:00
r := new_chunked_response_reader(d.reader)
r2 := new_stream_format_reader(r)
return r2
}
struct DockerError {
pub:
message string
}
// check_error should be called after read_response_head. If the status code of
// the response is an error, the body is consumed and the Docker HTTP error is
// returned as a V error. If the status isn't the error, this function is a
2023-01-05 16:48:17 +01:00
// no-op, and the body can be read.
fn (mut d DockerConn) check_error() ! {
if d.head.status().is_error() {
d.read_response_body()!
d_err := json.decode(DockerError, d.body)!
return error_with_code('$d.head.status(): $d_err.message', d.head.status_code)
}
}