Commit efbef67a713ac64fcfe61b3332ff768272454342

Authored by David RENÉ
1 parent b43ac9a0

[~] refactor read from connection

Showing 1 changed file with 58 additions and 34 deletions   Show diff stats
web/multihost_http_server.anubis
@@ -873,52 +873,75 @@ define One @@ -873,52 +873,75 @@ define One
873 define Maybe(ByteArray) 873 define Maybe(ByteArray)
874 read_from_connexion 874 read_from_connexion
875 ( 875 (
876 - HTTP_Buffered_Connection connection,  
877 - Int size,  
878 - Int time_out,  
879 - ByteArray result_buffer,  
880 - Int position 876 + HTTP_Buffered_Connection connection, //connection to read
  877 + Int size_to_read, //size to read
  878 + Int time_out, //time out
  879 + ByteArray result_buffer //the current read bytes
881 ) = 880 ) =
882 - //println(pid + "read_from_connexion(" + size + ")"); 881 + //println(pid + "read_from_connexion(" + size_to_read + ", "+time_out+")");
  882 + //println(pid + "connection buffer ["+length(*connection.buffer)+"]["+*connection.read_pos+"]["+to_string(*connection.buffer)+"]");
883 883
884 if *connection.read_pos < length(*connection.buffer) then 884 if *connection.read_pos < length(*connection.buffer) then
885 - //println(pid + " reading from buffer (size = " + length(*connection.buffer) + ", pos = " + *connection.read_pos);  
886 - //with t1_tmp = (UTime) unow,  
887 - with result = extract(*connection.buffer, *connection.read_pos, *connection.read_pos + size), 885 + //println(pid + " reading from buffer (size = " + length(*connection.buffer) + ", pos = " + *connection.read_pos+ ")");
  886 + with result = extract(*connection.buffer, *connection.read_pos, *connection.read_pos + size_to_read),
888 size_read = length(result), 887 size_read = length(result),
889 - put(result,result_buffer,position,0); 888 + //put(result, result_buffer, position, 0);
  889 + //println(pid + "read size ["+size_read+"]["+to_string(result)+"]");
  890 + //println(pid + "result_buffer size ["+length(result_buffer)+"]["+to_string(result_buffer)+"]");
  891 + with current_result_buffer = result_buffer + result,
  892 + //println(pid + "current_result_buffer size ["+length(current_result_buffer)+"]["+to_string(current_result_buffer)+"]");
  893 + //println(pid + "connection.read_pos "+*connection.read_pos+" + read_size ["+size_read+"]");
890 connection.read_pos <- *connection.read_pos + size_read; 894 connection.read_pos <- *connection.read_pos + size_read;
  895 + //println(pid + "new connection.read_pos["+*connection.read_pos+"]");
891 //accumulate_t1(t1_tmp); 896 //accumulate_t1(t1_tmp);
892 - if size > size_read then 897 + if size_to_read > size_read then
893 898
894 - terminal read_from_connexion(connection, size - size_read, time_out, result_buffer, position+size_read) 899 + terminal read_from_connexion(connection, size_to_read - size_read, time_out, current_result_buffer)
895 // { 900 // {
896 // error then error, 901 // error then error,
897 // timeout then ok(result), 902 // timeout then ok(result),
898 // ok(ba) then ok(result + ba) 903 // ok(ba) then ok(result + ba)
899 // } 904 // }
900 else 905 else
901 - success(result_buffer) 906 + // println(pid + "success: ["+length(current_result_buffer)+"]");
  907 + // println(pid + "content: ["+to_string(current_result_buffer)+"]");
  908 + success(current_result_buffer)
902 else 909 else
903 - with t0 = (UTime) unow, 910 + //with t0 = (UTime) unow,
904 //if unow > dead_line then record_dubious_connection(connection,dead_line,dos) else 911 //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+"]");
905 if read(connection.conn, 32768, time_out) is // read with timeout 913 if read(connection.conn, 32768, time_out) is // read with timeout
906 { 914 {
907 - failure then println(pid + "read failed ["+to_string(result_buffer)+"]"); failure, 915 + failure then println(pid + "read failed ["+to_string(result_buffer)+"] connection maybe lost"); failure,
908 success(ba) then 916 success(ba) then
909 with size_read = length(ba), 917 with size_read = length(ba),
  918 + println( pid + "read ["+size_read+"] bytes");
910 //the needed read is higher than the 919 //the needed read is higher than the
911 - if size > size_read then  
912 - put(ba, result_buffer, position, 0);  
913 - read_from_connexion(connection, size - size_read, time_out, result_buffer, position+size_read) 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)
914 else 926 else
915 //println(pid + "ba = " + length(ba) + " duration: " + __utime_to_string((UTime) unow - t0)); 927 //println(pid + "ba = " + length(ba) + " duration: " + __utime_to_string((UTime) unow - t0));
916 connection.buffer <- ba; 928 connection.buffer <- ba;
917 connection.read_pos <- 0; 929 connection.read_pos <- 0;
918 //println(pid + "rb = " + length(*read_buffer)); 930 //println(pid + "rb = " + length(*read_buffer));
919 931
920 - terminal read_from_connexion(connection, size, time_out, result_buffer, position)  
921 - }. 932 + terminal read_from_connexion(connection, size_to_read, time_out, result_buffer)
  933 + }
  934 +.
  935 +
  936 +define Maybe(ByteArray)
  937 + read_from_connexion
  938 + (
  939 + HTTP_Buffered_Connection connection, //connection to read
  940 + Int size_to_read, //size to read
  941 + Int time_out //time out
  942 + )=
  943 + read_from_connexion(connection, size_to_read, time_out, constant_byte_array(0,0))
  944 +.
922 945
923 define Result(Error,Word8) 946 define Result(Error,Word8)
924 next_char // reading a character (check the list first, and read on the connection 947 next_char // reading a character (check the list first, and read on the connection
@@ -938,7 +961,7 @@ define Result(Error,Word8) @@ -938,7 +961,7 @@ define Result(Error,Word8)
938 if nth(*connection.read_pos, *connection.buffer) is 961 if nth(*connection.read_pos, *connection.buffer) is
939 { 962 {
940 failure then 963 failure then
941 - if read_from_connexion(connection,1,0, constant_byte_array(1,0),0) is 964 + if read_from_connexion(connection, 1, 600 /*, constant_byte_array(1,0),0*/) is
942 { 965 {
943 failure then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection), 966 failure then /*accumulate_t2(t2_tmp);*/ error(cannot_read_from_connection),
944 //record_dubious_connection(connection,dead_line,dos), 967 //record_dubious_connection(connection,dead_line,dos),
@@ -953,6 +976,7 @@ define Result(Error,Word8) @@ -953,6 +976,7 @@ define Result(Error,Word8)
953 }, 976 },
954 success(c) then 977 success(c) then
955 connection.read_pos <- *connection.read_pos + 1; 978 connection.read_pos <- *connection.read_pos + 1;
  979 + //println(pid + "read [" + implode([c]) + "] \t new pos ["+*connection.read_pos+"]");
956 //accumulate_t2(t2_tmp); 980 //accumulate_t2(t2_tmp);
957 ok(c) 981 ok(c)
958 }, 982 },
@@ -1898,7 +1922,7 @@ public define Result(Error, ByteArray) @@ -1898,7 +1922,7 @@ public define Result(Error, ByteArray)
1898 if body_size = 0 then ok(constant_byte_array(0,0)) else 1922 if body_size = 0 then ok(constant_byte_array(0,0)) else
1899 if retries =< 0 then error(cannot_read_from_connection) else 1923 if retries =< 0 then error(cannot_read_from_connection) else
1900 1924
1901 - if read_from_connexion(connection,body_size,0,constant_byte_array(body_size,0),0) is 1925 + if read_from_connexion(connection, body_size, 600 /*,constant_byte_array(body_size,0),0*/) is
1902 { 1926 {
1903 failure then error(cannot_read_from_connection), 1927 failure then error(cannot_read_from_connection),
1904 success(new_bytes) then 1928 success(new_bytes) then
@@ -3100,18 +3124,18 @@ define String @@ -3100,18 +3124,18 @@ define String
3100 3124
3101 define String 3125 define String
3102 strip_port 3126 strip_port
3103 - (  
3104 - String name,  
3105 - Int i  
3106 - ) =  
3107 - if nth(i,name) is  
3108 - {  
3109 - failure then name,  
3110 - success(c) then  
3111 - if c = ':'  
3112 - then substr(name,0,i)  
3113 - else strip_port(name,i+1)  
3114 - }. 3127 + (
  3128 + String name,
  3129 + Int i
  3130 + ) =
  3131 + if nth(i,name) is
  3132 + {
  3133 + failure then name,
  3134 + success(c) then
  3135 + if c = ':'
  3136 + then substr(name, 0, i)
  3137 + else strip_port(name, i+1)
  3138 + }.
3115 3139
3116 3140
3117 3141