Optimise the read proc
This commit is contained in:
parent
4316fb2a73
commit
83b39b15f6
2 changed files with 10 additions and 10 deletions
|
|
@ -174,7 +174,7 @@ shutdown_concurrent_server :: proc(
|
||||||
thread.destroy(t)
|
thread.destroy(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_endpoint :: proc(url: string, expected_response: string) {
|
assert_endpoint_response :: proc(url: string, expected_response: string) {
|
||||||
state, stdout, stderr, err := os2.process_exec(
|
state, stdout, stderr, err := os2.process_exec(
|
||||||
{command = []string{"curl", "-s", url}},
|
{command = []string{"curl", "-s", url}},
|
||||||
context.temp_allocator,
|
context.temp_allocator,
|
||||||
|
|
@ -232,15 +232,15 @@ test_server_general_ok :: proc(t: ^testing.T) {
|
||||||
|
|
||||||
t := start_concurrent_server(&server)
|
t := start_concurrent_server(&server)
|
||||||
|
|
||||||
assert_endpoint(
|
assert_endpoint_response(
|
||||||
"http://127.0.0.1:8080/hello/world",
|
"http://127.0.0.1:8080/hello/world",
|
||||||
"<div>Hello firstworld</div>",
|
"<div>Hello firstworld</div>",
|
||||||
)
|
)
|
||||||
assert_endpoint(
|
assert_endpoint_response(
|
||||||
"http://127.0.0.1:8080/hello/lonely%20world/only",
|
"http://127.0.0.1:8080/hello/lonely%20world/only",
|
||||||
"<div>Hello secondlonely%20world</div>",
|
"<div>Hello secondlonely%20world</div>",
|
||||||
)
|
)
|
||||||
assert_endpoint(
|
assert_endpoint_response(
|
||||||
"http://127.0.0.1:8080/hello/world/and/worlds%20friend",
|
"http://127.0.0.1:8080/hello/world/and/worlds%20friend",
|
||||||
"<div>Hello thirdworldandworlds%20friend</div>",
|
"<div>Hello thirdworldandworlds%20friend</div>",
|
||||||
)
|
)
|
||||||
|
|
|
||||||
12
server.odin
12
server.odin
|
|
@ -26,11 +26,6 @@ Server :: struct($Error_Type: typeid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
request_ended :: proc(buf: []byte) -> bool {
|
|
||||||
HTTP_END_SEQUENCE :: []byte{'\r', '\n', '\r', '\n'}
|
|
||||||
return bytes.index(buf, HTTP_END_SEQUENCE) != -1
|
|
||||||
}
|
|
||||||
|
|
||||||
read_connection :: proc(
|
read_connection :: proc(
|
||||||
client_socket: net.TCP_Socket,
|
client_socket: net.TCP_Socket,
|
||||||
allocator := context.temp_allocator,
|
allocator := context.temp_allocator,
|
||||||
|
|
@ -39,6 +34,8 @@ read_connection :: proc(
|
||||||
err: http.RequestError,
|
err: http.RequestError,
|
||||||
) {
|
) {
|
||||||
TCP_CHUNK_SIZE :: 1024
|
TCP_CHUNK_SIZE :: 1024
|
||||||
|
HTTP_END_SEQUENCE :: []byte{'\r', '\n', '\r', '\n'}
|
||||||
|
|
||||||
buffer: bytes.Buffer
|
buffer: bytes.Buffer
|
||||||
bytes.buffer_init_allocator(&buffer, 0, 0, allocator)
|
bytes.buffer_init_allocator(&buffer, 0, 0, allocator)
|
||||||
|
|
||||||
|
|
@ -48,9 +45,12 @@ read_connection :: proc(
|
||||||
if net_err != nil do return data, .NetworkError
|
if net_err != nil do return data, .NetworkError
|
||||||
|
|
||||||
if bytes_read == 0 do break
|
if bytes_read == 0 do break
|
||||||
|
|
||||||
|
prev_len := len(buffer.buf)
|
||||||
bytes.buffer_write(&buffer, chunk[:bytes_read])
|
bytes.buffer_write(&buffer, chunk[:bytes_read])
|
||||||
|
|
||||||
if request_ended(buffer.buf[:]) do break
|
search_start := max(0, prev_len - 3)
|
||||||
|
if bytes.index(buffer.buf[search_start:], HTTP_END_SEQUENCE) != -1 do break
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer.buf[:], nil
|
return buffer.buf[:], nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue