. .


ST 4U 197: A Simple HTTP Server in VA Smalltalk

February 22, 2012 7:40:27.519

Today's Smalltalk 4 You looks at implementing a very simple HTTP server in VA Smalltalk. If you do this yourself, you would actually want to create some objects, not just use the code snippet below. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

HTTP Server.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Today we'll take a look at setting up a simple HTTP server in VA Smalltalk. Bear in mind that for any "real" task you would want to create some classes of your own; this example will be enough to get you started. The code you'll want to use is below:

| server handler result |
handler := DirectedMessage
		[:request :response| |htmlStream htmlResponse|
			htmlStream := WriteStream
			on: (Locale current preferredStringClass new: 128).
			htmlStream nextPutAll: '
			request header sstHttpStringOn: htmlStream.
			htmlStream nextPutAll: '
'. htmlResponse := htmlStream contents. response header: (SstHttpResponseHeader new status: SstHttpConstants::HttpOk; version: request header version; contentLength: htmlResponse size; yourself); contents: htmlResponse; yourself] selector: #value:value:. (result := (server := SstBasicServer new) addHandlerUrl: 'http://localhost:9988') isSstError ifTrue: [result raise]. server requestHandler: handler. (result := server startUp) isSstError ifTrue: [result raise]. server inspect "-- When finished, send #shutDown"

This code does a couple of things. First, it uses DirectedMessage to set up a response handler - something to deal with each inbound request. In our case, it's a simple block that spits out some basic header type information as the body of the response. This is the part where you would want to customize; it's pretty easy to see how you could generalize this example for some ad hoc application status reporting

After that, we set up the actual server as an instance of SstBasicServer. Again, for an actual application, you'll want to create your own class for the server. In our case, we specify the port, some basic error handling (note the use of #isSstError), and start the server. As with HTTP client usage, we use #startUp - and eventually, when we want to shut it down, #shutDown.

Execute the code above - you should see something like this:


Now, open up a browser on your server (assuming you are running it now), and you should see something like this:


Finally, using the workspace attached to the inspector, send #shutDown to the server - you should see something like this:


Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , ,

[st4u197-iPhone.m4v ( Size: 5909667 )]

posted by James Robertson

 Share Tweet This