思路:
使用ruby eventmachine和em-http-server gem,完成一个简单的提供文件下载功能的HttpServer;
使用了EM的FileStreamer来异步发送文件,发送文件时先组装了header,然后调用FileStreamer。
代码:
require 'rubygems' require 'eventmachine' require 'em-http-server' class HTTPHandler < EM::HttpServer::Server attr_accessor :filename, :filesize, :path def process_http_request #send file async if @http_request_method.to_s =~ /GET/ && @http_request_uri.to_s.end_with"HTTP/1.1 200 OK\n" send_data "Server: XiaoMi\n" send_data "Connection: Keep-Alive\n" send_data "Keep-Alive: timeout=15\n" send_data "Content-Type: application/octet-stream\n" send_data "Content-Disposition: filename='#{filename}'\n" send_data "Content-Length: #{filesize}\n" send_data "\n" streamer = EventMachine::FileStreamer.new(self, path) streamer.callback { # file was sent successfully close_connection_after_writing } else response = EM::DelegatedHttpResponse.new(self) response.status = 200 response.content_type 'text/html' response.content = "Package HttpServer<br>usage: wget http://host:port/#{filename}" response.send_response end end end EM::run do path = '/tmp/aaa.tar.gz' EM::start_server("0.0.0.0", 8080, HTTPHandler) do |conn| conn.filename = File.basename(path) conn.filesize = File.size(path) conn.path = path end end
PS:关于eventmachine安装错误的问题
在windows上安装 eventmachine 总是报错:
Building native extensions. This could take a while... ERROR: Error installing eventmachine: ERROR: Failed to build gem native extension.
或者另外一种:
ERROR: Error installing ruby-debug: The 'linecache' native gem requires installed build tools. Please update your PATH to include build tools or download the DevKit from 'http://rubyinstaller.org/downloads' and follow the instructions at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
后来经过了漫长的Google,找到了2个solution:
1.用更低版本的eventmachine
这个提示一直不断,下面还有一大难错误,都是C的编译错误后来网上找了两个方法
(1)
gem install eventmachine-win32
这个貌似安装的是较低版本的
(2)gem install
eventmachine --pre
这个貌似安装的是 beta 1.0.0的。
2.升级devkit
看了一下,上面没有提具体的解决方案,但是给出了问题产生的两个可能原因:
(1)没有C编译环境
(2)路径当中有空格
看看上面的错误日志,发现可能就是编译环境的问题。于是找了一下。
我的ruby是用one-click installer装的,版本是1.8.6-p398。
在rubyinstaller的addon页面,找到了DevKit。
看了一下DevKit的说明:
//Sometimes you just want RubyGems to build that cool native,
//C-based extension without squawking.
//Who's your buddy"htmlcode">
gem install rdiscount –platform=ruby
来测试是否成功。
按照安装步骤,完成了DevKit的安装,非常简单。
然后,再次安装eventmachine:
gem install eventmachine
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
- 《刀郎13CD合集》[WAV+CUE][8.4G]
- 群星.2024-天行健影视剧原声带【企鹅影视】【FLAC分轨】
- 郑源2008-源·情歌[首版][WAV+CUE]
- 郭峰1999-甘心情愿[首版][WAV+CUE]
- 关智斌.2006-寻找美惠【英皇娱乐】【FLAC分轨】
- 谢安琪.2007-THEFIRSTDAY(新歌+重绎)【新艺宝】【WAV+CUE】
- 群星.1992-1994-宝丽金绝佳经典2辑【宝丽金】【WAV+CUE】
- 雷婷-《我的爱回不来(紫银合金AQCD)》[WAV+CUE]
- 群星《妙音20周年辉煌经典纪念版》蓝光BSCD[正版原抓WAV+CUE]
- 【雨果唱片】音乐图书馆系列《绣金匾》1999【WAV整轨+CUE】
- 曜爆甘弦乐团《忆声弦命 滚石40【40首OST 致经典】》[320K/MP3][94.1MB]
- 曜爆甘弦乐团《忆声弦命 滚石40【40首OST 致经典】》[FLAC/分轨][208.1MB]
- 丁当《日与夜,跟自己说晚安》[320K/MP3][98.51MB]
- 何炅.2005-漫游【中唱】【WAV+CUE】
- 何嘉莉.1998-MYDREAMS(EP)【飞图】【WAV+CUE】