forked from vieter-v/vieter
				
			refactor(docker): remove old code
							parent
							
								
									e041682fea
								
							
						
					
					
						commit
						ce67208fbd
					
				|  | @ -253,14 +253,21 @@ fn (mut d Daemon) rebuild_base_image() bool { | ||||||
| fn (mut d Daemon) clean_old_base_images() { | fn (mut d Daemon) clean_old_base_images() { | ||||||
| 	mut i := 0 | 	mut i := 0 | ||||||
| 
 | 
 | ||||||
|  | 	mut dd := docker.new_conn() or { | ||||||
|  | 		d.lerror('Failed to connect to Docker socket.') | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	defer { | ||||||
|  | 		dd.close() or {} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	for i < d.builder_images.len - 1 { | 	for i < d.builder_images.len - 1 { | ||||||
| 		// For each builder image, we try to remove it by calling the Docker | 		// For each builder image, we try to remove it by calling the Docker | ||||||
| 		// API. If the function returns an error or false, that means the image | 		// API. If the function returns an error or false, that means the image | ||||||
| 		// wasn't deleted. Therefore, we move the index over. If the function | 		// wasn't deleted. Therefore, we move the index over. If the function | ||||||
| 		// returns true, the array's length has decreased by one so we don't | 		// returns true, the array's length has decreased by one so we don't | ||||||
| 		// move the index. | 		// move the index. | ||||||
| 		if !docker.remove_image(d.builder_images[i]) or { false } { | 		dd.remove_image(d.builder_images[i]) or { i += 1 } | ||||||
| 			i += 1 |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -30,7 +30,6 @@ pub fn (mut d DockerDaemon) containers() ?[]Container { | ||||||
| 	return data | 	return data | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| [params] |  | ||||||
| pub struct NewContainer { | pub struct NewContainer { | ||||||
| 	image      string   [json: Image] | 	image      string   [json: Image] | ||||||
| 	entrypoint []string [json: Entrypoint] | 	entrypoint []string [json: Entrypoint] | ||||||
|  | @ -74,26 +73,6 @@ pub fn (mut d DockerDaemon) start_container(id string) ? { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // create_container creates a container defined by the given configuration. If |  | ||||||
| // successful, it returns the ID of the newly created container. |  | ||||||
| pub fn create_container(c &NewContainer) ?string { |  | ||||||
| 	res := request_with_json('POST', urllib.parse('/v1.41/containers/create')?, c)? |  | ||||||
| 
 |  | ||||||
| 	if res.status_code != 201 { |  | ||||||
| 		return error('Failed to create container.') |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return json.decode(CreatedContainer, res.text)?.id |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // start_container starts a container with a given ID. It returns whether the |  | ||||||
| // container was started or not. |  | ||||||
| pub fn start_container(id string) ?bool { |  | ||||||
| 	res := request('POST', urllib.parse('/v1.41/containers/$id/start')?)? |  | ||||||
| 
 |  | ||||||
| 	return res.status_code == 204 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| struct ContainerInspect { | struct ContainerInspect { | ||||||
| pub mut: | pub mut: | ||||||
| 	state ContainerState [json: State] | 	state ContainerState [json: State] | ||||||
|  | @ -134,26 +113,6 @@ pub fn (mut d DockerDaemon) inspect_container(id string) ?ContainerInspect { | ||||||
| 	return data | 	return data | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // inspect_container returns the result of inspecting a container with a given |  | ||||||
| // ID. |  | ||||||
| pub fn inspect_container(id string) ?ContainerInspect { |  | ||||||
| 	res := request('GET', urllib.parse('/v1.41/containers/$id/json')?)? |  | ||||||
| 
 |  | ||||||
| 	if res.status_code != 200 { |  | ||||||
| 		return error('Failed to inspect container.') |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	mut data := json.decode(ContainerInspect, res.text)? |  | ||||||
| 
 |  | ||||||
| 	data.state.start_time = time.parse_rfc3339(data.state.start_time_str)? |  | ||||||
| 
 |  | ||||||
| 	if data.state.status == 'exited' { |  | ||||||
| 		data.state.end_time = time.parse_rfc3339(data.state.end_time_str)? |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return data |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // remove_container removes the container with the given id. | // remove_container removes the container with the given id. | ||||||
| pub fn (mut d DockerDaemon) remove_container(id string) ? { | pub fn (mut d DockerDaemon) remove_container(id string) ? { | ||||||
| 	d.send_request('DELETE', urllib.parse('/v1.41/containers/$id')?)? | 	d.send_request('DELETE', urllib.parse('/v1.41/containers/$id')?)? | ||||||
|  | @ -166,13 +125,6 @@ pub fn (mut d DockerDaemon) remove_container(id string) ? { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // remove_container removes a container with a given ID. |  | ||||||
| pub fn remove_container(id string) ?bool { |  | ||||||
| 	res := request('DELETE', urllib.parse('/v1.41/containers/$id')?)? |  | ||||||
| 
 |  | ||||||
| 	return res.status_code == 204 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // get_container_logs returns a reader object allowing access to the | // get_container_logs returns a reader object allowing access to the | ||||||
| // container's logs. | // container's logs. | ||||||
| pub fn (mut d DockerDaemon) get_container_logs(id string) ?&StreamFormatReader { | pub fn (mut d DockerDaemon) get_container_logs(id string) ?&StreamFormatReader { | ||||||
|  | @ -189,25 +141,3 @@ pub fn (mut d DockerDaemon) get_container_logs(id string) ?&StreamFormatReader { | ||||||
| 
 | 
 | ||||||
| 	return d.get_stream_format_reader() | 	return d.get_stream_format_reader() | ||||||
| } | } | ||||||
| 
 |  | ||||||
| // get_container_logs retrieves the logs for a Docker container, both stdout & |  | ||||||
| // stderr. |  | ||||||
| pub fn get_container_logs(id string) ?string { |  | ||||||
| 	res := request('GET', urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true')?)? |  | ||||||
| 	mut res_bytes := res.text.bytes() |  | ||||||
| 
 |  | ||||||
| 	// Docker uses a special "stream" format for their logs, so we have to |  | ||||||
| 	// clean up the data. |  | ||||||
| 	mut index := 0 |  | ||||||
| 
 |  | ||||||
| 	for index < res_bytes.len { |  | ||||||
| 		// The reverse is required because V reads in the bytes differently |  | ||||||
| 		t := res_bytes[index + 4..index + 8].reverse() |  | ||||||
| 		len_length := unsafe { *(&u32(&t[0])) } |  | ||||||
| 
 |  | ||||||
| 		res_bytes.delete_many(index, 8) |  | ||||||
| 		index += int(len_length) |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return res_bytes.bytestr() |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,91 +1,158 @@ | ||||||
| module docker | module docker | ||||||
| 
 | 
 | ||||||
| import net.unix | import net.unix | ||||||
| import net.urllib | import io | ||||||
| import net.http | import net.http | ||||||
|  | import strings | ||||||
|  | import net.urllib | ||||||
| import json | import json | ||||||
|  | import util | ||||||
| 
 | 
 | ||||||
| // send writes a request to the Docker socket, waits for a response & returns | const ( | ||||||
| // it. | 	socket               = '/var/run/docker.sock' | ||||||
| fn send(req &string) ?http.Response { | 	buf_len              = 10 * 1024 | ||||||
| 	// Open a connection to the socket | 	http_separator       = [u8(`\r`), `\n`, `\r`, `\n`] | ||||||
| 	mut s := unix.connect_stream(socket) or { | 	http_chunk_separator = [u8(`\r`), `\n`] | ||||||
| 		return error('Failed to connect to socket ${socket}.') | ) | ||||||
|  | 
 | ||||||
|  | pub struct DockerDaemon { | ||||||
|  | mut: | ||||||
|  | 	socket &unix.StreamConn | ||||||
|  | 	reader &io.BufferedReader | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // new_conn creates a new connection to the Docker daemon. | ||||||
|  | pub fn new_conn() ?&DockerDaemon { | ||||||
|  | 	s := unix.connect_stream(docker.socket)? | ||||||
|  | 
 | ||||||
|  | 	d := &DockerDaemon{ | ||||||
|  | 		socket: s | ||||||
|  | 		reader: io.new_buffered_reader(reader: s) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	defer { | 	return d | ||||||
| 		// This or is required because otherwise, the V compiler segfaults for | } | ||||||
| 		// some reason |  | ||||||
| 		// https://github.com/vlang/v/issues/13534 |  | ||||||
| 		s.close() or {} |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	// Write the request to the socket | // close closes the underlying socket connection. | ||||||
| 	s.write_string(req) or { return error('Failed to write request to socket ${socket}.') } | pub fn (mut d DockerDaemon) close() ? { | ||||||
|  | 	d.socket.close()? | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| 	s.wait_for_write()? | // send_request sends an HTTP request without body. | ||||||
|  | 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)? | ||||||
|  | 
 | ||||||
|  | 	// 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. | ||||||
|  | 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)? | ||||||
|  | 
 | ||||||
|  | 	// 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. | ||||||
|  | pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.URL, data &T) ? { | ||||||
|  | 	body := json.encode(data) | ||||||
|  | 
 | ||||||
|  | 	return d.send_request_with_body(method, url, 'application/json', body) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // 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. | ||||||
|  | pub fn (mut d DockerDaemon) read_response_head() ?http.Response { | ||||||
| 	mut c := 0 | 	mut c := 0 | ||||||
| 	mut buf := []u8{len: buf_len} | 	mut buf := []u8{len: 4} | ||||||
| 	mut res := []u8{} | 	mut res := []u8{} | ||||||
| 
 | 
 | ||||||
| 	for { | 	for { | ||||||
| 		c = s.read(mut buf) or { return error('Failed to read data from socket ${socket}.') } | 		c = d.reader.read(mut buf)? | ||||||
| 		res << buf[..c] | 		res << buf[..c] | ||||||
| 
 | 
 | ||||||
| 		if c < buf_len { | 		match_len := util.match_array_in_array(buf[..c], docker.http_separator) | ||||||
|  | 
 | ||||||
|  | 		if match_len == 4 { | ||||||
| 			break | 			break | ||||||
| 		} | 		} | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	// After reading the first part of the response, we parse it into an HTTP | 		if match_len > 0 { | ||||||
| 	// response. If it isn't chunked, we return early with the data. | 			mut buf2 := []u8{len: 4 - match_len} | ||||||
| 	parsed := http.parse_response(res.bytestr()) or { | 			c2 := d.reader.read(mut buf2)? | ||||||
| 		return error('Failed to parse HTTP response from socket ${socket}.') | 			res << buf2[..c2] | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	if parsed.header.get(http.CommonHeader.transfer_encoding) or { '' } != 'chunked' { | 			if buf2 == docker.http_separator[match_len..] { | ||||||
| 		return parsed |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	// We loop until we've encountered the end of the chunked response |  | ||||||
| 	// A chunked HTTP response always ends with '0\r\n\r\n'. |  | ||||||
| 	for res.len < 5 || res#[-5..] != [u8(`0`), `\r`, `\n`, `\r`, `\n`] { |  | ||||||
| 		// Wait for the server to respond |  | ||||||
| 		s.wait_for_write()? |  | ||||||
| 
 |  | ||||||
| 		for { |  | ||||||
| 			c = s.read(mut buf) or { return error('Failed to read data from socket ${socket}.') } |  | ||||||
| 			res << buf[..c] |  | ||||||
| 
 |  | ||||||
| 			if c < buf_len { |  | ||||||
| 				break | 				break | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Decode chunked response |  | ||||||
| 	return http.parse_response(res.bytestr()) | 	return http.parse_response(res.bytestr()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // request_with_body sends a request to the Docker socket with the given body. | // read_response_body reads `length` bytes from the stream. It can be used when | ||||||
| fn request_with_body(method string, url urllib.URL, content_type string, body string) ?http.Response { | // the response encoding isn't chunked to fully read it. | ||||||
| 	req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n' | pub fn (mut d DockerDaemon) read_response_body(length int) ?string { | ||||||
|  | 	if length == 0 { | ||||||
|  | 		return '' | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	return send(req) | 	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() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // request sends a request to the Docker socket with an empty body. | // read_response is a convenience function which always consumes the entire | ||||||
| fn request(method string, url urllib.URL) ?http.Response { | // response & returns it. It should only be used when we're certain that the | ||||||
| 	req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n' | // result isn't too large. | ||||||
|  | pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) { | ||||||
|  | 	head := d.read_response_head()? | ||||||
| 
 | 
 | ||||||
| 	return send(req) | 	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() | ||||||
|  | 	res := d.read_response_body(content_length)? | ||||||
|  | 
 | ||||||
|  | 	return head, res | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // request_with_json<T> sends a request to the Docker socket with a given JSON | // get_chunked_response_reader returns a ChunkedResponseReader using the socket | ||||||
| // payload | // as reader. | ||||||
| pub fn request_with_json<T>(method string, url urllib.URL, data &T) ?http.Response { | pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader { | ||||||
| 	body := json.encode(data) | 	r := new_chunked_response_reader(d.reader) | ||||||
| 
 | 
 | ||||||
| 	return request_with_body(method, url, 'application/json', body) | 	return r | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // get_stream_format_reader returns a StreamFormatReader using the socket as | ||||||
|  | // reader. | ||||||
|  | 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 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -32,11 +32,6 @@ pub fn (mut d DockerDaemon) pull_image(image string, tag string) ? { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // pull_image pulls tries to pull the image for the given image & tag |  | ||||||
| pub fn pull_image(image string, tag string) ?http.Response { |  | ||||||
| 	return request('POST', urllib.parse('/v1.41/images/create?fromImage=$image&tag=$tag')?) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // 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 DockerDaemon) create_image_from_container(id string, repo string, tag string) ?Image { | pub fn (mut d DockerDaemon) create_image_from_container(id string, repo string, tag string) ?Image { | ||||||
| 	d.send_request('POST', urllib.parse('/v1.41/commit?container=$id&repo=$repo&tag=$tag')?)? | 	d.send_request('POST', urllib.parse('/v1.41/commit?container=$id&repo=$repo&tag=$tag')?)? | ||||||
|  | @ -53,25 +48,6 @@ pub fn (mut d DockerDaemon) create_image_from_container(id string, repo string, | ||||||
| 	return data | 	return data | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // create_image_from_container creates a new image from a container with the |  | ||||||
| // given repo & tag, given the container's ID. |  | ||||||
| pub fn create_image_from_container(id string, repo string, tag string) ?Image { |  | ||||||
| 	res := request('POST', urllib.parse('/v1.41/commit?container=$id&repo=$repo&tag=$tag')?)? |  | ||||||
| 
 |  | ||||||
| 	if res.status_code != 201 { |  | ||||||
| 		return error('Failed to create image from container.') |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return json.decode(Image, res.text) or {} |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // remove_image removes the image with the given ID. |  | ||||||
| pub fn remove_image(id string) ?bool { |  | ||||||
| 	res := request('DELETE', urllib.parse('/v1.41/images/$id')?)? |  | ||||||
| 
 |  | ||||||
| 	return res.status_code == 200 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // remove_image removes the image with the given id. | // remove_image removes the image with the given id. | ||||||
| pub fn (mut d DockerDaemon) remove_image(id string) ? { | pub fn (mut d DockerDaemon) remove_image(id string) ? { | ||||||
| 	d.send_request('DELETE', urllib.parse('/v1.41/images/$id')?)? | 	d.send_request('DELETE', urllib.parse('/v1.41/images/$id')?)? | ||||||
|  |  | ||||||
|  | @ -1,158 +0,0 @@ | ||||||
| module docker |  | ||||||
| 
 |  | ||||||
| import net.unix |  | ||||||
| import io |  | ||||||
| import net.http |  | ||||||
| import strings |  | ||||||
| import net.urllib |  | ||||||
| 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`] |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| pub struct DockerDaemon { |  | ||||||
| mut: |  | ||||||
| 	socket &unix.StreamConn |  | ||||||
| 	reader &io.BufferedReader |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // new_conn creates a new connection to the Docker daemon. |  | ||||||
| pub fn new_conn() ?&DockerDaemon { |  | ||||||
| 	s := unix.connect_stream(docker.socket)? |  | ||||||
| 
 |  | ||||||
| 	d := &DockerDaemon{ |  | ||||||
| 		socket: s |  | ||||||
| 		reader: io.new_buffered_reader(reader: s) |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return d |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // close closes the underlying socket connection. |  | ||||||
| pub fn (mut d DockerDaemon) close() ? { |  | ||||||
| 	d.socket.close()? |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // send_request sends an HTTP request without body. |  | ||||||
| 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)? |  | ||||||
| 
 |  | ||||||
| 	// 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. |  | ||||||
| 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)? |  | ||||||
| 
 |  | ||||||
| 	// 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. |  | ||||||
| pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.URL, data &T) ? { |  | ||||||
| 	body := json.encode(data) |  | ||||||
| 
 |  | ||||||
| 	return d.send_request_with_body(method, url, 'application/json', body) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // 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. |  | ||||||
| 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 |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return http.parse_response(res.bytestr()) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // read_response_body reads `length` bytes from the stream. It can be used when |  | ||||||
| // the response encoding isn't chunked to fully read it. |  | ||||||
| pub fn (mut d DockerDaemon) read_response_body(length int) ?string { |  | ||||||
| 	if length == 0 { |  | ||||||
| 		return '' |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	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() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // read_response is a convenience function which always consumes the entire |  | ||||||
| // response & returns it. It should only be used when we're certain that the |  | ||||||
| // result isn't too large. |  | ||||||
| pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) { |  | ||||||
| 	head := d.read_response_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() |  | ||||||
| 	res := d.read_response_body(content_length)? |  | ||||||
| 
 |  | ||||||
| 	return head, res |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // get_chunked_response_reader returns a ChunkedResponseReader using the socket |  | ||||||
| // as reader. |  | ||||||
| pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader { |  | ||||||
| 	r := new_chunked_response_reader(d.reader) |  | ||||||
| 
 |  | ||||||
| 	return r |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // get_stream_format_reader returns a StreamFormatReader using the socket as |  | ||||||
| // reader. |  | ||||||
| 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 |  | ||||||
| } |  | ||||||
		Loading…
	
		Reference in New Issue