Initially working static file streaming!
parent
3ada24ef3b
commit
a43ebfeaf0
|
@ -7,6 +7,5 @@ pipeline:
|
||||||
build:
|
build:
|
||||||
image: 'chewingbever/vlang:latest'
|
image: 'chewingbever/vlang:latest'
|
||||||
commands:
|
commands:
|
||||||
- apt-get install -y --no-install-recommends openssl
|
|
||||||
- v vieter
|
- v vieter
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,9 @@ const pkgs_subpath = 'pkgs'
|
||||||
|
|
||||||
// Dummy struct to work around the fact that you can only share structs, maps &
|
// Dummy struct to work around the fact that you can only share structs, maps &
|
||||||
// arrays
|
// arrays
|
||||||
pub struct Dummy { x int }
|
pub struct Dummy {
|
||||||
|
x int
|
||||||
|
}
|
||||||
|
|
||||||
// Handles management of a repository. Package files are stored in '$dir/pkgs'
|
// Handles management of a repository. Package files are stored in '$dir/pkgs'
|
||||||
// & moved there if necessary.
|
// & moved there if necessary.
|
||||||
|
@ -19,12 +21,12 @@ pub:
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (r &Repo) pkg_dir() string {
|
pub fn (r &Repo) pkg_dir() string {
|
||||||
return os.join_path_single(r.dir, pkgs_subpath)
|
return os.join_path_single(r.dir, repo.pkgs_subpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns path to the given package, prepended with the repo's path.
|
// Returns path to the given package, prepended with the repo's path.
|
||||||
pub fn (r &Repo) pkg_path(pkg string) string {
|
pub fn (r &Repo) pkg_path(pkg string) string {
|
||||||
return os.join_path(r.dir, pkgs_subpath, pkg)
|
return os.join_path(r.dir, repo.pkgs_subpath, pkg)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (r &Repo) exists(pkg string) bool {
|
pub fn (r &Repo) exists(pkg string) bool {
|
||||||
|
|
|
@ -19,8 +19,31 @@ fn pretty_bytes(bytes int) string {
|
||||||
return '${n:.2}${prefixes[i]}'
|
return '${n:.2}${prefixes[i]}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_pkg_name(s string) bool {
|
||||||
|
return s.contains('.pkg')
|
||||||
|
}
|
||||||
|
|
||||||
|
['/:filename'; get]
|
||||||
|
fn (mut app App) get_root(filename string) web.Result {
|
||||||
|
mut full_path := ''
|
||||||
|
|
||||||
|
if is_pkg_name(filename) {
|
||||||
|
full_path = os.join_path_single(app.repo.pkg_dir(), filename)
|
||||||
|
} else {
|
||||||
|
full_path = os.join_path_single(app.repo.dir, filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
return app.file(full_path)
|
||||||
|
}
|
||||||
|
|
||||||
['/pkgs/:pkg'; put]
|
['/pkgs/:pkg'; put]
|
||||||
fn (mut app App) put_package(pkg string) web.Result {
|
fn (mut app App) put_package(pkg string) web.Result {
|
||||||
|
if !is_pkg_name(pkg) {
|
||||||
|
app.lwarn("Invalid package name '$pkg'.")
|
||||||
|
|
||||||
|
return app.text('Invalid filename.')
|
||||||
|
}
|
||||||
|
|
||||||
if app.repo.exists(pkg) {
|
if app.repo.exists(pkg) {
|
||||||
app.lwarn("Duplicate package '$pkg'")
|
app.lwarn("Duplicate package '$pkg'")
|
||||||
|
|
||||||
|
@ -43,7 +66,6 @@ fn (mut app App) put_package(pkg string) web.Result {
|
||||||
|
|
||||||
sw.stop()
|
sw.stop()
|
||||||
app.ldebug("Upload of package '$pkg' completed in ${sw.elapsed().seconds():.3}s.")
|
app.ldebug("Upload of package '$pkg' completed in ${sw.elapsed().seconds():.3}s.")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.")
|
app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.")
|
||||||
return app.text("Content-Type header isn't set.")
|
return app.text("Content-Type header isn't set.")
|
||||||
|
|
|
@ -257,20 +257,75 @@ pub fn (mut ctx Context) json_pretty<T>(j T) Result {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response HTTP_OK with file as payload
|
// Response HTTP_OK with file as payload
|
||||||
|
// This function manually implements responses because it needs to stream the file contents
|
||||||
pub fn (mut ctx Context) file(f_path string) Result {
|
pub fn (mut ctx Context) file(f_path string) Result {
|
||||||
|
if ctx.done {
|
||||||
|
return Result{}
|
||||||
|
}
|
||||||
|
ctx.done = true
|
||||||
|
|
||||||
|
if !os.is_file(f_path) {
|
||||||
|
return ctx.not_found()
|
||||||
|
}
|
||||||
|
|
||||||
ext := os.file_ext(f_path)
|
ext := os.file_ext(f_path)
|
||||||
data := os.read_file(f_path) or {
|
// data := os.read_file(f_path) or {
|
||||||
eprint(err.msg)
|
// eprint(err.msg)
|
||||||
|
// ctx.server_error(500)
|
||||||
|
// return Result{}
|
||||||
|
// }
|
||||||
|
// content_type := web.mime_types[ext]
|
||||||
|
// if content_type == '' {
|
||||||
|
// eprintln('no MIME type found for extension $ext')
|
||||||
|
// ctx.server_error(500)
|
||||||
|
|
||||||
|
// return Result{}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// First, we return the headers for the request
|
||||||
|
|
||||||
|
// We open the file before sending the headers in case reading fails
|
||||||
|
file_size := os.file_size(f_path)
|
||||||
|
|
||||||
|
file := os.open(f_path) or {
|
||||||
|
eprintln(err.msg)
|
||||||
ctx.server_error(500)
|
ctx.server_error(500)
|
||||||
return Result{}
|
return Result{}
|
||||||
}
|
}
|
||||||
content_type := web.mime_types[ext]
|
|
||||||
if content_type == '' {
|
// build header
|
||||||
eprintln('no MIME type found for extension $ext')
|
header := http.new_header_from_map({
|
||||||
ctx.server_error(500)
|
// http.CommonHeader.content_type: content_type
|
||||||
} else {
|
http.CommonHeader.content_length: file_size.str()
|
||||||
ctx.send_response_to_client(content_type, data)
|
}).join(ctx.header)
|
||||||
|
|
||||||
|
mut resp := http.Response{
|
||||||
|
header: header.join(web.headers_close)
|
||||||
}
|
}
|
||||||
|
resp.set_version(.v1_1)
|
||||||
|
resp.set_status(http.status_from_int(ctx.status.int()))
|
||||||
|
send_string(mut ctx.conn, resp.bytestr()) or { return Result{} }
|
||||||
|
|
||||||
|
mut buf := []byte{len: 1_000_000}
|
||||||
|
mut bytes_left := file_size
|
||||||
|
|
||||||
|
// Repeat as long as the stream still has data
|
||||||
|
for bytes_left > 0 {
|
||||||
|
// TODO check if just breaking here is safe
|
||||||
|
bytes_read := file.read(mut buf) or { break }
|
||||||
|
bytes_left -= u64(bytes_read)
|
||||||
|
|
||||||
|
mut to_write := bytes_read
|
||||||
|
|
||||||
|
for to_write > 0 {
|
||||||
|
// TODO don't just loop infinitely here
|
||||||
|
bytes_written := ctx.conn.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
||||||
|
|
||||||
|
to_write = to_write - bytes_written
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.done = true
|
||||||
return Result{}
|
return Result{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue