shaqian / flutter_ssh

SSH and SFTP client for Flutter
https://pub.dartlang.org/packages/ssh
MIT License
117 stars 83 forks source link

Android version should not use \r\n for execute #13

Open frjonsen opened 5 years ago

frjonsen commented 5 years ago

When receiving the response, the Android version uses BufferedReader.readLine() in the execute function, which removes linebreaks, then reconstructs them using \r\n. Preferably, .readLine() shouldn't be used at all, since there's no way of knowing what the original linebreak was. Reasonably, something more akin to the official example. At the very least, it should be changed to \n, as the SSH server is very likely a Linux server (even if there are some awkward Windows adaptions), which would be using \n, not \r\n.

shaqian commented 5 years ago

Hi frjonsen,

Are you suggesting to change .readline() to something like below? I will see if it would work.

      byte[] tmp=new byte[1024];
      while(true){
        while(in.available()>0){
          int i=in.read(tmp, 0, 1024);
          if(i<0)break;
          System.out.print(new String(tmp, 0, i));
        }
        ......
      }

Note "\r\n" is added to the response that is returned to Flutter app frontend. It's not for the server.

Thanks, Qian

frjonsen commented 5 years ago

Yes, the problem in my case was that I was calling ls -1 on the my server, which on any sane Linux server uses just \n. In my app I was then splitting on '\n', because that is what I was expecting. When printing he results, it looked correct, since \r\ is an invisible character. When I tried to cd to any directory returned from the result, my app broke (since I tried to cd to folder\r instead of folder).

The problem is mostly that getline is destructive, since there's no way to tell whether the original linebreak was \n or \n\r.

Thank you for looking into this.