lib/rack-xslview.rb in rack-xslview-0.2.2 vs lib/rack-xslview.rb in rack-xslview-0.2.3

- old
+ new

@@ -19,25 +19,28 @@ end def call(env) # No matter what, @app will be called - status, headers, body = original_response = @app.call(env) - + status, headers, body = @app.call(env) + original_response = Array[status, headers, body] exluded_status = Array[204, 301, 302, 304] - return original_response if exluded_status.include?(status) + return original_response if exluded_status.include?(status) || body.nil? return original_response unless headers["Content-Type"].to_s.match(/(ht|x)ml/) # If setup includes paths to exclude from xslt processing, check them checknoxsl(env) if @options[:noxsl] # Obtain entire request body, ensuring sure it can be processed myxml = getResponse(body) + + # One more check for an empty respone + return original_response if myxml.empty? # Should XSL file be reloaded? - if @options[:reload] == true + unless @options[:reload] == true @xslt = XML::XSLT.new() @xslt.xsl = REXML::Document.new @options[:xslfile] end # Set XML for stylesheet @@ -57,10 +60,16 @@ newbody = Array.[](@xslt.serve) # If we've made it this far, we can alter the headers headers.delete('Content-Length') headers['Content-Length'] = newbody[0].length.to_s + + # Content type override? + unless @options[:content_type].nil? + headers["Content-Type"] = @options[:content_type] + end + [status, headers, newbody] rescue XSLViewError # TODO Log: "Rack XSLView not processed" if env['RACK_ENV'] == 'development' original_response @@ -71,21 +80,23 @@ @options[:noxsl].each { |path| raise XSLViewError if env["PATH_INFO"].index(path) } end def getResponse(body) - raise XSLViewError if body.nil? - newbody = '' + newbody = [] body.each { |part| # Only check the first chunk to ensure 1) its not HTML and 2) its XML checkForXml(part) if newbody.empty? newbody << part.to_s } - return newbody + return newbody.join('') end def checkForXml(x) # Abort processing if content cannot be processed by libxslt + #puts "test for xml" + #puts x[0] raise XSLViewError unless x[0] == '<' + #puts "end xml test" if @options[:excludehtml] == true raise XSLViewError if x.include? '<html' end end