aboutsummaryrefslogtreecommitdiffstats
path: root/lib/LXRng/Context.pm
blob: 6909c5847cb6d56c867c3ef45a985a27187d9180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Copyright (C) 2008 Arne Georg Gleditsch <lxr@linux.no>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# The full GNU General Public License is included in this distribution
# in the file called COPYING.

package LXRng::Context;

use strict;
use LXRng;

use vars qw($cached_config $cached_config_stat $cached_config_age);

sub new {
    my ($self, %args) = @_;

    $self = bless({}, $self);

    my $config = $self->read_config();

    if ($args{'query'}) {
	# Argle.  Both CGI and CGI::Simple seem to botch this up, in
	# different ways.  CGI breaks if SCRIPT_NAME contains regex
	# metachars, and CGI::Simple does funny things if SCRIPT_NAME
	# is the empty string.  Do it by hand...
	my $host = 'http'.($ENV{'HTTPS'} eq 'ON' ? 's' : '').'://'.
	    ($ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'}).
	    ($ENV{'SERVER_PORT'} == ($ENV{'HTTPS'} eq 'ON' ? 443 : 80)
	     ? '' : ':'.$ENV{'SERVER_PORT'});
	my $path = $ENV{'REQUEST_URI'};
	$path =~ s/\?.*//;
	$path =~ s,/+,/,g;
	$$self{'req_base'} = $host.$ENV{'SCRIPT_NAME'};

	foreach my $p ($args{'query'}->param) {
	    $$self{'params'}{$p} = [$args{'query'}->param($p)];
	}
	my @prefs = $args{'query'}->cookie('lxr_prefs');
	if (@prefs) {
	    $$self{'prefs'} = { 
		map { /^(.*?)(?:=(.*)|)$/; ($1 => $2) } @prefs };
	}
	foreach my $tree (keys %$config) {
	    my $base = $$config{$tree}{'base_url'};
	    $base =~ s,^https?://[^/]+,,;
	    $base =~ s,/*$,/,;

	    if ($path =~ m,^\Q$base\E(\Q$tree\E|)([+][^/]+|)(?:$|/)(.*),) {
		@$self{'tree', 'path'} = ($1.$2, $3);
		last;
	    }
	}
	unless ($$self{'tree'}) {
	    if ($ENV{'PATH_INFO'} =~ m,^/?([^/]+)/?(.*),) {
		@$self{'tree', 'path'} = ($1, $2);
	    }
	}

	$$self{'tree'} = $args{'query'}->param('tree') 
	    if $args{'query'}->param('tree');
    }
    if ($args{'tree'}) {
	$$self{'tree'} = $args{'tree'};
    }

    if ($$self{'tree'} =~ s/[+]([^+]*)$//) {
	$$self{'release'} = $1 if $1 ne '*';
    }

    if ($$self{'tree'} and $$self{'tree'} !~ /^[+]/) {
	my $tree = $$self{'tree'};
	die("No config for tree $tree") 
	    unless exists($$config{$tree});

	$$self{'config'} = $$config{$tree};
	$$self{'config'}{'usage'} ||= $$self{'config'}{'index'};
    }

    if (exists $$self{'params'}{'v'} and $$self{'params'}{'v'}) {
	$$self{'release'} ||= $$self{'params'}{'v'}[0];
	delete($$self{'params'}{'v'});
    }

    if ($$self{'config'}) {
	$$self{'release'} ||= $$self{'config'}{'ver_default'};
	$$self{'release'} ||= $$self{'config'}{'ver_list'}[0];
    }

    return $self;
}

sub read_config {
    my ($self) = @_;

    my $confpath = $LXRng::ROOT.'/lxrng.conf';

    if ($cached_config) {
	my @stat = stat($confpath);
	if (@stat and
	    $stat[9] == $cached_config_stat and 
	    time - $cached_config_age < 3600)
	{
	    return $cached_config;
	}
	$cached_config_stat = $stat[9];
	$cached_config_age = time;
    }

    if (open(my $cfgfile, $confpath)) {
	my @config = eval("use strict; use warnings;\n".
			  "#line 1 \"configuration file\"\n".
			  join("", <$cfgfile>));
	die($@) if $@;

	die("Bad configuration file format\n")
	    unless @config == 1 and ref($config[0]) eq 'HASH';

	$cached_config = $config[0];
	return $config[0];
    }
    else {
	die("Couldn't open configuration file \"$confpath\".");
    }
}

sub release {
    my ($self, $value) = @_;

    $$self{'release'} = $value if @_ == 2;
    return $$self{'release'};
}

sub default_release {
    my ($self, $value) = @_;

    return $$self{'config'}{'ver_default'};
}

sub all_releases {
    my ($self) = @_;

    return $$self{'config'}{'ver_list'};
}

sub param {
    my ($self, $key) = @_;
    my @res;

    @res = @{$$self{'params'}{$key}} if
	exists $$self{'params'}{$key};

    return wantarray ? @res : $res[0];
}

sub path {
    my ($self, $value) = @_;

    $$self{'path'} = $value if @_ == 2;
    return $$self{'path'};
}

sub tree {
    my ($self) = @_;

    return $$self{'tree'};
}

sub vtree {
    my ($self) = @_;

    if ($self->release ne $self->default_release) {
	return $self->tree.'+'.$self->release;
    }
    else {
	return $self->tree;
    }
}

sub path_elements {
    my ($self) = @_;

    return [] if $self->path =~ /^[ +]/;

    my @path;
    return [map {
	push(@path, $_); { 'node' => $_, 'path' => join('', @path) }
    } $self->path =~ m,([^/]+\/?),g];
}

sub config {
    my ($self) = @_;

    return $$self{'config'} || {};
}

sub prefs {
    my ($self) = @_;

    return $$self{'prefs'};
}

sub base_url {
    my ($self, $notree) = @_;

    my $base = $self->config->{'base_url'};
    unless ($base) {
	$base = $$self{'req_base'};
    }

    $base =~ s,/*$,/,;

    return $base if $notree;

    $base .= $self->vtree.'/';
    $base =~ s,/+$,/,;

    return $base;
}

sub args_url {
    my ($self, %args) = @_;

    # Todo: escape
    my $args = join(';', map { $_.'='.$args{$_} } keys %args);
    $args = '?'.$args if $args;
    return $args;
}
    
1;