FileProvider icon indicating copy to clipboard operation
FileProvider copied to clipboard

FTP giving unexpected status messages; not uploading?

Open akirmse opened this issue 6 years ago • 3 comments
trafficstars

I'm trying to upload to an ftp server that requires anonymous login. Here's my code:

    private static let FTP_SERVER_NAME = "ftpnos.woc.noaa.gov"
    private static let FTP_USERNAME = "anonymous"
    private static let FTP_PASSWORD = "[email protected]"

    private func sendDataViaFtp(data: Data, remoteFilename: String, callback: @escaping (_ success: Bool) -> ()) {
        let ftpUrl = URL(string: "ftp://\(SurveyMarkerReporter.FTP_SERVER_NAME)")!
        let credential = URLCredential(user: Self.FTP_USERNAME, password: Self.FTP_PASSWORD, persistence: .forSession)
        
        if let uploader = FTPFileProvider(baseURL: ftpUrl, mode: .default, credential: credential) {
            uploader.writeContents(path: remoteFilename, contents: data, overwrite: true) { error in
                if let error = error {
                    log.warning("Unable to upload file via FTP: \(error)")
                    callback(false)
                    return
                }
                callback(true)
                return
            }
        } else {
            log.error("Couldn't create FTP uploader")
            callback(false)
        }
    }

I'm getting 220 and 331 status messages back (see logs below), which sound like potentially there isn't really an error, but uploading has not occurred. I don't control this FTP server and can't see what's happening on the server side.

2019-09-21 21:53:36.765 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 220, path: "", serverDescription: Optional("ProFTPD 1.3.3a Server (ftpnos.woc.noaa.gov) [10.21.2.65]"))
2019-09-21 21:53:46.820 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 331, path: "", serverDescription: Optional("Anonymous login ok, send your complete email address as your password"))
2019-09-21 21:53:48.636 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 331, path: "", serverDescription: Optional("Anonymous login ok, send your complete email address as your password"))
2019-09-21 21:53:50.015 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 331, path: "", serverDescription: Optional("Anonymous login ok, send your complete email address as your password"))

akirmse avatar Sep 22 '19 05:09 akirmse

Stepping through the FTPProvider code, I think maybe it's not expecting many lines of response when logging in? After many retries, one upload did eventually succeed, so this is close to working.

Here's a transcript of logging in via command-line ftp:

$ ftp ftpnos.woc.noaa.gov
Connected to kvm-east2-ftpnos.w2.wocvm.noaa.gov.
220-********  WARNING     WARNING    WARNING   WARNING   WARNING ********
220-This is a United States government computer system, which may be
220-accessed and used only for official Government business by authorized
220-personnel.  Unauthorized access or use of this computer system may
220-subject violators to criminal, civil, and/or administrative action.
220-
220-All information on this computer system may be intercepted, recorded,
220-read, copied, and disclosed by and to authorized personnel for official
220-purposes, including criminal investigations. Access or use of this
220-computer system by any person whether authorized or unauthorized,
220-constitutes consent to these terms.
220-********  WARNING     WARNING    WARNING   WARNING   WARNING ********
220-Notice: All files have a storage life of 30 days.  Anything older will 
220-be deleted automatically.  You have been warned.
220-
220 ProFTPD 1.3.3a Server (ftpnos.woc.noaa.gov) [10.21.2.65]
Name (ftpnos.woc.noaa.gov:akirmse): anonymous
331 Anonymous login ok, send your complete email address as your password
Password: 
230 Anonymous access granted, restrictions apply
Remote system type is UNIX.
Using binary mode to transfer files.

akirmse avatar Sep 23 '19 01:09 akirmse

After doing a little more tracing, I think the problem might be that the server sends some of the 220 responses, so FTPProvider immediately sends the login name. But then more of the initial 220 responses arrive, which the provider is incorrectly interpreting as the response to the username login.

I see there are some 0.1 sec sleeps in the code, perhaps an attempt to wait until the server has sent everything? But that doesn't seem very robust.

akirmse avatar Sep 23 '19 01:09 akirmse

Not be able to download file. Every time get exception because FTP server

I am using host like ftp://12.2.3.5:22

Please help me.

Thnks

Pinturaj avatar Sep 10 '20 04:09 Pinturaj