在下面的端口规范中,我试图解析一个 URL 以确定用户、密码、主机和路径。用户/密码值是可选的,但如果不存在,我想为每个设置默认值。
然而,如果 user/pass 不存在,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"
另外,在做这个题时,我在scheme中犯了一个错误,将解析规则绑定(bind)到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
]
]
]
最佳答案
制定计划https://github.com/rebol/r3/blob/master/src/mezz/sys-ports.r#L254用作默认规范系统/标准/端口规范网络,它没有用户并通过。
>> system/standard/port-spec-net
== make object! [
title: none
scheme: none
ref: none
path: none
host: none
port-id: 80
]
因此,如果您想要用户并在规范中未通过时也通过,则必须像这样创建自己的规范 https://github.com/rebol/r3/blob/master/src/mezz/prot-http.r#L417并将它们放在那里。
关于url-routing - Rebol 3 方案中的用户/密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445046/