1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: minor fixes

This commit is contained in:
Alexander Medvednikov 2019-12-20 03:02:16 +03:00
parent fbd9fedbfb
commit 61bfecfa09
4 changed files with 131 additions and 115 deletions

View File

@ -13,10 +13,8 @@ pub mut:
} }
fn main() { fn main() {
mut app := App{} println('noice')
app.vweb = vweb.Context{} vweb.run<App>(port)
vweb.run(mut app, port)
//vweb.run<App>(Port)
} }
pub fn (app mut App) init() { pub fn (app mut App) init() {
@ -32,6 +30,9 @@ pub fn (app mut App) index() {
$vweb.html() $vweb.html()
} }
pub fn (app mut App) reset() {
}
pub fn (app mut App) text() { pub fn (app mut App) text() {
app.vweb.text('Hello world') app.vweb.text('Hello world')
} }

View File

@ -1146,9 +1146,8 @@ pub fn vfmt(args []string) {
exit(1) exit(1)
} }
println('WIP') println('WIP')
/*
vexe := vexe_path() vexe := vexe_path()
//launch_tool('vfmt', '-d vfmt') // launch_tool('vfmt', '-d vfmt')
vroot := os.dir(vexe) vroot := os.dir(vexe)
os.chdir(vroot) os.chdir(vroot)
ret := os.system('$vexe -o $vroot/tools/vfmt -d vfmt v.v') ret := os.system('$vexe -o $vroot/tools/vfmt -d vfmt v.v')
@ -1156,10 +1155,10 @@ pub fn vfmt(args []string) {
println('err') println('err')
return return
} }
os.exec('$vroot/tools/vfmt $file') or { panic(err) } os.exec('$vroot/tools/vfmt $file')or{
//if !os.exists(' panic(err)
*/ }
// if !os.exists('
} }
pub fn create_symlink() { pub fn create_symlink() {

View File

@ -1584,6 +1584,7 @@ fn (p mut Parser) var_decl() {
p.check(.comma) p.check(.comma)
if p.tok == .key_mut { if p.tok == .key_mut {
p.check(.key_mut) p.check(.key_mut)
p.fspace()
var_mut << true var_mut << true
} }
else { else {

View File

@ -122,14 +122,26 @@ fn (ctx &Context) get_header(key string) string {
return ctx.req.headers[key] return ctx.req.headers[key]
} }
//pub fn run<T>(port int) { //fn handle_conn(conn net.Socket) {
pub fn run<T>(app mut T, port int) { //println('handle')
//}
pub fn foo<T>() {
}
pub fn run<T>(port int) {
//pub fn run<T>(app mut T, port int) {
println('Running a Vweb app on http://localhost:$port ...') println('Running a Vweb app on http://localhost:$port ...')
l := net.listen(port) or { panic('failed to listen') } l := net.listen(port) or { panic('failed to listen') }
//mut app := T{} mut app := T{}
app.vweb = Context{}
app.init() app.init()
//app.reset()
for { for {
conn := l.accept() or { panic('accept() failed') } conn := l.accept() or { panic('accept() failed') }
handle_conn(conn, mut app)
//foobar<T>() //foobar<T>()
// TODO move this to handle_conn<T>(conn, app) // TODO move this to handle_conn<T>(conn, app)
//message := readall(conn) //message := readall(conn)
@ -153,119 +165,122 @@ pub fn run<T>(app mut T, port int) {
continue continue
} }
*/ */
}
}
//first_line := strip(lines[0]) fn handle_conn<T>(conn net.Socket, app mut T) {
first_line := conn.read_line() //first_line := strip(lines[0])
println('firstline="$first_line"') first_line := conn.read_line()
$if debug { println(first_line) } println('firstline="$first_line"')
// Parse the first line $if debug { println(first_line) }
// "GET / HTTP/1.1" // Parse the first line
//first_line := s.all_before('\n') // "GET / HTTP/1.1"
vals := first_line.split(' ') //first_line := s.all_before('\n')
if vals.len < 2 { vals := first_line.split(' ')
println('no vals for http') if vals.len < 2 {
conn.send_string(HTTP_500) or {} println('no vals for http')
conn.close() or {} conn.send_string(HTTP_500) or {}
continue conn.close() or {}
} return
mut headers := []string //continue
mut body := '' }
mut in_headers := true mut headers := []string
mut len := 0 mut body := ''
mut body_len := 0 mut in_headers := true
//for line in lines[1..] { mut len := 0
for j := 0; j < 100; j++ { mut body_len := 0
//println(j) //for line in lines[1..] {
line := conn.read_line() for j := 0; j < 100; j++ {
sline := strip(line) //println(j)
if sline == '' { line := conn.read_line()
//if in_headers { sline := strip(line)
// End of headers, no body => exit if sline == '' {
if len == 0 { //if in_headers {
break // End of headers, no body => exit
} if len == 0 {
//} //else {
// End of body
//break
//}
//println('HHH')
in_headers = false
}
if in_headers {
//println(sline)
headers << sline
if sline.starts_with('Content-Length') {
len = sline.all_after(': ').int()
//println('GOT CL=$len')
}
} else {
body += sline + '\r\n'
body_len += body.len
if body_len >= len {
break break
} }
//println('body:$body') //} //else {
// End of body
//break
//}
//println('HHH')
in_headers = false
}
if in_headers {
//println(sline)
headers << sline
if sline.starts_with('Content-Length') {
len = sline.all_after(': ').int()
//println('GOT CL=$len')
} }
} } else {
body += sline + '\r\n'
mut action := vals[1][1..].all_before('/') body_len += body.len
if action.contains('?') { if body_len >= len {
action = action.all_before('?') break
}
if action == '' {
action = 'index'
}
req := http.Request{
headers: http.parse_headers(headers) //s.split_into_lines())
ws_func: 0
user_ptr: 0
method: vals[0]
url: vals[1]
}
$if debug {
println('req.headers = ')
println(req.headers)
println('vweb action = "$action"')
}
//mut app := T{
app.vweb = Context{
req: req
conn: conn
form: map[string]string
static_files: map[string]string
static_mime_types: map[string]string
}
//}
if req.method in methods_with_form {
body = strip(body)
println('body="$body"' )
app.vweb.parse_form(body)
}
if vals.len < 2 {
$if debug {
println('no vals for http')
} }
conn.close() or {} //println('body:$body')
continue
} }
}
// Serve a static file if it's one mut action := vals[1][1..].all_before('/')
// if app.vweb.handle_static() { if action.contains('?') {
// conn.close() action = action.all_before('?')
// continue }
// } if action == '' {
action = 'index'
// Call the right action }
req := http.Request{
headers: http.parse_headers(headers) //s.split_into_lines())
ws_func: 0
user_ptr: 0
method: vals[0]
url: vals[1]
}
$if debug {
println('req.headers = ')
println(req.headers)
println('vweb action = "$action"')
}
//mut app := T{
app.vweb = Context{
req: req
conn: conn
form: map[string]string
static_files: map[string]string
static_mime_types: map[string]string
}
//}
if req.method in methods_with_form {
body = strip(body)
println('body="$body"' )
app.vweb.parse_form(body)
}
if vals.len < 2 {
$if debug { $if debug {
println('action=$action') println('no vals for http')
}
app.$action() or {
conn.send_string(HTTP_404) or {}
} }
conn.close() or {} conn.close() or {}
reset := 'reset' return
app.$reset() //continue
} }
// Serve a static file if it's one
// if app.vweb.handle_static() {
// conn.close()
// continue
// }
// Call the right action
$if debug {
println('action=$action')
}
app.$action() or {
conn.send_string(HTTP_404) or {}
}
conn.close() or {}
app.reset()
} }
fn (ctx mut Context) parse_form(s string) { fn (ctx mut Context) parse_form(s string) {