diff --git a/file/FileManager.java b/file/FileManager.java index 091a01b99..c0fb5a57e 100644 --- a/file/FileManager.java +++ b/file/FileManager.java @@ -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()]; - s.getBytes(0, buf.length, buf, 0); + 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 = diff --git a/file/file-lib.pl b/file/file-lib.pl index 7902f0fbf..bf6749645 100755 --- a/file/file-lib.pl +++ b/file/file-lib.pl @@ -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"; } } diff --git a/file/lang.cgi b/file/lang.cgi index 132777488..23ad5e934 100755 --- a/file/lang.cgi +++ b/file/lang.cgi @@ -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, diff --git a/file/show.cgi b/file/show.cgi index e011f83f1..19251ee25 100755 --- a/file/show.cgi +++ b/file/show.cgi @@ -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;