Code: Select all
connection = OpenConnection(nil, server$, 80) Code: Select all
debugprint(connection).And the rest of the code fails because it is expecting receiving an integer.
Code: Select all
Global base, data$, count, done
base = {}
Function base:DownloadFile(server$, url$, downloadpath$, filename$)
Local connection
connection = OpenConnection(nil, server$, 80)
debugprint(connection)
SendData(connection, "GET " .. url$ .. " HTTP/1.0\r\n\r\n")
data$, count, done = ReceiveData(connection, #RECEIVEALL)
; Redirect
If FindStr(data$,"302 Found") > -1
CloseConnection(connection)
redirection$ = MidStr(data$,FindStr(data$,"Location: "),StrLen(data$))
redirection$ = MidStr(redirection$,10,FindStr(redirection$,"\n")-11)
server$ = ReplaceStr(redirection$, "http://", "")
server$ = MidStr(server$,0,FindStr(server$,"/"))
connection = OpenConnection(nil, server$, 80)
SendData(connection, "GET " .. redirection$ .. " HTTP/1.0\r\n\r\n")
data$ = nil
; Skip HTTP Headers
While data$ = nil Or FindStr(data$,"Content-Type:") = -1
data$, count, done = ReceiveData(connection, #RECEIVELINE)
Wend
data$, count, done = ReceiveData(connection, #RECEIVELINE) ; Skip blank line
data$, count, done = ReceiveData(connection, #RECEIVEBYTES, 65536) ; Read file data
ElseIf FindStr(data,"200 OK") > -1
EndIf
; Not Found
If FindStr(data$,"404 Found") > -1
Return(False)
; Found - Download it
Else
OpenFile(connection,downloadpath$ .. filename$, #MODE_WRITE)
Return(True)
EndIf
EndFunction
success = base:DownloadFile("www.os4depot.net","http://www.os4depot.net/share/game/board/africa.lha","RAM:","africa.lha")
Function SaveToDisk(id)
WriteString(id,data$)
CloseConnection(id)
CloseFile(id)
End()
Endfunction
Function base:BuildFile(msg)
Local segment$
segment$, count, done = ReceiveData(msg.id, #RECEIVEBYTES, 65536) ; Read file data
data$ = data$ .. segment$
If StrLen(data$) = 297517 Then SaveToDisk(msg.id)
EndFunction
Function base_BuildFile(msg) base:BuildFile(msg) EndFunction
InstallEventHandler({OnReceiveData = base_BuildFile})
Repeat
WaitEvent()
Forever