1#
2# Common apache code
3# (sourced by apache)
4#
5# Author:	Alan Robertson
6#		Sun Jiang Dong
7#
8# Support:	users@clusterlabs.org
9#
10# License:	GNU General Public License (GPL)
11#
12# Copyright:	(C) 2002-2005 International Business Machines
13#
14
15source_envfiles() {
16	for f; do
17		[ -f "$f" -a -r "$f" ] &&
18			. "$f"
19	done
20}
21
22apachecat() {
23	awk '
24	function procline() {
25		split($0,a);
26		if( a[1]~/^[Ii]nclude$/ ) {
27			includedir=a[2];
28			gsub("\"","",includedir);
29			procinclude(includedir);
30		} else {
31			if( a[1]=="ServerRoot" ) {
32				rootdir=a[2];
33				gsub("\"","",rootdir);
34			}
35			print;
36		}
37	}
38	function printfile(infile, a) {
39		while( (getline<infile) > 0 ) {
40			procline();
41		}
42		close(infile);
43	}
44	function allfiles(dir, cmd,f) {
45		cmd="find -L "dir" -type f";
46		while( ( cmd | getline f ) > 0 ) {
47			printfile(f);
48		}
49		close(cmd);
50	}
51	function listfiles(pattern, cmd,f) {
52		cmd="ls "pattern" 2>/dev/null";
53		while( ( cmd | getline f ) > 0 ) {
54			printfile(f);
55		}
56		close(cmd);
57	}
58	function procinclude(spec) {
59		if( rootdir!="" && spec!~/^\// ) {
60			spec=rootdir"/"spec;
61		}
62		if( isdir(spec) ) {
63			allfiles(spec); # read all files in a directory (and subdirs)
64		} else {
65			listfiles(spec); # there could be jokers
66		}
67	}
68	function isdir(s) {
69		return !system("test -d \""s"\"");
70	}
71	{ procline(); }
72	' $1 |
73	sed 's/#.*//;s/[[:blank:]]*$//;s/^[[:blank:]]*//' |
74	grep -v '^$'
75}
76
77#
78# set parameters (as shell vars) from our apache config file
79#
80get_apache_params() {
81	configfile=$1
82	shift 1
83	vars=$(echo "$@" | sed 's/ /,/g')
84
85	eval `
86	apachecat $configfile | awk -v vars="$vars" '
87	BEGIN{
88		split(vars,v,",");
89		for( i in v )
90			vl[i]=tolower(v[i]);
91	}
92	{
93		for( i in v )
94			if( tolower($1)==vl[i] ) {
95			print v[i]"="$2
96			delete vl[i]
97			break
98		}
99	}
100	'`
101}
102
103#
104# Return the location(s) that are handled by the given handler
105#
106FindLocationForHandler() {
107	PerlScript='while (<>) {
108		/<Location "?([^ >"]+)/i && ($loc=$1);
109		'"/SetHandler +$2"'/i && print "$loc\n";
110	}'
111	apachecat $1 | perl -e "$PerlScript"
112}
113
114#
115# Check if the port is valid
116#
117CheckPort() {
118	ocf_is_decimal "$1" && [ $1 -gt 0 ]
119}
120
121buildlocalurl() {
122	[ "x$Listen" != "x" ] &&
123	echo "http://${Listen}" ||
124	echo "${LOCALHOST}:${PORT}"
125}
126# the test url may need a local prefix (as specified in the
127# apache Listen directive)
128fixtesturl() {
129	echo $test_url | grep -qs "^http" && return
130	test_url="`buildlocalurl`$test_url"
131}
132#
133# Get all the parameters we need from the Apache config file
134#
135GetParams() {
136	ConfigFile=$1
137	if [ ! -f $ConfigFile ]; then
138		return $OCF_ERR_INSTALLED
139	fi
140	get_apache_params $ConfigFile ServerRoot PidFile Port Listen
141	case $PidFile in
142		/*) ;;
143		[[:alnum:]]*) PidFile=$ServerRoot/$PidFile;;
144		*)
145			# If the PidFile is not set in the config, set
146			# a default location.
147			PidFile=$HA_VARRUNDIR/${httpd_basename}-${OCF_RESOURCE_INSTANCE}.pid
148			# Force the daemon to use this location by using
149			# the -c option, which adds the PidFile directive
150			# as if it was in the configuration file to begin with.
151			PIDFILE_DIRECTIVE="true"
152			;;
153	esac
154
155	for p in "$PORT" "$Port" 80; do
156		if CheckPort "$p"; then
157			PORT="$p"
158			break
159		fi
160	done
161
162	echo $Listen | grep ':' >/dev/null || # Listen could be just port spec
163		Listen="localhost:$Listen"
164
165	#
166	# It's difficult to figure out whether the server supports
167	# the status operation.
168	# (we start our server with -DSTATUS - just in case :-))
169	#
170	# Typically (but not necessarily) the status URL is /server-status
171	#
172	# For us to think status will work, we have to have the following things:
173	#
174	# - The server-status handler has to be mapped to some URL somewhere
175	#
176	# We assume that:
177	#
178	# - the "main" web server at $PORT will also support it if we can find it
179	#   somewhere in the file
180	# - it will be supported at the same URL as the one we find in the file
181	#
182	# If this doesn't work for you, then set the statusurl attribute.
183	#
184	if
185		 [ "X$STATUSURL" = "X" ]
186	then
187		StatusURL=`FindLocationForHandler $1 server-status | tail -1`
188		STATUSURL="`buildlocalurl`$StatusURL"
189	fi
190
191	if ! test "$PidFile"; then
192		return $OCF_ERR_INSTALLED
193	else
194		return $OCF_SUCCESS
195	fi
196}
197