A2osX/BIN/HTTPGET.S.txt

542 lines
10 KiB
Plaintext
Raw Normal View History

2018-12-05 15:12:10 +00:00
NEW
2019-12-18 20:49:41 +00:00
AUTO 3,1
2018-12-05 15:12:10 +00:00
.LIST OFF
.OP 65C02
.OR $2000
2019-12-18 20:49:41 +00:00
.TF bin/httpget
2018-12-05 15:12:10 +00:00
*--------------------------------------
.INB INC/MACROS.I
.INB INC/A2OSX.I
2018-12-21 14:32:45 +00:00
.INB INC/MLI.I
2018-12-07 16:16:08 +00:00
.INB INC/MLI.E.I
2018-12-05 15:12:10 +00:00
.INB INC/ETH.I
.INB INC/LIBTCPIP.I
.INB INC/NET.HTTP.I
*--------------------------------------
RESP.BUFSIZE .EQ 1024
2019-10-06 20:57:41 +00:00
TIMEOUT.MAX .EQ 50 5 sec.
2018-12-05 15:12:10 +00:00
*--------------------------------------
2018-12-08 07:36:46 +00:00
.DUMMY
.OR ZPBIN
ZS.START
ZPIPCfgPtr .BS 2
ZPHostPtr .BS 2
ZPPtr .BS 2
ZPReqBufPtr .BS 2
2019-10-05 17:24:41 +00:00
2018-12-08 07:36:46 +00:00
ZPReqBufLen .BS 2
ZPEncodedBufPtr .BS 2
ZPEncodedBufLen .BS 2
ZPRespBufPtr .BS 2
2019-10-05 17:24:41 +00:00
hSocket .BS 1
2018-12-08 07:36:46 +00:00
ZS.END
.ED
2018-12-05 15:12:10 +00:00
*--------------------------------------
* 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 S.PS.F
.DA #0
.DA CS.END-CS.START Code Length To Relocate
.DA DS.END-DS.START Data Segment to Allocate
2019-10-03 06:25:27 +00:00
.DA #64 SS
2018-12-08 07:36:46 +00:00
.DA #ZS.END-ZS.START Zero Page Size
2018-12-05 15:12:10 +00:00
.DA 0
*--------------------------------------
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.DOEVENT
.DA CS.QUIT
L.LIBTCPIP .DA LIBTCPIP
L.SA.LOCAL .DA SA.LOCAL
L.SA.REMOTE .DA SA.REMOTE
L.SA.REMOTE.AD .DA SA.REMOTE+S.SOCKADDR.ADDR
L.MSG.IPKO .DA MSG.IPKO
L.MSG.USAGE .DA MSG.USAGE
L.MSG.UNKNOWN .DA MSG.UNKNOWN
2019-10-05 17:24:41 +00:00
L.MSG.CONNECT .DA MSG.CONNECT
2018-12-05 15:12:10 +00:00
L.MSG.SKTKO .DA MSG.SKTKO
2019-10-05 17:24:41 +00:00
L.MSG.CONNECTED .DA MSG.CONNECTED
2018-12-05 15:12:10 +00:00
L.MSG.SKTERR .DA MSG.SKTERR
L.MSG.IOERR .DA MSG.IOERR
L.HTTP.GET1 .DA HTTP.GET1
L.HTTP.GET2 .DA HTTP.GET2
L.HTTP.GET3 .DA HTTP.GET3
.DA 0
*--------------------------------------
CS.INIT >LDYA L.LIBTCPIP
>SYSCALL LoadLib
bcs .9
sta hLIBTCPIP
.9 rts
*--------------------------------------
2019-02-01 20:01:54 +00:00
CS.RUN >LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG is TCPIP loaded ?
2018-12-05 15:12:10 +00:00
>STYA ZPIPCfgPtr
lda (ZPIPCfgPtr) Configured ?
bmi CS.RUN.IPOK
>LDYA L.MSG.IPKO
>SYSCALL puts
lda #E.SYN
2018-12-07 16:16:08 +00:00
sec
2018-12-05 15:12:10 +00:00
rts
2019-10-03 06:25:27 +00:00
2018-12-05 15:12:10 +00:00
CS.RUN.IPOK ldy #S.IPCFG.IP+3
ldx #3
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
.1 lda (ZPIPCfgPtr),y
sta SA.LOCAL+S.SOCKADDR.ADDR,x
dey
dex
bpl .1
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
>INC.G ArgIndex
>SYSCALL ArgV
bcs .9
>STYA ZPHostPtr
jsr Init.Timeout
.2 >PUSHW L.SA.REMOTE.AD
>PUSHW ZPHostPtr
>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GETBYNAME
bcc CS.RUN.HOSTOK
>SLEEP
jsr Wait.Timeout
bcc .2
>PUSHW ZPHostPtr
>PUSHBI 2
>LDYA L.MSG.UNKNOWN
>SYSCALL printf
bra CS.RUN.ESYN
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
.9 >PUSHBI 0
>LDYA L.MSG.USAGE
>SYSCALL printf
CS.RUN.ESYN lda #E.SYN
2019-10-03 06:25:27 +00:00
sec
2018-12-05 15:12:10 +00:00
CS.RUN.RTS rts
CS.RUN.HOSTOK >INC.G ArgIndex
>SYSCALL ArgV
bcc .10
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
jmp CS.RUN.PORTOK
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
.10 >STYA ZPPtr
lda (ZPPtr)
cmp #'-'
beq .1
2019-10-05 17:24:41 +00:00
2019-10-03 06:25:27 +00:00
>LDYA ZPPtr
2018-12-05 15:12:10 +00:00
>SYSCALL atoi
bcs CS.RUN.RTS
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
>STYA SA.REMOTE+S.SOCKADDR.PORT
>INC.G ArgIndex
>SYSCALL ArgV
bcs .8
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
>STYA ZPPtr
lda (ZPPtr)
cmp #'-'
bne CS.RUN.ESYN
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
.1 ldy #1
lda (ZPPtr),y
2019-12-18 20:49:41 +00:00
cmp #'a'
bcc .11
cmp #'z'+1
bcs .11
eor #$20
.11 cmp #'U'
2018-12-05 15:12:10 +00:00
bne .2
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
>INC.G ArgIndex
>SYSCALL ArgV
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
.9 bcs CS.RUN.ESYN
2018-12-07 16:16:08 +00:00
>STYA ZPReqBufPtr
2018-12-05 15:12:10 +00:00
>SYSCALL strlen
2018-12-07 16:16:08 +00:00
>STYA ZPReqBufLen
2018-12-05 15:12:10 +00:00
.8 bra CS.RUN.PORTOK
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
.2 cmp #'F'
bne CS.RUN.ESYN
>INC.G ArgIndex
>SYSCALL ArgV
2019-12-18 20:49:41 +00:00
bcs .9
2019-10-05 17:24:41 +00:00
2018-12-21 14:32:45 +00:00
pha
2019-06-07 15:02:51 +00:00
>PUSHWZ Aux type
2018-12-21 14:32:45 +00:00
>PUSHBI S.FI.T.TXT
>PUSHBI O.RDONLY
pla
2018-12-05 15:12:10 +00:00
>SYSCALL LoadTxtFile
bcs .9
2019-10-05 17:24:41 +00:00
2018-12-07 16:16:08 +00:00
>STYA ZPReqBufLen
2018-12-05 15:12:10 +00:00
txa
>STA.G hReqBuf
>SYSCALL GetMemPtr
2018-12-07 16:16:08 +00:00
>STYA ZPReqBufPtr
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
CS.RUN.PORTOK >PUSHW ZPHostPtr
>PUSHW SA.REMOTE+S.SOCKADDR.PORT
ldx #3
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
.1 >PUSHB SA.REMOTE+S.SOCKADDR.ADDR,x
dex
bpl .1
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
>PUSHBI 8
2019-10-05 17:24:41 +00:00
>LDYA L.MSG.CONNECT
2018-12-05 15:12:10 +00:00
>SYSCALL printf
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
CS.RUN.OPENSKT >PUSHBI 0 no protocol
lda #S.SOCKET.T.STREAM
>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
bcs .9
2019-10-05 17:24:41 +00:00
sta hSocket
2018-12-05 15:12:10 +00:00
>PUSHW L.SA.LOCAL
2019-10-05 17:24:41 +00:00
lda hSocket
2018-12-05 15:12:10 +00:00
>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
2019-10-05 17:24:41 +00:00
bcc .1
.9 pha
>LDYA L.MSG.SKTKO
2018-12-05 15:12:10 +00:00
>SYSCALL puts
2019-10-05 17:24:41 +00:00
pla
2019-10-03 06:25:27 +00:00
sec
2019-10-05 17:24:41 +00:00
.99 rts
.1 jsr Init.Timeout
.2 >SLEEP
>PUSHW L.SA.REMOTE
lda hSocket
2018-12-05 15:12:10 +00:00
>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
2019-10-05 17:24:41 +00:00
bcc .3
cmp #ERR.SKT.NOCONN
bne .9
jsr Wait.Timeout
bcc .2
lda #ERR.SKT.NOCONN
bra .9
.3 >LDYA L.MSG.CONNECTED
2018-12-05 15:12:10 +00:00
>SYSCALL puts
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
>LDYAI RESP.BUFSIZE
>SYSCALL getmem
2019-10-05 17:24:41 +00:00
bcs .99
2018-12-06 21:58:47 +00:00
>STYA ZPRespBufPtr
2018-12-05 15:12:10 +00:00
txa
>STA.G hRespBuf
2019-10-05 17:24:41 +00:00
>SLEEP
2018-12-08 07:36:46 +00:00
CS.RUN.GET jsr CS.RUN.ENCODE
bcs .99
* >PUSHW ZPEncodedBufLen
* >PUSHW ZPEncodedBufPtr
* ldy #S.PS.hStdOut
* lda (pPs),y
* >SYSCALL fwrite
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
jsr CS.RUN.REQUEST
bcs .99
2018-12-07 16:16:08 +00:00
jsr CS.RUN.RESPONSE
bcs .99
2018-12-05 15:12:10 +00:00
.9 lda #0
sec
.99 rts
*--------------------------------------
CS.RUN.SKTERR pha
>PUSHA
>PUSHBI 1
>LDYA L.MSG.SKTERR
>SYSCALL printf
pla
sec
rts
*--------------------------------------
CS.RUN.IOERR pha
>PUSHA
>PUSHBI 1
>LDYA L.MSG.IOERR
>SYSCALL printf
pla
sec
rts
2018-12-07 16:16:08 +00:00
*--------------------------------------
CS.RUN.CONNECT
2018-12-08 07:36:46 +00:00
*--------------------------------------
CS.RUN.ENCODE >LDYA ZPReqBufPtr
>STYA ZPPtr
.10 lda (ZPPtr)
beq .4
inc ZPEncodedBufLen
bne .1
inc ZPEncodedBufLen+1
.1 inc ZPPtr
bne .11
inc ZPPtr+1
.11 jsr CS.RUN.TOENCODE
bcc .10
.3 lda ZPEncodedBufLen
clc
adc #2
sta ZPEncodedBufLen
bcc .10
inc ZPEncodedBufLen+1
bra .10
2018-12-07 16:16:08 +00:00
2018-12-08 07:36:46 +00:00
.4 >LDYA ZPEncodedBufLen
>SYSCALL GetMem
bcs .9
>STYA ZPEncodedBufPtr
>STYA ZPPtr
txa
>STA.G hEncodedBuf
2018-12-07 16:16:08 +00:00
2018-12-08 07:36:46 +00:00
.5 lda (ZPReqBufPtr)
beq .8
inc ZPReqBufPtr
bne .6
inc ZPReqBufPtr+1
.6 jsr CS.RUN.TOENCODE
bcc .7
jsr CS.RUN.ADDBYTETOBUF
bra .5
.7 jsr CS.RUN.ADDTOBUF
bra .5
.8 clc
2019-10-03 06:25:27 +00:00
.9 rts
2018-12-08 07:36:46 +00:00
*--------------------------------------
CS.RUN.TOENCODE ldx #URI.ToEncode.Cnt-1
.1 cmp URI.ToEncode,x
beq .9 CS
dex
bpl .1
clc
.9 rts
*--------------------------------------
CS.RUN.ADDBYTETOBUF
pha
lda #'%'
jsr CS.RUN.ADDTOBUF
pla
pha
lsr
lsr
lsr
lsr
ora #$30
cmp #$3A
bcc .1
adc #6
.1 jsr CS.RUN.ADDTOBUF
pla
and #$0F
ora #$30
cmp #$3A
bcc CS.RUN.ADDTOBUF
adc #6
*--------------------------------------
CS.RUN.ADDTOBUF sta (ZPPtr)
inc ZPPtr
bne .8
inc ZPPtr+1
2019-10-03 06:25:27 +00:00
.8 rts
2018-12-05 15:12:10 +00:00
*--------------------------------------
2018-12-06 07:46:26 +00:00
CS.RUN.REQUEST jsr Init.Timeout
2019-10-05 17:24:41 +00:00
>PUSHWI HTTP.GET1.LEN
>PUSHW L.HTTP.GET1
2018-12-05 15:12:10 +00:00
jsr .8
2019-10-05 17:24:41 +00:00
bcs .99
2018-12-05 15:12:10 +00:00
2019-10-05 17:24:41 +00:00
>PUSHW ZPEncodedBufLen
2018-12-08 07:36:46 +00:00
>PUSHW ZPEncodedBufPtr
2018-12-05 15:12:10 +00:00
jsr .8
bcs .9
2019-10-05 17:24:41 +00:00
2018-12-05 15:12:10 +00:00
>PUSHWI HTTP.GET2.LEN
>PUSHW L.HTTP.GET2
jsr .8
2019-10-05 17:24:41 +00:00
.99 bcs .9
2018-12-05 15:12:10 +00:00
>LDYA ZPHostPtr
>SYSCALL strlen
>PUSHYA
>PUSHW ZPHostPtr
jsr .8
bcs .9
>PUSHWI HTTP.GET3.LEN
>PUSHW L.HTTP.GET3
2019-10-05 17:24:41 +00:00
.8 lda #'!'
>SYSCALL putchar
lda hSocket
2018-12-05 15:12:10 +00:00
>LIBCALL hLIBTCPIP,LIBTCPIP.Write
.9 rts
*--------------------------------------
2018-12-07 16:16:08 +00:00
CS.RUN.RESPONSE jsr Init.Timeout
.1 >SLEEP
>PUSHWI RESP.BUFSIZE
>PUSHW ZPRespBufPtr
2019-10-05 17:24:41 +00:00
lda hSocket
2018-12-07 16:16:08 +00:00
>LIBCALL hLIBTCPIP,LIBTCPIP.Read
bcc .2
2019-10-05 17:24:41 +00:00
cmp #E.NODATA
2018-12-07 16:16:08 +00:00
bne .8
2019-10-05 17:24:41 +00:00
2018-12-07 16:16:08 +00:00
jsr Wait.Timeout
2019-10-05 17:24:41 +00:00
bcs .80
2018-12-08 07:36:46 +00:00
bra .1
2019-10-05 17:24:41 +00:00
2018-12-07 16:16:08 +00:00
.2 >PUSHYA len
>PUSHW ZPRespBufPtr
ldy #S.PS.hStdOut
lda (pPs),y
>SYSCALL fwrite
bcs .9
>INC.G bResponse
bra .1
2019-10-05 17:24:41 +00:00
2018-12-07 16:16:08 +00:00
.8 cmp #MLI.E.IO SKT Close ?
bne .9
2019-10-05 17:24:41 +00:00
2018-12-07 16:16:08 +00:00
.80 >LDA.G bResponse
beq .99
2019-10-05 17:24:41 +00:00
2018-12-08 07:36:46 +00:00
.81 clc
2018-12-07 16:16:08 +00:00
rts
2019-10-05 17:24:41 +00:00
2018-12-07 16:16:08 +00:00
.99 lda #MLI.E.IO
.9 sec
rts
*--------------------------------------
2018-12-05 15:12:10 +00:00
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
*--------------------------------------
2019-10-05 17:24:41 +00:00
CS.QUIT lda hSocket
2018-12-05 15:12:10 +00:00
beq .1
>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
.1 >LDA.G hReqBuf
beq .2
>SYSCALL freemem
2018-12-08 07:36:46 +00:00
.2 >LDA.G hEncodedBuf
2018-12-05 15:12:10 +00:00
beq .3
>SYSCALL freemem
2018-12-08 07:36:46 +00:00
.3 >LDA.G hRespBuf
beq .4
>SYSCALL freemem
.4 lda hLIBTCPIP
2018-12-05 15:12:10 +00:00
beq .8
>SYSCALL UnloadLib
.8 clc
rts
*--------------------------------------
Init.Timeout lda #TIMEOUT.MAX
>STA.G TimeOut
rts
*--------------------------------------
Wait.TimeOut sec
>LDA.G TimeOut
beq .9
clc
.9 rts
*--------------------------------------
CS.END
2019-12-03 15:42:17 +00:00
LIBTCPIP .AZ "libtcpip"
2018-12-05 15:12:10 +00:00
hLIBTCPIP .BS 1
MSG.IPKO .AZ "TCP/IP Not Loaded/Configured."
MSG.USAGE .AS "Usage : HTTPGET <ip|host> [port]\r\n"
.AS " -U Url\r\n"
.AZ " -F UrlFile\r\n"
MSG.UNKNOWN .AZ "%s: Unknown host\r\n"
2019-10-05 17:24:41 +00:00
MSG.CONNECT .AZ "Connecting to %d.%d.%d.%d:%D (%s)..."
2018-12-05 15:12:10 +00:00
MSG.SKTKO .AZ "Failed to Open Socket."
2019-10-05 17:24:41 +00:00
MSG.CONNECTED .AZ "Connected."
2018-12-05 15:12:10 +00:00
MSG.SKTERR .AZ "Socket Error : $%h\r\n"
MSG.IOERR .AZ "I/O Error : $%h\r\n"
*--------------------------------------
SA.LOCAL .DA #AF.INET S.SOCKADDR.AF
.BS 1
.BS 4 S.SOCKADDR.ADDR
.BS 2 S.SOCKADDR.PORT
*--------------------------------------
SA.REMOTE .DA #AF.INET S.SOCKADDR.AF
.BS 1
.BS 4 S.SOCKADDR.ADDR
.DA TCP.PORT.HTTP
*--------------------------------------
2018-12-08 07:36:46 +00:00
URI.ToEncode .DA #C.LF,#C.CR,#C.SPACE,#'"'
.AS "%-.\^_`{|}~"
URI.ToEncode.Cnt .EQ *-URI.ToEncode
*--------------------------------------
2018-12-06 07:46:26 +00:00
HTTP.GET1 .AS "GET /"
2018-12-05 15:12:10 +00:00
HTTP.GET1.LEN .EQ *-HTTP.GET1
2018-12-06 07:46:26 +00:00
HTTP.GET2 .AS " HTTP/1.1"
.DA #C.CR,#C.LF
.AS "User-Agent: A2osX.HTTPGET"
.DA #C.CR,#C.LF
.AS "Host: "
2018-12-05 15:12:10 +00:00
HTTP.GET2.LEN .EQ *-HTTP.GET2
2018-12-06 07:46:26 +00:00
HTTP.GET3 .DA #C.CR,#C.LF
2018-12-06 21:58:47 +00:00
.AS "Connection: close"
.DA #C.CR,#C.LF
2018-12-06 16:39:53 +00:00
.DA #C.CR,#C.LF
2018-12-05 15:12:10 +00:00
HTTP.GET3.LEN .EQ *-HTTP.GET3
*--------------------------------------
.DUMMY
.OR 0
DS.START
ArgIndex .BS 1
hReqBuf .BS 1
2018-12-08 07:36:46 +00:00
hEncodedBuf .BS 1
2018-12-05 15:12:10 +00:00
hRespBuf .BS 1
2018-12-07 16:16:08 +00:00
bResponse .BS 1
2018-12-05 15:12:10 +00:00
TimeOut .BS 1
DS.END
.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/HTTPGET.S
ASM