NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF SBIN/HTTPD
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/ETH.I
				.INB INC/LIBTCPIP.I
				.INB INC/NET.HTTP.I
*--------------------------------------
TIMEOUT.MAX		.EQ 30					30 sec.
CONN.MAX		.EQ 16
*--------------------------------------
ZPIPCfgPtr		.EQ ZPBIN
ZPSktPtr		.EQ ZPBIN+2
ZPLinePtr		.EQ ZPBIN+4
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #64					SS
				.DA #6					ZP
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------				
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.LIBTCPIP		.DA LIBTCPIP
L.SA.Local		.DA SA.Local
L.MSG.TCPWAIT	.DA MSG.TCPWAIT
L.MSG.INITOK	.DA MSG.INITOK
L.MSG.TCPIPERR	.DA MSG.TCPIPERR
L.MSG.SKTERR	.DA MSG.SKTERR
L.MSG.ABORT		.DA MSG.ABORT
L.MSG.INCOMING	.DA MSG.INCOMING
L.MSG.DBG0		.DA MSG.DBG0
L.MSG.DBG1		.DA MSG.DBG1
L.HTTP.200OK	.DA HTTP.200OK
				.DA 0				
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9
				sta hLIBTCPIP

.9				rts
*--------------------------------------
CS.RUN			jsr Init.Timeout

				>LDYA L.MSG.TCPWAIT
				>SYSCALL puts
				
.1				>SLEEP
				
				>LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG	is TCPIP loaded ?
				bcs .99
				>STYA ZPIPCfgPtr
				
				lda (ZPIPCfgPtr)		Configured ?
				bmi CS.RUN.INIT
				
				jsr Wait.Timeout
				bcs .99
				
				ldy #S.PS.hStdIn
				lda (pPs),y
				>SYSCALL feof
				bcs CS.RUN.RTS			I/O err
				
				tay
				bne .1

				>SYSCALL GetChar
				bcs CS.RUN.RTS

				cmp #$03
				bne .1
				
				>LDYA L.MSG.ABORT
				>SYSCALL puts
				lda #3
				clc
				rts
				
.99				>LDYA L.MSG.TCPIPERR
				>SYSCALL puts
				lda #E.SYN
				sec	
CS.RUN.RTS		rts
				
CS.RUN.INIT		ldx #3
				ldy #S.IPCFG.IP+3
				
.1				lda (ZPIPCfgPtr),y
				sta SA.LOCAL+S.SOCKADDR.ADDR,x
				dey
				dex
				bpl .1
				
				>PUSHBI 0				no protocol
				lda #S.SOCKET.T.STREAM
				>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
				bcs CS.RUN.ERR

				>STA.G hSrvSocket
				pha
				>PUSHW L.SA.LOCAL
				pla
				>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
				bcs CS.RUN.ERR
				
				>LDA.G hSrvSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Listen
				bcs CS.RUN.ERR
				
				>LDYAI 512
				>SYSCALL getmem
				bcs CS.RUN.RTS
				
				>STYA ZPLinePtr
				txa
				>STA.G hLineBuf
				
				>LDYA L.MSG.INITOK
				>SYSCALL puts

CS.RUN.LOOP		>SLEEP
				jsr CS.RUN.SERVER
				bcs CS.RUN.ERR
				
				bra CS.RUN.LOOP
				
.9				>LDYA L.MSG.SKTERR
				>SYSCALL puts
				lda #E.SYN
				sec	
				
CS.RUN.ERR		rts
*--------------------------------------
CS.RUN.SERVER	>LDA.G hSrvSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.accept
				bcs .8
				
				>STA.G hClntSocket
				>SYSCALL GetMemPtr
				>STYA ZPSktPtr

				ldx #3
				ldy #S.SOCKET.REM.ADDR+3
				
.3				>PUSHB (ZPSktPtr),y
				dey
				dex
				bpl .3
				
				>PUSHB.G hClntSocket

				>PUSHBI 5
				>LDYA L.MSG.INCOMING
				>SYSCALL printf

				jsr CS.RUN.CLIENT
				
.8				clc
.9				rts
*--------------------------------------
CS.RUN.CLIENT	
.1				>SLEEP

				>PUSHWI 512
				>PUSHW ZPLinePtr
				>LDA.G hClntSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.read
				bcc .2
				
				>PUSHYA
				>PUSHBI 2
				>LDYA L.MSG.DBG0
				>SYSCALL printf
				bra .8
				
.2				phy
				ply
				bne .3
				
				pha
				pla
				beq .1
				
.3				phy
				pha
				>PUSHYA
				>PUSHBI 2
				>LDYA L.MSG.DBG1
				>SYSCALL printf
				pla
				ply
				

				>PUSHYA
				>PUSHW ZPLinePtr
				ldy #S.PS.hStdOut
				lda (pPS),y

				>SYSCALL fwrite

.7				>PUSHWI HTTP.200OK.len
				>PUSHW L.HTTP.200OK
				>LDA.G hClntSocket

				>LIBCALL hLIBTCPIP,LIBTCPIP.write
				
.8				>LDA.G hClntSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown

				clc
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?
				
				>LDA.G TimeOut
				beq .9
				
				dec
				sta (pData),y
				
.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			ldy #hSrvSocket
				lda (pData),y
				beq .3
				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
				
.3				>LDA.G hLineBuf
				beq .4
				
				>SYSCALL FreeMem
				
.4				lda hLIBTCPIP
				beq .8
				>SYSCALL UnloadLib
				
.8				clc
				rts
*--------------------------------------
Init.Timeout	ldy #TimeOut
				lda #TIMEOUT.MAX
				sta (pData),y
				rts
*--------------------------------------
Wait.TimeOut	sec
				ldy #TimeOut
				lda (pData),y
				beq .9
				clc
.9				rts
*--------------------------------------
CS.END
*--------------------------------------
hLIBTCPIP		.BS 1
LIBTCPIP		.AZ "libtcpip"
MSG.TCPWAIT		.AZ "HTTPD:Waiting for TCP/IP initializing..."
MSG.INITOK		.AZ "HTTPD:Init Ok, Listening."
MSG.TCPIPERR	.AZ "HTTPD:TCP/IP Not initialized properly."
MSG.SKTERR		.AZ "HTTPD:Listen Error."
MSG.ABORT		.AZ "HTTPD:User Aborted."
MSG.INCOMING	.AZ "HTTPD:Incoming Connection [SKT=%h] From : %d.%d.%d.%d\r\n"
MSG.DBG0		.AZ "read: ERROR %h\r\n"
MSG.DBG1		.AZ "read: %D bytes\r\n"
*--------------------------------------
HTTP.200OK		.AS "HTTP/1.1 200 OK"
				.DA #C.CR,#C.LF
				.AS "Server: A2osX-HTTPD 0.92"
				.DA #C.CR,#C.LF
				.AS "Content-Type: text/html"
				.DA #C.CR,#C.LF
				.AS "Content-Length: 34"
				.DA #C.CR,#C.LF
				.AS "Connection: Close"
				.DA #C.CR,#C.LF
				.DA #C.CR,#C.LF
				.AS "<HTML><BODY>Hello!</BODY></HTML>"
				.DA #C.CR,#C.LF
HTTP.200OK.len	.EQ *-HTTP.200OK
*--------------------------------------
SA.LOCAL		.DA #AF.INET			S.SOCKADDR.AF
				.BS 1
				.BS 4					S.SOCKADDR.ADDR
				.DA TCP.PORT.HTTP
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
hLineBuf		.BS 1
hSrvSocket		.BS 1
hClntSocket		.BS 1
TimeOut			.BS 1
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/SBIN/HTTPD.S
ASM
