Web Overview
A web server is just a machine connected to the Internet, running a computer program that listens to requests for content, such as HTML files. Those requests come from client programs, such as browsers, running on other computers also connected to the Internet. For this to work, there has to be an agreed language for both the requests and what's sent back. That language is the HyperText Transfer Protocol (HTTP).
Rather than repeat what's been written many times, I refer you to
- How the Internet Works for a basic description of many aspects of how the web works.
- HTTP Made Really Easy by James Marshall, which focusses on the basics of HTTP in particular.
Web Servers with Lisp
The nice thing about the web is that all communication between programs is done using HTTP, which uses simple commands in plain text. That simplicity means that you can write a web server or browser in any language you like, as long as it lets you read and write to the network.
Writing a server or browser from scratch would be pretty tedious, though. Fortunately, many languages now have code libraries to take care of such details as reading and writing characters to the network, parsing HTTP commands, creating threads (lightweight parallel processes) for each request to support simultaneous users, and so on.
For Common Lisp there are several major code libraries for web services:
- Franz, Inc.'s AServe library for Allegro Common Lisp for PC's running Windows and Unix.
- Hunchentoot, a modern popular Lisp web server, used in many open-source projects.
In this course, we will use Franz, Inc.'s AServe library, because:
- Most students are using Allegro, so they already have AServe available and ready to load.
- AServe is very similar to HTTP libraries for other languages. For example, AServe's responses and entities are much like Java's requests and responses.
- AServe's HTML macro is simple to explain, easy to implement, and extends automatically to new HTML elements and XML.
- AServe includes Webactions, a framework for dynamic web pages that's friendlier to HTML designers and somewhat similar to frameworks in other languages like PHP, JSP, and ASP.
For those students not using Allegro, there is a portable AServe library that runs in a number of Common Lisp's. I use it in LispWorks Personal Edition.
How to Test AServe
If you are using the CS325 starter files, you already have AServe loaded and ready to use. If not,
- Allegro Users: just type
(require :aserve)
- Others: Use QuickLisp
(ql:quickload "aserve")
Then do the following to run the test code:
- load the example AServe code in
test-aserve.lisp
- start the server by evaluating
(net.aserve:start :port 8000)
- point a browser to http://localhost:8000/
8000 is a port. Ports are simply numbers operating systems use
as a kind of address to determine which program should process a
network message. Low numbers are pre-assigned to standard services, e.g.,
80 is the default for a web server. For development, use a safe high number.
In the unlikely case that your machine is already using port 8000
for some service, try some other number over 1024. Use that number in
your localhost
URL.
If you have a firewall running on your machine, and you should, the first time you start the server, you may get an alert saying that your Lisp is trying to access the network. Tell your firewall this is OK.
The URL http://localhost:8000/ should take you to the default AllegroServe page. If it doesn't, look at the error message you got in the browser.
- If the error says a connection could not be made, then make sure AServe started without error, that you gave the right port number, and that your firewall, if any, isn't rejecting messages on that port.
- If the errors says the page could not be found, then
the server is running. Try loading
test-aserve.lisp
. again and make sure there are no errors.
test-aserve.lisp
.
defines the following example pages:
- http://localhost:8000/apropos should take you to a page that lets you look up all symbols defined in your Lisp
- http://localhost:8000/base/fields.html you to a simple example of an HTML form that can be filled in and sent to Lisp for processing by Lisp code
- http://localhost:8000/critic should take you to a page where you can submit Lisp code to the Lisp Critic in a web page
These are all very simple examples. You can look at the code in
test-aserve.lisp
to see how AServe is used. In this class, however,
we will use Webactions.
Webactions is a framework for web sites built on top of, and included
with, AServe. For information on using
Webactions in this class, see
this writeup.
Shut Down AServe
While your server is running, any machine on the Internet can
access your pages. To access your server from another machine,
replace localhost
with your server's Internet name or IP address.
For example, if your machine's IP address was
555.100.10.100, then you could critique Lisp by
typing this URL in your browser:
http://555.100.10.100:8000/critic
Note: if you have a private IP address, you will need to get and use the public one to access your server. To see your public address, click here.
To avoid hackers attacking your system, and to reduce system load, shut down AServe when testing is done:
(net.aserve:shutdown)
Quitting Lisp will also shutdown the server.
For more examples, see:
- the AServe tutorial
- the AServe reference
- Web Programming with AllegroServe, a chapter from Peter Seibel's Practical Common Lisp