Use correct character set for translations and when editing files https://virtualmin.com/node/22973

This commit is contained in:
Jamie Cameron
2012-08-10 23:23:17 -07:00
parent c8a5c76a8c
commit 708225ac19
4 changed files with 20 additions and 8 deletions

View File

@ -1470,6 +1470,7 @@ class EditorWindow extends FixedFrame implements CbButtonCallback
FileManager filemgr;
GotoWindow goto_window;
FindReplaceWindow find_window;
String charset;
// Editing an existing file
EditorWindow(RemoteFile f, FileManager p)
@ -1490,6 +1491,7 @@ class EditorWindow extends FixedFrame implements CbButtonCallback
filemgr.set_cookie(uc);
int len = uc.getContentLength();
InputStream is = uc.getInputStream();
charset = filemgr.get_charset(uc.getContentType());
byte buf[];
if (len >= 0) {
// Length is known
@ -1513,7 +1515,8 @@ class EditorWindow extends FixedFrame implements CbButtonCallback
buf = nbuf;
}
}
String s = new String(buf, 0);
String s = charset == null ? new String(buf, 0)
: new String(buf, charset);
if (s.indexOf("\r\n") != -1) {
dosmode.setState(true);
s = FileManager.replace_str(s, "\r\n", "\n");
@ -1619,8 +1622,16 @@ class EditorWindow extends FixedFrame implements CbButtonCallback
filemgr.set_cookie(uc);
uc.setDoOutput(true);
OutputStream os = uc.getOutputStream();
byte buf[] = new byte[s.length()];
byte buf[];
if (charset == null) {
// Assume ascii
buf = new byte[s.length()];
s.getBytes(0, buf.length, buf, 0);
}
else {
// Convert back to original charset
buf = s.getBytes(charset);
}
os.write(buf);
os.close();
BufferedReader is =

View File

@ -467,17 +467,18 @@ else {
}
}
# print_content_type()
# print_content_type([type])
# Prints the content-type header, with a charset
sub print_content_type
{
local $type = $_[0] || "text/plain";
if ($userconfig{'nocharset'} || $config{'nocharset'}) {
# Never try to use charset
print "Content-type: text/plain\n\n";
print "Content-type: $type\n\n";
}
else {
$charset = &get_charset();
print "Content-type: text/plain; charset=$charset\n\n";
print "Content-type: $type; charset=$charset\n\n";
}
}

View File

@ -4,7 +4,7 @@
require './file-lib.pl';
print "Content-type: text/plain\n\n";
&print_content_type();
if (&get_charset() eq $default_charset) {
# Convert any HTML entities to their 'real' single-byte forms,

View File

@ -133,7 +133,7 @@ else {
print "X-no-links: 1\n";
print "Content-length: $st[7]\n";
print "Content-Disposition: Attachment\n" if ($download);
print "Content-type: $type\n\n";
&print_content_type($type);
if ($type =~ /^text\/html/i && !$in{'edit'}) {
while(read(FILE, $buf, 1024)) {
$data .= $buf;