]> granicus.if.org Git - ejabberd/commitdiff
mod_http_upload: Treat file and network errors differently
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>
Wed, 4 Jul 2018 05:55:52 +0000 (08:55 +0300)
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>
Wed, 4 Jul 2018 05:55:52 +0000 (08:55 +0300)
src/ejabberd_http.erl
src/mod_http_upload.erl

index ef1888b12b642d3c0afb22727c0d6224e3151bd4..7ce936d153e176818f3cce2662d96e10ed75b2c5 100644 (file)
@@ -598,35 +598,37 @@ recv_file(#request{length = Len, data = Trail,
                   sockmod = SockMod, socket = Socket}, Path) ->
     case file:open(Path, [write, exclusive, raw]) of
        {ok, Fd} ->
-           case file:write(Fd, Trail) of
-               ok ->
-                   NewLen = max(0, Len - byte_size(Trail)),
-                   case do_recv_file(NewLen, SockMod, Socket, Fd) of
-                       ok ->
-                           ok;
-                       {error, _} = Err ->
-                           file:delete(Path),
-                           Err
-                   end;
-               {error, _} = Err ->
-                   file:delete(Path),
-                   Err
-           end;
+           Res = case file:write(Fd, Trail) of
+                     ok ->
+                         NewLen = max(0, Len - byte_size(Trail)),
+                         do_recv_file(NewLen, SockMod, Socket, Fd);
+                     {error, _} = Err ->
+                         Err
+                 end,
+           file:close(Fd),
+           case Res of
+               ok -> ok;
+               {error, _} -> file:delete(Path)
+           end,
+           Res;
        {error, _} = Err ->
            Err
     end.
 
-do_recv_file(0, _SockMod, _Socket, Fd) ->
-    file:close(Fd);
+do_recv_file(0, _SockMod, _Socket, _Fd) ->
+    ok;
 do_recv_file(Len, SockMod, Socket, Fd) ->
     ChunkLen = min(Len, ?RECV_BUF),
-    try
-       {ok, Data} = SockMod:recv(Socket, ChunkLen, timer:seconds(30)),
-       ok = file:write(Fd, Data),
-       do_recv_file(Len-size(Data), SockMod, Socket, Fd)
-    catch _:{badmatch, {error, _} = Err} ->
-           file:close(Fd),
-           Err
+    case SockMod:recv(Socket, ChunkLen, timer:seconds(30)) of
+       {ok, Data} ->
+           case file:write(Fd, Data) of
+               ok ->
+                   do_recv_file(Len-size(Data), SockMod, Socket, Fd);
+               {error, _} = Err ->
+                   Err
+           end;
+       {error, _} ->
+           {error, closed}
     end.
 
 make_headers(State, Status, Reason, Headers, Data) ->
index 55a51af863ec3c281b5b29a68cf7b2aa9c5022c6..bc88db59333ebf523086e00bc22e0ba9dfbcb72a 100644 (file)
@@ -390,9 +390,13 @@ process(_LocalPath, #request{method = 'PUT', host = Host, ip = IP,
                    http_response(201, CustomHeaders);
                {ok, Headers, OutData} ->
                    http_response(201, Headers ++ CustomHeaders, OutData);
+               {error, closed} ->
+                   ?DEBUG("Cannot store file ~s from ~s for ~s: connection closed",
+                          [Path, ?ADDR_TO_STR(IP), Host]),
+                   http_response(404);
                {error, Error} ->
-                   ?INFO_MSG("Cannot store file ~s from ~s for ~s: ~s",
-                             [Path, ?ADDR_TO_STR(IP), Host, format_error(Error)]),
+                   ?ERROR_MSG("Cannot store file ~s from ~s for ~s: ~s",
+                              [Path, ?ADDR_TO_STR(IP), Host, format_error(Error)]),
                    http_response(500)
            end;
        {error, size_mismatch} ->