serverside 0.2.0 → 0.2.5
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.
- data/CHANGELOG +26 -0
- data/README +39 -22
- data/Rakefile +18 -2
- data/bin/serverside +21 -5
- data/doc/rdoc/classes/ServerSide/Application.html +12 -19
- data/doc/rdoc/classes/ServerSide/Connection/Base.html +203 -116
- data/doc/rdoc/classes/ServerSide/Connection/Const.html +35 -5
- data/doc/rdoc/classes/ServerSide/Connection/Router.html +107 -78
- data/doc/rdoc/classes/ServerSide/Server.html +1 -1
- data/doc/rdoc/classes/ServerSide/StaticFiles/Const.html +10 -0
- data/doc/rdoc/classes/ServerSide/StaticFiles.html +73 -43
- data/doc/rdoc/classes/ServerSide/Template.html +200 -0
- data/doc/rdoc/classes/ServerSide.html +13 -8
- data/doc/rdoc/created.rid +1 -1
- data/doc/rdoc/files/CHANGELOG.html +44 -1
- data/doc/rdoc/files/README.html +63 -35
- data/doc/rdoc/files/lib/serverside/application_rb.html +1 -1
- data/doc/rdoc/files/lib/serverside/connection_rb.html +1 -1
- data/doc/rdoc/files/lib/serverside/routing_rb.html +1 -1
- data/doc/rdoc/files/lib/serverside/server_rb.html +1 -1
- data/doc/rdoc/files/lib/serverside/static_rb.html +1 -1
- data/doc/rdoc/files/lib/serverside/template_rb.html +108 -0
- data/doc/rdoc/fr_class_index.html +1 -2
- data/doc/rdoc/fr_file_index.html +1 -0
- data/doc/rdoc/fr_method_index.html +18 -16
- data/lib/serverside/application.rb +0 -28
- data/lib/serverside/connection.rb +38 -12
- data/lib/serverside/routing.rb +23 -2
- data/lib/serverside/server.rb +1 -1
- data/lib/serverside/static.rb +12 -0
- data/lib/serverside/template.rb +36 -0
- data/test/functional/primitive_static_server_test.rb +26 -2
- data/test/functional/static_rfuzz.rb +3 -39
- data/test/spec/core_ext_spec.rb +68 -0
- data/test/unit/connection_test.rb +37 -1
- data/test/unit/template_test.rb +78 -0
- metadata +8 -6
- data/doc/rdoc/classes/ServerSide/Application/Base.html +0 -196
- data/doc/rdoc/classes/ServerSide/Application/Static.html +0 -156
- data/test/unit/application_test.rb +0 -16
@@ -84,8 +84,32 @@
|
|
84
84
|
<p>
|
85
85
|
The <a href="Router.html">Router</a> class defines a kind of connection
|
86
86
|
that can route requests to different handlers based on rules that can be
|
87
|
-
specified either by lambdas or by hashes
|
88
|
-
patterns.
|
87
|
+
specified either by lambdas or by hashes that contain variable names
|
88
|
+
corresponding to patterns.
|
89
|
+
</p>
|
90
|
+
<p>
|
91
|
+
The simplest form of a routing rule specifies a path pattern:
|
92
|
+
</p>
|
93
|
+
<pre>
|
94
|
+
ServerSide.route('/static') {serve_static('.'/@path)}
|
95
|
+
</pre>
|
96
|
+
<p>
|
97
|
+
But you can also check for other attributes of the request:
|
98
|
+
</p>
|
99
|
+
<pre>
|
100
|
+
ServerSide.route(:path => '/static', :host => '^:subdomain\.mydomain') {
|
101
|
+
serve_static(@parameters[:subdomain]/@path)
|
102
|
+
}
|
103
|
+
</pre>
|
104
|
+
<p>
|
105
|
+
It also possible to pass a lambda as a rule:
|
106
|
+
</p>
|
107
|
+
<pre>
|
108
|
+
ServerSide.route(lambda {@headers['Agent'] =~ /Moz/}) {serve_static('moz'/@path)}
|
109
|
+
</pre>
|
110
|
+
<p>
|
111
|
+
Routing rules are evaluated in backwards, so the rules should be ordered
|
112
|
+
from the general to the specific.
|
89
113
|
</p>
|
90
114
|
|
91
115
|
</div>
|
@@ -171,12 +195,12 @@ constant’s tag name
|
|
171
195
|
onclick="toggleCode('M000046-source');return false;">[Source]</a></p>
|
172
196
|
<div class="method-source-code" id="M000046-source">
|
173
197
|
<pre>
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
198
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 111</span>
|
199
|
+
111: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">cache_constant</span>(<span class="ruby-identifier">value</span>)
|
200
|
+
112: <span class="ruby-identifier">tag</span> = <span class="ruby-identifier">value</span>.<span class="ruby-identifier">const_tag</span>
|
201
|
+
113: <span class="ruby-identifier">class_eval</span> <span class="ruby-node">"#{tag} = #{value.inspect}.freeze"</span> <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">nil</span>
|
202
|
+
114: <span class="ruby-identifier">tag</span>
|
203
|
+
115: <span class="ruby-keyword kw">end</span>
|
180
204
|
</pre>
|
181
205
|
</div>
|
182
206
|
</div>
|
@@ -200,13 +224,13 @@ received.
|
|
200
224
|
onclick="toggleCode('M000042-source');return false;">[Source]</a></p>
|
201
225
|
<div class="method-source-code" id="M000042-source">
|
202
226
|
<pre>
|
203
|
-
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
227
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 46</span>
|
228
|
+
46: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">compile_rules</span>
|
229
|
+
47: <span class="ruby-ivar">@@rules</span> <span class="ruby-operator">||=</span> []
|
230
|
+
48: <span class="ruby-identifier">code</span> = <span class="ruby-ivar">@@rules</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-value str">'lambda {'</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">m</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">rule_to_statement</span>(<span class="ruby-identifier">r</span>[<span class="ruby-value">0</span>], <span class="ruby-identifier">r</span>[<span class="ruby-value">1</span>])}
|
231
|
+
49: <span class="ruby-identifier">code</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">'default_handler}'</span>
|
232
|
+
50: <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">:respond</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">eval</span>(<span class="ruby-identifier">code</span>))
|
233
|
+
51: <span class="ruby-keyword kw">end</span>
|
210
234
|
</pre>
|
211
235
|
</div>
|
212
236
|
</div>
|
@@ -232,25 +256,25 @@ method creates a lambda for extracting the parameter values.
|
|
232
256
|
onclick="toggleCode('M000044-source');return false;">[Source]</a></p>
|
233
257
|
<div class="method-source-code" id="M000044-source">
|
234
258
|
<pre>
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
259
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 83</span>
|
260
|
+
83: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">condition_part</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>)
|
261
|
+
84: <span class="ruby-identifier">p_parse</span>, <span class="ruby-identifier">p_count</span> = <span class="ruby-value str">''</span>, <span class="ruby-value">0</span>
|
262
|
+
85: <span class="ruby-keyword kw">while</span> (<span class="ruby-constant">String</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">value</span>) <span class="ruby-operator">&&</span> (<span class="ruby-identifier">value</span> <span class="ruby-operator">=~</span> <span class="ruby-constant">ParamRegexp</span>)
|
263
|
+
86: <span class="ruby-identifier">value</span> = <span class="ruby-identifier">value</span>.<span class="ruby-identifier">dup</span>
|
264
|
+
87: <span class="ruby-identifier">p_name</span> = <span class="ruby-identifier">$1</span>
|
265
|
+
88: <span class="ruby-identifier">p_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
266
|
+
89: <span class="ruby-identifier">value</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-constant">ParamRegexp</span>, <span class="ruby-value str">'(.+)'</span>)
|
267
|
+
90: <span class="ruby-identifier">p_parse</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"@parameters[:#{p_name}] = $#{p_count}\n"</span>
|
268
|
+
91: <span class="ruby-keyword kw">end</span>
|
269
|
+
92: <span class="ruby-identifier">cond</span> = <span class="ruby-node">"(@#{key} =~ #{cache_constant(Regexp.new(value))})"</span>
|
270
|
+
93: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">p_count</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
|
271
|
+
94: <span class="ruby-identifier">cond</span>
|
272
|
+
95: <span class="ruby-keyword kw">else</span>
|
273
|
+
96: <span class="ruby-identifier">tag</span> = <span class="ruby-identifier">define_proc</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">eval</span>(
|
274
|
+
97: <span class="ruby-node">"lambda {if #{cond}\n#{p_parse}true\nelse\nfalse\nend}"</span>))
|
275
|
+
98: <span class="ruby-node">"(#{tag})"</span>
|
276
|
+
99: <span class="ruby-keyword kw">end</span>
|
277
|
+
100: <span class="ruby-keyword kw">end</span>
|
254
278
|
</pre>
|
255
279
|
</div>
|
256
280
|
</div>
|
@@ -274,12 +298,12 @@ symbol)
|
|
274
298
|
onclick="toggleCode('M000045-source');return false;">[Source]</a></p>
|
275
299
|
<div class="method-source-code" id="M000045-source">
|
276
300
|
<pre>
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
301
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 103</span>
|
302
|
+
103: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">define_proc</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
303
|
+
104: <span class="ruby-identifier">tag</span> = <span class="ruby-identifier">block</span>.<span class="ruby-identifier">proc_tag</span>
|
304
|
+
105: <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">tag</span>.<span class="ruby-identifier">to_sym</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">instance_methods</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">tag</span>)
|
305
|
+
106: <span class="ruby-identifier">tag</span>.<span class="ruby-identifier">to_sym</span>
|
306
|
+
107: <span class="ruby-keyword kw">end</span>
|
283
307
|
</pre>
|
284
308
|
</div>
|
285
309
|
</div>
|
@@ -302,10 +326,10 @@ Returns true if routes were defined.
|
|
302
326
|
onclick="toggleCode('M000040-source');return false;">[Source]</a></p>
|
303
327
|
<div class="method-source-code" id="M000040-source">
|
304
328
|
<pre>
|
305
|
-
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line
|
306
|
-
|
307
|
-
|
308
|
-
|
329
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 25</span>
|
330
|
+
25: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">has_routes?</span>
|
331
|
+
26: <span class="ruby-ivar">@@rules</span> <span class="ruby-operator">&&</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@@rules</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">false</span>
|
332
|
+
27: <span class="ruby-keyword kw">end</span>
|
309
333
|
</pre>
|
310
334
|
</div>
|
311
335
|
</div>
|
@@ -325,20 +349,25 @@ Returns true if routes were defined.
|
|
325
349
|
Adds a routing rule. The normalized rule is a hash containing keys (acting
|
326
350
|
as instance variable names) with patterns as values. If the rule is not a
|
327
351
|
hash, it is normalized into a pattern checked against the request path.
|
328
|
-
Pattern values can also be arrays,
|
329
|
-
pattern.
|
352
|
+
Pattern values can also be arrays, any member of which is checked as a
|
353
|
+
pattern. The rule can also be a <a href="../../Proc.html">Proc</a> or
|
354
|
+
lambda which is run with the connection object’s binding. A contrived
|
355
|
+
example:
|
330
356
|
</p>
|
357
|
+
<pre>
|
358
|
+
ServerSide.route(lambda{path = 'mypage'}) {serve_static('mypage.html')}
|
359
|
+
</pre>
|
331
360
|
<p><a class="source-toggle" href="#"
|
332
361
|
onclick="toggleCode('M000041-source');return false;">[Source]</a></p>
|
333
362
|
<div class="method-source-code" id="M000041-source">
|
334
363
|
<pre>
|
335
|
-
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
364
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 37</span>
|
365
|
+
37: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">route</span>(<span class="ruby-identifier">rule</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
366
|
+
38: <span class="ruby-ivar">@@rules</span> <span class="ruby-operator">||=</span> []
|
367
|
+
39: <span class="ruby-identifier">rule</span> = {<span class="ruby-identifier">:path</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">rule</span>} <span class="ruby-keyword kw">unless</span> (<span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">rule</span>) <span class="ruby-operator">||</span> (<span class="ruby-constant">Proc</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">rule</span>)
|
368
|
+
40: <span class="ruby-ivar">@@rules</span>.<span class="ruby-identifier">unshift</span> [<span class="ruby-identifier">rule</span>, <span class="ruby-identifier">block</span>]
|
369
|
+
41: <span class="ruby-identifier">compile_rules</span>
|
370
|
+
42: <span class="ruby-keyword kw">end</span>
|
342
371
|
</pre>
|
343
372
|
</div>
|
344
373
|
</div>
|
@@ -361,11 +390,11 @@ Sets the default handler for incoming requests.
|
|
361
390
|
onclick="toggleCode('M000047-source');return false;">[Source]</a></p>
|
362
391
|
<div class="method-source-code" id="M000047-source">
|
363
392
|
<pre>
|
364
|
-
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
393
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 118</span>
|
394
|
+
118: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">route_default</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
395
|
+
119: <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">:default_handler</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
396
|
+
120: <span class="ruby-identifier">compile_rules</span>
|
397
|
+
121: <span class="ruby-keyword kw">end</span>
|
369
398
|
</pre>
|
370
399
|
</div>
|
371
400
|
</div>
|
@@ -389,22 +418,22 @@ against their respective values.
|
|
389
418
|
onclick="toggleCode('M000043-source');return false;">[Source]</a></p>
|
390
419
|
<div class="method-source-code" id="M000043-source">
|
391
420
|
<pre>
|
392
|
-
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
421
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 55</span>
|
422
|
+
55: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">rule_to_statement</span>(<span class="ruby-identifier">rule</span>, <span class="ruby-identifier">block</span>)
|
423
|
+
56: <span class="ruby-identifier">proc_tag</span> = <span class="ruby-identifier">define_proc</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
424
|
+
57: <span class="ruby-keyword kw">if</span> <span class="ruby-constant">Proc</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">rule</span>
|
425
|
+
58: <span class="ruby-identifier">cond</span> = <span class="ruby-identifier">define_proc</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">rule</span>).<span class="ruby-identifier">to_s</span>
|
426
|
+
59: <span class="ruby-keyword kw">else</span>
|
427
|
+
60: <span class="ruby-identifier">cond</span> = <span class="ruby-identifier">rule</span>.<span class="ruby-identifier">to_a</span>.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">kv</span><span class="ruby-operator">|</span>
|
428
|
+
61: <span class="ruby-keyword kw">if</span> <span class="ruby-constant">Array</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">kv</span>[<span class="ruby-value">1</span>]
|
429
|
+
62: <span class="ruby-value str">'('</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">kv</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">condition_part</span>(<span class="ruby-identifier">kv</span>[<span class="ruby-value">0</span>], <span class="ruby-identifier">v</span>)}.<span class="ruby-identifier">join</span>(<span class="ruby-value str">'||'</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">')'</span>
|
430
|
+
63: <span class="ruby-keyword kw">else</span>
|
431
|
+
64: <span class="ruby-identifier">condition_part</span>(<span class="ruby-identifier">kv</span>[<span class="ruby-value">0</span>], <span class="ruby-identifier">kv</span>[<span class="ruby-value">1</span>])
|
432
|
+
65: <span class="ruby-keyword kw">end</span>
|
433
|
+
66: }.<span class="ruby-identifier">join</span>(<span class="ruby-value str">'&&'</span>)
|
434
|
+
67: <span class="ruby-keyword kw">end</span>
|
435
|
+
68: <span class="ruby-node">"return #{proc_tag} if #{cond}\n"</span>
|
436
|
+
69: <span class="ruby-keyword kw">end</span>
|
408
437
|
</pre>
|
409
438
|
</div>
|
410
439
|
</div>
|
@@ -440,10 +469,10 @@ Alias for <a href="Router.html#M000048">unhandled</a>
|
|
440
469
|
onclick="toggleCode('M000048-source');return false;">[Source]</a></p>
|
441
470
|
<div class="method-source-code" id="M000048-source">
|
442
471
|
<pre>
|
443
|
-
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line
|
444
|
-
|
445
|
-
|
446
|
-
|
472
|
+
<span class="ruby-comment cmt"># File lib/serverside/routing.rb, line 123</span>
|
473
|
+
123: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">unhandled</span>
|
474
|
+
124: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">403</span>, <span class="ruby-value str">'text'</span>, <span class="ruby-value str">'No handler found.'</span>)
|
475
|
+
125: <span class="ruby-keyword kw">end</span>
|
447
476
|
</pre>
|
448
477
|
</div>
|
449
478
|
</div>
|
@@ -131,7 +131,7 @@ techniques.
|
|
131
131
|
<div class="method-description">
|
132
132
|
<p>
|
133
133
|
Creates a new server by opening a listening socket and starting an accept
|
134
|
-
loop. When a new connection is accepted, a new instance of the
|
134
|
+
loop. When a new connection is accepted, a new instance of the supplied
|
135
135
|
connection class is instantiated and passed the connection for processing.
|
136
136
|
</p>
|
137
137
|
<p><a class="source-toggle" href="#"
|
@@ -136,6 +136,11 @@ Frozen constants to be used by the module.
|
|
136
136
|
<td>=</td>
|
137
137
|
<td class="context-item-value">'text/plain'.freeze</td>
|
138
138
|
</tr>
|
139
|
+
<tr class="top-aligned-row context-row">
|
140
|
+
<td class="context-item-name">TextHTML</td>
|
141
|
+
<td>=</td>
|
142
|
+
<td class="context-item-value">'text/html'.freeze</td>
|
143
|
+
</tr>
|
139
144
|
<tr class="top-aligned-row context-row">
|
140
145
|
<td class="context-item-name">MaxCacheFileSize</td>
|
141
146
|
<td>=</td>
|
@@ -161,6 +166,11 @@ Frozen constants to be used by the module.
|
|
161
166
|
<td>=</td>
|
162
167
|
<td class="context-item-value">'File not found.'.freeze</td>
|
163
168
|
</tr>
|
169
|
+
<tr class="top-aligned-row context-row">
|
170
|
+
<td class="context-item-name">RHTML</td>
|
171
|
+
<td>=</td>
|
172
|
+
<td class="context-item-value">/\.rhtml$/.freeze</td>
|
173
|
+
</tr>
|
164
174
|
</table>
|
165
175
|
</div>
|
166
176
|
</div>
|
@@ -89,7 +89,8 @@ over HTTP. It is mainly used by ServerSide::Connection::Static.
|
|
89
89
|
<div class="name-list">
|
90
90
|
<a href="#M000025">serve_dir</a>
|
91
91
|
<a href="#M000024">serve_file</a>
|
92
|
-
<a href="#
|
92
|
+
<a href="#M000027">serve_static</a>
|
93
|
+
<a href="#M000026">serve_template</a>
|
93
94
|
</div>
|
94
95
|
</div>
|
95
96
|
|
@@ -134,14 +135,14 @@ Serves a directory listing over HTTP in the form of an HTML page.
|
|
134
135
|
onclick="toggleCode('M000025-source');return false;">[Source]</a></p>
|
135
136
|
<div class="method-source-code" id="M000025-source">
|
136
137
|
<pre>
|
137
|
-
<span class="ruby-comment cmt"># File lib/serverside/static.rb, line
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
138
|
+
<span class="ruby-comment cmt"># File lib/serverside/static.rb, line 65</span>
|
139
|
+
65: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">serve_dir</span>(<span class="ruby-identifier">dir</span>)
|
140
|
+
66: <span class="ruby-identifier">html</span> = (<span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">DirListingStart</span> <span class="ruby-operator">%</span> [<span class="ruby-ivar">@path</span>, <span class="ruby-ivar">@path</span>]) <span class="ruby-operator">+</span>
|
141
|
+
67: <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">entries</span>(<span class="ruby-identifier">dir</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value str">''</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">fn</span><span class="ruby-operator">|</span>
|
142
|
+
68: (<span class="ruby-identifier">fn</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">m</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">m</span> <span class="ruby-operator"><<</span> <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">DirListing</span> <span class="ruby-operator">%</span> [<span class="ruby-ivar">@path</span><span class="ruby-operator">/</span><span class="ruby-identifier">fn</span>, <span class="ruby-identifier">fn</span>]
|
143
|
+
69: } <span class="ruby-operator">+</span> <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">DirListingStop</span>
|
144
|
+
70: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">200</span>, <span class="ruby-value str">'text/html'</span>, <span class="ruby-identifier">html</span>)
|
145
|
+
71: <span class="ruby-keyword kw">end</span>
|
145
146
|
</pre>
|
146
147
|
</div>
|
147
148
|
</div>
|
@@ -167,37 +168,37 @@ rendered.
|
|
167
168
|
onclick="toggleCode('M000024-source');return false;">[Source]</a></p>
|
168
169
|
<div class="method-source-code" id="M000024-source">
|
169
170
|
<pre>
|
170
|
-
<span class="ruby-comment cmt"># File lib/serverside/static.rb, line
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
171
|
+
<span class="ruby-comment cmt"># File lib/serverside/static.rb, line 43</span>
|
172
|
+
43: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">serve_file</span>(<span class="ruby-identifier">fn</span>)
|
173
|
+
44: <span class="ruby-identifier">stat</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">stat</span>(<span class="ruby-identifier">fn</span>)
|
174
|
+
45: <span class="ruby-identifier">etag</span> = (<span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">ETagFormat</span> <span class="ruby-operator">%</span> [<span class="ruby-identifier">stat</span>.<span class="ruby-identifier">mtime</span>.<span class="ruby-identifier">to_i</span>, <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">size</span>, <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">ino</span>]).<span class="ruby-identifier">freeze</span>
|
175
|
+
46: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">etag</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@headers</span>[<span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">IfNoneMatch</span>]
|
176
|
+
47: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@static_files</span>[<span class="ruby-identifier">fn</span>] <span class="ruby-operator">&&</span> (<span class="ruby-ivar">@@static_files</span>[<span class="ruby-identifier">fn</span>][<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">etag</span>)
|
177
|
+
48: <span class="ruby-identifier">content</span> = <span class="ruby-ivar">@@static_files</span>[<span class="ruby-identifier">fn</span>][<span class="ruby-value">1</span>]
|
178
|
+
49: <span class="ruby-keyword kw">else</span>
|
179
|
+
50: <span class="ruby-identifier">content</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">fn</span>).<span class="ruby-identifier">freeze</span>
|
180
|
+
51: <span class="ruby-ivar">@@static_files</span>[<span class="ruby-identifier">fn</span>] = [<span class="ruby-identifier">etag</span>.<span class="ruby-identifier">freeze</span>, <span class="ruby-identifier">content</span>]
|
181
|
+
52: <span class="ruby-keyword kw">end</span>
|
182
|
+
53:
|
183
|
+
54: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">200</span>, <span class="ruby-ivar">@@mime_types</span>[<span class="ruby-constant">File</span>.<span class="ruby-identifier">extname</span>(<span class="ruby-identifier">fn</span>)], <span class="ruby-identifier">content</span>, <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">size</span>,
|
184
|
+
55: {<span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">ETag</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">etag</span>, <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">CacheControl</span> =<span class="ruby-operator">></span> <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">MaxAge</span>})
|
185
|
+
56: <span class="ruby-keyword kw">else</span>
|
186
|
+
57: <span class="ruby-ivar">@conn</span> <span class="ruby-operator"><<</span> ((<span class="ruby-ivar">@persistent</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">NotModifiedPersist</span> <span class="ruby-operator">:</span>
|
187
|
+
58: <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">NotModifiedClose</span>) <span class="ruby-operator">%</span> <span class="ruby-identifier">etag</span>)
|
188
|
+
59: <span class="ruby-keyword kw">end</span>
|
189
|
+
60: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
|
190
|
+
61: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">404</span>, <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">TextPlain</span>, <span class="ruby-value str">'Error reading file.'</span>)
|
191
|
+
62: <span class="ruby-keyword kw">end</span>
|
191
192
|
</pre>
|
192
193
|
</div>
|
193
194
|
</div>
|
194
195
|
</div>
|
195
196
|
|
196
|
-
<div id="method-
|
197
|
-
<a name="
|
197
|
+
<div id="method-M000027" class="method-detail">
|
198
|
+
<a name="M000027"></a>
|
198
199
|
|
199
200
|
<div class="method-heading">
|
200
|
-
<a href="#
|
201
|
+
<a href="#M000027" class="method-signature">
|
201
202
|
<span class="method-name">serve_static</span><span class="method-args">(path)</span>
|
202
203
|
</a>
|
203
204
|
</div>
|
@@ -206,20 +207,49 @@ rendered.
|
|
206
207
|
<p>
|
207
208
|
Serves static files and directory listings.
|
208
209
|
</p>
|
210
|
+
<p><a class="source-toggle" href="#"
|
211
|
+
onclick="toggleCode('M000027-source');return false;">[Source]</a></p>
|
212
|
+
<div class="method-source-code" id="M000027-source">
|
213
|
+
<pre>
|
214
|
+
<span class="ruby-comment cmt"># File lib/serverside/static.rb, line 80</span>
|
215
|
+
80: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">serve_static</span>(<span class="ruby-identifier">path</span>)
|
216
|
+
81: <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">file?</span>(<span class="ruby-identifier">path</span>)
|
217
|
+
82: <span class="ruby-identifier">serve_file</span>(<span class="ruby-identifier">path</span>)
|
218
|
+
83: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">serve_template</span>(<span class="ruby-identifier">path</span>)
|
219
|
+
84: <span class="ruby-keyword kw">return</span>
|
220
|
+
85: <span class="ruby-keyword kw">elsif</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">directory?</span>(<span class="ruby-identifier">path</span>)
|
221
|
+
86: <span class="ruby-identifier">serve_dir</span>(<span class="ruby-identifier">path</span>)
|
222
|
+
87: <span class="ruby-keyword kw">else</span>
|
223
|
+
88: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">404</span>, <span class="ruby-value str">'text'</span>, <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">FileNotFound</span>)
|
224
|
+
89: <span class="ruby-keyword kw">end</span>
|
225
|
+
90: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
|
226
|
+
91: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">500</span>, <span class="ruby-value str">'text'</span>, <span class="ruby-identifier">e</span>.<span class="ruby-identifier">message</span>)
|
227
|
+
92: <span class="ruby-keyword kw">end</span>
|
228
|
+
</pre>
|
229
|
+
</div>
|
230
|
+
</div>
|
231
|
+
</div>
|
232
|
+
|
233
|
+
<div id="method-M000026" class="method-detail">
|
234
|
+
<a name="M000026"></a>
|
235
|
+
|
236
|
+
<div class="method-heading">
|
237
|
+
<a href="#M000026" class="method-signature">
|
238
|
+
<span class="method-name">serve_template</span><span class="method-args">(fn, b = nil)</span>
|
239
|
+
</a>
|
240
|
+
</div>
|
241
|
+
|
242
|
+
<div class="method-description">
|
209
243
|
<p><a class="source-toggle" href="#"
|
210
244
|
onclick="toggleCode('M000026-source');return false;">[Source]</a></p>
|
211
245
|
<div class="method-source-code" id="M000026-source">
|
212
246
|
<pre>
|
213
|
-
<span class="ruby-comment cmt"># File lib/serverside/static.rb, line
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
77: <span class="ruby-keyword kw">else</span>
|
220
|
-
78: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">404</span>, <span class="ruby-value str">'text'</span>, <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">FileNotFound</span>)
|
221
|
-
79: <span class="ruby-keyword kw">end</span>
|
222
|
-
80: <span class="ruby-keyword kw">end</span>
|
247
|
+
<span class="ruby-comment cmt"># File lib/serverside/static.rb, line 73</span>
|
248
|
+
73: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">serve_template</span>(<span class="ruby-identifier">fn</span>, <span class="ruby-identifier">b</span> = <span class="ruby-keyword kw">nil</span>)
|
249
|
+
74: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">fn</span> <span class="ruby-operator">=~</span> <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">RHTML</span>) <span class="ruby-operator">||</span> (<span class="ruby-constant">File</span>.<span class="ruby-identifier">file?</span>(<span class="ruby-identifier">fn</span> = <span class="ruby-identifier">fn</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'.rhtml'</span>))
|
250
|
+
75: <span class="ruby-identifier">send_response</span>(<span class="ruby-value">200</span>, <span class="ruby-constant">Const</span><span class="ruby-operator">::</span><span class="ruby-constant">TextHTML</span>, <span class="ruby-constant">Template</span>.<span class="ruby-identifier">render</span>(<span class="ruby-identifier">fn</span>, <span class="ruby-identifier">b</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">binding</span>))
|
251
|
+
76: <span class="ruby-keyword kw">end</span>
|
252
|
+
77: <span class="ruby-keyword kw">end</span>
|
223
253
|
</pre>
|
224
254
|
</div>
|
225
255
|
</div>
|