Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Ocaml server pages?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-10-03 (20:45)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Ocaml server pages?
On Fri, Oct 01, 2004 at 08:03:39PM +0200, Nicolas Cannasse wrote:
> > Note that there are at least 4 different ways to do web stuff with
> > OCaml, all with their own pros and cons.  At some point I intend to
> > write up a summary, but in the meantime, here are the three (other)
> > packages in no particular order.  You can go to their web pages and
> > read about them:
> Could you quickly sum up theses differences for this list readers ?
> I'm recently interested in high level server side languages.
> Any hint to show me what's "best" is apprecied.

I'm not going to claim that any of the four approaches is best, but I
will try to summarise them as far as my understanding goes.  Please
don't flame me too much for this; and do also note that I am the
author of mod_caml.

** mod_caml

mod_caml sits inside the Apache process, and its strengths and
limitations derive from this fact.  Because it's inside the process,
it has full access to the Apache API, which means that you can, for
example, make subrequests, or write an authentication handler, or
write a more sane URL mapping system than mod_rewrite, or [coming
soon, with Apache 2.0] use OCaml code to post-process the output of
some other CGI script written in another language.

It's completely tied to Apache.  You have no other webserver choice
once you've decided to use mod_caml.

Apache preforks itself, so there are actually several instances of
your code running, in separate processes, all working on separate HTTP

Because it uses Dynlink to load code, you can only use bytecode.  (I
would argue that most web applications are heavily IO bound, so this
isn't a problem.  But I can certainly imagine some compute-intensive
application, or part of an application, where the lack of native
compilation would be a serious shortcoming).

It doesn't use Gerd's Netcgi module, but instead uses its own which is
basically a reimplementation of Perl's in OCaml.  This is good
news for ex-Perl refugees like me, of course.  mod_caml itself is
independent of ocamlnet, but I personally use quite a lot of ocamlnet
functions.  The 2.0 release of COCANWIKI will depend on ocamlnet for
URL parsing, generating and parsing MIME emails, and maybe other

It comes with a template library to enforce correct separation of code
and HTML.  You can see this in action by grabbing the COCANWIKI 1.0
distribution from and comparing the
scripts/ and templates/ directories.

** ocamlnet & asxcaml

ocamlnet runs outside the web server, using either FastCGI or JSERV to
communicate with the web server.  (Or you can run your scripts as
ordinary CGI external programs, but I wouldn't recommend that for
serious use).  Your program is linked together into a single binary
which, when started, is a FastCGI or JSERV server, and processes
requests either sequentially or by forking a new process for each
request.  The webserver then acts as a proxy between the browser and
your program.

There are two important consequences of this: (1) You can use native
code. (2) Your whole program is compiled together a single unit.

There's no templating or database access, but you could easily add
these by using mod_caml's templating system or CamlTemplate, and
ocamldbi respectively.

AS/XCaml is built on top of ocamlnet, and I'm not going to claim to
fully understand it or even to have used it.  However the big feature
is the statically typed SQL implementation.  This is a big improvement
over ocamldbi (which can throw type errors at runtime if your database
returns a field with an unexpected type).  The reason I didn't do this
for ocamldbi was because it would involve fully parsing every variant
of SQL and/or limiting the complexity of SQL queries permitted.
AS/XCaml uses an XML file to describe your database schema, and from
this (I'm guessing) derives the schema and queries.  In concept this
is very similar to PDL

** mod_ocaml

This is another Apache module.  It's similar in concept to JSP or PHP.
You actually embed OCaml code into your webpage.  The webpage is then
compiled (with ocamlc) and run on the fly.  Your OCaml code prints

Having worked in a professional web applications outfit for a few
years, I can't in all conscience recommend embedding code into web
pages which designers might try to edit.


Richard Jones.
Merjis Ltd. - improving website return on investment
Learning Objective CAML for C, C++, Perl and Java programmers: