Skip to content

Instantly share code, notes, and snippets.

@dubiouscript
Forked from jimfoltz/tw5-server.rb
Created May 26, 2019 15:12
Show Gist options
  • Save dubiouscript/19192c2f86dc2404bc92656dc449956c to your computer and use it in GitHub Desktop.
Save dubiouscript/19192c2f86dc2404bc92656dc449956c to your computer and use it in GitHub Desktop.

Revisions

  1. @jimfoltz jimfoltz revised this gist May 13, 2018. 1 changed file with 17 additions and 7 deletions.
    24 changes: 17 additions & 7 deletions tw5-server.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    require 'webrick'
    require 'fileutils'

    if ARGV.length != 0
    root = ARGV.first.gsub('\\', '/')
    else
    root = '.'
    end
    BACKUP_DIR = 'bak'

    module WEBrick
    module HTTPServlet
    @@ -10,9 +18,11 @@ class FileHandler
    class DefaultFileHandler
    def do_PUT(req, res)
    file = "#{@config[:DocumentRoot]}#{req.path}"
    #req.body {|buf| c << buf}
    res.body = ''
    #File.open(file, "w+") {|f| f.puts(c)}
    unless Dir.exists? BACKUP_DIR
    Dir.mkdir BACKUP_DIR
    end
    FileUtils.cp(file, "#{BACKUP_DIR}/#{File.basename(file, '.html')}.#{Time.now.to_i.to_s}.html")
    File.open(file, "w+") {|f| f.puts(req.body)}
    end

    @@ -26,11 +36,11 @@ def do_OPTIONS(req, res)
    end
    end

    #sRoot = "#{Dir.pwd}/html"
    sRoot = "#{Dir.pwd}"
    server = WEBrick::HTTPServer.new({:Port => 8000, :DocumentRoot => root})

    server = WEBrick::HTTPServer.new({:Port => 8000, :DocumentRoot => sRoot})

    trap "INT" do server.shutdown end
    trap "INT" do
    puts "Shutting down..."
    server.shutdown
    end

    server.start
  2. @jimfoltz jimfoltz created this gist May 12, 2018.
    36 changes: 36 additions & 0 deletions tw5-server.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    require 'webrick'

    module WEBrick
    module HTTPServlet

    class FileHandler
    alias do_PUT do_GET
    end

    class DefaultFileHandler
    def do_PUT(req, res)
    file = "#{@config[:DocumentRoot]}#{req.path}"
    #req.body {|buf| c << buf}
    res.body = ''
    #File.open(file, "w+") {|f| f.puts(c)}
    File.open(file, "w+") {|f| f.puts(req.body)}
    end

    def do_OPTIONS(req, res)
    res['allow'] = "GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav"
    res['x-api-access-type'] = 'file'
    res['dav'] = 'tw5/put'
    end

    end
    end
    end

    #sRoot = "#{Dir.pwd}/html"
    sRoot = "#{Dir.pwd}"

    server = WEBrick::HTTPServer.new({:Port => 8000, :DocumentRoot => sRoot})

    trap "INT" do server.shutdown end

    server.start