avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver FtpConnection.java
Date Thu, 10 Oct 2002 16:03:21 GMT
rana_b      2002/10/10 09:03:21

  Modified:    ftpserver/src/java/org/apache/avalon/ftpserver
                        FtpConnection.java
  Log:
  STOR file corruption bug fixed
  
  Revision  Changes    Path
  1.14      +74 -54    jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpConnection.java
  
  Index: FtpConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpConnection.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- FtpConnection.java	20 May 2002 10:20:17 -0000	1.13
  +++ FtpConnection.java	10 Oct 2002 16:03:21 -0000	1.14
  @@ -42,7 +42,12 @@
   public
   class FtpConnection extends BaseFtpConnection {
   
  -    private static final SimpleDateFormat DATE_FMT = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
  +    // as SimpleDateFormat is not thread-safe we have to use ThreadLocal
  +    private final static ThreadLocal DATE_FMT = new ThreadLocal() {
  +        protected Object initialValue() {
  +            return new SimpleDateFormat("yyyyMMddHHmmss.SSS"); 
  +        }
  +    };
   
       // command state specific temporary variables
       private boolean mbReset   = false;
  @@ -69,7 +74,8 @@
           return mUser.hasLoggedIn() ||
                   cmd.equals("USER") ||
                   cmd.equals("PASS") ||
  -                cmd.equals("HELP");
  +                cmd.equals("HELP") ||
  +                cmd.equals("SYST");
       }
   
       /**
  @@ -105,7 +111,9 @@
   
           // reset state variables
           resetState();
  -        mDataConnection.reset();
  +         
  +        // and abort any data connection
  +        mDataConnection.closeDataSocket();
           out.write(mFtpStatus.getResponse(226, request, mUser, null));
       }
   
  @@ -155,11 +163,13 @@
                     return;
                }
   
  +            // go to the end of the file
               is = dataSoc.getInputStream();
               RandomAccessFile raf = new RandomAccessFile(requestedFile, "rw");
               raf.seek(raf.length());
               os = mUser.getOutputStream( new FileOutputStream(raf.getFD()) );
   
  +            // receive data from client
               StreamConnector msc = new StreamConnector(is, os);
               msc.setMaxTransferRate(mUser.getMaxUploadRate());
               msc.setObserver(this);
  @@ -180,7 +190,7 @@
           finally {
              IoUtils.close(is);
              IoUtils.close(os);
  -           mDataConnection.reset();
  +           mDataConnection.closeDataSocket(); 
           }
       }
   
  @@ -292,6 +302,7 @@
        * information as a response.
        */
        public void doHELP(FtpRequest request, FtpWriter out) throws IOException {
  +        resetState();
   
           // print global help
           if(!request.hasArgument()) {
  @@ -325,22 +336,23 @@
           // reset state variables
           resetState();
   
  -         out.write(mFtpStatus.getResponse(150, request, mUser, null));
  +        out.write(mFtpStatus.getResponse(150, request, mUser, null));
           Writer os = null;
           try {
  +         
  +             // get data connection
                Socket dataSoc = mDataConnection.getDataSocket();
                if (dataSoc == null) {
                     out.write(mFtpStatus.getResponse(550, request, mUser, null));
                     return;
                }
   
  +             // transfer listing data
               os = new OutputStreamWriter(dataSoc.getOutputStream());
  -
               if (!mUser.getVirtualDirectory().printList(request.getArgument(), os)) {
                	out.write(mFtpStatus.getResponse(501, request, mUser, null));
               }
               else {
  -                os.flush();
                   out.write(mFtpStatus.getResponse(226, request, mUser, null));
               }
           }
  @@ -349,7 +361,7 @@
           }
           finally {
               IoUtils.close(os);
  -            mDataConnection.reset();
  +            mDataConnection.closeDataSocket();
           }
       }
   
  @@ -361,15 +373,15 @@
        */
        public void doMDTM(FtpRequest request, FtpWriter out) throws IOException {
   
  +        // reset state
  +        resetState();
  +         
           // argument check
           if(!request.hasArgument()) {
              out.write(mFtpStatus.getResponse(501, request, mUser, null));
              return;
           }
   
  -        // reset state variables
  -        resetState();
  -
           // get filenames
           String fileName = request.getArgument();
           fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
  @@ -378,7 +390,8 @@
   
           // now print date
           if(reqFile.exists()) {
  -            String args[] = {DATE_FMT.format(new Date(reqFile.lastModified()))};
  +             SimpleDateFormat fmt = (SimpleDateFormat)DATE_FMT.get();
  +             String args[] = {fmt.format(new Date(reqFile.lastModified()))};
               out.write(mFtpStatus.getResponse(213, request, mUser, args));
           }
           else {
  @@ -397,7 +410,7 @@
        */
        public void doMKD(FtpRequest request, FtpWriter out) throws IOException {
   
  -       // reset state variables
  +       // reset state
          resetState();
   
          // argument check
  @@ -406,7 +419,7 @@
              return;
          }
   
  -       // get filenames
  +       // get filename
          String fileName = request.getArgument();
          fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
          String physicalName = mUser.getVirtualDirectory().getPhysicalName(fileName);
  @@ -437,7 +450,7 @@
        */
        public void doMODE(FtpRequest request, FtpWriter out) throws IOException {
   
  -        // reset state variables
  +        // reset state
           resetState();
   
           // argument check
  @@ -446,6 +459,7 @@
              return;
           }
   
  +        // set mode
           if (mUser.setMode(request.getArgument().charAt(0))) {
               out.write(mFtpStatus.getResponse(200, request, mUser, null));
           }
  @@ -467,25 +481,26 @@
        */
        public void doNLST(FtpRequest request, FtpWriter out) throws IOException {
   
  -        // reset state variables
  +        // reset state
           resetState();
   
           out.write(mFtpStatus.getResponse(150, request, mUser, null));
           Writer os = null;
           try {
  +         
  +             // get data connection
                Socket dataSoc = mDataConnection.getDataSocket();
                if (dataSoc == null) {
                     out.write(mFtpStatus.getResponse(550, request, mUser, null));
                     return;
                }
   
  +            // print listing data
               os = new OutputStreamWriter(dataSoc.getOutputStream());
  -
               if (!mUser.getVirtualDirectory().printNList(request.getArgument(), os)) {
                	out.write(mFtpStatus.getResponse(501, request, mUser, null));
               }
               else {
  -                os.flush();
                   out.write(mFtpStatus.getResponse(226, request, mUser, null));
               }
           }
  @@ -494,7 +509,7 @@
           }
           finally {
               IoUtils.close(os);
  -            mDataConnection.reset();
  +            mDataConnection.closeDataSocket();
           }
       }
   
  @@ -507,10 +522,7 @@
        * server send an OK reply.
        */
        public void doNOOP(FtpRequest request, FtpWriter out) throws IOException {
  -
  -        // reset state variables
           resetState();
  -
           out.write(mFtpStatus.getResponse(200, request, mUser, null));
       }
   
  @@ -563,25 +575,27 @@
        */
        public void doPASV(FtpRequest request, FtpWriter out) throws IOException {
   
  +         // reset state
  +         resetState();
  +         
  +         // set data connection
            if (!mDataConnection.setPasvCommand()) {
                out.write(mFtpStatus.getResponse(550, request, mUser, null));
                return;
            }
   
  +         // get connection info
           InetAddress servAddr = mDataConnection.getInetAddress();
           if(servAddr == null) {
                servAddr = mConfig.getSelfAddress();
           }
   
  -         int servPort = mDataConnection.getPort();
  +        int servPort = mDataConnection.getPort();
   
  +         // send connection info to client
           String addrStr = servAddr.getHostAddress().replace( '.', ',' ) + ',' + (servPort>>8)
+ ',' + (servPort&0xFF);
           String[] args = {addrStr};
  -
  -         out.write(mFtpStatus.getResponse(227, request, mUser, args));
  -         if (!mDataConnection.listenPasvConnection()) {
  -            out.write(mFtpStatus.getResponse(425, request, mUser, args));
  -         }
  +        out.write(mFtpStatus.getResponse(227, request, mUser, args));
       }
   
   
  @@ -608,9 +622,6 @@
           // reset state variables
           resetState();
   
  -        InetAddress clientAddr = null;
  -        int clientPort = 0;
  -
           // argument check
           if(!request.hasArgument()) {
              out.write(mFtpStatus.getResponse(501, request, mUser, null));
  @@ -626,6 +637,7 @@
           // get data server
           String dataSrvName = st.nextToken() + '.' + st.nextToken() + '.' +
                                st.nextToken() + '.' + st.nextToken();
  +        InetAddress clientAddr = null;
           try {
               clientAddr = InetAddress.getByName(dataSrvName);
           }
  @@ -635,6 +647,7 @@
           }
   
           // get data server port
  +        int clientPort = 0;
           try {
               int hi = Integer.parseInt(st.nextToken());
               int lo = Integer.parseInt(st.nextToken());
  @@ -644,6 +657,7 @@
              out.write(mFtpStatus.getResponse(552, request, mUser, null));
              return;
           }
  +         
           mDataConnection.setPortCommand(clientAddr, clientPort);
           out.write(mFtpStatus.getResponse(200, request, mUser, null));
       }
  @@ -656,8 +670,6 @@
        * directory to be returned in the reply.
        */
        public void doPWD(FtpRequest request, FtpWriter out) throws IOException {
  -
  -        // reset state variables
           resetState();
           String args[] = {mUser.getVirtualDirectory().getCurrentDirectory()};
           out.write(mFtpStatus.getResponse(257, request, mUser, args));
  @@ -671,11 +683,7 @@
        * in progress, the server closes the control connection.
        */
        public void doQUIT(FtpRequest request, FtpWriter out) throws IOException {
  -
  -        // reset state variables
           resetState();
  -
  -        // and exit
           out.write(mFtpStatus.getResponse(221, request, mUser, null));
           ConnectionService conService = mConfig.getConnectionService();
           if (conService != null) {
  @@ -702,7 +710,6 @@
              return;
           }
   
  -        // set state variables
           resetState();
           mlSkipLen = 0;
           String skipNum = request.getArgument();
  @@ -743,7 +750,7 @@
              return;
           }
   
  -        // get filenames
  +        // get filename
           String fileName = request.getArgument();
           fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
           String physicalName = mUser.getVirtualDirectory().getPhysicalName(fileName);
  @@ -769,15 +776,16 @@
   
               os = mUser.getOutputStream(dataSoc.getOutputStream());
   
  +             // move to the appropriate offset
               RandomAccessFile raf = new RandomAccessFile(requestedFile, "r");
               raf.seek(skipLen);
               is = new FileInputStream(raf.getFD());
   
  +             // send file data to client
               StreamConnector msc = new StreamConnector(is, os);
               msc.setMaxTransferRate(mUser.getMaxDownloadRate());
               msc.setObserver(this);
               msc.connect();
  -
               if(msc.hasException()) {
                   out.write(mFtpStatus.getResponse(451, request, mUser, args));
                   return;
  @@ -794,7 +802,7 @@
           finally {
               IoUtils.close(is);
               IoUtils.close(os);
  -            mDataConnection.reset();
  +            mDataConnection.closeDataSocket();
           }
       }
   
  @@ -862,7 +870,7 @@
           // set state variable
           mbRenFr = true;
   
  -        // get filenames
  +        // get filename
           String fileName = request.getArgument();
           fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
           mstRenFr = mUser.getVirtualDirectory().getPhysicalName(fileName);
  @@ -932,6 +940,7 @@
        * the protocol.
        */
        public void doSITE(FtpRequest request, FtpWriter out) throws IOException {
  +         resetState();
            SiteCommandHandler siteCmd = new SiteCommandHandler( mConfig, mUser );
            out.write( siteCmd.getResponse(request) );
       }
  @@ -944,15 +953,15 @@
        */
        public void doSIZE(FtpRequest request, FtpWriter out) throws IOException {
   
  +         // reset state variables
  +         resetState();
  +         
           // argument check
           if(!request.hasArgument()) {
              out.write(mFtpStatus.getResponse(501, request, mUser, null));
              return;
           }
   
  -        // reset state variables
  -        resetState();
  -
           // get filenames
           String fileName = request.getArgument();
           fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
  @@ -977,12 +986,16 @@
        * the control connection in the form of a reply.
        */
        public void doSTAT(FtpRequest request, FtpWriter out) throws IOException {
  +         
  +         // reset state variables
  +         resetState();
  +        
  +         // write the status info
           String args[] = {
               mConfig.getSelfAddress().getHostAddress(),
               mControlSocket.getInetAddress().getHostAddress(),
               mUser.getName()
           };
  -
           out.write(mFtpStatus.getResponse(211, request, mUser, args));
       }
   
  @@ -1027,23 +1040,26 @@
           InputStream is = null;
           OutputStream os = null;
           try {
  +             
  +             // get data connection
               Socket dataSoc = mDataConnection.getDataSocket();
               if (dataSoc == null) {
                   out.write(mFtpStatus.getResponse(550, request, mUser, null));
                   return;
               }
  -
               is = dataSoc.getInputStream();
   
  +             // go to the appropriate offset
               RandomAccessFile raf = new RandomAccessFile(requestedFile, "rw");
  +            raf.setLength(skipLen);
               raf.seek(skipLen);
               os = mUser.getOutputStream( new FileOutputStream(raf.getFD()) );
   
  +            // get data from client
               StreamConnector msc = new StreamConnector(is, os);
               msc.setMaxTransferRate(mUser.getMaxUploadRate());
               msc.setObserver(this);
               msc.connect();
  -
               if(msc.hasException()) {
                   out.write(mFtpStatus.getResponse(451, request, mUser, null));
                   return;
  @@ -1060,7 +1076,7 @@
           finally {
               IoUtils.close(is);
               IoUtils.close(os);
  -            mDataConnection.reset();
  +            mDataConnection.closeDataSocket();
           }
       }
   
  @@ -1097,6 +1113,8 @@
           InputStream is = null;
           OutputStream os = null;
           try {
  +         
  +             // get data connection
                Socket dataSoc = mDataConnection.getDataSocket();
                if (dataSoc == null) {
                     out.write(mFtpStatus.getResponse(550, request, mUser, args));
  @@ -1104,6 +1122,7 @@
                }
   
   
  +            // receive data from client
               is = dataSoc.getInputStream();
               os = mUser.getOutputStream( new FileOutputStream(requestedFile) );
   
  @@ -1111,7 +1130,6 @@
               msc.setMaxTransferRate(mUser.getMaxUploadRate());
               msc.setObserver(this);
               msc.connect();
  -
               if(msc.hasException()) {
                   out.write(mFtpStatus.getResponse(451, request, mUser, null));
                   return;
  @@ -1121,7 +1139,7 @@
               }
   
                out.write(mFtpStatus.getResponse(226, request, mUser, null));
  -             mDataConnection.reset();
  +             mDataConnection.closeDataSocket();
                out.write(mFtpStatus.getResponse(250, request, mUser, args));
           }
           catch(IOException ex) {
  @@ -1130,7 +1148,7 @@
           finally {
              IoUtils.close(is);
              IoUtils.close(os);
  -           mDataConnection.reset();
  +           mDataConnection.closeDataSocket(); 
           }
       }
   
  @@ -1152,6 +1170,7 @@
              return;
           }
   
  +         // set structure
           if (mUser.setStructure(request.getArgument().charAt(0))) {
               out.write(mFtpStatus.getResponse(200, request, mUser, null));
           }
  @@ -1172,6 +1191,7 @@
           // reset state variables
           resetState();
   
  +        // and print server system info 
           String args[] = {mConfig.getSystemName()};
           out.write(mFtpStatus.getResponse(215, request, mUser, args));
       }
  @@ -1214,7 +1234,7 @@
        */
        public void doUSER(FtpRequest request, FtpWriter out) throws IOException {
   
  -        // set state variables
  +         // reset state variables
           resetState();
   
           // argument check
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message