AjTalk In C# (2): A Simple Web Server

Previous Post
Next Post

I’m working completing my Smalltalk VM, written in C#, AjTalk, repo at https://github.com/ajlopez/AjTalk . Some weeks ago, I wrote a simple web server, based on my PythonSharp (1) A Minimal Web Server. The code:


Object subclass: #WebServer
	instanceVariableNames: 'root listener bytes'
	classVariableNames: ''
	poolDictionaries: ''
	category: ''

!WebServer class methods!

	^self basicNew initialize
! !

!WebServer methods!

	bytes := @System.Array !!CreateInstance: @System.Byte with: 1024 * 16.
	listener := @System.Net.HttpListener !!new.
	listener !!Prefixes !!Add: 'http://*:8000/'.
	root := 'c:/apache-tomcat-6.0.18/webapps/docs'.
	@System.Console !!WriteLine: 'initialize'

process: context
	| filename input nbytes |
	filename := context !!Request !!Url !!AbsolutePath.

	@System.Console !!WriteLine: filename.

	(filename = '' or: [filename = '/']) ifTrue: [filename := 'index.html'].

	(filename !!StartsWith: '/') ifTrue: [filename := filename !!Substring: 1].
	@System.Console !!WriteLine: filename.

	filename := @System.IO.Path !!Combine: root with: filename.

	@System.Console !!WriteLine: filename.	

	(@System.IO.File !!Exists: filename) 
	ifFalse: [ context !!Response !!Abort. ]
	ifTrue: [		
		input := @System.IO.FileStream !!new: filename with: @System.IO.FileMode !!Open.
		[[nbytes := input !!Read: bytes with: 0 with: bytes !!Length] value > 0] whileTrue: [
			context !!Response !!OutputStream !!Write: bytes with: 0 with: nbytes.

		input !!Close.

		context !!Response !!OutputStream !!Close

	listener !!Start.
	@System.Console !!WriteLine: 'start'.
	[true] whileTrue: [
		| context |
		@System.Console !!WriteLine: 'get context'.
		context := listener !!GetContext.
		@System.Console !!WriteLine: 'new request'.
		self process: context.
	@System.Console !!WriteLine: 'end start'
! !

WebServer new start

It’s only a quick and dirty proof of concept. You can launch it using the console program (AjTalk.Console project):

ajtalk lib\Library.st Programs\WebServer.st

The result at http://localhost:8000

I wrote a more clear module/example at


Module import: #Web.


| server |

server := Web.Server new
	root: 'c:/apache-tomcat-6.0.18/webapps/docs';
	addPrefix: 'http://*:8000/';

Module import: is my way of loading programs, a la Python import. But this is a topic for other post.

Keep tuned!

Angel “Java” Lopez



2 thoughts on “AjTalk In C# (2): A Simple Web Server

  1. Pingback: AjTalk in C# (1): A minimal Hello, World image « Angel ”Java” Lopez on Blog

  2. Pingback: AjTalk in C# (3) Environments « Angel ”Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s