blib/lib/SVN/Web.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« bin/svnweb-install ↑ Summary blib/lib/SVN/Web/Test.pm »
1 #!/bin/false
2 package SVN::Web;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use SVN::Client;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use SVN::Ra;
11 use YAML ();
12 use Template;
13 use File::Spec;
14 use Plack::Request;
15  
16 use SVN::Web::X;
17 use FindBin;
18  
19 use SVN::Web::I18N;
20  
21 # Add the localisations that ship with SVN::Web as the default, and set
22 # the default language. This will be overridden later, but ensures that
23 # any error messages generated *before* it's overridden are generated
24 # properly.
25 SVN::Web::I18N::add_directory(
26 File::Spec->catdir( substr( __FILE__, 0, -3 ), 'I18N' ) );
27 SVN::Web::I18N::loc_lang('en');
28  
29 our $VERSION = 0.63;
30  
31 my $template;
32 my $config;
33  
34 my %REPOS;
35  
36 sub load_config {
37 return if defined $config;
38 my $file = shift || 'config.yaml';
39 my $config = YAML::LoadFile($file);
40 set_config($config);
41 }
42  
43 sub canonicalise_config {
44  
45 # Catch missing / incorrect 'version' entries
46 die "Config file does not define a 'version' key."
47 unless exists $config->{version} and defined $config->{version};
48  
49 die
50 "Configuration file version ($config->{version}) does not match SVN::Web version ($VERSION)"
51 if $config->{version} != $VERSION;
52  
53 # Deal with possibly conflicting 'templatedir' and 'templatedirs' settings.
54  
55 # If neither of them are set, use 'templatedirs'
56 if ( !exists $config->{templatedir} and !exists $config->{templatedirs} ) {
57 $config->{templatedirs} = [ File::Spec->catdir(qw(template trac)) ];
58 }
59  
60 # If 'templatedir' is the only one set, use it.
61 if ( exists $config->{templatedir} and !exists $config->{templatedirs} ) {
62 $config->{templatedirs} = [ $config->{templatedir} ];
63 delete $config->{templatedir};
64 }
65  
66 # If they're both set then throw an error
67 if ( exists $config->{templatedir} and exists $config->{templatedirs} ) {
68 die "templatedir and templatedirs both defined in config.yaml";
69 }
70  
71 # Handle tt_compile_dir. If it doesn't exist then set it to undef.
72 # If it does exist, and is defined, append a '.' and the current
73 # real UID, to help ensure uniqueness.
74 unless ( exists $config->{tt_compile_dir} ) {
75 $config->{tt_compile_dir} = undef; # undef == no compiling
76 }
77 else {
78 if ( defined $config->{tt_compile_dir} ) {
79 $config->{tt_compile_dir} .= '.' . $<;
80 }
81 }
82  
83 # Handle timedate_format
84 unless ( exists $config->{timedate_format} ) {
85 $config->{timedate_format} = '%Y/%m/%d %H:%M:%S';
86 }
87  
88 # Set the timezone, if not already specified.
89 $config->{timezone} = '' unless exists $config->{timezone};
90  
91 # If cache/opts/directory_umask is configured, and it has a leading
92 # 0 then ensure it's treated as an octal number.
93 $config->{cache}{opts}{directory_umask} =
94 oct( $config->{cache}{opts}{directory_umask} )
95 if exists $config->{cache}{opts}{directory_umask}
96 and $config->{cache}{opts}{directory_umask} =~ m/^0/;
97  
98 # Add any additional language directories
99 if ( defined $config->{language_dirs} ) {
100 for my $dir ( @{ $config->{language_dirs} } ) {
101 SVN::Web::I18N::add_directory($dir);
102 }
103 }
104  
105 return;
106 }
107  
108 sub set_config {
109 $config = shift;
110  
111 canonicalise_config();
112 }
113  
114 sub get_config {
115 return $config;
116 }
117  
118 my $repospool = SVN::Pool->new();
119  
120 sub get_repos {
121 my $repos = shift;
122  
123 SVN::Web::X->throw(
124 error => '(unconfigured repository)',
125 vars => []
126 ) unless exists $config->{repos}{$repos} || $config->{reposparent};
127  
128 my $repo_uri =
129 $config->{reposparent}
130 ? File::Spec->catdir( $config->{reposparent}, $repos )
131 : $config->{repos}{$repos};
132  
133 SVN::Web::X->throw(
134 error => '(no such repo %1 %2)',
135 vars => [ $repos, $repo_uri ]
136 )
137 unless defined $repos
138 and ( exists $config->{repos}{$repos} or -e $repo_uri );
139  
140 $repo_uri =~ s{/$}{}g; # Trim trailing '/', SVN::Repos::open fails
141 # otherwise
142  
143 # If there's a leading '/' then tack 'file://' on to the start
144 $repo_uri = "file://$repo_uri" if $repo_uri =~ m{^/};
145  
146 # warn "REPO_URI: $repo_uri";
147  
148 eval {
149 my $client = SVN::Client->new( pool => $repospool );
150 my $auth = $client->auth;
151 $REPOS{$repos}{uri} ||= $repo_uri;
152 $REPOS{$repos}{ra} ||= SVN::Ra->new(
153 url => $repo_uri,
154 pool => $repospool,
155 auth => $auth,
156 );
157 };
158  
159 if ($@) {
160 my $e = $@;
161 SVN::Web::X->throw(
162 error => '(SVN::Client->new() failed: %1 %2)',
163 vars => [ $repo_uri, $e ]
164 );
165 }
166  
167 if ( $config->{block} ) {
168 for my $blocked ( @{ $config->{block} } ) {
169 delete $REPOS{$blocked};
170 }
171 }
172 }
173  
174 sub get_action {
175 my $cfg = shift;
176 my $action_pkg;
177  
178 if ( exists $config->{actions}{ $cfg->{action} } ) {
179 if ( ref( $config->{actions}{ $cfg->{action} } ) eq 'HASH' ) {
180 if ( exists $config->{actions}{ $cfg->{action} }{class} ) {
181 $action_pkg = $config->{actions}{ $cfg->{action} }{class};
182 }
183 }
184 }
185  
186 unless ($action_pkg) {
187 $action_pkg = $cfg->{action};
188 $action_pkg =~ s{^(\w)}{\U$1};
189 $action_pkg = __PACKAGE__ . "::$action_pkg";
190 }
191  
192 eval "require $action_pkg && $action_pkg->can('run')"
193 or SVN::Web::X->throw(
194 error => '(missing package %1 for action %2: %3)',
195 vars => [ $action_pkg, $cfg->{action}, $@ ]
196 );
197  
198 my $repos = $cfg->{repos} ? $REPOS{ $cfg->{repos} } : undef;
199  
200 return $action_pkg->new(
201 %$cfg,
202 reposname => $cfg->{repos},
203 repos => $repos,
204 config => $config
205 );
206 }
207  
208 sub run {
209 my $cfg = shift;
210  
211 my $action;
212 my $html;
213 my $cache;
214  
215 if ( defined $config->{cache}{class} ) {
216 eval "require $config->{cache}{class}"
217 or SVN::Web::X->throw(
218 error => '(require %1 failed: %2)',
219 vars => [ $config->{cache}{class}, $@ ]
220 );
221  
222 $config->{cache}{opts} = {} unless exists $config->{cache}{opts};
223 $config->{cache}{opts}{namespace} = $cfg->{repos};
224 $cache = $config->{cache}{class}->new( $config->{cache}{opts} );
225 }
226  
227 if ( defined $cfg->{repos} && length $cfg->{repos} ) {
228 get_repos( $cfg->{repos} );
229 }
230  
231 if ( $cfg->{repos} && $REPOS{ $cfg->{repos} } ) {
232 @{ $cfg->{navpaths} } = File::Spec::Unix->splitdir( $cfg->{path} );
233 shift @{ $cfg->{navpaths} };
234  
235 # should use attribute or things alike
236 $action = get_action(
237 {
238 %$cfg,
239 opts => exists $config->{actions}{ $cfg->{action} }{opts}
240 ? $config->{actions}{ $cfg->{action} }{opts}
241 : {},
242 }
243 );
244 }
245 else {
246 $cfg->{action} = 'list';
247 $action = get_action(
248 {
249 %$cfg,
250 opts => exists $config->{actions}{ $cfg->{action} }{opts}
251 ? $config->{actions}{ $cfg->{action} }{opts}
252 : {},
253 }
254 );
255 }
256  
257 # Determine the language to use
258 my $lang =
259 get_language( $cfg->{cgi}, $config->{languages},
260 $config->{default_language} );
261  
262 $cfg->{lang} = $lang; # Note the preference, stored in a cookie
263 # later
264  
265 SVN::Web::I18N::loc_lang($lang); # Set the localisation language
266  
267 # Generate output, from the cache if necessary.
268  
269 # Does the action support caching? If so, get the cache key
270 my $cache_key;
271 if ( defined $cache and $action->can('cache_key') ) {
272 $cache_key =
273 join( ':', $cfg->{action}, $lang ) . ':' . $action->cache_key();
274 }
275  
276 # If there's a key, retrieve the data from the cache
277 $html = $cache->get($cache_key) if defined $cache_key;
278  
279 # No data? Get the action to generate it, then cache it
280 unless ( defined $html ) {
281  
282 # Create a default pool for the action's allocation
283 my $pool = SVN::Pool->new_default();
284  
285 $REPOS{ $cfg->{repos} }{client} = SVN::Client->new( config => {} );
286  
287 $html = $action->run();
288  
289 $pool->clear();
290  
291 if ( defined $cache_key ) {
292 $cache->set( $cache_key, $html, $cfg->{cache}{expires_in} );
293 }
294 }
295  
296 return $html;
297 }
298  
299 sub get_language {
300 my $obj = shift; # Plack object
301 my $languages = shift; # Hash ref of valid langauges
302 my $default_lang = shift; # Default language
303  
304 my $lang = $obj->param('lang');
305  
306 # If lang was included in the query string then delete it now we've
307 # got it. This stops it showing up in from calls to self_url().
308  
309 ## FIXME for Plack
310  
311 # If no valid lang=.. param was found then check the user's cookies
312  
313 unless ( defined $lang ) {
314 $lang = $obj->cookies->{'svnweb-lang'};
315 }
316  
317 # If $lang is not defined, or if it's not in the hash of valid languages
318 # then use the default configured language, falling back to English as
319 # a last resort.
320 if ( !defined $lang or !exists $languages->{$lang} ) {
321 $lang = $default_lang;
322 $lang = 'en' unless defined $lang;
323 }
324  
325 die "lang is not defined" unless defined $lang;
326  
327 return $lang;
328 }
329  
330 sub psgi_output {
331 my ( $req, $cfg, $html ) = @_;
332  
333 return unless defined $html;
334  
335 my $res = $req->new_response(200);
336 $res->cookies->{ 'svnweb-lang' } = $cfg->{lang};
337  
338  
339 if ( ref $html ) {
340  
341 if ( $html->{template} ) {
342 my $body;
343 $template->process(
344 $html->{template},
345 {
346 c => $cfg,
347 %{ $html->{data} }
348 }, \$body
349 ) or die "Template::process() error: " . $template->error;
350  
351 $res->content_type('text/html; charset=utf-8');
352 $res->body(Encode::encode('utf8',$body));
353 }
354 else {
355 $res->content_type($html->{mimetype} || 'text/plain');
356 $res->body($html->{body});
357 }
358 }
359 else {
360 $res->content_type('text/plain');
361 $res->body($html);
362 }
363  
364 return $res->finalize
365  
366 }
367  
368 sub get_template {
369 Template->new(
370 {
371 INCLUDE_PATH => $config->{templatedirs},
372 COMPILE_DIR => $config->{tt_compile_dir},
373 PRE_CHOMP => 2,
374 POST_CHOMP => 2,
375 FILTERS => {
376 l => ( [ \&loc_filter, 1 ] ),
377 anchor => ( [ \&anchor_filter, 1 ] ),
378 },
379 ENCODING => 'utf8',
380 }
381 );
382 }
383  
384 sub run_psgi {
385  
386 my $c = shift;
387 my $env = shift;
388 my $req = Plack::Request->new($env);
389  
390 $template ||= get_template();
391  
392 my ( $html, $cfg );
393  
394 $cfg = {
395 style => $config->{style},
396 cgi => $req,
397 languages => $config->{languages},
398 };
399  
400 eval {
401 my ( $action, $base, $repo, $script, $path ) = crack_url($req);
402  
403 SVN::Web::X->throw(
404 error => '(action %1 not supported)',
405 vars => [$action]
406 ) unless exists $config->{actions}{ lc($action) };
407  
408 $cfg->{repos} = $repo;
409 $cfg->{action} = $action;
410 $cfg->{path} = $path;
411 $cfg->{script} = $script;
412 $cfg->{base_uri} = $base;
413 $cfg->{self_uri} = $req->path();
414 $cfg->{config} = $config;
415  
416 $html = run($cfg);
417 };
418  
419 if ( my $e = SVN::Web::X->caught() ) {
420 $html->{template} = 'x';
421 $html->{data}{error_msg} =
422 SVN::Web::I18N::loc( $e->error(), @{ $e->vars() } );
423 }
424 else {
425 if ($@) {
426 $html->{template} = 'x';
427 $html->{data}{error_msg} = $@;
428 }
429 }
430  
431 return psgi_output( $req, $cfg, $html );
432 }
433  
434 sub loc_filter {
435 my $context = shift;
436 my @args = @_;
437 return sub { SVN::Web::I18N::loc( $_[0], @args ) };
438 }
439  
440 sub anchor_filter {
441 my $context = shift;
442 return sub {
443 my $str = shift;
444 $str =~ s/[\/% ]/_/g;
445 return $str;
446 };
447 }
448  
449 # Crack a URL and determine the components we need.
450 sub crack_url {
451  
452 my $obj = shift;
453  
454 my $path_info = Encode::decode('utf8',$obj->path);
455  
456 # warn "PATH_INFO: $path_info";
457  
458 my ( $action, $base, $repo, $script, $path );
459  
460 # Determine $repo.
461 #
462 # This is used as the key in to the hash of configured repositories.
463 # It may be the empty string, in which case the action to run is
464 # the 'list repositories' action.
465 if ( $path_info eq '/' ) {
466 $repo = ''; # No repo, show repo list
467 $action = 'list';
468 }
469 else {
470  
471 # Start with $repo equal to $filename. Then remove $location.
472 # This needs to be quoted for systems where the path may include
473 # backslashes. There may also be a trailing directory separator
474 # which needs removing.
475 #
476 # XXX In an ideal world File::Spec would tell us what the directory
477 # separator is. For the time being, punt, and use both forward and
478 # backward slashes.
479 ($repo) = $path_info =~ m{^/([^/]+)/?};
480 $repo = uri_unescape($repo);
481 }
482  
483 # warn "REPO: $repo";
484  
485 # Determine $action
486 #
487 # This will be used as the key in to the hash of configured actions
488 # and their classes. If no action is included in the URL then the
489 # default action is 'browse'.
490 unless ( defined $action ) {
491 my @path = split( '/', $path_info );
492 # warn "SPLIT PATH: ", join('|', @path);
493 $action = $path[2] || 'browse';
494 }
495  
496 # warn "ACTION: $action";
497  
498 # Determine $path
499 #
500 # This is the path in the repository that we will be acting on. Some
501 # actions don't need this set.
502 if ( $action eq 'list' ) {
503 $path = '/';
504 }
505 else {
506 if ( $path_info eq '/' ) {
507 $path = '/';
508 }
509 else {
510 $path = $path_info;
511 $path =~ s{^/$repo(/$action)?}{};
512 $path =~ s{/+$}{} unless $path eq '/';
513 }
514 }
515  
516 # Unescape it, as it will have been escaped on the web page
517 $path = uri_unescape($path);
518  
519 # warn "PATH: $path";
520  
521 # Determine $script
522 #
523 # $script is the URI that points to the SVN::Web script. If this
524 # is CGI then it's something like 'http://host//svnweb/index.cgi'.
525 # If it's an Apache handler then it will be a directory reference,
526 # like '/svnweb', or possibly '/'.
527  
528 $script = $obj->script_name;
529  
530 # warn "SCRIPT: $script";
531  
532 # Determine $base
533 #
534 # $base is the URI that points to the directory that contains index.cgi,
535 # config.yaml, css/, etc. It's needed to generate links to the .css
536 # files.
537  
538 # In all cases, $base is a substring of $script. In the mod_perl and
539 # svnweb-server cases it's identical
540 # $base = $script; # Only in mod_perl case
541 $base = $obj->base; # Only in mod_perl case
542  
543 $base =~ s{/$}{}; # Remove trailing slash
544  
545 # warn "BASE: $base";
546  
547 return ( $action, $base, $repo, $script, $path );
548 }
549  
550 1;
551  
552 __END__
« bin/svnweb-install ↑ Summary blib/lib/SVN/Web/Test.pm »