2010-05-06 60 views
0

一個IO流導致我有一個小雜種Web服務器發送一個過程的標準輸出到一個HTTP響應:複製腐敗

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 

這工作的大部分時間很好,但有時字符得到複製。例如,這是我從「man ls」命令中得到的結果:

LS(1)       User Commands       LS(1) 



NNAAMMEE 
     ls - list directory contents 

SSYYNNOOPPSSIISS 
     llss [_O_P_T_I_O_N]... [_F_I_L_E]... 

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 

由於某些神祕原因大寫字母被重複。任何人都可以解釋發生了什麼?

回答

2

這就是man的實際輸出。您看到的重複字符(以及其他一些字符,例如退格字符)用於在控制檯中顯示時進行格式輸出,例如加下劃線等。

爲了過濾出這些格式化字符,請參閱在Linux Man Pages HOWTO中的How do I get a plain text man page without all that ^H^_ stuff?

順便說一句,我不確定text/html是這種輸出的正確的mimetype。