Commit 949610f9d1d08e5d9af777975857a7b1d79962cd

Authored by David RENÉ
1 parent 00ba34f0

in Anubis 1.19 read from file or connection now return a Maybe(ByteArray) instead of a ReadResult

Showing 1 changed file with 52 additions and 57 deletions   Show diff stats
web/CXM_multihost_http_server.anubis
@@ -870,15 +870,15 @@ define One @@ -870,15 +870,15 @@ define One
870 } 870 }
871 }. 871 }.
872 872
873 -define ReadResult  
874 - read_from_connexion  
875 - (  
876 - HTTP_Buffered_Connection connection,  
877 - Int size,  
878 - Int time_out,  
879 - ByteArray result_buffer,  
880 - Int position  
881 - ) = 873 +define Maybe(ByteArray)
  874 + read_from_connexion
  875 + (
  876 + HTTP_Buffered_Connection connection,
  877 + Int size,
  878 + Int time_out,
  879 + ByteArray result_buffer,
  880 + Int position
  881 + ) =
882 //println(pid + "read_from_connexion(" + size + ")"); 882 //println(pid + "read_from_connexion(" + size + ")");
883 883
884 if *connection.read_pos < length(*connection.buffer) then 884 if *connection.read_pos < length(*connection.buffer) then
@@ -898,15 +898,14 @@ define ReadResult @@ -898,15 +898,14 @@ define ReadResult
898 // ok(ba) then ok(result + ba) 898 // ok(ba) then ok(result + ba)
899 // } 899 // }
900 else 900 else
901 - ok(result_buffer) 901 + success(result_buffer)
902 else 902 else
903 with t0 = (UTime) unow, 903 with t0 = (UTime) unow,
904 //if unow > dead_line then record_dubious_connection(connection,dead_line,dos) else 904 //if unow > dead_line then record_dubious_connection(connection,dead_line,dos) else
905 if read(connection.conn, 32768, time_out) is // the connection is closed after 10 minutes of inactivity 905 if read(connection.conn, 32768, time_out) is // the connection is closed after 10 minutes of inactivity
906 { 906 {
907 - error then println(pid + "read failed ["+to_string(result_buffer)+"]"); error,  
908 - timeout then timeout,  
909 - ok(ba) then 907 + failure then println(pid + "read failed ["+to_string(result_buffer)+"]"); failure,
  908 + success(ba) then
910 with size_read = length(ba), 909 with size_read = length(ba),
911 //the needed read is higher than the 910 //the needed read is higher than the
912 if size > size_read then 911 if size > size_read then
@@ -941,10 +940,9 @@ define Result(Error,Word8) @@ -941,10 +940,9 @@ define Result(Error,Word8)
941 failure then 940 failure then
942 if read_from_connexion(connection,1,600, constant_byte_array(1,0),0) is // the connection is closed after 10 minutes of inactivity 941 if read_from_connexion(connection,1,600, constant_byte_array(1,0),0) is // the connection is closed after 10 minutes of inactivity
943 { 942 {
944 - error then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection),  
945 - timeout then /*accumulate_t2(t2_tmp);*/ error(timeout(600)), 943 + failure then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection),
946 //record_dubious_connection(connection,dead_line,dos), 944 //record_dubious_connection(connection,dead_line,dos),
947 - ok(ba) then if nth(0,ba) is 945 + success(ba) then if nth(0,ba) is
948 { 946 {
949 failure then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection), 947 failure then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection),
950 success(c) then 948 success(c) then
@@ -1897,28 +1895,27 @@ public define Result(Error, ByteArray) @@ -1897,28 +1895,27 @@ public define Result(Error, ByteArray)
1897 ByteArray so_far, // when calling this function, 'so_far' is the empty byte array 1895 ByteArray so_far, // when calling this function, 'so_far' is the empty byte array
1898 Int retries // this function is called with retries = 10 1896 Int retries // this function is called with retries = 10
1899 )= 1897 )=
1900 - if body_size = 0 then ok(constant_byte_array(0,0)) else  
1901 - if retries =< 0 then error(cannot_read_from_connection) else 1898 + if body_size = 0 then ok(constant_byte_array(0,0)) else
  1899 + if retries =< 0 then error(cannot_read_from_connection) else
1902 1900
1903 - if read_from_connexion(connection,body_size,60,constant_byte_array(body_size,0),0) is  
1904 - {  
1905 - error then error(cannot_read_from_connection),  
1906 - timeout then error(timeout(60)),  
1907 - ok(new_bytes) then with 1901 + if read_from_connexion(connection,body_size,60,constant_byte_array(body_size,0),0) is
  1902 + {
  1903 + failure then error(cannot_read_from_connection),
  1904 + success(new_bytes) then
  1905 + with
1908 ba = so_far + new_bytes, // contains all the bytes read so far 1906 ba = so_far + new_bytes, // contains all the bytes read so far
1909 nr = length(ba), // total read since the beginning 1907 nr = length(ba), // total read since the beginning
1910 nn = length(new_bytes), // number of bytes just read 1908 nn = length(new_bytes), // number of bytes just read
1911 - if nr < body_size // must read more bytes  
1912 - then if nn > 0 // if connection seems to work  
1913 -  
1914 - then  
1915 - println("http body ("+body_size+") read "+nn+" bytes current size "+nr+" Bytes");  
1916 - read_http_body(connection,body_size,ba,1000) // continue reading  
1917 - else sleep(100); // otherwise, sleep 1/10 of second  
1918 - read_http_body(connection,body_size,ba, // and retry reading  
1919 - retries-1) // but no more than 10 times 1909 + if nr < body_size then // must read more bytes
  1910 + if nn > 0 then // if connection seems to work
  1911 + println("http body ("+body_size+") read "+nn+" bytes current size "+nr+" Bytes");
  1912 + read_http_body(connection,body_size,ba,1000) // continue reading
  1913 + else
  1914 + sleep(100); // otherwise, sleep 1/10 of second
  1915 + read_http_body(connection,body_size,ba,retries-1) // and retry reading but no more than 10 times
1920 else ok(ba) // required number of bytes has been read 1916 else ok(ba) // required number of bytes has been read
1921 - }. 1917 + }
  1918 +.
1922 1919
1923 1920
1924 Note: During sleeping, 'anbexec' runs other machines. Actually, calling 'sleep', even 1921 Note: During sleeping, 'anbexec' runs other machines. Actually, calling 'sleep', even
@@ -2226,32 +2223,30 @@ define List(HTTP_header) @@ -2226,32 +2223,30 @@ define List(HTTP_header)
2226 Sending the body of the answer (i.e. the file itself). 2223 Sending the body of the answer (i.e. the file itself).
2227 2224
2228 define One 2225 define One
2229 - send_file_body  
2230 - (  
2231 - Web_Site_Description desc,  
2232 - Connection connection, // connection with the client  
2233 - Connection file, // file to be sent already opened  
2234 - Int size, // size of file  
2235 - Int sent, // bytes already sent  
2236 - String filename // name of file  
2237 - ) =  
2238 - if sent >= size then 2226 + send_file_body
  2227 + (
  2228 + Web_Site_Description desc,
  2229 + Connection connection, // connection with the client
  2230 + Connection file, // file to be sent already opened
  2231 + Int size, // size of file
  2232 + Int sent, // bytes already sent
  2233 + String filename // name of file
  2234 + ) =
  2235 + if sent >= size then
2239 //TODO add call back on success if need 2236 //TODO add call back on success if need
2240 unique 2237 unique
2241 - else  
2242 - if read(file,min(65536,size-sent),60) is  
2243 - {  
2244 - error then log_journal_msg(desc,"Cannot read from file '"+filename+"'.\n"),  
2245 - timeout then log_journal_msg(desc,"Cannot read from file timeout'"+filename+"'.\n"),  
2246 - ok(ba) then  
2247 - with nr = length(ba), // get the number of bytes read  
2248 - if reliable_write(connection, ba) is  
2249 - {  
2250 - failure then log_journal_msg(desc,"Cannot write into connection delirering '"+filename+"' (sent="+sent+"; size="+size+"; current="+nr+").\n"),  
2251 - success(nw) then  
2252 - send_file_body(desc,connection,file,size,sent+nw,filename)  
2253 - }  
2254 - }. 2238 + else if read(file,min(65536,size-sent),60) is
  2239 + {
  2240 + failure then log_journal_msg(desc,"Cannot read from file '"+filename+"'.\n"),
  2241 + success(ba) then
  2242 + with nr = length(ba), // get the number of bytes read
  2243 + if reliable_write(connection, ba) is
  2244 + {
  2245 + failure then log_journal_msg(desc,"Cannot write into connection delirering '"+filename+"' (sent="+sent+"; size="+size+"; current="+nr+").\n"),
  2246 + success(nw) then send_file_body(desc,connection,file,size,sent+nw,filename)
  2247 + }
  2248 + }
  2249 +.
2255 2250
2256 2251
2257 define String 2252 define String