feat(docker): start of new socket implementation
							parent
							
								
									c01d849b9e
								
							
						
					
					
						commit
						473c7ec06b
					
				|  | @ -5,10 +5,6 @@ import net.urllib | ||||||
| import net.http | import net.http | ||||||
| import json | import json | ||||||
| 
 | 
 | ||||||
| const socket = '/var/run/docker.sock' |  | ||||||
| 
 |  | ||||||
| const buf_len = 1024 |  | ||||||
| 
 |  | ||||||
| // send writes a request to the Docker socket, waits for a response & returns | // send writes a request to the Docker socket, waits for a response & returns | ||||||
| // it. | // it. | ||||||
| fn send(req &string) ?http.Response { | fn send(req &string) ?http.Response { | ||||||
|  |  | ||||||
|  | @ -0,0 +1,47 @@ | ||||||
|  | module docker | ||||||
|  | 
 | ||||||
|  | import net.unix | ||||||
|  | import io | ||||||
|  | import net.http | ||||||
|  | 
 | ||||||
|  | const socket = '/var/run/docker.sock' | ||||||
|  | 
 | ||||||
|  | const buf_len = 10 * 1024 | ||||||
|  | 
 | ||||||
|  | pub struct DockerDaemon { | ||||||
|  | mut: | ||||||
|  | 	socket &unix.StreamConn | ||||||
|  | 	reader &io.BufferedReader | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn new_conn() ?DockerDaemon { | ||||||
|  | 	s := unix.connect_stream(socket) ? | ||||||
|  | 
 | ||||||
|  | 	d := DockerDaemon{socket: s, reader: io.new_buffered_reader(reader: s)} | ||||||
|  | 
 | ||||||
|  | 	return d | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn (mut d DockerDaemon) send_request(req &string) ? { | ||||||
|  | 	d.socket.write_string(req) ? | ||||||
|  | 	d.socket.wait_for_write() ? | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // read_response_head consumes the socket's contents until it encounters | ||||||
|  | // '\n\n', after which it parses the response as an HTTP response. | ||||||
|  | fn (mut d DockerDaemon) read_response_head() ?http.Response { | ||||||
|  | 	mut c := 0 | ||||||
|  | 	mut buf := [buf_len]u8{len: docker.buf_len} | ||||||
|  | 	mut res := []u8{} | ||||||
|  | 
 | ||||||
|  | 	for { | ||||||
|  | 		c = d.socket.read(mut buf) ? | ||||||
|  | 		res << buf[..c] | ||||||
|  | 
 | ||||||
|  | 		if res#[-2..] == [u8(`\n`), `\n`] { | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return http.parse_response(res.bytestr()) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue