在下面的端口规范中,我尝试解析 URL 以确定用户、通行证、主机和路径。用户/密码值是可选的,但如果不存在,我想为每个设置默认值。
但是,如果用户/通行证不存在,则user
/pass
会从port/spec
对象中省略——这是为什么呢?
>> read sch://foo/bar.r
PORT/SPEC is an object of value:
title string! "Wee Scheme"
scheme word! sch
ref url! sch://foo/bar.r
path string! "/bar.r"
host string! "foo"
>> read sch://foo:bar@foo/bar.r
PORT/SPEC is an object of value:
title string! "Wee Scheme"
scheme word! sch
ref url! sch://foo:bar@foo/bar.r
path string! "/bar.r"
pass string! "bar"
user string! "foo"
host string! "foo"
另外,在起草这个问题时,我在方案中犯了一个错误,并将解析规则绑定到port
,而不是——port/spec
然后如何在port/spec
对象中设置值?
规格:
sys/make-scheme [
name: 'sch
title: "Wee Scheme"
actor: [
read: func [port][
parse port/spec/ref use [chars][
chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9" "-_!+%.,"]
bind [
"sch://" [
copy user some chars #":"
copy pass some chars #"@"
| (user: 'mince pass: 'tatties)
]
copy host some chars
copy path [some [#"/" any chars]]
end
] port
]
? port/spec
]
]
]