LoginSignup
3
5

More than 5 years have passed since last update.

ローカルファイルをサクッとHTTP配信する

Posted at

Ruby ビルトインの WEBrick を使う。

カレントディレクトリをドキュメントルートとする

ワンライナーで。

$ ruby -rwebrick -e 'WEBrick::HTTPServer.new(:DocumentRoot => "./", :Port => 3333).start'

任意の処理を差し込む

例えば「10秒スリープ」とか差し込んで遅いレスポンスをシミュレーションする。

server.rb
require 'webrick'

WEBrick::HTTPServer.new(
  DocumentRoot: "./",
  Port: 3333,
  RequestCallback: ->(req, res) {
    sleep 10
    res.body = File.open(".#{req.path}").read
  }
).start
$ ruby server.rb

無理やりワンライナーで書けば以下。

$ ruby -rwebrick -e 'WEBrick::HTTPServer.new(DocumentRoot: "./", Port: 3333, RequestCallback: ->(req, res){sleep 10; res.body = File.open(".#{req.path}").read}).start'

注意点

上記実装の場合、ディレクトリトラバーサルとか怪しげなことが出来そうな気がしなくもない(未確認)。「個人でのちょっとしたテスト用」ぐらいに思ってください。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5