How to use eventmachine to add a file download function for an HTTP server in Ruby

Source: Internet
Author: User
This article mainly introduces the example of Ruby using eventmachine to add the file download function to the HTTP server. The author also shares the solution to the problem of installing eventmachine on Windows, for more information, see:

Use ruby eventmachine and em-http-server gem to complete a simple HttpServer that provides the file download function;
The FileStreamer of EM is used to send files asynchronously. when a file is sent, the header is assembled and FileStreamer is called.

Code:

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
usage: wget #: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 endend

PS: eventmachine installation error
When eventmachine is installed on windows, an error is always reported:

Building native extensions. This could take a while...ERROR: Error installing eventmachine:    ERROR: Failed to build gem native extension.

Or another one:

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'

After a long journey to Google, I found two solution:

1. use eventmachine of a lower version

This prompt keeps going, and there is still one more difficult error, which is the C compilation error. later, I found two methods on the Internet.
(1)

gem install eventmachine-win32

This seems to have been installed in a lower version.
(2) gem install

 eventmachine --pre

This seems to have been installed with beta 1.0.0.


2. upgrade devkit

After reading this, I did not provide a specific solution, but gave two possible causes:
(1) No C compiling environment
(2) there are spaces in the path
Look at the error log above and find that it may be a problem with the compiling environment. So I found it.
My ruby is installed with one-click installer. the version is 1.8.6-p398.
On the addon page of rubyinstaller, find DevKit.
I have read about DevKit:

// Sometimes you just want RubyGems to build that cool native,
// C-based extension without squawking.
// Who's your buddy? DevKit!
It seems that this is what I need.

The error occurs because you need to build tools when installing eventmachine, but not in the system. The error message also provides a solution:
(1) go to the http://rubyinstaller.org/downloads/ to download dev kit-DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe
(2) install dev kit as per http://github.com/oneclick/rubyinstaller/wiki/Development-Kit/

The installation steps are as follows:
If the old version of dev kit has been installed in the system, delete it.
Download the dev kit mentioned above
Decompress the downloaded file to a specified directory, such as c:/devkit. (Note: The Directory cannot contain spaces)
Run ruby dk. rb and run ruby dk. rb init and ruby dk. rb install as prompted to enhance ruby.
Can run

gem install rdiscount –platform=ruby

To test whether it is successful.
Follow the installation steps to complete DevKit installation, which is very simple.

Then, install eventmachine again:

gem install eventmachine

For more information about how to use eventmachine to add the file download function to the HTTP server in Ruby, refer to PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.