开发者

Copying a IO stream results in corruption

I have a small Mongrel webserver that sends the stdout of a process to a http response:

response.start(200) do |head,out|
  head["Content-Type"] = "text/html"
  status = POpen4::popen4(command) do |stdout, stderr, stdin, pid|
    stdin.close()
    FileUtils.copy_stream(stdout, out)
    FileUtils.copy_stream(stderr, out)
    puts "Sent response."
  end
end

This works well most of the time, but sometimes characters get duplicated. For example this is what I get from the "man ls" command:

LS(1)                            User Commands                           LS(1)



NNAAMMEE
       ls - list directory contents

SSYYNNOOPPSSIISS
       llss [_O_P_T_I_O_N]... [_F_I_L_E]开发者_JS百科...

DDEESSCCRRIIPPTTIIOONN
       List  information  about  the FILEs (the current directory by default).
       Sort entries alphabetically if none of --ccffttuuvvSSUUXX nor ----ssoorrtt.

       Mandatory arguments to long options are  mandatory  for  short  options

For some mysterious reason capital letters get duplicated. Can anyone explain what's going on?


That's the actual output of man. Duplicate characters you see (and some others you don't, such as backspace character) are used for formatting output when displaying in console, such as underlining text etc.

In order to filter out these formatting characters, take a look at How do I get a plain text man page without all that ^H^_ stuff? in Linux Man Pages HOWTO.

BTW, I am not sure that text/html is the right mimetype for this kind of output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜