/
file_response.rb
80 lines (61 loc) · 1.35 KB
/
file_response.rb
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
##
# Fake response for dealing with file:/// requests
class Mechanize::FileResponse
def initialize(file_path)
@file_path = file_path
@uri = nil
end
def read_body
raise Mechanize::ResponseCodeError.new(self) unless
File.exist? @file_path
if directory?
yield dir_body
else
::File.open(@file_path, 'rb') do |io|
yield io.read
end
end
end
def code
File.exist?(@file_path) ? 200 : 404
end
def content_length
return dir_body.length if directory?
File.exist?(@file_path) ? File.stat(@file_path).size : 0
end
def each_header; end
def [](key)
return nil if key.casecmp('Content-Type') != 0
return 'text/html' if directory?
return 'text/html' if ['.html', '.xhtml'].any? { |extn|
@file_path.end_with?(extn)
}
nil
end
def each
end
def get_fields(key)
[]
end
def http_version
'0'
end
def message
File.exist?(@file_path) ? 'OK' : 'Not Found'
end
def uri
@uri ||= URI "file://#{@file_path}"
end
private
def dir_body
body = %w[<html><body>]
body.concat Dir[File.join(@file_path, '*')].map { |f|
"<a href=\"file://#{f}\">#{File.basename(f)}</a>"
}
body << %w[</body></html>]
body.join("\n").force_encoding(Encoding::BINARY)
end
def directory?
File.directory?(@file_path)
end
end