-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.rb
More file actions
55 lines (42 loc) · 1.28 KB
/
Server.rb
File metadata and controls
55 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'webrick'
include WEBrick
class SourceCodeServlet < WEBrick::HTTPServlet::AbstractServlet
@@path_prefix = "./temp"
def do_GET(request, response)
response.status = 200
response['Content-Type'] = "text/plain"
response.body = "WeeebRick"
end
def parse_query(content_type, body)
boundary = content_type.match(/^multipart\/form-data; boundary=(.+)/)[1]
boundary = HTTPUtils::dequote(boundary)
return HTTPUtils::parse_form_data(body, boundary)
end
def do_POST(request, response)
body = String.new
request.body do |chunk|
body << chunk
end
filedata = parse_query(request['content-type'], body)
path = filedata['path']
if(not @@path_prefix.nil? and not @@path_prefix.empty?)
path = @@path_prefix + path[1, path.size-1]
end
content_file = filedata['upload']
FileUtils.mkdir_p(File.dirname(path))
file = File.new(path, "w")
file.write(content_file)
file.close()
response.status = 200
response['Content-Type'] = "text/plain"
response.body = "OK"
end
end
server = HTTPServer.new(
:Port => 8000,
:DocumentRoot => './'
)
server.mount "/", SourceCodeServlet
trap("INT"){ server.shutdown }
WEBrick::Daemon.start
server.start