From 949610f9d1d08e5d9af777975857a7b1d79962cd Mon Sep 17 00:00:00 2001 From: totoro Date: Tue, 5 Jan 2021 15:12:23 +0900 Subject: [PATCH] in Anubis 1.19 read from file or connection now return a Maybe(ByteArray) instead of a ReadResult --- web/CXM_multihost_http_server.anubis | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------- 1 file changed, 52 insertions(+), 57 deletions(-) diff --git a/web/CXM_multihost_http_server.anubis b/web/CXM_multihost_http_server.anubis index 9d31090..1b891e4 100644 --- a/web/CXM_multihost_http_server.anubis +++ b/web/CXM_multihost_http_server.anubis @@ -870,15 +870,15 @@ define One } }. -define ReadResult - read_from_connexion - ( - HTTP_Buffered_Connection connection, - Int size, - Int time_out, - ByteArray result_buffer, - Int position - ) = +define Maybe(ByteArray) + read_from_connexion + ( + HTTP_Buffered_Connection connection, + Int size, + Int time_out, + ByteArray result_buffer, + Int position + ) = //println(pid + "read_from_connexion(" + size + ")"); if *connection.read_pos < length(*connection.buffer) then @@ -898,15 +898,14 @@ define ReadResult // ok(ba) then ok(result + ba) // } else - ok(result_buffer) + success(result_buffer) else with t0 = (UTime) unow, //if unow > dead_line then record_dubious_connection(connection,dead_line,dos) else if read(connection.conn, 32768, time_out) is // the connection is closed after 10 minutes of inactivity { - error then println(pid + "read failed ["+to_string(result_buffer)+"]"); error, - timeout then timeout, - ok(ba) then + failure then println(pid + "read failed ["+to_string(result_buffer)+"]"); failure, + success(ba) then with size_read = length(ba), //the needed read is higher than the if size > size_read then @@ -941,10 +940,9 @@ define Result(Error,Word8) failure then if read_from_connexion(connection,1,600, constant_byte_array(1,0),0) is // the connection is closed after 10 minutes of inactivity { - error then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection), - timeout then /*accumulate_t2(t2_tmp);*/ error(timeout(600)), + failure then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection), //record_dubious_connection(connection,dead_line,dos), - ok(ba) then if nth(0,ba) is + success(ba) then if nth(0,ba) is { failure then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection), success(c) then @@ -1897,28 +1895,27 @@ public define Result(Error, ByteArray) ByteArray so_far, // when calling this function, 'so_far' is the empty byte array Int retries // this function is called with retries = 10 )= - if body_size = 0 then ok(constant_byte_array(0,0)) else - if retries =< 0 then error(cannot_read_from_connection) else + if body_size = 0 then ok(constant_byte_array(0,0)) else + if retries =< 0 then error(cannot_read_from_connection) else - if read_from_connexion(connection,body_size,60,constant_byte_array(body_size,0),0) is - { - error then error(cannot_read_from_connection), - timeout then error(timeout(60)), - ok(new_bytes) then with + if read_from_connexion(connection,body_size,60,constant_byte_array(body_size,0),0) is + { + failure then error(cannot_read_from_connection), + success(new_bytes) then + with ba = so_far + new_bytes, // contains all the bytes read so far nr = length(ba), // total read since the beginning nn = length(new_bytes), // number of bytes just read - if nr < body_size // must read more bytes - then if nn > 0 // if connection seems to work - - then - println("http body ("+body_size+") read "+nn+" bytes current size "+nr+" Bytes"); - read_http_body(connection,body_size,ba,1000) // continue reading - else sleep(100); // otherwise, sleep 1/10 of second - read_http_body(connection,body_size,ba, // and retry reading - retries-1) // but no more than 10 times + if nr < body_size then // must read more bytes + if nn > 0 then // if connection seems to work + println("http body ("+body_size+") read "+nn+" bytes current size "+nr+" Bytes"); + read_http_body(connection,body_size,ba,1000) // continue reading + else + sleep(100); // otherwise, sleep 1/10 of second + read_http_body(connection,body_size,ba,retries-1) // and retry reading but no more than 10 times else ok(ba) // required number of bytes has been read - }. + } +. Note: During sleeping, 'anbexec' runs other machines. Actually, calling 'sleep', even @@ -2226,32 +2223,30 @@ define List(HTTP_header) Sending the body of the answer (i.e. the file itself). define One - send_file_body - ( - Web_Site_Description desc, - Connection connection, // connection with the client - Connection file, // file to be sent already opened - Int size, // size of file - Int sent, // bytes already sent - String filename // name of file - ) = - if sent >= size then + send_file_body + ( + Web_Site_Description desc, + Connection connection, // connection with the client + Connection file, // file to be sent already opened + Int size, // size of file + Int sent, // bytes already sent + String filename // name of file + ) = + if sent >= size then //TODO add call back on success if need unique - else - if read(file,min(65536,size-sent),60) is - { - error then log_journal_msg(desc,"Cannot read from file '"+filename+"'.\n"), - timeout then log_journal_msg(desc,"Cannot read from file timeout'"+filename+"'.\n"), - ok(ba) then - with nr = length(ba), // get the number of bytes read - if reliable_write(connection, ba) is - { - failure then log_journal_msg(desc,"Cannot write into connection delirering '"+filename+"' (sent="+sent+"; size="+size+"; current="+nr+").\n"), - success(nw) then - send_file_body(desc,connection,file,size,sent+nw,filename) - } - }. + else if read(file,min(65536,size-sent),60) is + { + failure then log_journal_msg(desc,"Cannot read from file '"+filename+"'.\n"), + success(ba) then + with nr = length(ba), // get the number of bytes read + if reliable_write(connection, ba) is + { + failure then log_journal_msg(desc,"Cannot write into connection delirering '"+filename+"' (sent="+sent+"; size="+size+"; current="+nr+").\n"), + success(nw) then send_file_body(desc,connection,file,size,sent+nw,filename) + } + } +. define String -- libgit2 0.21.4