From 9268ef0302d9ec3d73dc1fa7f38809655f2adbeb Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 13 Aug 2022 17:49:05 +0200 Subject: [PATCH] refactor(web): some small cleanup --- CHANGELOG.md | 2 ++ src/server/repo_remove.v | 2 +- src/web/consts.v | 2 +- src/web/web.v | 33 ++++++++++++++++----------------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7138a84..5aa0e434 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 providing a Git repository * CLI commands for searching the AUR & directly adding packages * HTTP routes for removing packages, arch-repos & repos +* All endpoints serving files now support HTTP byte range requests ### Changed @@ -25,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Branch name for 'git' targets is now optional; if not provided, the repository will be cloned with the default branch * Build containers now explicitely set the PATH variable +* Refactor of web framework ### Removed diff --git a/src/server/repo_remove.v b/src/server/repo_remove.v index 5d5ef156..316b3872 100644 --- a/src/server/repo_remove.v +++ b/src/server/repo_remove.v @@ -8,7 +8,7 @@ import web.response { new_response } ['/:repo/:arch/:pkg'; delete] fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result { if !app.is_authorized() { - return app.json(http.Status.unauthorized, new_response('Unauthorized.')) + return app.json(.unauthorized, new_response('Unauthorized.')) } res := app.repo.remove_pkg_from_arch_repo(repo, arch, pkg, true) or { diff --git a/src/web/consts.v b/src/web/consts.v index 7b1d2b47..1b5bf08e 100644 --- a/src/web/consts.v +++ b/src/web/consts.v @@ -9,7 +9,7 @@ pub struct Result {} pub const ( methods_with_form = [http.Method.post, .put, .patch] headers_close = http.new_custom_header_from_map({ - 'Server': 'VWeb' + 'Server': 'Vieter' http.CommonHeader.connection.str(): 'close' }) or { panic('should never fail') } diff --git a/src/web/web.v b/src/web/web.v index 51acd2f2..8434a803 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -91,15 +91,22 @@ fn (mut ctx Context) send_reader(mut reader io.Reader, size u64) ? { } } +// send_custom_response sends the given http.Response to the client. It can be +// used to overwrite the Context object & send a completely custom +// http.Response instead. +fn (mut ctx Context) send_custom_response(resp &http.Response) ? { + ctx.send_string(resp.bytestr())? +} + // send_response_header constructs a valid HTTP response with an empty body & // sends it to the client. pub fn (mut ctx Context) send_response_header() ? { mut resp := http.Response{ header: ctx.header.join(headers_close) } - resp.set_version(.v1_1) resp.set_status(ctx.status) - ctx.send_string(resp.bytestr())? + + ctx.send_custom_response(resp)? } // send is a convenience function for sending the HTTP response with an empty @@ -222,10 +229,8 @@ pub fn (mut ctx Context) status(status http.Status) Result { // server_error Response a server error pub fn (mut ctx Context) server_error(ecode int) Result { - $if debug { - eprintln('> ctx.server_error ecode: $ecode') - } - ctx.send_string(http_500.bytestr()) or {} + ctx.send_custom_response(http_500) or {} + return Result{} } @@ -234,23 +239,17 @@ pub fn (mut ctx Context) redirect(url string) Result { mut resp := http_302 resp.header = resp.header.join(ctx.header) resp.header.add(.location, url) - ctx.send_string(resp.bytestr()) or { return Result{} } + + ctx.send_custom_response(resp) or {} + return Result{} } // not_found Send an not_found response pub fn (mut ctx Context) not_found() Result { - return ctx.status(http.Status.not_found) -} + ctx.send_custom_response(http_404) or {} -// add_header Adds an header to the response with key and val -pub fn (mut ctx Context) add_header(key string, val string) { - ctx.header.add_custom(key, val) or {} -} - -// get_header Returns the header data from the key -pub fn (ctx &Context) get_header(key string) string { - return ctx.req.header.get_custom(key) or { '' } + return Result{} } interface DbInterface {