Parse more characters in URLs

This commit is contained in:
Fabrice Reix 2021-10-17 10:14:03 +02:00 committed by Fabrice Reix
parent c608ff36da
commit b6f8797904
6 changed files with 92 additions and 5 deletions

View File

@ -0,0 +1,3 @@
curl 'http://localhost:8000/~user'
curl 'http://localhost:8000/%7Euser'
curl 'http://localhost:8000/!$&()*+,;=:@[]'

View File

@ -1,6 +1,14 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/~user</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="raw"><span class="line">```user```</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/%7Euser</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
</span></span></code></pre>
<span class="raw"><span class="line">```user```</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"></span><span class="comment"># TODO: add single quote (needs to be escaped in curl command-line)</span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/!$&amp;()*+,;=:@[]</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="raw"><span class="line">```weird```</span></span>
</span></span><span class="line"></span>
</code></pre>

View File

@ -1,5 +1,13 @@
GET http://localhost:8000/~user
HTTP/1.0 200
```user```
GET http://localhost:8000/%7Euser
HTTP/1.0 200
```user```
# TODO: add single quote (needs to be escaped in curl command-line)
GET http://localhost:8000/!$&()*+,;=:@[]
HTTP/1.0 200
```weird```

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/~user"},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/%7Euser"},"response":{"version":"HTTP/1.0","status":200}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/~user"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"user"}}},{"request":{"method":"GET","url":"http://localhost:8000/%7Euser"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"user"}}},{"request":{"method":"GET","url":"http://localhost:8000/!$&()*+,;=:@[]"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"weird"}}}]}

View File

@ -3,4 +3,8 @@ from tests import app
@app.route("/~user")
def url_with_tilde():
return ''
return 'user'
@app.route("/!$&()*+,;=:@[]")
def url_weird():
return 'weird'

View File

@ -70,8 +70,9 @@ pub fn url(reader: &mut Reader) -> ParseResult<'static, Template> {
None => break,
Some(c) => {
if c.is_alphanumeric()
|| vec![
':', '/', '.', '-', '?', '=', '&', '_', '%', '*', ',', '@', '~',
| vec![
':', '/', '.', '-', '?', '=', '&', '_', '%', '*', ',', '@',
'~', '+', '!', '$', '\'', '(', ')', ';', '[', ']',
]
.contains(&c)
{
@ -251,4 +252,67 @@ mod tests {
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(error.inner, ParseError::Url {});
}
#[test]
fn test_valid_urls() {
// from official url_test.go file
let valid_urls = [
"http://www.google.com",
"http://www.google.com/",
"http://www.google.com/file%20one%26two",
"http://www.google.com/#file%20one%26two",
"ftp://webmaster@www.google.com/",
"ftp://john%20doe@www.google.com/",
"http://www.google.com/?",
"http://www.google.com/?foo=bar?",
"http://www.google.com/?q=go+language",
"http://www.google.com/?q=go%20language",
"http://www.google.com/a%20b?q=c+d",
"http:www.google.com/?q=go+language",
"http:www.google.com/?q=go+language",
"http:%2f%2fwww.google.com/?q=go+language",
"http://user:password@google.com",
"http://user:password@google.com",
"http://j@ne:password@google.com",
"http://j%40ne:password@google.com",
"http://jane:p@ssword@google.com",
"http://j@ne:password@google.com/p@th?q=@go",
"http://j%40ne:password@google.com/p@th?q=@go",
"http://www.google.com/?q=go+language#foo",
"http://www.google.com/?q=go+language#foo&bar",
"http://www.google.com/?q=go+language#foo&bar",
"http://www.google.com/?q=go+language#foo%26bar",
"http://www.google.com/?q=go+language#foo%26bar",
"http://%3Fam:pa%3Fsword@google.com",
"http://192.168.0.1/",
"http://192.168.0.1:8080/",
"http://[fe80::1]/",
"http://[fe80::1]:8080/",
"http://[fe80::1%25en0]/",
"http://[fe80::1%25en0]:8080/",
"http://[fe80::1%25%65%6e%301-._~]/",
"http://[fe80::1%25en01-._~]/",
"http://[fe80::1%25%65%6e%301-._~]:8080/",
"http://rest.rsc.io/foo%2fbar/baz%2Fquux?alt=media",
"http://host/!$&'()*+,;=:@[hello]",
"http://example.com/oid/[order_id]",
"http://192.168.0.2:8080/foo",
"http://192.168.0.2:/foo",
"http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:8080/foo",
"http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:/foo",
"http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080/foo",
"http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:/foo",
"http://hello.世界.com/foo",
"http://hello.%E4%B8%96%E7%95%8C.com/foo",
"http://hello.%e4%b8%96%e7%95%8c.com/foo",
"http://hello.%E4%B8%96%E7%95%8C.com/foo",
"http://hello.%E4%B8%96%E7%95%8C.com/foo",
"http://example.com//foo",
];
for s in valid_urls {
//eprintln!("{}", s);
let mut reader = Reader::init(s);
assert!(url(&mut reader).is_ok());
}
}
}