mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Add gzip helper to enable/disable HTTP::DeflateHandler
This commit is contained in:
		
							parent
							
								
									7c8915bfa4
								
							
						
					
					
						commit
						4cd30839a5
					
				
					 3 changed files with 21 additions and 4 deletions
				
			
		|  | @ -122,4 +122,15 @@ describe "Macros" do | ||||||
|       response.headers["Content-Length"].should eq("6") |       response.headers["Content-Length"].should eq("6") | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  | 
 | ||||||
|  |   describe "#gzip" do | ||||||
|  |     it "adds HTTP::DeflateHandler to handlers" do | ||||||
|  |       gzip true | ||||||
|  |       Kemal.config.handlers.last.is_a?(HTTP::DeflateHandler).should eq true | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     it "doesn't add HTTP::DeflateHandler to handlers by default" do | ||||||
|  |       Kemal.config.handlers.last.is_a?(HTTP::DeflateHandler).should eq false | ||||||
|  |     end | ||||||
|  |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -71,3 +71,10 @@ def send_file(env, data : Slice(UInt8), mime_type : String? = nil) | ||||||
|   env.response.content_length = data.bytesize |   env.response.content_length = data.bytesize | ||||||
|   env.response.write data |   env.response.write data | ||||||
| end | end | ||||||
|  | 
 | ||||||
|  | # Configures an `HTTP::Server::Response` to compress the response | ||||||
|  | # output, either using gzip or deflate, depending on the `Accept-Encoding` request header. | ||||||
|  | # It's disabled by default. | ||||||
|  | def gzip(status : Bool = false) | ||||||
|  |   add_handler HTTP::DeflateHandler.new if status | ||||||
|  | end | ||||||
|  |  | ||||||
|  | @ -23,6 +23,5 @@ module Kemal | ||||||
|       else                      "application/octet-stream" |       else                      "application/octet-stream" | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|      |  | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue