2022-05-11 10:03:43 +02:00
|
|
|
module docker
|
|
|
|
|
|
|
|
import net.unix
|
|
|
|
import io
|
|
|
|
import net.http
|
2022-05-13 21:28:24 +02:00
|
|
|
import strings
|
|
|
|
import net.urllib
|
|
|
|
import json
|
2022-05-14 21:55:19 +02:00
|
|
|
import util
|
2022-05-11 10:03:43 +02:00
|
|
|
|
2022-05-13 21:28:24 +02:00
|
|
|
const (
|
2022-05-14 21:55:19 +02:00
|
|
|
socket = '/var/run/docker.sock'
|
|
|
|
buf_len = 10 * 1024
|
|
|
|
http_separator = [u8(`\r`), `\n`, `\r`, `\n`]
|
|
|
|
http_chunk_separator = [u8(`\r`), `\n`]
|
2022-05-13 21:28:24 +02:00
|
|
|
)
|
2022-05-11 10:03:43 +02:00
|
|
|
|
|
|
|
pub struct DockerDaemon {
|
|
|
|
mut:
|
|
|
|
socket &unix.StreamConn
|
|
|
|
reader &io.BufferedReader
|
|
|
|
}
|
|
|
|
|
2022-05-13 21:28:24 +02:00
|
|
|
pub fn new_conn() ?&DockerDaemon {
|
|
|
|
s := unix.connect_stream(docker.socket)?
|
2022-05-11 10:03:43 +02:00
|
|
|
|
2022-05-13 21:28:24 +02:00
|
|
|
d := &DockerDaemon{
|
|
|
|
socket: s
|
|
|
|
reader: io.new_buffered_reader(reader: s)
|
|
|
|
}
|
2022-05-11 10:03:43 +02:00
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2022-05-13 21:28:24 +02:00
|
|
|
pub fn (mut d DockerDaemon) send_request(method string, url urllib.URL) ? {
|
|
|
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
|
|
|
|
|
|
|
d.socket.write_string(req)?
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut d DockerDaemon) send_request_with_body(method string, url urllib.URL, content_type string, body string) ? {
|
|
|
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n'
|
|
|
|
|
|
|
|
d.socket.write_string(req)?
|
|
|
|
}
|
|
|
|
|
2022-05-13 22:03:06 +02:00
|
|
|
pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.URL, data &T) ? {
|
2022-05-13 21:28:24 +02:00
|
|
|
body := json.encode(data)
|
|
|
|
|
2022-05-13 22:03:06 +02:00
|
|
|
return d.send_request_with_body(method, url, 'application/json', body)
|
2022-05-11 10:03:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read_response_head consumes the socket's contents until it encounters
|
2022-05-13 21:28:24 +02:00
|
|
|
// '\r\n\r\n', after which it parses the response as an HTTP response.
|
2022-05-13 22:15:30 +02:00
|
|
|
// Importantly, this function never consumes past the HTTP separator, so the
|
|
|
|
// body can be read fully later on.
|
2022-05-13 21:28:24 +02:00
|
|
|
pub fn (mut d DockerDaemon) read_response_head() ?http.Response {
|
2022-05-11 10:03:43 +02:00
|
|
|
mut c := 0
|
2022-05-13 21:28:24 +02:00
|
|
|
mut buf := []u8{len: 4}
|
2022-05-11 10:03:43 +02:00
|
|
|
mut res := []u8{}
|
|
|
|
|
|
|
|
for {
|
2022-05-13 21:28:24 +02:00
|
|
|
c = d.reader.read(mut buf)?
|
2022-05-11 10:03:43 +02:00
|
|
|
res << buf[..c]
|
|
|
|
|
2022-05-14 21:55:19 +02:00
|
|
|
match_len := util.match_array_in_array(buf[..c], docker.http_separator)
|
|
|
|
// mut i := 0
|
|
|
|
// mut match_len := 0
|
2022-05-13 21:28:24 +02:00
|
|
|
|
2022-05-14 21:55:19 +02:00
|
|
|
// for i + match_len < c {
|
|
|
|
// if buf[i + match_len] == docker.http_separator[match_len] {
|
|
|
|
// match_len += 1
|
|
|
|
// } else {
|
|
|
|
// i += match_len + 1
|
|
|
|
// match_len = 0
|
|
|
|
// }
|
|
|
|
//}
|
2022-05-13 21:28:24 +02:00
|
|
|
|
|
|
|
if match_len == 4 {
|
2022-05-11 10:03:43 +02:00
|
|
|
break
|
2022-05-13 21:28:24 +02:00
|
|
|
} else 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
|
|
|
|
}
|
2022-05-11 10:03:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.parse_response(res.bytestr())
|
|
|
|
}
|
2022-05-13 21:28:24 +02:00
|
|
|
|
|
|
|
pub fn (mut d DockerDaemon) read_response_body(length int) ?string {
|
2022-05-13 22:15:30 +02:00
|
|
|
if length == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2022-05-13 21:28:24 +02:00
|
|
|
mut buf := []u8{len: docker.buf_len}
|
|
|
|
mut c := 0
|
|
|
|
mut builder := strings.new_builder(docker.buf_len)
|
|
|
|
|
|
|
|
for builder.len < length {
|
|
|
|
c = d.reader.read(mut buf) or { break }
|
|
|
|
|
|
|
|
builder.write(buf[..c])?
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.str()
|
|
|
|
}
|
2022-05-13 22:03:06 +02:00
|
|
|
|
|
|
|
pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
|
|
|
head := d.read_response_head()?
|
|
|
|
content_length := head.header.get(http.CommonHeader.content_length)?.int()
|
|
|
|
res := d.read_response_body(content_length)?
|
|
|
|
|
|
|
|
return head, res
|
|
|
|
}
|
2022-05-14 21:55:19 +02:00
|
|
|
|
|
|
|
pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader {
|
|
|
|
r := new_chunked_response_reader(d.reader)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2022-05-14 22:39:52 +02:00
|
|
|
|
|
|
|
pub fn (mut d DockerDaemon) get_stream_format_reader() &StreamFormatReader {
|
|
|
|
r := new_chunked_response_reader(d.reader)
|
|
|
|
r2 := new_stream_format_reader(r)
|
|
|
|
|
|
|
|
return r2
|
|
|
|
}
|