Class: Pindo::Command::Web::Run::BrotliFileHandler

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/pindo/command/web/run.rb

Overview

处理.br扩展名请求的servlet

Instance Method Summary collapse

Constructor Details

#initialize(server, root_dir, debug = false) ⇒ BrotliFileHandler

Returns a new instance of BrotliFileHandler.



67
68
69
70
71
# File 'lib/pindo/command/web/run.rb', line 67

def initialize(server, root_dir, debug=false)
    super(server)
    @root_dir = root_dir
    @debug = debug
end

Instance Method Details

#do_GET(req, res) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pindo/command/web/run.rb', line 73

def do_GET(req, res)
    # 只处理.br结尾的文件
    unless req.path.end_with?('.br')
        res.status = 404
        return
    end
  
    file_path = File.join(@root_dir, req.path[1..-1])
    UI.debug("处理.br文件请求: #{req.path}", @debug)
  
    if File.exist?(file_path)
        # 确定原始文件类型
        base_name = File.basename(req.path, ".br")
        ext = File.extname(base_name)
        
        content_type = case ext
            when ".js" then "application/javascript"
            when ".wasm" then "application/wasm"
            when ".data" then "application/octet-stream"
            when ".json" then "application/json"
            else "application/octet-stream"
        end
        
        # 读取文件
        file_content = File.binread(file_path)
        
        # 设置响应
        res.status = 200
        res.header["Content-Encoding"] = "br"
        res.content_type = content_type
        res.body = file_content
    else
        UI.debug("未找到.br文件: #{file_path}", @debug)
        res.status = 404
    end
end