Commit e8d6381ff37bbd75fccac6c48502a80d0220bb79

Authored by totoro
1 parent cde771f1

[!] speed up drastically the upload management on web server

Showing 1 changed file with 40 additions and 45 deletions   Show diff stats
web/CXM_multihost_http_server.anubis
... ... @@ -748,7 +748,7 @@ define String
748 748  
749 749 *** [2.3] A set of state variables for the server.
750 750  
751   -type SState:
  751 +public type SState:
752 752 sstate
753 753 (
754 754 Var(List(Word8)) unput_chars, // for reading requests
... ... @@ -1822,8 +1822,8 @@ define Result(Error,Int)
1822 1822 {
1823 1823 [ ] then ok(0),
1824 1824 [h . t] then if h is http_header(name,value) then
1825   - if name = "content-length"
1826   - then if decimal_scan(value) is
  1825 + if name = "content-length" then
  1826 + if decimal_scan(value) is
1827 1827 {
1828 1828 failure then error(incorrect_content_length_value),
1829 1829 success(n) then ok(n)
... ... @@ -1831,15 +1831,7 @@ define Result(Error,Int)
1831 1831 else get_body_size(t)
1832 1832 }.
1833 1833  
1834   -
1835   -
1836   -
1837   -
1838   -
1839   -
1840   -
1841   -
1842   -
  1834 +
1843 1835 *** [4.10] Reading the body of the request.
1844 1836  
1845 1837 The body of the request may be very big (it contains uploaded files, if any). We read
... ... @@ -2737,21 +2729,23 @@ define Maybe((String,Maybe(String)))
2737 2729 *** [5.7.4] Saving an uploaded file under a temporary filename.
2738 2730  
2739 2731 define Maybe(String) // returns the temporary file name
2740   - save_uploaded_file
2741   - (
2742   - Web_Site_Description desc,
2743   - RStream body_fd,
  2732 + save_uploaded_file
  2733 + (
  2734 + String web_site_dir,
  2735 +// Web_Site_Description desc,
  2736 +
  2737 + RStream body_fd,
2744 2738 // ByteArray body,
2745   - Int start,
2746   - Int end,
2747   - SState s
2748   - ) =
  2739 + Int start,
  2740 + Int end,
  2741 + SState s
  2742 + ) =
2749 2743 s.uploaded_file_count <- 1 + *(s.uploaded_file_count);
2750 2744 with tfn = "_"+to_decimal(virtual_machine_id)+"_"+to_decimal(*(s.uploaded_file_count)),
2751 2745 // println("save_uploaded_file to :"+site_directory(desc)+"/upload_temporary/"+tfn);
2752 2746 // println("start offset = "+start+" end offset = "+end);
2753 2747 //make data_io which is the size of the file to extract
2754   - if copy_Data_IO_to_file(make_data_io(body_fd, start, end - start), site_directory(desc)+"/upload_temporary/"+tfn) is copy_ok(_)
  2748 + if copy_Data_IO_to_file(make_data_io(body_fd, start, end - start), web_site_dir+"/upload_temporary/"+tfn) is copy_ok(_)
2755 2749 then success(tfn)
2756 2750 else failure
2757 2751 .
... ... @@ -2818,11 +2812,12 @@ define String
2818 2812 define Maybe(Web_arg)
2819 2813 get_multipart_entity
2820 2814 (
2821   - Web_Site_Description desc,
2822   - String body_temp_file,
2823   - Int start_offset, //real offset in source file of the part
2824   - Int end_offset, //real offset in source file of the part
2825   - SState s
  2815 + //Web_Site_Description desc,
  2816 + String web_site_dir,
  2817 + String body_temp_file,
  2818 + Int start_offset, //real offset in source file of the part
  2819 + Int end_offset, //real offset in source file of the part
  2820 + SState s
2826 2821 )=
2827 2822 //Get header of the part
2828 2823 if find_the_first(body_temp_file, crlf+crlf, start_offset, end_offset) is
... ... @@ -2849,10 +2844,10 @@ define Maybe(Web_arg)
2849 2844 failure,
2850 2845  
2851 2846 success(fn) then
2852   - if save_uploaded_file(desc, body_fd, start_offset+k+4, end_offset-2, s) is
  2847 + if save_uploaded_file(web_site_dir, body_fd, start_offset+k+4, end_offset-2, s) is
2853 2848 {
2854 2849 failure then failure,
2855   - success(tfn) then success(upload(name, remove_path(fn), site_directory(desc)+"/upload_temporary/"+tfn))
  2850 + success(tfn) then success(upload(name, remove_path(fn), web_site_dir+"/upload_temporary/"+tfn))
2856 2851  
2857 2852 }
2858 2853 }
... ... @@ -2864,39 +2859,40 @@ define Maybe(Web_arg)
2864 2859  
2865 2860  
2866 2861  
2867   -define List(Web_arg)
  2862 +public define List(Web_arg)
2868 2863 read_multipart_form_data_encoded_web_args
2869 2864 (
2870   - Web_Site_Description desc,
  2865 + String web_site_dir,
  2866 + //Web_Site_Description desc,
2871 2867 String body_temp_file,
2872 2868 String __boundary,
2873 2869 Int file_offset,
2874 2870 SState s
2875 2871 ) =
2876 2872 with boundary_length = length(__boundary),
2877   -// println(
2878   -//"read_multipart_form_data_encoded_web_args
2879   -// boundary["+__boundary+"]
2880   -// body_temp_file : "+body_temp_file+"
2881   -// file_offset : "+file_offset);
  2873 + println(
  2874 +"read_multipart_form_data_encoded_web_args
  2875 + boundary["+__boundary+"]
  2876 + body_temp_file : "+body_temp_file+"
  2877 + file_offset : "+file_offset);
2882 2878 //get the first boundary position
2883 2879 if find_the_first(body_temp_file, __boundary, file_offset) is
2884 2880 {
2885   - failure then println("first boundary not found");[ ],
  2881 + failure then println("first boundary NOT found");[ ],
2886 2882 success(first) then
2887 2883 with first = first + file_offset, //adjust the offset to real offset in file
2888 2884 //get the second boundary position
2889 2885 if find_the_first(body_temp_file, __boundary, first + boundary_length) is
2890 2886 {
2891   - failure then println("last boundary found");[ ],
  2887 + failure then println("last boundary NOT found");[ ],
2892 2888 success(last) then
2893 2889 with last = last + first + boundary_length, //adjust the offset to real offset in file
2894 2890 //Extract the file content here
2895   - if get_multipart_entity(desc, body_temp_file, first+boundary_length, last, s) is
  2891 + if get_multipart_entity(web_site_dir, body_temp_file, first+boundary_length, last, s) is
2896 2892 {
2897 2893 failure then [ ],
2898 2894 success(wa) then
2899   - [wa . read_multipart_form_data_encoded_web_args(desc, body_temp_file, __boundary, last, s)]
  2895 + [wa . read_multipart_form_data_encoded_web_args(web_site_dir, body_temp_file, __boundary, last, s)]
2900 2896 }
2901 2897 }
2902 2898 }.
... ... @@ -2921,7 +2917,7 @@ define One
2921 2917 failure then unique,
2922 2918 success(boundary) then
2923 2919 with all_web_args = query_string(request_line) +
2924   - read_multipart_form_data_encoded_web_args(desc,
  2920 + read_multipart_form_data_encoded_web_args(desc.site_directory,
2925 2921 body_temp_file,
2926 2922 "--"+boundary,
2927 2923 0,
... ... @@ -3204,7 +3200,7 @@ define One
3204 3200 if get_body_size(headers) is
3205 3201 {
3206 3202 error(msg) then log_journal_msg(desc,format(msg)),
3207   - ok(body_size) then
  3203 + ok(body_size) then
3208 3204 if rqline is request_line(type, uri, qstring) then
3209 3205 with rqline2 = request_line(type, handle_redirection(redirections(desc), uri, headers), qstring),
3210 3206  
... ... @@ -3215,7 +3211,7 @@ define One
3215 3211 {
3216 3212 //WWW_URL
3217 3213 www_url then
3218   - if read_http_body(connection,body_size,constant_byte_array(0,0),1000) is
  3214 + if read_http_body(connection, body_size, constant_byte_array(0,0), 1000) is
3219 3215 {
3220 3216 error(msg) then log_journal_msg(desc,format(msg)),
3221 3217 ok(body) then
... ... @@ -3228,8 +3224,7 @@ define One
3228 3224  
3229 3225 if body_size > 0 then
3230 3226 with t0 = (UTime)unow,
3231   - println("read body, size "+body_size);
3232   -
  3227 +
3233 3228 if get_socket_from_connection(conn(connection)) is
3234 3229 {
3235 3230 failure then println("can't get socket"),
... ... @@ -3264,7 +3259,7 @@ define One
3264 3259 println("Can't copy data from stream to temporary file ")
3265 3260 }}}
3266 3261 else
3267   - println("bbody_size = 0 !")
  3262 + println("body_size = 0 !")
3268 3263 }
3269 3264  
3270 3265 //print_delta("before send_answer");
... ...