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 _
= Defaults
.registerDefault ("WebNodes",
65 (TList (TBase
"node", dl
), dl
),
66 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes
), dl
)))
68 val _
= Defaults
.registerDefault ("SSL",
70 (fn () => (EVar
"false", dl
)))
72 val _
= Defaults
.registerDefault ("User",
73 (TBase
"your_user", dl
),
74 (fn () => (EString (Domain
.getUser ()), dl
)))
76 val _
= Defaults
.registerDefault ("Group",
77 (TBase
"your_group", dl
),
78 (fn () => (EString (Domain
.getUser ()), dl
)))
80 val _
= Defaults
.registerDefault ("DocumentRoot",
81 (TBase
"your_path", dl
),
82 (fn () => (EString (Config
.homeBase ^
"/" ^ Domain
.getUser () ^
"/public_html"), dl
)))
84 val _
= Defaults
.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"
129 val apache_option
= fn (EVar
"execCGI", _
) => SOME
"ExecCGI"
130 |
(EVar
"includesNOEXEC", _
) => SOME
"IncludesNOEXEC"
131 |
(EVar
"indexes", _
) => SOME
"Indexes"
134 val autoindex_width
= fn (EVar
"autofit", _
) => SOME
"*"
135 |
(EApp ((EVar
"characters", _
), n
), _
) =>
136 Option
.map
Int.toString (Env
.int n
)
139 val autoindex_option
= fn (EApp ((EVar
"descriptionWidth", _
), w
), _
) =>
140 Option
.map (fn w
=> ("DescriptionWidth", SOME w
))
142 |
(EVar
"fancyIndexing", _
) => SOME ("FancyIndexing", NONE
)
143 |
(EVar
"foldersFirst", _
) => SOME ("FoldersFirst", NONE
)
144 |
(EVar
"htmlTable", _
) => SOME ("HTMLTable", NONE
)
145 |
(EVar
"iconsAreLinks", _
) => SOME ("IconsAreLinks", NONE
)
146 |
(EApp ((EVar
"iconHeight", _
), n
), _
) =>
147 Option
.map (fn w
=> ("IconHeight", SOME (Int.toString w
)))
149 |
(EApp ((EVar
"iconWidth", _
), n
), _
) =>
150 Option
.map (fn w
=> ("IconWidth", SOME (Int.toString w
)))
152 |
(EVar
"ignoreCase", _
) => SOME ("IgnoreCase", NONE
)
153 |
(EVar
"ignoreClient", _
) => SOME ("IgnoreClient", NONE
)
154 |
(EApp ((EVar
"nameWidth", _
), w
), _
) =>
155 Option
.map (fn w
=> ("NameWidth", SOME w
))
157 |
(EVar
"scanHtmlTitles", _
) => SOME ("ScanHTMLTitles", NONE
)
158 |
(EVar
"suppressColumnSorting", _
) => SOME ("SuppressColumnSorting", NONE
)
159 |
(EVar
"suppressDescription", _
) => SOME ("SuppressDescription", NONE
)
160 |
(EVar
"suppressHtmlPreamble", _
) => SOME ("SuppressHTMLPreamble", NONE
)
161 |
(EVar
"suppressIcon", _
) => SOME ("SuppressIcon", NONE
)
162 |
(EVar
"suppressLastModified", _
) => SOME ("SuppressLastModified", NONE
)
163 |
(EVar
"suppressRules", _
) => SOME ("SuppressRules", NONE
)
164 |
(EVar
"suppressSize", _
) => SOME ("SuppressSize", NONE
)
165 |
(EVar
"trackModified", _
) => SOME ("TrackModified", NONE
)
166 |
(EVar
"versionSort", _
) => SOME ("VersionSort", NONE
)
167 |
(EVar
"xhtml", _
) => SOME ("XHTML", NONE
)
171 val vhostsChanged
= ref
false
173 val () = Slave
.registerPreHandler
174 (fn () => vhostsChanged
:= false)
176 val () = Slave
.registerFileHandler (fn fs
=>
178 val spl
= OS
.Path
.splitDirFile (#file fs
)
180 if String.isSuffix
".vhost" (#file spl
)
181 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
182 (vhostsChanged
:= true;
185 (ignore (OS
.Process
.system (Config
.rm
187 ^ Config
.Apache
.confDir
190 ignore (OS
.Process
.system (Config
.rm
192 ^ Config
.Apache
.logDir
194 ^ #
base (OS
.Path
.splitBaseExt
198 ignore (OS
.Process
.system (Config
.cp
202 ^ Config
.Apache
.confDir
209 val () = Slave
.registerPostHandler
211 (if !vhostsChanged
then
212 Slave
.shellF ([Config
.Apache
.reload
],
213 fn cl
=> "Error reloading Apache with " ^ cl
)
217 val vhostFiles
: TextIO.outstream list ref
= ref
[]
218 fun write s
= app (fn file
=> TextIO.output (file
, s
)) (!vhostFiles
)
220 val rewriteEnabled
= ref
false
221 val currentVhost
= ref
""
222 val currentVhostId
= ref
""
224 val pre
= ref (fn _
: {nodes
: string list
, id
: string, hostname
: string} => ())
229 pre
:= (fn x
=> (old x
; f x
))
232 val post
= ref (fn () => ())
237 post
:= (fn () => (old (); f ()))
240 val aliaser
= ref (fn _
: string => ())
241 fun registerAliaser f
=
245 aliaser
:= (fn x
=> (old x
; f x
))
248 val () = Env
.containerV_one
"vhost"
252 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
254 val ssl
= Env
.env Env
.bool (env
, "SSL")
255 val user
= Env
.env Env
.string (env
, "User")
256 val group
= Env
.env Env
.string (env
, "Group")
257 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
258 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
260 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
261 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
262 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
264 currentVhost
:= fullHost
;
265 currentVhostId
:= vhostId
;
267 rewriteEnabled
:= false;
268 vhostFiles
:= map (fn node
=>
270 val file
= Domain
.domainFile
{node
= node
,
273 TextIO.output (file
, "<VirtualHost ");
274 TextIO.output (file
, Domain
.nodeIp node
);
275 TextIO.output (file
, ":");
276 TextIO.output (file
, if ssl
then
280 TextIO.output (file
, ">\n");
284 write
"\tServerName ";
286 write
"\n\tSuexecUserGroup ";
290 write
"\n\tDocumentRoot ";
292 write
"\n\tServerAdmin ";
294 write
"\n\tErrorLog ";
295 write Config
.Apache
.logDir
;
298 write
"/error.log\n\tCustomLog ";
299 write Config
.Apache
.logDir
;
302 write
"/access.log combined\n";
303 !pre
{nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
306 write
"</VirtualHost>\n";
307 app
TextIO.closeOut (!vhostFiles
)))
309 val () = Env
.container_one
"location"
310 ("prefix", Env
.string)
312 (write
"\t<Location ";
315 fn () => write
"\t</Location>\n")
317 val () = Env
.container_one
"directory"
318 ("directory", Env
.string)
320 (write
"\t<Directory ";
323 fn () => write
"\t</Directory>\n")
325 fun checkRewrite () =
326 if !rewriteEnabled
then
329 (write
"\tRewriteEngine on\n";
330 rewriteEnabled
:= true)
332 val () = Env
.action_three
"localProxyRewrite"
333 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
334 (fn (from
, to
, port
) =>
336 write
"\tRewriteRule\t";
338 write
"\thttp://localhost:";
339 write (Int.toString port
);
344 val () = Env
.action_two
"proxyPass"
345 ("from", Env
.string, "to", Env
.string)
347 (write
"\tProxyPass\t";
353 val () = Env
.action_two
"proxyPassReverse"
354 ("from", Env
.string, "to", Env
.string)
356 (write
"\tProxyPassReverse\t";
362 val () = Env
.action_three
"rewriteRule"
363 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
364 (fn (from
, to
, flags
) =>
366 write
"\tRewriteRule\t";
372 | flag
::rest
=> (write
" [";
374 app (fn flag
=> (write
",";
379 val () = Env
.action_three
"rewriteCond"
380 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
381 (fn (from
, to
, flags
) =>
383 write
"\tRewriteCond\t";
389 | flag
::rest
=> (write
" [";
391 app (fn flag
=> (write
",";
396 val () = Env
.action_one
"rewriteLogLevel"
400 write
"\tRewriteLog ";
401 write Config
.Apache
.logDir
;
403 write (!currentVhostId
);
404 write
"/rewrite.log\n\tRewriteLogLevel ";
405 write (Int.toString level
);
408 val () = Env
.action_two
"alias"
409 ("from", Env
.string, "to", Env
.string)
417 val () = Env
.action_two
"scriptAlias"
418 ("from", Env
.string, "to", Env
.string)
420 (write
"\tScriptAlias\t";
426 val () = Env
.action_two
"errorDocument"
427 ("code", Env
.string, "handler", Env
.string)
428 (fn (code
, handler
) =>
429 (write
"\tErrorDocument\t";
435 val () = Env
.action_one
"options"
436 ("options", Env
.list apache_option
)
440 | _
=> (write
"\tOptions";
441 app (fn opt
=> (write
" "; write opt
)) opts
;
444 val () = Env
.action_one
"set_options"
445 ("options", Env
.list apache_option
)
449 | _
=> (write
"\tOptions";
450 app (fn opt
=> (write
" +"; write opt
)) opts
;
453 val () = Env
.action_one
"unset_options"
454 ("options", Env
.list apache_option
)
458 | _
=> (write
"\tOptions";
459 app (fn opt
=> (write
" -"; write opt
)) opts
;
462 val () = Env
.action_one
"directoryIndex"
463 ("filenames", Env
.list Env
.string)
465 (write
"\tDirectoryIndex";
466 app (fn opt
=> (write
" "; write opt
)) opts
;
469 val () = Env
.action_one
"serverAlias"
472 (write
"\tServerAlias ";
477 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
478 |
(EVar
"digest", _
) => SOME
"digest"
481 val () = Env
.action_one
"authType"
484 (write
"\tAuthType ";
488 val () = Env
.action_one
"authName"
491 (write
"\tAuthName \"";
495 val () = Env
.action_one
"authUserFile"
498 (write
"\tAuthUserFile ";
502 val () = Env
.action_none
"requireValidUser"
503 (fn () => write
"\tRequire valid-user\n")
505 val () = Env
.action_one
"requireUser"
506 ("users", Env
.list Env
.string)
510 | _
=> (write
"\tRequire user";
511 app (fn name
=> (write
" "; write name
)) names
;
514 val () = Env
.action_one
"requireGroup"
515 ("groups", Env
.list Env
.string)
519 | _
=> (write
"\tRequire group";
520 app (fn name
=> (write
" "; write name
)) names
;
523 val () = Env
.action_none
"orderAllowDeny"
524 (fn () => write
"\tOrder allow,deny\n")
526 val () = Env
.action_none
"orderDenyAllow"
527 (fn () => write
"\tOrder deny,allow\n")
529 val () = Env
.action_none
"allowFromAll"
530 (fn () => write
"\tAllow from all\n")
532 val () = Env
.action_one
"allowFrom"
533 ("entries", Env
.list Env
.string)
537 | _
=> (write
"\tAllow from";
538 app (fn name
=> (write
" "; write name
)) names
;
541 val () = Env
.action_none
"denyFromAll"
542 (fn () => write
"\tDeny from all\n")
544 val () = Env
.action_one
"denyFrom"
545 ("entries", Env
.list Env
.string)
549 | _
=> (write
"\tDeny from";
550 app (fn name
=> (write
" "; write name
)) names
;
553 val () = Env
.action_none
"satisfyAll"
554 (fn () => write
"\tSatisfy all\n")
556 val () = Env
.action_none
"satisfyAny"
557 (fn () => write
"\tSatisfy any\n")
559 val () = Env
.action_one
"forceType"
561 (fn ty
=> (write
"\tForceType ";
565 val () = Env
.action_none
"forceTypeOff"
566 (fn () => write
"\tForceType None\n")
568 val () = Env
.action_two
"action"
569 ("what", Env
.string, "how", Env
.string)
570 (fn (what
, how
) => (write
"\tAction ";
576 val () = Env
.action_one
"addDefaultCharset"
577 ("charset", Env
.string)
578 (fn ty
=> (write
"\tAddDefaultCharset ";
582 val () = Env
.action_one
"davSvn"
584 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
588 val () = Env
.action_one
"authzSvnAccessFile"
590 (fn path
=> (write
"\tAuthzSVNAccessFile ";
594 val () = Env
.action_two
"addDescription"
595 ("description", Env
.string, "patterns", Env
.list Env
.string)
599 | _
=> (write
"\tAddDescription \"";
600 write (String.toString desc
);
602 app (fn pat
=> (write
" "; write pat
)) pats
;
605 val () = Env
.action_one
"indexOptions"
606 ("options", Env
.list autoindex_option
)
610 | _
=> (write
"\tIndexOptions";
611 app (fn (opt
, arg
) =>
614 Option
.app (fn arg
=>
615 (write
"="; write arg
)) arg
)) opts
;
618 val () = Env
.action_one
"set_indexOptions"
619 ("options", Env
.list autoindex_option
)
623 | _
=> (write
"\tIndexOptions";
624 app (fn (opt
, arg
) =>
627 Option
.app (fn arg
=>
628 (write
"="; write arg
)) arg
)) opts
;
631 val () = Env
.action_one
"unset_indexOptions"
632 ("options", Env
.list autoindex_option
)
636 | _
=> (write
"\tIndexOptions";
642 val () = Env
.action_one
"headerName"
644 (fn name
=> (write
"\tHeaderName ";
648 val () = Env
.action_one
"readmeName"
650 (fn name
=> (write
"\tReadmeName ";