Skip to content

Commit

Permalink
feat: error when the content-length header is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Mar 23, 2024
1 parent 911673f commit 0202b7a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 14 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ A utility for application developers to talk to web servers to exchange blocks o
.http post -b 22 -l 1024 -h 192.168.1.100 -p 8080 -u /send
; Download and save Manic Miner http://zxdb.remysharp.com/get/18840
.http -h zxdb.remysharp.com -u /18840 -f manic.tap
.http -h zxdb.remysharp.com -u /get/18840 -f manic.tap
; Load http://data.remysharp.com/1 directly into bank 26 and flash border red (2)
.http get -b 26 -h data.remysharp.com -u /1 -v 2
; Load http://data.remysharp.com/18 (the text "hello world") into bank 20 and flash border red (2)
.http get -b 20 -h data.remysharp.com -u /18 -v 2
```

Options:
Expand Down Expand Up @@ -109,6 +109,7 @@ I've written a number of [example servers](https://github.com/remy/next-http/tre
- `K` Could not read the http content length header correctly
- `L` Out of memory to buffer file download: try with `-r` to disable rolling banks
- `M` Not enough memory to download: try with `-r` to disable rolling banks
- `N` content-length missing: .http requires the `content-length` header from the server to work properly

## Testing

Expand Down
Binary file modified http
Binary file not shown.
Binary file modified http-debug.dot
Binary file not shown.
12 changes: 11 additions & 1 deletion src/headers.asm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ tmpBuffer:
; Modifies: AF, HL, DE
contentLengthSub:
or a ; reset carry for sbc
ld hl, (contentLength) ; do the LSW first
ld hl, (contentLength) ; do the LSW first
sbc hl, de
ld (contentLength), hl
ret nc
Expand Down Expand Up @@ -93,6 +93,10 @@ findContentLength
call Uart.read ; load A with the next character
dec de

;; if there's an immediate CR, then we're on a blank, and we need to bail
cp CR
jp z, .contentLengthNotFound

;; convert character to uppercase
cp 'a' ; if A < 'a' then skip case shift
jr c, .noCaseShiftC
Expand Down Expand Up @@ -234,11 +238,17 @@ findContentLength
call Uart.read : dec de ; LR
jp .processHeader

.contentLengthNotFound
; set carry to indicate failure
scf
jr .exit

.slurpToEndOfAllHeaders
call Uart.read : dec de : cp CR : jr nz, .slurpToEndOfAllHeaders
call Uart.read : dec de ; LR
call Uart.read : dec de : cp CR : jr nz, .slurpToEndOfAllHeaders
call Uart.read : dec de ; LR
and a ; clear carry
.exit
pop ix
ret
Expand Down
8 changes: 7 additions & 1 deletion src/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Get
call Wifi.tcpSendString
LoadPackets
call Wifi.getPacket
jr c, .contentLenghtMissing

;; reduce the content length left to read
ld de, (Wifi.bufferLength)
Expand Down Expand Up @@ -382,10 +383,15 @@ LoadPackets

jr LoadPackets

.contentLenghtMissing:
ld hl, Err.contentLengthMissing
jp Error

.contentLenghtError:
ld hl, Err.contentLength
jp Error


PreExitCheck
ld a, (State.fileMode)
cp NOT_WRITING_TO_FILE
Expand Down Expand Up @@ -472,7 +478,7 @@ diagBinPcLo EQU ((100*diagBinSz)%8192)*10/8192
;; delete any autoexec.bas
SHELLEXEC "(hdfmonkey rm /Applications/cspect/app/cspect-next-2gb.img /nextzxos/autoexec.bas > /dev/null) || exit 0"
SHELLEXEC "hdfmonkey put /Applications/cspect/app/cspect-next-2gb.img http-debug.dot /devel/http-debug.dot"
SHELLEXEC "mono /Applications/cspect/app/cspect.exe -r -w5 -basickeys -zxnext -nextrom -exit -brk -tv -mmc=/Applications/cspect/app/cspect-next-2gb.img -map=./http.map -sd2=/Applications/cspect/app/empty-32mb.img" ; -com='/dev/tty.wchusbserial1430:11520'
SHELLEXEC "mono /Applications/cspect/app/cspect.exe -r -w5 -basickeys -zxnext -nextrom -exit -brk -mmc=/Applications/cspect/app/cspect-next-2gb.img -map=./http.map"
ENDIF : ENDIF
DISPLAY "TEST BUILD"
ENDIF
1 change: 1 addition & 0 deletions src/messages.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fileOpen DC "J Can't open file for writing"
contentLength DC "K Content length error"
outOfMemory DC "L Out of memory: try '-r'"
notEnoughMemory DC "M Not enough memory: try '-r'"
contentLengthMissing DC "N content-length missing"
ENDMODULE

MODULE Msg
Expand Down
2 changes: 1 addition & 1 deletion src/version.inc.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
IFNDEF TESTING
DEFINE VERSION "1.0.3"
DEFINE VERSION "1.0.4"
ELSE
DEFINE VERSION "TEST"
ENDIF
Expand Down
32 changes: 24 additions & 8 deletions src/wifi.asm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ init:
EspCmdOkErr "AT+CIPMUX=0" ; Single connection mode
jr c, .initError

and a
and a ; reset the carry flag
ret
.initError
call RetartESP
Expand All @@ -49,7 +49,7 @@ RetartESP:
EspCmd "AT+RST"
jr .checkWifiConnect
.alreadyTried
scf
scf ; set the carry: fail
ret
.checkWifiConnect:
call Uart.read
Expand All @@ -60,10 +60,9 @@ RetartESP:
call Uart.read : cp ' ' : jr nz, .checkWifiConnect
call Uart.read : cp 'G' : jr nz, .checkWifiConnect ; searching for "WIFI CONNECTED"
call flushToLF
and a
ret


and a ; clear the carry
ret
closeTCP
EspCmdOkErr "AT+CIPCLOSE"
ret
Expand Down Expand Up @@ -102,7 +101,8 @@ checkOkErr:
call Uart.read : cp 'K' : jr nz, checkOkErr
call Uart.read : cp 13 : jr nz, checkOkErr
call flushToLF
and a

and a ; clear the carry
ret
.errStart
call Uart.read : cp 'R' : jr nz, checkOkErr
Expand All @@ -124,6 +124,8 @@ checkOkErr:
flushToLF
call Uart.read
cp 10 : jr nz, flushToLF

and a ; clear the carry
ret

; HL = buff
Expand Down Expand Up @@ -350,13 +352,15 @@ getPacket:
call Uart.read : cp 13 : jr nz, getPacket
ld a, 1
ld (closed), a
and a ; clear the cary
ret
.cspectHack
;; I don't know why, but cspect gives us OK\n\r after the http request
call Uart.read : cp 13 : jr nz, getPacket
call Uart.read : cp 10 : jr nz, getPacket
ld a, 1
ld (closed), a
and a ; clear the cary
ret
.ipdBegun
call Uart.read : cp 'I' : jr nz, getPacket
Expand Down Expand Up @@ -384,20 +388,30 @@ getPacket:
;; we're searching for "content-length:"
call Headers.findContentLength

;; if there's a carry flag, then it failed and we bail
;; out leaving the carry in place
jr nc, .processHeaderNoError
pop hl
ret

.processHeaderNoError
ld a, 0
ld (firstRead), a
.headerProcessed
pop hl

ld b, d ; load DE (back) into BC
ld b, d ; load DE (back) into BC
ld c, e
ld (bufferLength), bc ; save the length for saving to file
ld (bufferLength), bc ; save the length for saving to file

;; check if the header was all we got in the IPD request
ld a, b
or c
jr nz, .headerProcessedContinue
ld (bufferPointer), hl

;; then we need to clear the carry flag, as there's no error
and a
ret

.headerProcessedContinue
Expand Down Expand Up @@ -476,6 +490,7 @@ getPacket:
or c
jr nz, .readp
ld (bufferPointer), hl
and a ; clear the cary
ret

.outOfMemory
Expand All @@ -498,6 +513,7 @@ getPacket:
ld a, b
or c
jr nz, .skipSavingBuffer
and a ; clear the cary
ret

.slurp
Expand Down

0 comments on commit 0202b7a

Please sign in to comment.