http/vweb: use Method enum & add helpers

pull/6002/head
joe-conigliaro 2020-07-28 14:13:19 +10:00
parent fec9920b1a
commit 395e886b2e
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 40 additions and 21 deletions

View File

@ -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 { pub fn get(url string) ?Response {
return fetch_with_method(.get, url, FetchConfig{}) 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 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 { fn (req &Request) build_request_cookies_header() string {
if req.cookies.keys().len < 1 { if req.cookies.keys().len < 1 {
return '' return ''

View File

@ -12,8 +12,7 @@ import strings
import time import time
pub const ( pub const (
methods_with_form = ['POST', 'PUT', 'PATCH'] methods_with_form = [http.Method.post, .put, .patch]
method_all = ['GET','POST','PUT','PATCH','DELETE', 'HEAD', 'OPTIONS']
header_server = 'Server: VWeb\r\n' header_server = 'Server: VWeb\r\n'
header_connection_close = 'Connection: close\r\n' header_connection_close = 'Connection: close\r\n'
headers_close = '${header_server}${header_connection_close}\r\n' headers_close = '${header_server}${header_connection_close}\r\n'
@ -286,7 +285,7 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
data: strip(body) data: strip(body)
ws_func: 0 ws_func: 0
user_ptr: 0 user_ptr: 0
method: vals[0] method: http.method_from_str(vals[0])
url: vals[1] url: vals[1]
} }
$if debug { $if debug {
@ -371,7 +370,7 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
// since such methods have a priority. // since such methods have a priority.
// For example URL `/register` matches route `/:user`, but `fn register()` // For example URL `/register` matches route `/:user`, but `fn register()`
// should be called first. // 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') println('easy match method=$method.name')
app.$method(vars) app.$method(vars)
return return
@ -380,27 +379,27 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
// Get methods // Get methods
// Get is default // Get is default
if 'post' in attrs { 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('/')) route_words_a = attrs.filter(it.to_lower() != 'post').map(it[1..].split('/'))
} }
} else if 'put' in attrs { } 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('/')) route_words_a = attrs.filter(it.to_lower() != 'put').map(it[1..].split('/'))
} }
} else if 'patch' in attrs { } 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('/')) route_words_a = attrs.filter(it.to_lower() != 'patch').map(it[1..].split('/'))
} }
} else if 'delete' in attrs { } 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('/')) route_words_a = attrs.filter(it.to_lower() != 'delete').map(it[1..].split('/'))
} }
} else if 'head' in attrs { } 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('/')) route_words_a = attrs.filter(it.to_lower() != 'head').map(it[1..].split('/'))
} }
} else if 'options' in attrs { } 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('/')) route_words_a = attrs.filter(it.to_lower() != 'options').map(it[1..].split('/'))
} }
} else { } else {