ruby实现的一个异步文件下载HttpServer实例
网络编程
1.使用ruby eventmachine和em-http-server gem,完成一个简单的提供文件下载功能的HttpServer
2.使用了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 OKn" send_data "Server: XiaoMin" send_data "Connection: Keep-Aliven" send_data "Keep-Alive: timeout=15n" send_data "Content-Type: application/octet-streamn" 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
Windows下Ruby on Rails开发环境安装配置图文教程
本文详细介绍如何在Windows配置RubyonRails开发环境,希望对ROR初学者能有帮助。一、下载并安装RubyWindows下安装Ruby最好选择RubyInstaller(一键安装包)。下
快速正确的安装 Ruby, Rails 运行环境
如何快速正确的安装Ruby,Rails运行环境对于新入门的开发者,如何安装Ruby,RubyGems和Rails的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子
在 Ubuntu 12.04 Server 上安装部署 Ruby on Rails 应用
本教程只适合UbuntuServer用于部署项目到线上,建议使用同样的Ubuntu版本,以免遇到一些版本不同带来的问题。本教程适合新手初次部署Rails应用;本文测
编辑:一起学习网
标签:运行环境,文件,教程,本文,正确