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
)
50 val dl
= ErrorMsg
.dummyLoc
52 val _
= Main
.registerDefault ("WebNodes",
53 (TList (TBase
"node", dl
), dl
),
54 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes
), dl
)))
56 val _
= Main
.registerDefault ("SSL",
58 (fn () => (EVar
"false", dl
)))
60 val _
= Main
.registerDefault ("User",
61 (TBase
"your_user", dl
),
62 (fn () => (EString (Domain
.getUser ()), dl
)))
64 val _
= Main
.registerDefault ("Group",
65 (TBase
"your_group", dl
),
66 (fn () => (EString (Domain
.getUser ()), dl
)))
68 val _
= Main
.registerDefault ("DocumentRoot",
69 (TBase
"your_path", dl
),
70 (fn () => (EString ("/home/" ^ Domain
.getUser () ^
"/public_html"), dl
)))
72 val _
= Main
.registerDefault ("ServerAdmin",
74 (fn () => (EString (Domain
.getUser () ^
"@" ^ Config
.defaultDomain
), dl
)))
77 val redirect_code
= fn (EVar
"temp", _
) => SOME
"temp"
78 |
(EVar
"permanent", _
) => SOME
"permanent"
79 |
(EVar
"seeother", _
) => SOME
"seeother"
80 |
(EVar
"redir300", _
) => SOME
"300"
81 |
(EVar
"redir301", _
) => SOME
"301"
82 |
(EVar
"redir302", _
) => SOME
"302"
83 |
(EVar
"redir303", _
) => SOME
"303"
84 |
(EVar
"redir304", _
) => SOME
"304"
85 |
(EVar
"redir305", _
) => SOME
"305"
86 |
(EVar
"redir307", _
) => SOME
"307"
89 val flag
= fn (EVar
"redirect", _
) => SOME
"R"
90 |
(EVar
"forbidden", _
) => SOME
"F"
91 |
(EVar
"gone", _
) => SOME
"G"
92 |
(EVar
"last", _
) => SOME
"L"
93 |
(EVar
"chain", _
) => SOME
"C"
94 |
(EVar
"nosubreq", _
) => SOME
"NS"
95 |
(EVar
"nocase", _
) => SOME
"NC"
96 |
(EVar
"qsappend", _
) => SOME
"QSA"
97 |
(EVar
"noescape", _
) => SOME
"NE"
98 |
(EVar
"passthrough", _
) => SOME
"PT"
99 |
(EApp ((EVar
"mimeType", _
), e
), _
) =>
100 Option
.map (fn s
=> "T=" ^ s
) (Env
.string e
)
101 |
(EApp ((EVar
"redirectWith", _
), e
), _
) =>
102 Option
.map (fn s
=> "R=" ^ s
) (redirect_code e
)
103 |
(EApp ((EVar
"skip", _
), e
), _
) =>
104 Option
.map (fn n
=> "S=" ^
Int.toString n
) (Env
.int e
)
105 |
(EApp ((EApp ((EVar
"env", _
), e1
), _
), e2
), _
) =>
106 (case Env
.string e1
of
108 | SOME s1
=> Option
.map (fn s2
=> "E=" ^ s1 ^
":" ^ s2
)
113 val cond_flag
= fn (EVar
"cond_nocase", _
) => SOME
"NC"
114 |
(EVar
"ornext", _
) => SOME
"OR"
118 val vhostsChanged
= ref
false
120 val () = Slave
.registerPreHandler
121 (fn () => vhostsChanged
:= false)
123 val () = Slave
.registerFileHandler (fn fs
=>
125 val spl
= OS
.Path
.splitDirFile (#file fs
)
127 if String.isSuffix
".vhost" (#file spl
)
128 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
129 (vhostsChanged
:= true;
132 ignore (OS
.Process
.system (Config
.rm
134 ^ Config
.Apache
.confDir
138 ignore (OS
.Process
.system (Config
.cp
142 ^ Config
.Apache
.confDir
149 val () = Slave
.registerPostHandler
151 (if !vhostsChanged
then
152 Slave
.shellF ([Config
.Apache
.reload
],
153 fn cl
=> "Error reloading Apache with " ^ cl
)
157 val vhostFiles
: TextIO.outstream list ref
= ref
[]
158 fun write s
= app (fn file
=> TextIO.output (file
, s
)) (!vhostFiles
)
160 val rewriteEnabled
= ref
false
162 val () = Env
.containerV_one
"vhost"
166 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
168 val ssl
= Env
.env Env
.bool (env
, "SSL")
169 val user
= Env
.env Env
.string (env
, "User")
170 val group
= Env
.env Env
.string (env
, "Group")
171 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
172 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
174 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
175 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
177 rewriteEnabled
:= false;
178 vhostFiles
:= map (fn node
=>
180 val file
= Domain
.domainFile
{node
= node
,
183 TextIO.output (file
, "<VirtualHost ");
184 TextIO.output (file
, Domain
.nodeIp node
);
185 TextIO.output (file
, ":");
186 TextIO.output (file
, if ssl
then
190 TextIO.output (file
, ">\n");
194 write
"\tSuexecUserGroup ";
198 write
"\n\tDocumentRoot ";
200 write
"\n\tServerAdmin ";
204 fn () => (write
"</VirtualHost>\n";
205 app
TextIO.closeOut (!vhostFiles
)))
207 fun checkRewrite () =
208 if !rewriteEnabled
then
211 (write
"\tRewriteEngine on\n";
212 rewriteEnabled
:= true)
214 val () = Env
.action_three
"localProxyRewrite"
215 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
216 (fn (from
, to
, port
) =>
218 write
"\tRewriteRule\t";
220 write
"\thttp://localhost:";
221 write (Int.toString port
);
226 val () = Env
.action_two
"proxyPass"
227 ("from", Env
.string, "to", Env
.string)
229 (write
"\tProxyPass\t";
235 val () = Env
.action_two
"proxyPassReverse"
236 ("from", Env
.string, "to", Env
.string)
238 (write
"\tProxyPassReverse\t";
244 val () = Env
.action_three
"rewriteRule"
245 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
246 (fn (from
, to
, flags
) =>
248 write
"\tRewriteRule\t";
254 | flag
::rest
=> (write
" [";
256 app (fn flag
=> (write
",";
261 val () = Env
.action_three
"rewriteCond"
262 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
263 (fn (from
, to
, flags
) =>
265 write
"\tRewriteCond\t";
271 | flag
::rest
=> (write
" [";
273 app (fn flag
=> (write
",";