rack-archive-zip-extract 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07bea84e8dd599718d60edf93979698acfa2ca84
4
- data.tar.gz: 755f333cee8ead09814ef8d2253016ad5b9a4d0e
3
+ metadata.gz: 9a22645366df92782010048441902b26effc0112
4
+ data.tar.gz: 60eb6ea71ff64cc4a268f7e96e05c4b7c89e86b4
5
5
  SHA512:
6
- metadata.gz: 7d5e00114d0e8638ae5c7894ba507e5600c500d25be33f3f9528b5721996d7559501c7cca4983e1c0bbaed8e98e71150ba49a8104a7620c22fa80bbf66b85235
7
- data.tar.gz: 661f0577924ef2c25f5199cfab991f4705f76b5d83dba65a0b243b02323ef762720a8c043571fdc9f2853521e7f655fbc8f056b4cb050aa81e6c40e3cca6f236
6
+ metadata.gz: f5fbc79aa06160b54f2007bda4b6ffa4ab6b3a5176cd438e1570cd83a6987e80452e04a36eb89f32207c592faef44756fce6c5a12bbc52a9f3f3d6f8781fa814
7
+ data.tar.gz: aaa2f76f60a407db7d67d0d996ef466edb5e4dc6d26b9ce973cf35cb3828b15d09560e8744c14d896e2e958ae3a83f6dd9c21347e2a35716eb07325589218d88
data/CHANGELOG.markdown CHANGED
@@ -1,6 +1,11 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ 0.0.6
5
+ -----
6
+
7
+ * Make Content-Type header customizable
8
+
4
9
  0.0.5
5
10
  -----
6
11
 
data/README.markdown CHANGED
@@ -47,6 +47,16 @@ You can tell the app extensions by option argument:
47
47
 
48
48
  run Rack::Archive::Zip::Extract.new('path/to/docroot', extensions: ['.epub', '.zip'])
49
49
 
50
+ ### Mime types
51
+
52
+ By default, Rack::Archive::Zip::Extract uses [`Rack::Mime::MIME_TYPES`][mime_types] as a mime type table. You can add and/or update mime type settings by passing hash table to initializer:
53
+
54
+ run Rack::Archive::Zip::Extract.new('path/to/docroot', mime_types: {'.html' => 'application/xhtml+xml', '.apk => 'application/vnd.android.package-archive'})
55
+
56
+ In example above, Rack::Archive::Zip::Example sends "`Content-Type: application/xhtml+xml`" header for access to path with extension "`.html`" instead of "`text/html`" and "`application/vnd.android.package-archive`" for "`*.apk`" instead of default "`application/octet-stream`".
57
+
58
+ [mime_types]: http://rack.rubyforge.org/doc/Rack/Mime.html#MIME_TYPES
59
+
50
60
  ### Buffer size
51
61
 
52
62
  Buffer size for reading file in zip archive is set to {Rack::Archive::Zip::Extract::ExtractedFile::BUFFER_SIZE 8192} bytes by default.
@@ -31,15 +31,18 @@ module Rack::Archive
31
31
  METHOD_NOT_ALLOWED = [status_code(:method_not_allowd), {'Allow'.freeze => Rack::File::ALLOWED_VERBS.join(', ').freeze}, []]
32
32
  NOT_FOUND = [status_code(:not_found), {}, []]
33
33
  NOT_MODIFIED = [status_code(:not_modified), {}, []]
34
+ DEFAULT_CONTENT_TYPE = 'application/octet-stream'.freeze
34
35
 
35
36
  # @param root [Pathname, #to_path, String] path to document root
36
37
  # @param extensions [Array<String>] extensions which is recognized as a zip file
38
+ # @param mime_types [Hash{String => String}] pairs of extesion and content type
37
39
  # @param buffer_size [Integer] buffer size to read content, in bytes
38
40
  # @raise [ArgumentError] if +root+ is not a directory
39
- def initialize(root, extensions: %w[.zip], buffer_size: ExtractedFile::BUFFER_SIZE)
41
+ def initialize(root, extensions: %w[.zip], mime_types: {}, buffer_size: ExtractedFile::BUFFER_SIZE)
40
42
  @root = root.kind_of?(Pathname) ? root : Pathname(root)
41
43
  @root = @root.expand_path
42
44
  @extensions = extensions.map {|extention| extention.dup.freeze}.lazy
45
+ @mime_types = Rack::Mime::MIME_TYPES.merge(mime_types)
43
46
  @buffer_size = buffer_size
44
47
  raise ArgumentError, "Not a directory: #{@root}" unless @root.directory?
45
48
  end
@@ -54,24 +57,20 @@ module Rack::Archive
54
57
  }.select {|file| file}.first
55
58
  return NOT_FOUND if file.nil?
56
59
 
57
- if_modified_since = env[IF_MODIFIED_SINCE]
58
- if_modified_since = Time.parse(if_modified_since) if if_modified_since
59
-
60
+ if_modified_since = Time.parse(env[IF_MODIFIED_SINCE]) rescue Time.new(0)
60
61
  if_none_match = env[IF_NONE_MATCH]
61
- etag = file.name.hash.to_s(16) + file.mtime.hash.to_s(16)
62
62
 
63
- if if_modified_since && file.mtime <= if_modified_since or
64
- if_none_match && if_none_match == etag
63
+ if file.mtime <= if_modified_since or env[IF_NONE_MATCH] == file.etag
65
64
  file.close
66
65
  NOT_MODIFIED
67
66
  else
68
67
  [
69
68
  status_code(:ok),
70
69
  {
71
- CONTENT_TYPE => Rack::Mime.mime_type(::File.extname(path_info)),
70
+ CONTENT_TYPE => @mime_types.fetch(::File.extname(path_info), DEFAULT_CONTENT_TYPE),
72
71
  CONTENT_LENGTH => file.size.to_s,
73
72
  LAST_MODIFIED => file.mtime.httpdate,
74
- ETAG => etag
73
+ ETAG => file.etag
75
74
  },
76
75
  file
77
76
  ]
@@ -124,6 +123,8 @@ module Rack::Archive
124
123
  class ExtractedFile
125
124
  BUFFER_SIZE = 8192
126
125
 
126
+ attr_reader :etag, :mtime, :size
127
+
127
128
  # @param archive [Zip::Archive]
128
129
  # @param path [String]
129
130
  # @param buffer_size [Integer]
@@ -132,6 +133,9 @@ module Rack::Archive
132
133
  raise ArgumentError, 'archive already closed' unless archive.open?
133
134
  @archive = archive
134
135
  @file = @archive.fopen(path)
136
+ @mtime = @file.mtime
137
+ @size = @file.size
138
+ @etag = @file.name.hash.to_s(16) + @mtime.hash.to_s(16) + @size.hash.to_s(16)
135
139
  @buffer_size = buffer_size
136
140
  end
137
141
 
@@ -141,18 +145,6 @@ module Rack::Archive
141
145
  end
142
146
  end
143
147
 
144
- def name
145
- @file.name
146
- end
147
-
148
- def mtime
149
- @file.mtime
150
- end
151
-
152
- def size
153
- @file.size
154
- end
155
-
156
148
  def close
157
149
  @file.close
158
150
  @archive.close
@@ -95,6 +95,13 @@ class TestRackArchiveZipExtract < Test::Unit::TestCase
95
95
 
96
96
  assert_equal content_type, response['Content-Type']
97
97
  end
98
+
99
+ def test_mime_types_can_be_added
100
+ mime_types = Rack::Archive::Zip::Extract.new(__dir__, mime_types: {'.html' => 'application/xhtml+xml'})
101
+ response = request('/sample/sample.html', mime_types)
102
+
103
+ assert_equal 'application/xhtml+xml', response['Content-Type']
104
+ end
98
105
  end
99
106
 
100
107
  def test_extension_of_file_can_be_changed
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-archive-zip-extract
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - KITAITI Makoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-28 00:00:00.000000000 Z
11
+ date: 2014-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack