Ruby使用eventmachine为HTTP服务器添加文件下载功能

yipeiwu_com5年前服务器

思路:
使用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?(filename)
   send_data "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? DevKit!
看来这就是我需要的。
 
出错的原因是安装eventmachine的时候,需要build tools,但系统中没有。出错信息中同时也给出了解决的法案:
(1) 到 http://rubyinstaller.org/downloads/ 去下载dev kit – DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe
(2)按照 http://github.com/oneclick/rubyinstaller/wiki/Development-Kit/ 安装dev kit
主要安装步骤如下:
如果原来系统中已经安装了旧版的dev kit, 则删除它
下载上面提到的dev kit
解压下载下来的文件到指定的目录,如c:/devkit。(注意:目录不能有空格)
运行ruby dk.rb,然后按照提示分别运行ruby dk.rb init 和 ruby dk.rb install来增强ruby
可以运行

gem install rdiscount –platform=ruby 

来测试是否成功。
按照安装步骤,完成了DevKit的安装,非常简单。

然后,再次安装eventmachine:

gem install eventmachine 

相关文章

Python内置的HTTP协议服务器SimpleHTTPServer使用指南

首先确保装了Python,我装的是2.x版本,对了,我的操作系统是WIN7,其实对于Python来说,什么操作系统并不重要。Python内置了一个简单的HTTP服务器,只需要在命令行下面...

Python面向对象之Web静态服务器

Python面向对象之Web静态服务器

本文实例为大家分享了Python Web静态服务器的具体代码,供大家参考,具体内容如下 功能:用户访问服务器可以返回指定页面 步骤: 1.创建服务器类 2.创建初始化服务器配置...

利用python实现对web服务器的目录探测的方法

利用python实现对web服务器的目录探测的方法

一、python Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。 python 是一门简单易学的语言,并且功能强大也很灵活,在渗透测试中的应用广泛,让我们一起打...

详解配置 Apache 服务器支持 PHP 文件的解析

详解配置 Apache 服务器支持 PHP 文件的解析

详解配置 Apache 服务器支持 PHP 文件的解析 【说明】 1. 本例中 Apache 版本为 httpd-2.4.20-x64-vc14 ,安装路径为 E:\Apache24...

尝试使用Python多线程抓取代理服务器IP地址的示例

这里以抓取 http://www.proxy.com.ru 站点的代理服务器为例,代码如下: #!/usr/bin/env python #coding:utf-8 import u...