instiki/vendor/plugins/rack/doc/classes/Rack/Utils/Multipart.html
Jacques Distler 4e14ccc74d Instiki 0.16.3: Rails 2.3.0
Instiki now runs on the Rails 2.3.0 Candidate Release.
Among other improvements, this means that it now 
automagically selects between WEBrick and Mongrel.

Just run

    ./instiki --daemon
2009-02-04 14:26:08 -06:00

246 lines
15 KiB
HTML

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Module: Rack::Utils::Multipart</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
<script type="text/javascript">
// <![CDATA[
function popupCode( url ) {
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
}
function toggleCode( id ) {
if ( document.getElementById )
elem = document.getElementById( id );
else if ( document.all )
elem = eval( "document.all." + id );
else
return false;
elemStyle = elem.style;
if ( elemStyle.display != "block" ) {
elemStyle.display = "block"
} else {
elemStyle.display = "none"
}
return true;
}
// Make codeblocks hidden by default
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
// ]]>
</script>
</head>
<body>
<div id="classHeader">
<table class="header-table">
<tr class="top-aligned-row">
<td><strong>Module</strong></td>
<td class="class-name-in-header">Rack::Utils::Multipart</td>
</tr>
<tr class="top-aligned-row">
<td><strong>In:</strong></td>
<td>
<a href="../../../files/lib/rack/utils_rb.html">
lib/rack/utils.rb
</a>
<br />
</td>
</tr>
</table>
</div>
<!-- banner header -->
<div id="bodyContent">
<div id="contextContent">
<div id="description">
<p>
A multipart form data parser, adapted from IOWA.
</p>
<p>
Usually, <a href="../Request.html#M000177">Rack::Request#POST</a> takes
care of calling this.
</p>
</div>
</div>
<div id="method-list">
<h3 class="section-bar">Methods</h3>
<div class="name-list">
<a href="#M000028">parse_multipart</a>&nbsp;&nbsp;
</div>
</div>
</div>
<!-- if includes -->
<div id="section">
<div id="constants-list">
<h3 class="section-bar">Constants</h3>
<div class="name-list">
<table summary="Constants">
<tr class="top-aligned-row context-row">
<td class="context-item-name">EOL</td>
<td>=</td>
<td class="context-item-value">&quot;\r\n&quot;</td>
</tr>
</table>
</div>
</div>
<!-- if method_list -->
<div id="methods">
<h3 class="section-bar">Public Class methods</h3>
<div id="method-M000028" class="method-detail">
<a name="M000028"></a>
<div class="method-heading">
<a href="#M000028" class="method-signature">
<span class="method-name">parse_multipart</span><span class="method-args">(env)</span>
</a>
</div>
<div class="method-description">
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000028-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000028-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/utils.rb, line 259</span>
259: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">parse_multipart</span>(<span class="ruby-identifier">env</span>)
260: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'CONTENT_TYPE'</span>] <span class="ruby-operator">=~</span>
261: <span class="ruby-regexp re">%r|\Amultipart/form-data.*boundary=\&quot;?([^\&quot;;,]+)\&quot;?|n</span>
262: <span class="ruby-keyword kw">nil</span>
263: <span class="ruby-keyword kw">else</span>
264: <span class="ruby-identifier">boundary</span> = <span class="ruby-node">&quot;--#{$1}&quot;</span>
265:
266: <span class="ruby-identifier">params</span> = {}
267: <span class="ruby-identifier">buf</span> = <span class="ruby-value str">&quot;&quot;</span>
268: <span class="ruby-identifier">content_length</span> = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'CONTENT_LENGTH'</span>].<span class="ruby-identifier">to_i</span>
269: <span class="ruby-identifier">input</span> = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'rack.input'</span>]
270:
271: <span class="ruby-identifier">boundary_size</span> = <span class="ruby-identifier">boundary</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">+</span> <span class="ruby-constant">EOL</span>.<span class="ruby-identifier">size</span>
272: <span class="ruby-identifier">bufsize</span> = <span class="ruby-value">16384</span>
273:
274: <span class="ruby-identifier">content_length</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">boundary_size</span>
275:
276: <span class="ruby-identifier">status</span> = <span class="ruby-identifier">input</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">boundary_size</span>)
277: <span class="ruby-identifier">raise</span> <span class="ruby-constant">EOFError</span>, <span class="ruby-value str">&quot;bad content body&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">status</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">boundary</span> <span class="ruby-operator">+</span> <span class="ruby-constant">EOL</span>
278:
279: <span class="ruby-identifier">rx</span> = <span class="ruby-node">/(?:#{EOL})?#{Regexp.quote boundary}(#{EOL}|--)/</span>
280:
281: <span class="ruby-identifier">loop</span> {
282: <span class="ruby-identifier">head</span> = <span class="ruby-keyword kw">nil</span>
283: <span class="ruby-identifier">body</span> = <span class="ruby-value str">''</span>
284: <span class="ruby-identifier">filename</span> = <span class="ruby-identifier">content_type</span> = <span class="ruby-identifier">name</span> = <span class="ruby-keyword kw">nil</span>
285:
286: <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">head</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">buf</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">rx</span>
287: <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">head</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">i</span> = <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">index</span>(<span class="ruby-value str">&quot;\r\n\r\n&quot;</span>)
288: <span class="ruby-identifier">head</span> = <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">slice!</span>(<span class="ruby-value">0</span>, <span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">2</span>) <span class="ruby-comment cmt"># First \r\n</span>
289: <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">slice!</span>(<span class="ruby-value">0</span>, <span class="ruby-value">2</span>) <span class="ruby-comment cmt"># Second \r\n</span>
290:
291: <span class="ruby-identifier">filename</span> = <span class="ruby-identifier">head</span>[<span class="ruby-regexp re">/Content-Disposition:.* filename=&quot;?([^\&quot;;]*)&quot;?/n</span><span class="ruby-identifier">i</span>, <span class="ruby-value">1</span>]
292: <span class="ruby-identifier">content_type</span> = <span class="ruby-identifier">head</span>[<span class="ruby-regexp re">/Content-Type: (.*)\r\n/n</span><span class="ruby-identifier">i</span>, <span class="ruby-value">1</span>]
293: <span class="ruby-identifier">name</span> = <span class="ruby-identifier">head</span>[<span class="ruby-regexp re">/Content-Disposition:.* name=&quot;?([^\&quot;;]*)&quot;?/n</span><span class="ruby-identifier">i</span>, <span class="ruby-value">1</span>]
294:
295: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">filename</span>
296: <span class="ruby-identifier">body</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">&quot;RackMultipart&quot;</span>)
297: <span class="ruby-identifier">body</span>.<span class="ruby-identifier">binmode</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:binmode</span>)
298: <span class="ruby-keyword kw">end</span>
299:
300: <span class="ruby-keyword kw">next</span>
301: <span class="ruby-keyword kw">end</span>
302:
303: <span class="ruby-comment cmt"># Save the read body part.</span>
304: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">head</span> <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">boundary_size</span><span class="ruby-operator">+</span><span class="ruby-value">4</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">size</span>)
305: <span class="ruby-identifier">body</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">slice!</span>(<span class="ruby-value">0</span>, <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> (<span class="ruby-identifier">boundary_size</span><span class="ruby-operator">+</span><span class="ruby-value">4</span>))
306: <span class="ruby-keyword kw">end</span>
307:
308: <span class="ruby-identifier">c</span> = <span class="ruby-identifier">input</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">bufsize</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">content_length</span> <span class="ruby-value">? </span><span class="ruby-identifier">bufsize</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">content_length</span>)
309: <span class="ruby-identifier">raise</span> <span class="ruby-constant">EOFError</span>, <span class="ruby-value str">&quot;bad content body&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">empty?</span>
310: <span class="ruby-identifier">buf</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span>
311: <span class="ruby-identifier">content_length</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">size</span>
312: <span class="ruby-keyword kw">end</span>
313:
314: <span class="ruby-comment cmt"># Save the rest.</span>
315: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">i</span> = <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">rx</span>)
316: <span class="ruby-identifier">body</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">slice!</span>(<span class="ruby-value">0</span>, <span class="ruby-identifier">i</span>)
317: <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">slice!</span>(<span class="ruby-value">0</span>, <span class="ruby-identifier">boundary_size</span><span class="ruby-operator">+</span><span class="ruby-value">2</span>)
318:
319: <span class="ruby-identifier">content_length</span> = <span class="ruby-value">-1</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">$1</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;--&quot;</span>
320: <span class="ruby-keyword kw">end</span>
321:
322: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">filename</span>
323: <span class="ruby-identifier">body</span>.<span class="ruby-identifier">rewind</span>
324: <span class="ruby-identifier">data</span> = {<span class="ruby-identifier">:filename</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">filename</span>, <span class="ruby-identifier">:type</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">content_type</span>,
325: <span class="ruby-identifier">:name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">name</span>, <span class="ruby-identifier">:tempfile</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">body</span>, <span class="ruby-identifier">:head</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">head</span>}
326: <span class="ruby-keyword kw">else</span>
327: <span class="ruby-identifier">data</span> = <span class="ruby-identifier">body</span>
328: <span class="ruby-keyword kw">end</span>
329:
330: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">name</span>
331: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">name</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/\[\]\z/</span>
332: <span class="ruby-identifier">params</span>[<span class="ruby-identifier">name</span>] <span class="ruby-operator">||=</span> []
333: <span class="ruby-identifier">params</span>[<span class="ruby-identifier">name</span>] <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">data</span>
334: <span class="ruby-keyword kw">else</span>
335: <span class="ruby-identifier">params</span>[<span class="ruby-identifier">name</span>] = <span class="ruby-identifier">data</span>
336: <span class="ruby-keyword kw">end</span>
337: <span class="ruby-keyword kw">end</span>
338:
339: <span class="ruby-keyword kw">break</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">buf</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">content_length</span> <span class="ruby-operator">==</span> <span class="ruby-value">-1</span>
340: }
341:
342: <span class="ruby-identifier">params</span>
343: <span class="ruby-keyword kw">end</span>
344: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="validator-badges">
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>
</body>
</html>