fix(docker): read_response now handles chunked data
parent
1811ebbe3f
commit
79fd9c1f87
|
@ -56,7 +56,6 @@ pub fn create_build_image(base_image string) ?string {
|
||||||
|
|
||||||
// This loop waits until the container has stopped, so we can remove it after
|
// This loop waits until the container has stopped, so we can remove it after
|
||||||
for {
|
for {
|
||||||
println('wot')
|
|
||||||
data := dd.inspect_container(id)?
|
data := dd.inspect_container(id)?
|
||||||
|
|
||||||
if !data.state.running {
|
if !data.state.running {
|
||||||
|
|
|
@ -27,11 +27,8 @@ pub fn (mut d DockerDaemon) pull_image(image string, tag string) ? {
|
||||||
mut buf := []u8{len: 1024}
|
mut buf := []u8{len: 1024}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
c := body.read(mut buf)?
|
c := body.read(mut buf) or { break }
|
||||||
|
|
||||||
if c == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
print(buf[..c].bytestr())
|
print(buf[..c].bytestr())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,9 @@ pub fn (mut d DockerDaemon) send_request(method string, url urllib.URL) ? {
|
||||||
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
||||||
|
|
||||||
d.socket.write_string(req)?
|
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.
|
// send_request_with_body sends an HTTP request with the given body.
|
||||||
|
@ -45,6 +48,9 @@ pub fn (mut d DockerDaemon) send_request_with_body(method string, url urllib.URL
|
||||||
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n'
|
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)?
|
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_json<T> is a convenience wrapper around
|
||||||
|
@ -108,11 +114,21 @@ pub fn (mut d DockerDaemon) read_response_body(length int) ?string {
|
||||||
return builder.str()
|
return builder.str()
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_response is a convenience function combining read_response_head &
|
// read_response is a convenience function which always consumes the entire
|
||||||
// read_response_body. It can be used when you know for certain the response
|
// response & returns it. It should only be used when we're certain that the
|
||||||
// won't be chunked.
|
// result isn't too large.
|
||||||
pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
||||||
head := d.read_response_head()?
|
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()
|
content_length := head.header.get(http.CommonHeader.content_length)?.int()
|
||||||
res := d.read_response_body(content_length)?
|
res := d.read_response_body(content_length)?
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,17 @@ pub fn exit_with_message(code int, msg string) {
|
||||||
exit(code)
|
exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reader_to_writer tries to consume the entire reader & write it to the writer.
|
||||||
|
pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ? {
|
||||||
|
mut buf := []u8{len: 10 * 1024}
|
||||||
|
|
||||||
|
for {
|
||||||
|
c := reader.read(mut buf) or { break }
|
||||||
|
|
||||||
|
writer.write(buf) or { break }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// reader_to_file writes the contents of a BufferedReader to a file
|
// reader_to_file writes the contents of a BufferedReader to a file
|
||||||
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
||||||
mut file := os.create(path)?
|
mut file := os.create(path)?
|
||||||
|
|
Loading…
Reference in New Issue