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)
|
||||
}
|
||||
|
||||
assert_endpoint :: proc(url: string, expected_response: string) {
|
||||
assert_endpoint_response :: proc(url: string, expected_response: string) {
|
||||
state, stdout, stderr, err := os2.process_exec(
|
||||
{command = []string{"curl", "-s", url}},
|
||||
context.temp_allocator,
|
||||
|
|
@ -232,15 +232,15 @@ test_server_general_ok :: proc(t: ^testing.T) {
|
|||
|
||||
t := start_concurrent_server(&server)
|
||||
|
||||
assert_endpoint(
|
||||
assert_endpoint_response(
|
||||
"http://127.0.0.1:8080/hello/world",
|
||||
"<div>Hello firstworld</div>",
|
||||
)
|
||||
assert_endpoint(
|
||||
assert_endpoint_response(
|
||||
"http://127.0.0.1:8080/hello/lonely%20world/only",
|
||||
"<div>Hello secondlonely%20world</div>",
|
||||
)
|
||||
assert_endpoint(
|
||||
assert_endpoint_response(
|
||||
"http://127.0.0.1:8080/hello/world/and/worlds%20friend",
|
||||
"<div>Hello thirdworldandworlds%20friend</div>",
|
||||
)
|
||||
|
|
|
|||
12
server.odin
12
server.odin
|
|
@ -26,11 +26,6 @@ Server :: struct($Error_Type: typeid) {
|
|||
}
|
||||
|
||||
@(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(
|
||||
client_socket: net.TCP_Socket,
|
||||
allocator := context.temp_allocator,
|
||||
|
|
@ -39,6 +34,8 @@ read_connection :: proc(
|
|||
err: http.RequestError,
|
||||
) {
|
||||
TCP_CHUNK_SIZE :: 1024
|
||||
HTTP_END_SEQUENCE :: []byte{'\r', '\n', '\r', '\n'}
|
||||
|
||||
buffer: bytes.Buffer
|
||||
bytes.buffer_init_allocator(&buffer, 0, 0, allocator)
|
||||
|
||||
|
|
@ -48,9 +45,12 @@ read_connection :: proc(
|
|||
if net_err != nil do return data, .NetworkError
|
||||
|
||||
if bytes_read == 0 do break
|
||||
|
||||
prev_len := len(buffer.buf)
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue