sogrbilja commented on issue #9570:
URL: https://github.com/apache/netbeans/issues/9570#issuecomment-5396308911
I used Claude Opus 5 with access to the repo and it found the solution by
doing this:
## Root cause
`FtpClient` never sets a control encoding, so commons-net keeps its default
of
`ISO-8859-1`. The control channel writer is an `OutputStreamWriter` over that
charset, and Java's encoder is configured with `CodingErrorAction.REPLACE`,
so
every character outside Latin-1 is replaced with a single `0x3F` — `?` —
before
anything reaches the socket.
That matches the log exactly: `производи` is 9 code points and the server
receives 9 question marks. It's a one-to-one character substitution, i.e.
charset replacement, not truncation.
Two details that make this confusing to diagnose:
- **The name looks correct in the IDE's FTP log.** `PrintCommandListener` is
fed
`ProtocolCommandEvent.getMessage()`, which is the Java `String` *before*
the
writer encodes it. The log is truthful, it's just observing the wrong side
of
the encoder.
- **No `FEAT` appears in the session.** commons-net only issues `FEAT` when
UTF-8
autodetection is enabled, and it isn't. (It never sends `OPTS UTF8 ON` in
any
configuration — there is no API for it.)
A tree-wide search for `setControlEncoding`, `getControlEncoding` and
`setAutodetectUTF8` returns zero hits anywhere in the repository. The FTP
connection model has no encoding field either: the 16 keys in
`FtpConnectionProvider` (lines 40–55) contain nothing charset-related, and
`FtpConfigurationPanel` exposes no such control — so there is no user-side
workaround. (`ENCRYPTION` on line 43 is TLS/SSL selection, a false friend.)
## Fix
One call in `FtpClient.connect()`, immediately before `connect()`:
```diff
---
a/php/php.project/src/org/netbeans/modules/php/project/connections/ftp/FtpClient.java
+++
b/php/php.project/src/org/netbeans/modules/php/project/connections/ftp/FtpClient.java
@@ -177,6 +177,12 @@
ftpClient.setActivePortRange(activePortMin,
activePortMax);
}
}
+ // Negotiate UTF-8 on the control channel (RFC 2640) when the
server
+ // advertises it in FEAT; otherwise the commons-net default of
+ // ISO-8859-1 turns every non-ASCII character into "?".
+ // Must be set before connect(): the FEAT probe and the control
+ // stream re-creation both happen inside _connectAction_().
+ ftpClient.setAutodetectUTF8(true);
ftpClient.connect(configuration.getHost(),
configuration.getPort());
```
**Placement is not incidental.** The autodetection runs inside
`_connectAction_()`, which the superclass invokes as the last step of
`connect()`. Anything below that line is too late. Worth noting for anyone
attempting a variant of this fix: calling `setControlEncoding("UTF-8")`
*after*
`connect()` compiles, runs, changes what `getControlEncoding()` returns, and
has
no effect on the wire — the reader and writer were already constructed with
the
old charset. Autodetection is the only path in the library that rebuilds
them.
**One call covers every operation.** `FtpClient` holds a single `final
FTPClient`
field created once at line 93; store, delete, rename, `listFiles`,
`changeWorkingDirectory`, `makeDirectory` and `SITE chmod` all delegate to
it.
No per-call-site changes are needed.
## Why autodetect rather than forcing UTF-8
Hard-coding `setControlEncoding("UTF-8")` fixes this case and introduces a
worse
one. ISO-8859-1 maps bytes `0x00`–`0xFF` to `U+0000`–`U+00FF` one-to-one and
never fails in either direction, so today's client is accidentally a
transparent
byte pipe: a name read from a listing on a CP1251/KOI8-R/Shift_JIS server
renders
as mojibake but re-encodes to the original bytes, so `CWD`/`DELE`/`RNFR`
against
it still succeed.
UTF-8 is not byte-transparent. Invalid sequences decode to `U+FFFD`, which
re-encodes to `EF BF BD` — different bytes — so files that are reachable
today
become unreachable on those servers, and a sync could mis-target them. That's
why the bug only bites on names the IDE *originates* (a local file being
uploaded), and why the conditional form is the right fix.
## Verification
Tested against Apache NetBeans 31, which bundles the same
`commons-net-3.11.1`
the build declares.
Recompiling the **unmodified** source of `FtpClient.java` with JDK 21
reproduced
the shipped bytecode byte-for-byte, confirming a clean baseline. After the
edit,
the only semantic delta in the class is four instructions
(`aload_0 / getfield ftpClient / iconst_1 / invokevirtual
setAutodetectUTF8`);
the three inner classes differ solely in `LineNumberTable`, shifted by the
six
added lines.
Driven through the same jar the IDE loads, against a mock server that
advertises
`UTF8` in `FEAT`, capturing the raw bytes of the `MKD` argument:
| | control encoding | `FEAT` | bytes on the wire |
|---|---|---|---|
| before | ISO-8859-1 | not sent | `3F 3F 3F 3F 3F 3F 3F 3F 3F` (9) |
| after | UTF-8 | sent | `D0 BF D1 80 D0 BE D0 B8 D0 B7 D0 B2 D0 BE D0 B4 D0
B8` (18) |
The second row is exactly the correct UTF-8 encoding of `производи`; the
first
reproduces the reported log. Confirmed working against the real Pure-FTPd
server
from the original report.
## Expected side effect
Every connection now performs a `FEAT` exchange, which is visible in the
IDE's
FTP output window. A server that rejects `FEAT` is handled correctly
(`hasFeature` returns false, client stays on ISO-8859-1), but the negative
reply
will be printed as an error line, since `PrintCommandListener` routes
negative
replies to `io.getErr()`. Suppressing it isn't straightforward — the probe
happens inside `connect()`, so the remove/re-add listener trick used by
`preventNoOperationTimeout()` can't wrap it without also hiding the greeting
and
login.
Not addressed here: servers that advertise `UTF8` but require an explicit
`OPTS UTF8 ON`. commons-net has no API for it; it would need a manual
`sendCommand("OPTS", "UTF8 ON")` after login, and shouldn't be added
speculatively since servers that don't know the command will log a 500/502.
Two notes before you post:
- I'd trim the "Verification" section if the maintainers prefer terse issues
— the bytecode-diff detail matters most in a PR, less in an issue thread.
- The diff applies to master as of this checkout. If you want, I can turn
this into an actual PR branch — though this checkout isn't a git repo, so
that'd mean cloning fresh first.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists