It served web pages and JSON dynamically generated by DWScript, using an SQLite database for storage. It ran on a Windows 2008 R2 Standard installation, minimized so that OS+Server stayed around 256 MB RAM.
I didn’t mention it much before I grew confident that, provided you’re careful, you can indeed run a Windows-based, high-performance Pascal-based web server at a very low cost, even lower than most Linux-based VPS and with a higher performance than the cheaper Linux options.
DWScript Web Server
- It makes use of the SimpleServer library, which leverages an HTTP.SYS 2.0 server (based on Synopse mORMot).
- It serves pages through the TdwsHTMLFilter component, which allows to intermingle Pascal with HTML/CSS/JS in a way similar to what PHP does.
- It implements a multi-threaded compiled script cache, which is automatically updated when website source files are changed.
- It has multiple options to limit CPU usage, bandwidth, etc. which help make it safe to run in a metered environment (where you want to cap your costs) or in a shared environment (where you don’t want a service using all available resources)
The executable can run both as an application and a windows service. Though it can be used on its own merits, it’s more intended as a starting point/demonstrator.
The HTTP Server API is is the foundation upon which IIS or WCF are built, it’s a low-level, high-performance server layer that allows multiple services to share the same http port. Security-wise, for the wild web it handles SSL, and for intranets, it can handle NTLM and Kerberos domain authentication.
What this means in practice is that you can use the DWS Web Server to handle just a portion of traffic of a website, leaving the rest to IIS, WCF services, or other HTTP.SYS-capable servers like mORMot. On an Entreprise network, it means you benefit from Windows Domain Single-Sign On for all three major browsers (Chrome, FireFox, and Internet Explorer).
To use http.sys, there are some restriction which involve administrative rights at least at some point. To run the server executable you need to ensure at least one of the following:
- run it as an application with administrator rights
- run it as a service (you need administrator rights to install the service)
- run it as an application as a normal user (after having registered the url acl)
For the last option, you’ll need to use the netsh http add urlacl command. To enable SSL, you’ll need to register your certificate (for testing purposes, you can get a free SSL certificate from StartSSL).
Though it has ‘html’ in its name, the filter can be used for generating just anything really (css, js, json, xml, or even Pascal). In the DWS Web Server, it’s paired with the WebEnvironment module, which if still limited, provides access to HTTP request and response details.
It’s named “filter” because it works by turning a “.dws” file into a Pascal program, which will then be executed. For instance the following “.dws” source
<h1><?pas=FormatDateTime("yyyy-mm-dd", Now)?></h1> <p><?pas for var i := 1 to 3 do PrintLn(i*10); ?></p>
would be filtered to the following Pascal source
Print('<h1>'); Print(FormatDateTime("yyyy-mm-dd", Now); Print('</h1>'#13#10'<p>'); for var i := 1 to 3 do PrintLn(i*10); Print('</p>');
which if executed on the 13th of August 2013 would generate
<h1>2013-08-13</h1> <p>10 20 30 </p>
So to summarize <?pas ?> delimiters encapsulates Pascal statements, and <?pas= ?> encapsulate a Pascal expression. The delimiters can be adjusted in the TdwsHTMLFilter component, and besides being powerful, the whole filtering+script combination is very competitive performance-wise.
The TdwsHtmlFilter got various performance enhancement, but mostly dates back to the early days of DWScript, so it’s a kinda the original essence of DWScript programming.
You can easily make a syntax highlighting editor for .dws files with SynEdit, use a TSynMultiSyn component to combine the HTML highlighter with the DWS highlighter (and probably the JS and CSS highlighter as well, if you want a complete solution).
In the sample web server, pages (aka scripts) are compiled only once, then executed multiple times, with parallel executions of the same page being possible. The engine is coupled with http.sys through IOCP.
A TdwsFileNotifier (which also uses IOCP) watches the website directory and invalidates compiled scripts when necessary, this means you can edit the .dws & .pas files, and just hit F5 in your browser to see the result.
While these provide a lot of capability, you can and should of course use your own modules as well to expose business or other functionality to your scripts.
Since DWS is under a sand-boxed model, script users will only be able to access what functionality you expose them. The engine is also aiming at safeguarding for issues that might plague ISAPI DLLs or native Delphi-based servers, such as dangling pointers, memory leaks or infinite loops.