AjTalk en C# (2): Un Simple Servidor Web

Published on Author lopezLeave a comment

Anterior Post 
Siguiente Post 

Estoy trabajando completando mi máquina virtual Smalltalk, escrita en C#, el AjTalk, el repositorio en https://github.com/ajlopez/AjTalk . Hace unas semanas, escribí un ejemplo de simple servidor web, basado en mi trabajo previo en PythonSharp (1) Un servidor web mínimo. El código de este nuevo servidor en:

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
!

Como el anterior, es una prueba de concepto, “quick and dirty”, para mostrar que puedo reutilizar las clases de .NET. Pueden lanzarlo desde el programa de consola de AjTalk (el resultado de compilar el proyecto AjTalk.Console):

ajtalk lib\Library.st Programs\WebServer.st

El resultado en http://localhost:8000 (jeje, reusando unas páginas estáticas que tengo en mi disco local):

Luego escribí un ejemplo más claro y modular en

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
!

Pero ahí estoy usando Module import:, algo que implementé basado en lo que hice de PythonSharp import para cargar programas. Pero esa lógica ya es tema para otro post.

Nos leemos!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Leave a Reply

Your email address will not be published. Required fields are marked *