관리-도구
편집 파일: M000263.html
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>serving (Rack::File)</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> </head> <body class="standalone-code"> <pre><span class="ruby-comment cmt"># File lib/rack/file.rb, line 59</span> <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">serving</span>(<span class="ruby-identifier">env</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">"REQUEST_METHOD"</span>] <span class="ruby-operator">==</span> <span class="ruby-value str">"OPTIONS"</span> <span class="ruby-keyword kw">return</span> [<span class="ruby-value">200</span>, {<span class="ruby-value str">'Allow'</span> =<span class="ruby-operator">></span> <span class="ruby-constant">ALLOW_HEADER</span>, <span class="ruby-constant">CONTENT_LENGTH</span> =<span class="ruby-operator">></span> <span class="ruby-value str">'0'</span>}, []] <span class="ruby-keyword kw">end</span> <span class="ruby-identifier">last_modified</span> = <span class="ruby-constant">F</span>.<span class="ruby-identifier">mtime</span>(<span class="ruby-ivar">@path</span>).<span class="ruby-identifier">httpdate</span> <span class="ruby-keyword kw">return</span> [<span class="ruby-value">304</span>, {}, []] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'HTTP_IF_MODIFIED_SINCE'</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">last_modified</span> <span class="ruby-identifier">headers</span> = { <span class="ruby-value str">"Last-Modified"</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">last_modified</span> } <span class="ruby-identifier">headers</span>[<span class="ruby-constant">CONTENT_TYPE</span>] = <span class="ruby-identifier">mime_type</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">mime_type</span> <span class="ruby-comment cmt"># Set custom headers</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">field</span>, <span class="ruby-identifier">content</span><span class="ruby-operator">|</span> <span class="ruby-identifier">headers</span>[<span class="ruby-identifier">field</span>] = <span class="ruby-identifier">content</span> } <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@headers</span> <span class="ruby-identifier">response</span> = [ <span class="ruby-value">200</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">env</span>[<span class="ruby-constant">REQUEST_METHOD</span>] <span class="ruby-operator">==</span> <span class="ruby-value str">"HEAD"</span> <span class="ruby-operator">?</span> [] <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span> ] <span class="ruby-identifier">size</span> = <span class="ruby-identifier">filesize</span> <span class="ruby-identifier">ranges</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">byte_ranges</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">size</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">ranges</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">ranges</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">></span> <span class="ruby-value">1</span> <span class="ruby-comment cmt"># No ranges, or multiple ranges (which we don't support):</span> <span class="ruby-comment cmt"># TODO: Support multiple byte-ranges</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">0</span>] = <span class="ruby-value">200</span> <span class="ruby-ivar">@range</span> = <span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">size</span><span class="ruby-operator">-</span><span class="ruby-value">1</span> <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">ranges</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-comment cmt"># Unsatisfiable. Return error, and file size:</span> <span class="ruby-identifier">response</span> = <span class="ruby-identifier">fail</span>(<span class="ruby-value">416</span>, <span class="ruby-value str">"Byte range unsatisfiable"</span>) <span class="ruby-identifier">response</span>[<span class="ruby-value">1</span>][<span class="ruby-value str">"Content-Range"</span>] = <span class="ruby-node">"bytes */#{size}"</span> <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">response</span> <span class="ruby-keyword kw">else</span> <span class="ruby-comment cmt"># Partial content:</span> <span class="ruby-ivar">@range</span> = <span class="ruby-identifier">ranges</span>[<span class="ruby-value">0</span>] <span class="ruby-identifier">response</span>[<span class="ruby-value">0</span>] = <span class="ruby-value">206</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">1</span>][<span class="ruby-value str">"Content-Range"</span>] = <span class="ruby-node">"bytes #{@range.begin}-#{@range.end}/#{size}"</span> <span class="ruby-identifier">size</span> = <span class="ruby-ivar">@range</span>.<span class="ruby-identifier">end</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@range</span>.<span class="ruby-identifier">begin</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">end</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">2</span>] = [<span class="ruby-identifier">response_body</span>] <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">response_body</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">1</span>][<span class="ruby-constant">CONTENT_LENGTH</span>] = <span class="ruby-identifier">size</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-identifier">response</span> <span class="ruby-keyword kw">end</span></pre> </body> </html>