log/net: switch => match

pull/2560/head
Don Alfons Nisnoni 2019-10-27 14:45:03 +08:00 committed by Alexander Medvednikov
parent 7ba5248e56
commit ed55826686
2 changed files with 123 additions and 111 deletions

View File

@ -50,56 +50,56 @@ pub fn (l Log) fatal(s string){
pub fn (l Log) error(s string){ pub fn (l Log) error(s string){
if l.level >= ERROR{ if l.level >= ERROR{
switch l.output { match l.output {
case 'terminal': 'terminal'{
f := term.red('E') f := term.red('E')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default: l.log_file(s, 'E')
l.log_file(s, 'E') }
} }
} }
} }
pub fn (l Log) warn(s string){ pub fn (l Log) warn(s string){
if l.level >= WARN{ if l.level >= WARN{
switch l.output { match l.output {
case 'terminal': 'terminal'{
f := term.yellow('W') f := term.yellow('W')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default: l.log_file(s, 'W')
l.log_file(s, 'W') }
} }
} }
} }
pub fn (l Log) info(s string){ pub fn (l Log) info(s string){
if l.level >= INFO{ if l.level >= INFO{
switch l.output { match l.output {
case 'terminal': 'terminal'{
f := term.white('I') f := term.white('I')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default: l.log_file(s, 'I')
l.log_file(s, 'I') }
} }
} }
} }
pub fn (l Log) debug(s string){ pub fn (l Log) debug(s string){
if l.level >= DEBUG{ if l.level >= DEBUG{
switch l.output { match l.output {
case 'terminal': 'terminal' {
f := term.blue('D') f := term.blue('D')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default: l.log_file(s, 'D')
l.log_file(s, 'D') }
} }
} }
} }

View File

@ -114,9 +114,10 @@ fn should_escape(c byte, mode EncodingMode) bool {
// (1) we always escape sub-delims outside of the fragment, and (2) we always // (1) we always escape sub-delims outside of the fragment, and (2) we always
// escape single quote to avoid breaking callers that had previously assumed that // escape single quote to avoid breaking callers that had previously assumed that
// single quotes would be escaped. See issue #19917. // single quotes would be escaped. See issue #19917.
switch c { match c {
case `!`, `(`, `)`, `*`: `!`, `(`, `)`, `*`{
return false return false
}
} }
} }
@ -153,50 +154,53 @@ fn unescape(s_ string, mode EncodingMode) ?string {
mut has_plus := false mut has_plus := false
for i := 0; i < s.len; { for i := 0; i < s.len; {
x := s[i] x := s[i]
switch x { match x {
case `%`: `%` {
if s == '' { if s == '' {
break break
}
n++
if i+2 >= s.len || !ishex(s[i+1]) || !ishex(s[i+2]) {
s = s.right(i)
if s.len > 3 {
s = s.left(3)
} }
return error(error_msg(err_msg_escape, s)) n++
} if i+2 >= s.len || !ishex(s[i+1]) || !ishex(s[i+2]) {
// Per https://tools.ietf.org/html/rfc3986#page-21 s = s.right(i)
// in the host component %-encoding can only be used if s.len > 3 {
// for non-ASCII bytes. s = s.left(3)
// But https://tools.ietf.org/html/rfc6874#section-2 }
// introduces %25 being allowed to escape a percent sign return error(error_msg(err_msg_escape, s))
// in IPv6 scoped-address literals. Yay.
if mode == .encode_host && unhex(s[i+1]) < 8 && s.substr(i, i+3) != '%25' {
return error(error_msg(err_msg_escape, s.substr(i, i+3)))
}
if mode == .encode_zone {
// RFC 6874 says basically 'anything goes' for zone identifiers
// and that even non-ASCII can be redundantly escaped,
// but it seems prudent to restrict %-escaped bytes here to those
// that are valid host name bytes in their unescaped form.
// That is, you can use escaping in the zone identifier but not
// to introduce bytes you couldn't just write directly.
// But Windows puts spaces here! Yay.
v := byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2]))
if s.substr(i, i+3) != '%25' && v != ` ` && should_escape(v, .encode_host) {
error(error_msg(err_msg_escape, s.substr(i, i+3)))
} }
// Per https://tools.ietf.org/html/rfc3986#page-21
// in the host component %-encoding can only be used
// for non-ASCII bytes.
// But https://tools.ietf.org/html/rfc6874#section-2
// introduces %25 being allowed to escape a percent sign
// in IPv6 scoped-address literals. Yay.
if mode == .encode_host && unhex(s[i+1]) < 8 && s.substr(i, i+3) != '%25' {
return error(error_msg(err_msg_escape, s.substr(i, i+3)))
}
if mode == .encode_zone {
// RFC 6874 says basically 'anything goes' for zone identifiers
// and that even non-ASCII can be redundantly escaped,
// but it seems prudent to restrict %-escaped bytes here to those
// that are valid host name bytes in their unescaped form.
// That is, you can use escaping in the zone identifier but not
// to introduce bytes you couldn't just write directly.
// But Windows puts spaces here! Yay.
v := byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2]))
if s.substr(i, i+3) != '%25' && v != ` ` && should_escape(v, .encode_host) {
error(error_msg(err_msg_escape, s.substr(i, i+3)))
}
}
i += 3
} }
i += 3
case `+`: `+`{
has_plus = mode == .encode_query_component has_plus = mode == .encode_query_component
i++ i++
default: } else {
if (mode == .encode_host || mode == .encode_zone) && s[i] < 0x80 && should_escape(s[i], mode) { if (mode == .encode_host || mode == .encode_zone) && s[i] < 0x80 && should_escape(s[i], mode) {
error(error_msg('invalid character in host name', s.substr(i, i+1))) error(error_msg('invalid character in host name', s.substr(i, i+1)))
}
i++
} }
i++
} }
} }
@ -207,18 +211,20 @@ fn unescape(s_ string, mode EncodingMode) ?string {
mut t := strings.new_builder(s.len - 2*n) mut t := strings.new_builder(s.len - 2*n)
for i := 0; i < s.len; i++ { for i := 0; i < s.len; i++ {
x := s[i] x := s[i]
switch x { match x {
case `%`: `%` {
t.write( byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2])).str() ) t.write( byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2])).str() )
i += 2 i += 2
case `+`: }
if mode == .encode_query_component { `+` {
t.write(' ') if mode == .encode_query_component {
} else { t.write(' ')
t.write('+') } else {
t.write('+')
}
} else {
t.write(s[i].str())
} }
default:
t.write(s[i].str())
} }
} }
return t.str() return t.str()
@ -694,16 +700,19 @@ fn valid_encoded_path(s string) bool {
// so we check the sub-delims ourselves and let // so we check the sub-delims ourselves and let
// should_escape handle the others. // should_escape handle the others.
x := s[i] x := s[i]
switch x { match x {
case `!`, `$`, `&`, `\\`, `(`, `)`, `*`, `+`, `,`, `;`, `=`, `:`, `@`: `!`, `$`, `&`, `\\`, `(`, `)`, `*`, `+`, `,`, `;`, `=`, `:`, `@` {
// ok // ok
case `[`, `]`: }
// ok - not specified in RFC 3986 but left alone by modern browsers `[`, `]` {
case `%`: // ok - not specified in RFC 3986 but left alone by modern browsers
// ok - percent encoded, will decode }
default: `%` {
if should_escape(s[i], .encode_path) { // ok - percent encoded, will decode
return false } else {
if should_escape(s[i], .encode_path) {
return false
}
} }
} }
} }
@ -914,15 +923,17 @@ fn resolve_path(base, ref string) string {
mut dst := []string mut dst := []string
src := full.split('/') src := full.split('/')
for _, elem in src { for _, elem in src {
switch elem { match elem {
case '.': '.' {
// drop // drop
case '..': }
if dst.len > 0 { '..' {
dst = dst.left(dst.len-1) if dst.len > 0 {
dst = dst.left(dst.len-1)
}
} else {
dst << elem
} }
default:
dst << elem
} }
} }
last := src[src.len-1] last := src[src.len-1]
@ -1069,12 +1080,13 @@ pub fn valid_userinfo(s string) bool {
if `0` <= r && r <= `9` { if `0` <= r && r <= `9` {
continue continue
} }
switch r { match r {
case `-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`, `-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`,
`(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@`: `(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@` {
continue continue
default: } else {
return false return false
}
} }
} }
return true return true