2021-02-27 23:18:25 +01:00
|
|
|
module vweb
|
|
|
|
|
|
|
|
import io
|
|
|
|
|
|
|
|
struct StringReader {
|
|
|
|
text string
|
|
|
|
mut:
|
|
|
|
place int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut s StringReader) read(mut buf []byte) ?int {
|
|
|
|
if s.place >= s.text.len {
|
|
|
|
return none
|
|
|
|
}
|
2021-04-09 09:53:33 +02:00
|
|
|
max_bytes := 100
|
|
|
|
end := if s.place + max_bytes >= s.text.len { s.text.len } else { s.place + max_bytes }
|
|
|
|
n := copy(buf, s.text[s.place..end].bytes())
|
2021-02-27 23:18:25 +01:00
|
|
|
s.place += n
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reader(s string) &io.BufferedReader {
|
2021-05-13 09:26:10 +02:00
|
|
|
return io.new_buffered_reader(
|
|
|
|
reader: &StringReader{
|
|
|
|
text: s
|
|
|
|
}
|
|
|
|
)
|
2021-02-27 23:18:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_request_not_http() {
|
2021-07-13 07:06:39 +02:00
|
|
|
mut reader_ := reader('hello')
|
|
|
|
parse_request(mut reader_) or { return }
|
2021-02-27 23:18:25 +01:00
|
|
|
panic('should not have parsed')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_request_no_headers() {
|
2021-07-13 07:06:39 +02:00
|
|
|
mut reader_ := reader('GET / HTTP/1.1\r\n\r\n')
|
|
|
|
req := parse_request(mut reader_) or { panic('did not parse: $err') }
|
2021-02-27 23:18:25 +01:00
|
|
|
assert req.method == .get
|
|
|
|
assert req.url == '/'
|
|
|
|
assert req.version == .v1_1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_request_two_headers() {
|
2021-07-13 07:06:39 +02:00
|
|
|
mut reader_ := reader('GET / HTTP/1.1\r\nTest1: a\r\nTest2: B\r\n\r\n')
|
|
|
|
req := parse_request(mut reader_) or { panic('did not parse: $err') }
|
2021-04-09 18:17:33 +02:00
|
|
|
assert req.header.custom_values('Test1') == ['a']
|
|
|
|
assert req.header.custom_values('Test2') == ['B']
|
2021-02-27 23:18:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_request_two_header_values() {
|
2021-07-13 07:06:39 +02:00
|
|
|
mut reader_ := reader('GET / HTTP/1.1\r\nTest1: a; b\r\nTest2: c\r\nTest2: d\r\n\r\n')
|
|
|
|
req := parse_request(mut reader_) or { panic('did not parse: $err') }
|
2021-04-09 18:17:33 +02:00
|
|
|
assert req.header.custom_values('Test1') == ['a; b']
|
|
|
|
assert req.header.custom_values('Test2') == ['c', 'd']
|
2021-02-27 23:18:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_request_body() {
|
2021-07-13 07:06:39 +02:00
|
|
|
mut reader_ := reader('GET / HTTP/1.1\r\nTest1: a\r\nTest2: b\r\nContent-Length: 4\r\n\r\nbodyabc')
|
|
|
|
req := parse_request(mut reader_) or { panic('did not parse: $err') }
|
2021-02-27 23:18:25 +01:00
|
|
|
assert req.data == 'body'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_request_line() {
|
|
|
|
method, target, version := parse_request_line('GET /target HTTP/1.1') or {
|
|
|
|
panic('did not parse: $err')
|
|
|
|
}
|
|
|
|
assert method == .get
|
|
|
|
assert target.str() == '/target'
|
|
|
|
assert version == .v1_1
|
|
|
|
}
|
2021-03-04 17:00:03 +01:00
|
|
|
|
|
|
|
fn test_parse_form() {
|
|
|
|
assert parse_form('foo=bar&bar=baz') == map{
|
|
|
|
'foo': 'bar'
|
|
|
|
'bar': 'baz'
|
|
|
|
}
|
|
|
|
assert parse_form('foo=bar=&bar=baz') == map{
|
|
|
|
'foo': 'bar='
|
|
|
|
'bar': 'baz'
|
|
|
|
}
|
|
|
|
assert parse_form('foo=bar%3D&bar=baz') == map{
|
|
|
|
'foo': 'bar='
|
|
|
|
'bar': 'baz'
|
|
|
|
}
|
|
|
|
assert parse_form('foo=b%26ar&bar=baz') == map{
|
|
|
|
'foo': 'b&ar'
|
|
|
|
'bar': 'baz'
|
|
|
|
}
|
|
|
|
assert parse_form('a=b& c=d') == map{
|
|
|
|
'a': 'b'
|
|
|
|
' c': 'd'
|
|
|
|
}
|
|
|
|
assert parse_form('a=b&c= d ') == map{
|
|
|
|
'a': 'b'
|
|
|
|
'c': ' d '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_multipart_form() {
|
|
|
|
boundary := '6844a625b1f0b299'
|
|
|
|
names := ['foo', 'fooz']
|
|
|
|
file := 'bar.v'
|
|
|
|
ct := 'application/octet-stream'
|
|
|
|
contents := ['baz', 'buzz']
|
2021-04-20 19:18:53 +02:00
|
|
|
data := "--------------------------$boundary
|
2021-03-04 17:00:03 +01:00
|
|
|
Content-Disposition: form-data; name=\"${names[0]}\"; filename=\"$file\"
|
|
|
|
Content-Type: $ct
|
|
|
|
|
|
|
|
${contents[0]}
|
|
|
|
--------------------------$boundary
|
|
|
|
Content-Disposition: form-data; name=\"${names[1]}\"
|
|
|
|
|
|
|
|
${contents[1]}
|
|
|
|
--------------------------$boundary--
|
2021-04-20 19:18:53 +02:00
|
|
|
"
|
2021-03-04 17:00:03 +01:00
|
|
|
form, files := parse_multipart_form(data, boundary)
|
|
|
|
assert files == map{
|
|
|
|
names[0]: [FileData{
|
|
|
|
filename: file
|
|
|
|
content_type: ct
|
2021-04-16 07:46:06 +02:00
|
|
|
data: contents[0]
|
2021-03-04 17:00:03 +01:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
assert form == map{
|
2021-04-16 07:46:06 +02:00
|
|
|
names[1]: contents[1]
|
2021-03-04 17:00:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 09:53:33 +02:00
|
|
|
|
|
|
|
fn test_parse_large_body() ? {
|
2021-06-15 17:28:16 +02:00
|
|
|
body := 'ABCEF\r\n'.repeat(1024 * 1024) // greater than max_bytes
|
2021-04-09 09:53:33 +02:00
|
|
|
req := 'GET / HTTP/1.1\r\nContent-Length: $body.len\r\n\r\n$body'
|
2021-07-13 07:06:39 +02:00
|
|
|
mut reader_ := reader(req)
|
|
|
|
result := parse_request(mut reader_) ?
|
2021-04-09 09:53:33 +02:00
|
|
|
assert result.data.len == body.len
|
|
|
|
assert result.data == body
|
|
|
|
}
|