Based on your feedback, and some help from #emacs, I made a patch
that is very minimal to the existing code, with better commit
message, and attached it here.
The patch removes the =(unless (bolp) ...)= guarding inserting CRLF.
The RFC says the "boundary delimiter MUST occur at the beginning of a
line, i.e., following a CRLF". =(bolp)= is not enough to guarantee
the boundary is preceded by CRLF. It can be true when the point
is after just "\n".
Because CRLF is inserted unconditionally after the =cond=, the code
does not include the boundary's CRLF in each branch of the =cond=.
> when `filedata` is an empty string, this add an additional \r\n
> compared to the current code. This seems right to me
Me too, and all the other clients I tested.
> I expect the decoding software will skip the \r\n at the of the
> header and then look for \r\n<BOUNDARY>, so it's important to have
> two \r\n
What you said is true. In addition, they also accept
"HEADER\r\nfile content\n--BOUNDARY"
as the content "file content", and consider the last "\n" as attached
to the boundary. That's where the file's final "\n" gets lost if the
file's content was initially "file content\n".
> There remain some questions on this patch:
While fixing this bug, I found a few more problems in addition to the
two that you mention here. I was not addressing them yet, since I
thought I should fix one bug per patch.
> I suspect we can also simply this code by moving the first (insert
> "--" boundary "\r\n") before the loop, and the second into the loop
> so we can make it insert "\r\n--" boundary "\r\n" (and thus remove
> \r\n from the end of each of the preceding branches).
Almost, but not quite, or at least not without some awkward (to my
eye) repositioning of inserting boundaries, "--", and "\r\n". The
final boundary complicates things. It is different from all the
others, it is "--BOUNDARY--" instead of "--BOUNDARY"
Here's what I ended up with when I tried that,
https://git.sr.ht/~ozzloy/emacs-bug-63941/tree/simplify-insert-boundaries-and---/item/mm-url.el#L397This passes the tests in =emacs/tests/lisp/gnus/mm-url-tests.el=.