MiniWebServer

BlitzMax Forums/BlitzMax Programming/MiniWebServer

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

This is a mini webserver I am hoping to run online sometime.

Run the following in the same folder as an index.html file and then launch a webbrowser to localhost:4663 which is the same port as googledesktop but they don't seem to mind each other.

I'm going to start compiling BlitzMax CGI programs on my remote account first which is another topic but would like to keep developing the following so if anyone wants to help, I need some sort of session manager code that looks after cookies etc.

I'll probably look at support for standard CGI-bin process launching next.

A web front end for the miniwebserver user interface would also be good, perhaps it could listen on another port and allow you to do session control stuff from the browser, sync files, show stats etc.

' minwebserver.bmx

Strict

Local socket:TSocket=CreateTCPSocket()
BindSocket socket,4663
SocketListen socket
Print "listening on port 4663"

While True
	Local client:TSocket=SocketAccept(socket,20)
	If client New TConnection.Create(client)
	TConnection.PollAll
'	Print "poll"
Wend

End

Type TConnection

	Global connections:TList=New TList

	Field id
	Field _socket:TSocket
	Field _stream:TStream
	Field _data$
	
	Method getline$()
		Local p,l$
		p=_data.find("~n")
		If p=-1 p=_data.length
		l=_data[..p]
		_data=_data[p+1..]		
		Return l
	End Method
	
'	Method WritePage()

	Method ServePage(uri$)
		Local file$,size,p

		Print "serving "+uri+" from connection#"+id
		
		If uri$="/" 
			file="index.html"
		Else
			file=uri
			If file[..1]="/" file=file[1..]
			p=file.find("?")
			If p>0 file=file[..p]
'			file=uri[..p]
'			uri=args[p..]
		EndIf

		If file And FileType(file)=FILETYPE_FILE
			size=FileSize(file)
			If size=0
				Print "ServePage "+uri+" is empty"
				Return
			EndIf			
			WriteLine _stream,"HTTP/1.1 200 OK"
			WriteLine _stream,"Content-Type: text/html; charset=UTF-8"
			WriteLine _stream,"Content-Length: "+size
			WriteLine _stream,"Pragma: no-cache"
			WriteLine _stream,"Expires: Fri, 01 Jan 1990 00:00:00 GMT"
			WriteLine _stream,"Cache-control: no-cache, no-store, must-revalidate"
			WriteLine _stream,"Connection: keep-alive"
			WriteLine _stream,"Keep-Alive: 30000"
			WriteLine _stream,""
			WriteLine _stream,LoadText(file)					
'			DebugLog file+" sent"
		Else
			DebugLog "Failed To find file "+file
		EndIf		
'		debuglog "Served!"
	End Method
	
	Field getname$
	
	Method Poll()
		Local bytes,mess$
		bytes=SocketReadAvail(_socket)
		If bytes
			_data:+ReadString(_stream,bytes)
		EndIf
		While _data
			mess=Trim(getline())
'			Print "*"+mess
			If mess[..4]="GET "
				getname=mess[4..]
				getname=getname.replace(" HTTP/1.1","")
			EndIf			
			If mess=""
				If getname
					ServePage getname
					getname=""
				Else
					Print "blank line unknown!"
				EndIf
			Else
				'If getname="" Print "$"+mess+"$"
				'Print mess
			EndIf
		Wend	
	End Method
	
	Method Create:TConnection(client:TSocket)
		Print "creating connection!"
		_socket=client
		_stream=CreateSocketStream(client)
		connections.AddLast Self
		id=connections.count()
		Return Self
	End Method

	Function PollAll()
		Local connect:TConnection
		For connect=EachIn connections
			connect.Poll
		Next
	End Function		
End Type