Angel \”Java\” Lopez on Blog

December 15, 2012

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

Filed under: AjTalk, C Sharp, Open Source Projects, Programming Languages, Smalltalk — ajlopez @ 7:52 pm

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:

https://github.com/ajlopez/AjTalk/blob/master/Src/AjTalk.Console/Programs/WebServer.st

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

!WebServer class methods!

new
	^self basicNew initialize
! !

!WebServer methods!

initialize
	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
	]
!

start
	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

https://github.com/ajlopez/AjTalk/blob/master/Src/AjTalk.Console/Programs/WebSiteTomcat.st

Module import: #Web.

!

| server |

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

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

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. [...] by AjTalk In C# (2): A Simple Web Server « Angel ”Java” Lopez on Blog — December 15, 2012 @ 7:52 [...]

    Pingback by AjTalk in C# (1): A minimal Hello, World image « Angel ”Java” Lopez on Blog — December 15, 2012 @ 7:55 pm

  2. [...] Previous Post [...]

    Pingback by AjTalk in C# (3) Environments « Angel ”Java” Lopez on Blog — December 26, 2012 @ 4:46 pm


RSS feed for comments on this post. TrackBack URI

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

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: