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
"rewrite_arg"
31 (CharVector
.all
Char.isAlphaNum
)
33 val dl
= ErrorMsg
.dummyLoc
35 val _
= Main
.registerDefault ("WebNodes",
36 (TList (TBase
"node", dl
), dl
),
37 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes
), dl
)))
39 val _
= Main
.registerDefault ("SSL",
41 (fn () => (EVar
"false", dl
)))
43 val _
= Main
.registerDefault ("User",
44 (TBase
"your_user", dl
),
45 (fn () => (EString (Domain
.getUser ()), dl
)))
47 val _
= Main
.registerDefault ("Group",
48 (TBase
"your_group", dl
),
49 (fn () => (EString (Domain
.getUser ()), dl
)))
51 val _
= Main
.registerDefault ("DocumentRoot",
52 (TBase
"your_path", dl
),
53 (fn () => (EString ("/home/" ^ Domain
.getUser () ^
"/public_html"), dl
)))
55 val _
= Main
.registerDefault ("ServerAdmin",
57 (fn () => (EString (Domain
.getUser () ^
"@" ^ Config
.defaultDomain
), dl
)))
60 val redirect_code
= fn (EVar
"temp", _
) => SOME
"temp"
61 |
(EVar
"permanent", _
) => SOME
"permanent"
62 |
(EVar
"seeother", _
) => SOME
"seeother"
63 |
(EVar
"redir300", _
) => SOME
"300"
64 |
(EVar
"redir301", _
) => SOME
"301"
65 |
(EVar
"redir302", _
) => SOME
"302"
66 |
(EVar
"redir303", _
) => SOME
"303"
67 |
(EVar
"redir304", _
) => SOME
"304"
68 |
(EVar
"redir305", _
) => SOME
"305"
69 |
(EVar
"redir307", _
) => SOME
"307"
72 val flag
= fn (EVar
"redirect", _
) => SOME
"R"
73 |
(EVar
"forbidden", _
) => SOME
"F"
74 |
(EVar
"gone", _
) => SOME
"G"
75 |
(EVar
"last", _
) => SOME
"L"
76 |
(EVar
"chain", _
) => SOME
"C"
77 |
(EVar
"nosubreq", _
) => SOME
"NS"
78 |
(EVar
"nocase", _
) => SOME
"NC"
79 |
(EVar
"qsappend", _
) => SOME
"QSA"
80 |
(EVar
"noescape", _
) => SOME
"NE"
81 |
(EVar
"passthrough", _
) => SOME
"PT"
82 |
(EApp ((EVar
"mimeType", _
), e
), _
) =>
83 Option
.map (fn s
=> "T=" ^ s
) (Env
.string e
)
84 |
(EApp ((EVar
"redirectWith", _
), e
), _
) =>
85 Option
.map (fn s
=> "R=" ^ s
) (redirect_code e
)
86 |
(EApp ((EVar
"skip", _
), e
), _
) =>
87 Option
.map (fn n
=> "S=" ^
Int.toString n
) (Env
.int e
)
88 |
(EApp ((EApp ((EVar
"env", _
), e1
), _
), e2
), _
) =>
89 (case Env
.string e1
of
91 | SOME s1
=> Option
.map (fn s2
=> "E=" ^ s1 ^
":" ^ s2
)
97 val vhostsChanged
= ref
false
99 val () = Slave
.registerPreHandler
100 (fn () => vhostsChanged
:= false)
102 val () = Slave
.registerFileHandler (fn fs
=>
104 val spl
= OS
.Path
.splitDirFile (#file fs
)
106 if String.isSuffix
".vhost" (#file spl
)
107 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
108 (vhostsChanged
:= true;
111 ignore (OS
.Process
.system (Config
.rm
113 ^ Config
.Apache
.confDir
117 ignore (OS
.Process
.system (Config
.cp
121 ^ Config
.Apache
.confDir
128 val () = Slave
.registerPostHandler
130 (if !vhostsChanged
then
131 Slave
.shellF ([Config
.Apache
.reload
],
132 fn cl
=> "Error reloading Apache with " ^ cl
)
136 val vhostFiles
: TextIO.outstream list ref
= ref
[]
137 fun write s
= app (fn file
=> TextIO.output (file
, s
)) (!vhostFiles
)
139 val rewriteEnabled
= ref
false
141 val () = Env
.containerV_one
"vhost"
145 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
147 val ssl
= Env
.env Env
.bool (env
, "SSL")
148 val user
= Env
.env Env
.string (env
, "User")
149 val group
= Env
.env Env
.string (env
, "Group")
150 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
151 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
153 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
154 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
156 rewriteEnabled
:= false;
157 vhostFiles
:= map (fn node
=>
159 val file
= Domain
.domainFile
{node
= node
,
162 TextIO.output (file
, "<VirtualHost ");
163 TextIO.output (file
, Domain
.nodeIp node
);
164 TextIO.output (file
, ":");
165 TextIO.output (file
, if ssl
then
169 TextIO.output (file
, ">\n");
173 write
"\tSuexecUserGroup ";
177 write
"\n\tDocumentRoot ";
179 write
"\n\tServerAdmin ";
183 fn () => (write
"</VirtualHost>\n";
184 app
TextIO.closeOut (!vhostFiles
)))
186 fun checkRewrite () =
187 if !rewriteEnabled
then
190 (write
"\tRewriteEngine on\n";
191 rewriteEnabled
:= true)
193 val () = Env
.action_three
"localProxyRewrite"
194 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
195 (fn (from
, to
, port
) =>
197 write
"\tRewriteRule\t";
199 write
"\thttp://localhost:";
200 write (Int.toString port
);
205 val () = Env
.action_three
"localProxyPass"
206 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
207 (fn (from
, to
, port
) =>
212 | _
=> if String.sub (to
, 0) = #
"/" then
217 write
"\tProxyPass\t";
219 write
"\thttp://localhost:";
220 write (Int.toString port
);
225 val () = Env
.action_three
"localProxyPassReverse"
226 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
227 (fn (from
, to
, port
) =>
232 | _
=> if String.sub (to
, 0) = #
"/" then
237 write
"\tProxyPassReverse\t";
239 write
"\thttp://localhost:";
240 write (Int.toString port
);
245 val () = Env
.action_three
"rewriteRule"
246 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
247 (fn (from
, to
, flags
) =>
249 write
"\tRewriteRule\t";
255 | flag
::rest
=> (write
" [";
257 app (fn flag
=> (write
",";