1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
4 * This program is free software
; you can redistribute it
and/or
5 * modify it under the terms
of the GNU General Public License
6 * as published by the Free Software Foundation
; either version
2
7 * of the License
, or (at your option
) any later version
.
9 * This program is distributed
in the hope that it will be useful
,
10 * but WITHOUT ANY WARRANTY
; without even the implied warranty
of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
. See the
12 * GNU General Public License for more details
.
14 * You should have received a copy
of the GNU General Public License
15 * along
with this program
; if not
, write to the Free Software
16 * Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
02110-1301, USA
.
19 (* Apache HTTPD handling
*)
21 structure Apache
:> APACHE
= struct
25 val _
= Env
.type_one
"proxy_port"
29 val _
= Env
.type_one
"proxy_target"
33 fun default () = List.exists (fn s
' => s
= s
') Config
.Apache
.proxyTargets
35 case String.fields (fn ch
=> ch
= #
":") s
of
36 ["http", "//localhost", rest
] =>
37 (case String.fields (fn ch
=> ch
= #
"/") rest
of
39 (case Int.fromString port
of
41 | SOME n
=> n
> 1024 orelse default ())
46 val _
= Env
.type_one
"rewrite_arg"
48 (CharVector
.all
Char.isAlphaNum
)
51 size s
> 0 andalso size s
< 1000 andalso CharVector
.all
52 (fn ch
=> Char.isAlphaNum ch
58 val _
= Env
.type_one
"location"
62 val dl
= ErrorMsg
.dummyLoc
64 val _
= Main
.registerDefault ("WebNodes",
65 (TList (TBase
"node", dl
), dl
),
66 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes
), dl
)))
68 val _
= Main
.registerDefault ("SSL",
70 (fn () => (EVar
"false", dl
)))
72 val _
= Main
.registerDefault ("User",
73 (TBase
"your_user", dl
),
74 (fn () => (EString (Domain
.getUser ()), dl
)))
76 val _
= Main
.registerDefault ("Group",
77 (TBase
"your_group", dl
),
78 (fn () => (EString (Domain
.getUser ()), dl
)))
80 val _
= Main
.registerDefault ("DocumentRoot",
81 (TBase
"your_path", dl
),
82 (fn () => (EString ("/home/" ^ Domain
.getUser () ^
"/public_html"), dl
)))
84 val _
= Main
.registerDefault ("ServerAdmin",
86 (fn () => (EString (Domain
.getUser () ^
"@" ^ Config
.defaultDomain
), dl
)))
89 val redirect_code
= fn (EVar
"temp", _
) => SOME
"temp"
90 |
(EVar
"permanent", _
) => SOME
"permanent"
91 |
(EVar
"seeother", _
) => SOME
"seeother"
92 |
(EVar
"redir300", _
) => SOME
"300"
93 |
(EVar
"redir301", _
) => SOME
"301"
94 |
(EVar
"redir302", _
) => SOME
"302"
95 |
(EVar
"redir303", _
) => SOME
"303"
96 |
(EVar
"redir304", _
) => SOME
"304"
97 |
(EVar
"redir305", _
) => SOME
"305"
98 |
(EVar
"redir307", _
) => SOME
"307"
101 val flag
= fn (EVar
"redirect", _
) => SOME
"R"
102 |
(EVar
"forbidden", _
) => SOME
"F"
103 |
(EVar
"gone", _
) => SOME
"G"
104 |
(EVar
"last", _
) => SOME
"L"
105 |
(EVar
"chain", _
) => SOME
"C"
106 |
(EVar
"nosubreq", _
) => SOME
"NS"
107 |
(EVar
"nocase", _
) => SOME
"NC"
108 |
(EVar
"qsappend", _
) => SOME
"QSA"
109 |
(EVar
"noescape", _
) => SOME
"NE"
110 |
(EVar
"passthrough", _
) => SOME
"PT"
111 |
(EApp ((EVar
"mimeType", _
), e
), _
) =>
112 Option
.map (fn s
=> "T=" ^ s
) (Env
.string e
)
113 |
(EApp ((EVar
"redirectWith", _
), e
), _
) =>
114 Option
.map (fn s
=> "R=" ^ s
) (redirect_code e
)
115 |
(EApp ((EVar
"skip", _
), e
), _
) =>
116 Option
.map (fn n
=> "S=" ^
Int.toString n
) (Env
.int e
)
117 |
(EApp ((EApp ((EVar
"env", _
), e1
), _
), e2
), _
) =>
118 (case Env
.string e1
of
120 | SOME s1
=> Option
.map (fn s2
=> "E=" ^ s1 ^
":" ^ s2
)
125 val cond_flag
= fn (EVar
"cond_nocase", _
) => SOME
"NC"
126 |
(EVar
"ornext", _
) => SOME
"OR"
130 val vhostsChanged
= ref
false
132 val () = Slave
.registerPreHandler
133 (fn () => vhostsChanged
:= false)
135 val () = Slave
.registerFileHandler (fn fs
=>
137 val spl
= OS
.Path
.splitDirFile (#file fs
)
139 if String.isSuffix
".vhost" (#file spl
)
140 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
141 (vhostsChanged
:= true;
144 ignore (OS
.Process
.system (Config
.rm
146 ^ Config
.Apache
.confDir
150 ignore (OS
.Process
.system (Config
.cp
154 ^ Config
.Apache
.confDir
161 val () = Slave
.registerPostHandler
163 (if !vhostsChanged
then
164 Slave
.shellF ([Config
.Apache
.reload
],
165 fn cl
=> "Error reloading Apache with " ^ cl
)
169 val vhostFiles
: TextIO.outstream list ref
= ref
[]
170 fun write s
= app (fn file
=> TextIO.output (file
, s
)) (!vhostFiles
)
172 val rewriteEnabled
= ref
false
174 val () = Env
.containerV_one
"vhost"
178 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
180 val ssl
= Env
.env Env
.bool (env
, "SSL")
181 val user
= Env
.env Env
.string (env
, "User")
182 val group
= Env
.env Env
.string (env
, "Group")
183 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
184 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
186 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
187 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
189 rewriteEnabled
:= false;
190 vhostFiles
:= map (fn node
=>
192 val file
= Domain
.domainFile
{node
= node
,
195 TextIO.output (file
, "<VirtualHost ");
196 TextIO.output (file
, Domain
.nodeIp node
);
197 TextIO.output (file
, ":");
198 TextIO.output (file
, if ssl
then
202 TextIO.output (file
, ">\n");
206 write
"\tSuexecUserGroup ";
210 write
"\n\tDocumentRoot ";
212 write
"\n\tServerAdmin ";
216 fn () => (write
"</VirtualHost>\n";
217 app
TextIO.closeOut (!vhostFiles
)))
219 val () = Env
.container_one
"location"
220 ("prefix", Env
.string)
222 (write
"\t<Location ";
225 fn () => write
"\t</Location>\n")
227 val () = Env
.container_one
"directory"
228 ("directory", Env
.string)
230 (write
"\t<Directory ";
233 fn () => write
"\t</Directory>\n")
235 fun checkRewrite () =
236 if !rewriteEnabled
then
239 (write
"\tRewriteEngine on\n";
240 rewriteEnabled
:= true)
242 val () = Env
.action_three
"localProxyRewrite"
243 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
244 (fn (from
, to
, port
) =>
246 write
"\tRewriteRule\t";
248 write
"\thttp://localhost:";
249 write (Int.toString port
);
254 val () = Env
.action_two
"proxyPass"
255 ("from", Env
.string, "to", Env
.string)
257 (write
"\tProxyPass\t";
263 val () = Env
.action_two
"proxyPassReverse"
264 ("from", Env
.string, "to", Env
.string)
266 (write
"\tProxyPassReverse\t";
272 val () = Env
.action_three
"rewriteRule"
273 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
274 (fn (from
, to
, flags
) =>
276 write
"\tRewriteRule\t";
282 | flag
::rest
=> (write
" [";
284 app (fn flag
=> (write
",";
289 val () = Env
.action_three
"rewriteCond"
290 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
291 (fn (from
, to
, flags
) =>
293 write
"\tRewriteCond\t";
299 | flag
::rest
=> (write
" [";
301 app (fn flag
=> (write
",";