Nice bit of refactoring #1

Merged
Jef Roosens merged 9 commits from fix-segfaults into main 2023-01-05 16:58:20 +01:00
9 changed files with 178 additions and 217 deletions

View File

@ -1,6 +1,6 @@
# =====CONFIG===== # =====CONFIG=====
V_PATH ?= v V_PATH ?= v
V := $(V_PATH) -showcc V := $(V_PATH) -showcc -d use_openssl
all: vdocker all: vdocker
@ -10,6 +10,10 @@ all: vdocker
vdocker: vdocker:
$(V) -g -shared . $(V) -g -shared .
.PHONY: c
c:
$(V) -o docker.c .
# =====DOCS===== # =====DOCS=====
.PHONY: api-docs .PHONY: api-docs

View File

@ -42,7 +42,7 @@ reference](https://docs.docker.com/engine/api/v1.41/).
- [ ] Inspect an image - [ ] Inspect an image
- [ ] Get the history of an image - [ ] Get the history of an image
- [ ] Push an image - [ ] Push an image
- [ ] Tag an image - [x] Tag an image
- [ ] Remove an image - [ ] Remove an image
- [ ] Search images - [ ] Search images
- [ ] Delete unused images - [ ] Delete unused images

View File

@ -1,8 +1,6 @@
module docker module docker
import json
import time import time
import net.http { Method }
import types { ContainerListItem } import types { ContainerListItem }
[params] [params]
@ -14,7 +12,7 @@ pub struct ContainerListConfig {
} }
pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerListItem { pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerListItem {
d.get('/containers/json') d.request(.get, '/containers/json')
d.params(c) d.params(c)
d.send()! d.send()!
@ -37,30 +35,18 @@ pub:
} }
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer { pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
d.send_request_with_json(Method.post, '/containers/create', c)! d.request(.post, '/containers/create')
head, res := d.read_response()! d.body_json(c)
d.send()!
if head.status_code != 201 { return d.read_json_response<CreatedContainer>()
data := json.decode(DockerError, res)!
return error(data.message)
}
data := json.decode(CreatedContainer, res)!
return data
} }
// start_container starts the container with the given id. // start_container starts the container with the given id.
pub fn (mut d DockerConn) container_start(id string) ! { pub fn (mut d DockerConn) container_start(id string) ! {
d.send_request(Method.post, '/containers/$id/start')! d.request(.post, '/containers/$id/start')
head, body := d.read_response()! d.send()!
d.read_response()!
if head.status_code != 204 {
data := json.decode(DockerError, body)!
return error(data.message)
}
} }
struct ContainerInspect { struct ContainerInspect {
@ -82,16 +68,10 @@ pub mut:
} }
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect { pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
d.send_request(Method.get, '/containers/$id/json')! d.request(.get, '/containers/$id/json')
head, body := d.read_response()! d.send()!
if head.status_code != 200 { mut data := d.read_json_response<ContainerInspect>()!
data := json.decode(DockerError, body)!
return error(data.message)
}
mut data := json.decode(ContainerInspect, body)!
// The Docker engine API *should* always return UTC time. // The Docker engine API *should* always return UTC time.
data.state.start_time = time.parse_rfc3339(data.state.start_time_str)! data.state.start_time = time.parse_rfc3339(data.state.start_time_str)!
@ -104,27 +84,20 @@ pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
} }
pub fn (mut d DockerConn) container_remove(id string) ! { pub fn (mut d DockerConn) container_remove(id string) ! {
d.send_request(Method.delete, '/containers/$id')! d.request(.delete, '/containers/$id')
head, body := d.read_response()! d.send()!
d.read_response()!
if head.status_code != 204 {
data := json.decode(DockerError, body)!
return error(data.message)
}
} }
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader { pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
d.send_request(Method.get, '/containers/$id/logs?stdout=true&stderr=true')! d.request(.get, '/containers/$id/logs')
head := d.read_response_head()! d.params({
'stdout': 'true'
if head.status_code != 200 { 'stderr': 'true'
content_length := head.header.get(http.CommonHeader.content_length)!.int() })
body := d.read_response_body(content_length)! d.send()!
data := json.decode(DockerError, body)! d.read_response_head()!
d.check_error()!
return error(data.message)
}
return d.get_stream_format_reader() return d.get_stream_format_reader()
} }

155
docker.v
View File

@ -4,7 +4,6 @@ import net.unix
import io import io
import net.http import net.http
import strings import strings
import net.urllib
import json import json
import util import util
@ -27,7 +26,11 @@ mut:
url string url string
params map[string]string params map[string]string
content_type string content_type string
body string // Before send: body of the request
// After send: body of response
body string
// HTTP head of the response
head http.Response
} }
// new_conn creates a new connection to the Docker daemon. // new_conn creates a new connection to the Docker daemon.
@ -47,113 +50,71 @@ pub fn (mut d DockerConn) close() ! {
d.socket.close()! d.socket.close()!
} }
// send_request sends an HTTP request without body.
fn (mut d DockerConn) send_request(method http.Method, url_str string) ! {
url := urllib.parse('/$docker.api_version$url_str')!
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
d.socket.write_string(req)!
// When starting a new request, the reader needs to be reset.
d.reader = io.new_buffered_reader(reader: d.socket)
}
// send_request_with_body sends an HTTP request with the given body.
fn (mut d DockerConn) send_request_with_body(method http.Method, url_str string, content_type string, body string) ! {
url := urllib.parse('/$docker.api_version$url_str')!
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)!
// When starting a new request, the reader needs to be reset.
d.reader = io.new_buffered_reader(reader: d.socket)
}
// send_request_with_json<T> is a convenience wrapper around
// send_request_with_body that encodes the input as JSON.
fn (mut d DockerConn) send_request_with_json<T>(method http.Method, url_str string, data &T) ! {
body := json.encode(data)
return d.send_request_with_body(method, url_str, 'application/json', body)
}
// read_response_head consumes the socket's contents until it encounters // 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. // '\r\n\r\n', after which it parses the response as an HTTP response.
// Importantly, this function never consumes the reader past the HTTP // Importantly, this function never consumes the reader past the HTTP
// separator, so the body can be read fully later on. // separator, so the body can be read fully later on.
fn (mut d DockerConn) read_response_head() !http.Response { fn (mut d DockerConn) read_response_head() ! {
mut res := []u8{} mut res := []u8{}
util.read_until_separator(mut d.reader, mut res, docker.http_separator)! util.read_until_separator(mut d.reader, mut res, docker.http_separator)!
return http.parse_response(res.bytestr()) d.head = http.parse_response(res.bytestr())!
} }
// read_response_body reads `length` bytes from the stream. It can be used when // read_response_body consumes the rest of the HTTP response and stores it as
// the response encoding isn't chunked to fully read it. // the response body. This function should only be called after
fn (mut d DockerConn) read_response_body(length int) !string { // read_response_head. This function always reads the entire response into
if length == 0 { // memory, even if it's chunked.
return '' fn (mut d DockerConn) read_response_body() ! {
if d.head.status() == .no_content {
return
} }
mut buf := []u8{len: docker.buf_len}
mut c := 0
mut builder := strings.new_builder(docker.buf_len) mut builder := strings.new_builder(docker.buf_len)
for builder.len < length { if d.head.header.get(.transfer_encoding) or { '' } == 'chunked' {
c = d.reader.read(mut buf) or { break } mut body_stream := d.get_chunked_response_reader()
builder.write(buf[..c])! util.reader_to_writer(mut body_stream, mut builder)!
} else {
content_length := d.head.header.get(.content_length)!.int()
if content_length == 0 {
d.body = ''
return
}
mut buf := []u8{len: docker.buf_len}
mut c := 0
for builder.len < content_length {
c = d.reader.read(mut buf)!
builder.write(buf[..c])!
}
} }
return builder.str() d.body = builder.str()
} }
// read_response is a convenience function which always consumes the entire // read_response is a convenience function that always consumes the entire
// response & returns it. It should only be used when we're certain that the // response and loads it into memory. It should only be used when we're certain
// result isn't too large. // that the result isn't too large, as even chunked responses will get fully
fn (mut d DockerConn) read_response() !(http.Response, string) { // loaded into memory.
head := d.read_response_head()! fn (mut d DockerConn) read_response() ! {
d.read_response_head()!
if head.status().is_error() { d.check_error()!
content_length := head.header.get(.content_length)!.int() d.read_response_body()!
body := d.read_response_body(content_length)!
mut err := json.decode(DockerError, body)!
err.status = head.status_code
return err
}
// 204 means "No Content", so we can assume nothing follows after this
if head.status() == .no_content {
return head, ''
}
if head.header.get(http.CommonHeader.transfer_encoding) or { '' } == 'chunked' {
mut builder := strings.new_builder(1024)
mut body := d.get_chunked_response_reader()
util.reader_to_writer(mut body, mut builder)!
return head, builder.str()
}
content_length := head.header.get(http.CommonHeader.content_length)!.int()
body := d.read_response_body(content_length)!
return head, body
} }
// 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.
fn (mut d DockerConn) read_json_response<T>() !T { fn (mut d DockerConn) read_json_response<T>() !T {
head, body := d.read_response()! d.read_response()!
if head.status_code < 200 || head.status_code > 300 { data := json.decode(T, d.body)!
data := json.decode(DockerError, body)!
return docker_error(head.status_code, data.message)
}
mut data := json.decode(T, body)!
//$for field in T.fields { //$for field in T.fields {
//$if field.typ is time.Time { //$if field.typ is time.Time {
@ -165,7 +126,7 @@ fn (mut d DockerConn) read_json_response<T>() !T {
} }
// get_chunked_response_reader returns a ChunkedResponseReader using the socket // get_chunked_response_reader returns a ChunkedResponseReader using the socket
// as reader. // as reader. This function should only be called after check_error.
fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader { fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
r := new_chunked_response_reader(d.reader) r := new_chunked_response_reader(d.reader)
@ -173,10 +134,28 @@ fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
} }
// get_stream_format_reader returns a StreamFormatReader using the socket as // get_stream_format_reader returns a StreamFormatReader using the socket as
// reader. // reader. This function should only be called after check_error.
fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader { fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
r := new_chunked_response_reader(d.reader) r := new_chunked_response_reader(d.reader)
r2 := new_stream_format_reader(r) r2 := new_stream_format_reader(r)
return r2 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
// 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)
}
}

View File

@ -1,22 +0,0 @@
module docker
struct DockerError {
pub mut:
status int [skip]
message string
}
fn (err DockerError) code() int {
return err.status
}
fn (err DockerError) msg() string {
return err.message
}
fn docker_error(status int, message string) DockerError {
return DockerError{
status: status
message: message
}
}

View File

@ -1,31 +1,26 @@
module docker module docker
import net.http { Method }
import types { Image } import types { Image }
import json
pub fn (mut d DockerConn) image_inspect(image string) !Image { pub fn (mut d DockerConn) image_inspect(image string) !Image {
d.send_request(.get, '/images/$image/json')! d.request(.get, '/images/$image/json')
_, body := d.read_response()! d.send()!
data := json.decode(Image, body)! data := d.read_json_response<Image>()!
return data return data
} }
// pull_image pulls the given image:tag. // image_pull pulls the given image:tag.
pub fn (mut d DockerConn) pull_image(image string, tag string) ! { pub fn (mut d DockerConn) image_pull(image string, tag string) ! {
d.send_request(Method.post, '/images/create?fromImage=$image&tag=$tag')! d.request(.post, '/images/create')
head := d.read_response_head()! d.params({
'fromImage': image
if head.status().is_error() { 'tag': tag
content_length := head.header.get(.content_length)!.int() })
body := d.read_response_body(content_length)! d.send()!
mut err := json.decode(DockerError, body)! d.read_response_head()!
err.status = head.status_code d.check_error()!
return err
}
// Keep reading the body until the pull has completed // Keep reading the body until the pull has completed
mut body := d.get_chunked_response_reader() mut body := d.get_chunked_response_reader()
@ -38,17 +33,31 @@ pub fn (mut d DockerConn) pull_image(image string, tag string) ! {
} }
// create_image_from_container creates a new image from a container. // create_image_from_container creates a new image from a container.
pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) !Image { pub fn (mut d DockerConn) image_from_container(id string, repo string, tag string) !Image {
d.send_request(.post, '/commit?container=$id&repo=$repo&tag=$tag')! d.request(.post, '/commit')
_, body := d.read_response()! d.params({
'container': id
'repo': repo
'tag': tag
})
d.send()!
data := json.decode(Image, body)! return d.read_json_response<Image>()!
return data
} }
// remove_image removes the image with the given id. // remove_image removes the image with the given id.
pub fn (mut d DockerConn) remove_image(id string) ! { pub fn (mut d DockerConn) image_remove(id string) ! {
d.send_request(.delete, '/images/$id')! d.request(.delete, '/images/$id')
d.send()!
d.read_response()!
}
pub fn (mut d DockerConn) image_tag(name string, repo string, tag string) ! {
d.request(.post, '/images/$name/tag')
d.params({
'repo': repo
'tag': tag
})
d.send()!
d.read_response()! d.read_response()!
} }

View File

@ -3,25 +3,39 @@ module docker
import net.http import net.http
import net.urllib import net.urllib
import io import io
import json
fn (mut d DockerConn) request(method http.Method, url_str string) { fn (mut d DockerConn) request(method http.Method, url string) {
d.method = method d.method = method
d.url = url_str d.url = url
d.params.clear()
d.content_type = '' d.content_type = ''
d.body = '' d.body = ''
d.params.clear()
} }
fn (mut d DockerConn) get(url_str string) { fn (mut d DockerConn) body(content_type string, body string) {
d.request(http.Method.get, url_str) d.content_type = content_type
d.body = body
}
fn (mut d DockerConn) body_json<T>(data T) {
d.content_type = 'application/json'
d.body = json.encode(data)
} }
fn (mut d DockerConn) params<T>(o T) { fn (mut d DockerConn) params<T>(o T) {
$for field in T.fields { $if T is map[string]string {
v := o.$(field.name) for key, value in o {
d.params[key] = urllib.query_escape(value.replace("'", '"'))
}
} $else {
$for field in T.fields {
v := o.$(field.name)
if !isnil(v) { if !isnil(v) {
d.params[field.name] = urllib.query_escape(v.str().replace("'", '"')) d.params[field.name] = urllib.query_escape(v.str().replace("'", '"'))
}
} }
} }
} }

22
types/volume.v 100644
View File

@ -0,0 +1,22 @@
module types
import time
pub struct UsageData {
size int [json: Size]
ref_count int [json: RefCount]
}
pub struct Volume {
pub mut:
created_at_str string [json: CreatedAt]
created_at time.Time [skip]
name string [json: Name]
driver string [json: Driver]
mountpoint string [json: Mountpoint]
status map[string]string [json: Status]
labels map[string]string [json: Labels]
scope string [json: Scope]
options map[string]string [json: Options]
usage_data UsageData [json: UsageData]
}

View File

@ -1,26 +1,7 @@
module docker module docker
import net.http { Method }
import time import time
import types { Volume }
struct UsageData {
size int [json: Size]
ref_count int [json: RefCount]
}
struct Volume {
created_at_str string [json: CreatedAt]
pub mut:
created_at time.Time [skip]
name string [json: Name]
driver string [json: Driver]
mountpoint string [json: Mountpoint]
status map[string]string [json: Status]
labels map[string]string [json: Labels]
scope string [json: Scope]
options map[string]string [json: Options]
usage_data UsageData [json: UsageData]
}
[params] [params]
pub struct VolumeListFilter { pub struct VolumeListFilter {
@ -36,7 +17,8 @@ struct VolumeListResponse {
} }
pub fn (mut d DockerConn) volume_list() !VolumeListResponse { pub fn (mut d DockerConn) volume_list() !VolumeListResponse {
d.send_request(Method.get, '/volumes')! d.request(.get, '/volumes')
d.send()!
mut data := d.read_json_response<VolumeListResponse>()! mut data := d.read_json_response<VolumeListResponse>()!