From 395e886b2eb2a74e018b56d3431827f0b09761ad Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Tue, 28 Jul 2020 14:13:19 +1000 Subject: [PATCH] http/vweb: use Method enum & add helpers --- vlib/net/http/http.v | 42 +++++++++++++++++++++++++++++++----------- vlib/vweb/vweb.v | 19 +++++++++---------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/vlib/net/http/http.v b/vlib/net/http/http.v index 34feefaf59..ca09236882 100644 --- a/vlib/net/http/http.v +++ b/vlib/net/http/http.v @@ -75,6 +75,37 @@ pub fn new_request(method Method, url_, data string) ?Request { } } +fn (methods []Method) contains(m Method) bool { + for method in methods { + if method == m { + return true + } + } + return false +} + +fn (m Method) str() string { + return match m { + .get { 'GET' } + .post { 'POST' } + .header { 'HEADER' } + .put { 'PUT' } + .connect { 'CONNECT' } + else { '' } + } +} + +pub fn method_from_str(m string) Method { + return match m { + 'GET' { Method.get } + 'POST' { Method.post } + 'HEADER' { Method.header } + 'PUT' { Method.put } + 'CONNECT' { Method.connect } + else { Method.get } // should we default to GET? + } +} + pub fn get(url string) ?Response { return fetch_with_method(.get, url, FetchConfig{}) } @@ -383,17 +414,6 @@ fn (req &Request) build_request_headers(method Method, host_name, path string) s req.data } -fn (m Method) str() string { - return match m { - .get { 'GET' } - .post { 'POST' } - .header { 'HEADER' } - .put { 'PUT' } - .connect { 'CONNECT' } - else { '' } - } -} - fn (req &Request) build_request_cookies_header() string { if req.cookies.keys().len < 1 { return '' diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 28178b2356..497b1d1fb1 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -12,8 +12,7 @@ import strings import time pub const ( - methods_with_form = ['POST', 'PUT', 'PATCH'] - method_all = ['GET','POST','PUT','PATCH','DELETE', 'HEAD', 'OPTIONS'] + methods_with_form = [http.Method.post, .put, .patch] header_server = 'Server: VWeb\r\n' header_connection_close = 'Connection: close\r\n' headers_close = '${header_server}${header_connection_close}\r\n' @@ -286,7 +285,7 @@ fn handle_conn(conn net.Socket, mut app T) { data: strip(body) ws_func: 0 user_ptr: 0 - method: vals[0] + method: http.method_from_str(vals[0]) url: vals[1] } $if debug { @@ -371,7 +370,7 @@ fn handle_conn(conn net.Socket, mut app T) { // since such methods have a priority. // For example URL `/register` matches route `/:user`, but `fn register()` // should be called first. - if (req.method == 'GET' && url_words[0] == method.name && url_words.len == 1) || (req.method == 'POST' && url_words[0] + '_post' == method.name) { + if (req.method == .get && url_words[0] == method.name && url_words.len == 1) || (req.method == .post && url_words[0] + '_post' == method.name) { println('easy match method=$method.name') app.$method(vars) return @@ -380,27 +379,27 @@ fn handle_conn(conn net.Socket, mut app T) { // Get methods // Get is default if 'post' in attrs { - if req.method == 'POST' { + if req.method == .post { route_words_a = attrs.filter(it.to_lower() != 'post').map(it[1..].split('/')) } } else if 'put' in attrs { - if req.method == 'PUT' { + if req.method == .put { route_words_a = attrs.filter(it.to_lower() != 'put').map(it[1..].split('/')) } } else if 'patch' in attrs { - if req.method == 'PATCH' { + if req.method == .patch { route_words_a = attrs.filter(it.to_lower() != 'patch').map(it[1..].split('/')) } } else if 'delete' in attrs { - if req.method == 'DELETE' { + if req.method == .delete { route_words_a = attrs.filter(it.to_lower() != 'delete').map(it[1..].split('/')) } } else if 'head' in attrs { - if req.method == 'HEAD' { + if req.method == .head { route_words_a = attrs.filter(it.to_lower() != 'head').map(it[1..].split('/')) } } else if 'options' in attrs { - if req.method == 'OPTIONS' { + if req.method == .options { route_words_a = attrs.filter(it.to_lower() != 'options').map(it[1..].split('/')) } } else {