Commit 0cf37d9cece17fa0fbeadf7a2701d48207508e5f

Authored by David RENÉ
1 parent 2d68d527

[~] improve read from connection and add fill buffer which can called from everywhere

Showing 1 changed file with 55 additions and 22 deletions   Show diff stats
web/multihost_http_server.anubis
... ... @@ -870,6 +870,34 @@ define One
870 870 }
871 871 }.
872 872  
  873 +define Maybe(Int)
  874 + fill_buffer
  875 + (
  876 + HTTP_Buffered_Connection connection,
  877 + Int time_out
  878 + )=
  879 + if read(connection.conn, 32768, time_out) is // the connection is closed after 10 minutes of inactivity
  880 + {
  881 + failure then
  882 + println("fill_buffer read failure");failure,
  883 + success(ba) then
  884 + with size_read = length(ba),
  885 + //println("size read "+size_read);
  886 + //the needed read is higher than the
  887 + if size_read = 0 then //time_out
  888 + success(0)
  889 +// read_from_connexion(connection, size, time_out, result_buffer)
  890 +// else if size > size_read then
  891 +// put(ba, result_buffer, position, 0);
  892 +// read_from_connexion(connection, size - size_read, time_out, result_buffer, position+size_read)
  893 + else
  894 + //println(pid + "ba = " + length(ba));
  895 + connection.buffer <- ba;
  896 + connection.read_pos <- 0;
  897 + success(size_read)
  898 + }
  899 +.
  900 +
873 901 define Maybe(ByteArray)
874 902 read_from_connexion
875 903 (
... ... @@ -907,30 +935,35 @@ define Maybe(ByteArray)
907 935 // println(pid + "content: ["+to_string(current_result_buffer)+"]");
908 936 success(current_result_buffer)
909 937 else
  938 + if fill_buffer(connection, time_out) is
  939 + {
  940 + failure then failure
  941 + success(n) then read_from_connexion(connection, size_to_read, time_out, result_buffer)
  942 + }
910 943 //with t0 = (UTime) unow,
911 944 //if unow > dead_line then record_dubious_connection(connection,dead_line,dos) else
912   - println(pid +"try to read fill buffer with 32768 bytes time out ["+time_out+"]");
913   - if read(connection.conn, 32768, time_out) is // read with timeout
914   - {
915   - failure then println(pid + "read failed ["+to_string(result_buffer)+"] connection maybe lost"); failure,
916   - success(ba) then
917   - with size_read = length(ba),
918   - println( pid + "read ["+size_read+"] bytes");
919   - //the needed read is higher than the
920   - if size_read = 0 then
921   - read_from_connexion(connection, size_to_read, time_out, result_buffer)
922   - else if size_to_read > size_read then
923   - //put(ba, result_buffer, position, 0);
924   - with current_result_buffer = result_buffer + ba,
925   - read_from_connexion(connection, size_to_read - size_read, time_out, current_result_buffer)
926   - else
927   - //println(pid + "ba = " + length(ba) + " duration: " + __utime_to_string((UTime) unow - t0));
928   - connection.buffer <- ba;
929   - connection.read_pos <- 0;
930   - //println(pid + "rb = " + length(*read_buffer));
931   -
932   - terminal read_from_connexion(connection, size_to_read, time_out, result_buffer)
933   - }
  945 +// println(pid +"try to read fill buffer with 32768 bytes time out ["+time_out+"]");
  946 +// if read(connection.conn, 32768, time_out) is // read with timeout
  947 +// {
  948 +// failure then println(pid + "read failed ["+to_string(result_buffer)+"] connection maybe lost"); failure,
  949 +// success(ba) then
  950 +// with size_read = length(ba),
  951 +// println( pid + "read ["+size_read+"] bytes");
  952 +// //the needed read is higher than the
  953 +// if size_read = 0 then
  954 +// read_from_connexion(connection, size_to_read, time_out, result_buffer)
  955 +// else if size_to_read > size_read then
  956 +// //put(ba, result_buffer, position, 0);
  957 +// with current_result_buffer = result_buffer + ba,
  958 +// read_from_connexion(connection, size_to_read - size_read, time_out, current_result_buffer)
  959 +// else
  960 +// //println(pid + "ba = " + length(ba) + " duration: " + __utime_to_string((UTime) unow - t0));
  961 +// connection.buffer <- ba;
  962 +// connection.read_pos <- 0;
  963 +// //println(pid + "rb = " + length(*read_buffer));
  964 +//
  965 +// terminal read_from_connexion(connection, size_to_read, time_out, result_buffer)
  966 +// }
934 967 .
935 968  
936 969 define Maybe(ByteArray)
... ...