REM
REM htget.bas
REM Download a page from an http server.
REM

debug= 0

proxy$="" : REM Modify this if you want to use some proxy by default.

argpos= 1

label moreargs

opt$= programarg$ (argpos)

if lower$ (opt$) = "-d" then debug= 1: argpos= argpos + 1: goto moreargs

if lower$ (opt$) = "-p" then proxy$= programarg$ (argpos + 1): argpos= argpos + 2: goto moreargs


url$= programarg$ (argpos)

open error as #2

if url$ = "" then url$= "http://www.arrakis.es/~ninsesabe/blassic/"

if instr (url$, "http://") = 1 then url$= mid$ (url$, 8)

p= instr (url$, "/")

if p <> 0 then host$= left$ (url$, p - 1) : page$= mid$ (url$, p) else host$= url$: page$= ""

if page$ = "" then page$= "/"

if proxy$ <> "" then page$= "http://" + host$ + page$: host$= proxy$

port= 80

p= instr (host$, ":")
if p <> 0 then port= val (mid$ (host$, p + 1) ): host$= left$ (host$, p - 1)

if debug then print #2, url$; ": Requesting "; page$; " from "; host$

if debug then print "Connecting to: "; host$; " on port "; port

on error goto no_host

socket host$, port as #1

on error goto 0

rem	Added the CR in the lines of request, some servers give
rem	a "Bad request" response without him.

cr$= chr$ (13)

if debug then print "Sending HTTP request:"
req$= "GET " + page$ + " HTTP/1.1"
if debug then print #2, req$
print #1, req$; cr$
req$= "host: " + host$
if debug then print #2, req$
print #1, req$; cr$
req$= "Connection: close"
if debug then print #2, req$
print #1, req$; cr$
req$= "User-agent: Blassic htget/2.0 (compatible with itself;" + osname$ + ") Un saludo a mi mama"
if debug then print #2, req$
print #1, req$; cr$

rem
rem Uncomment the following lines if needed.
rem
rem req$= "Cache-control: no-cache"
rem if debug then print #2, req$
rem print #1, req$; cr$

if debug then print #2
print #1, cr$

if debug then print #2, "Waiting response.."

rem Skip http header

isChunked= 0
a$= "*"
l= 0
while not eof (1) and a$ <> "" and a$ <> chr$ (13)
	line input #1, a$
	if debug then print #2, a$
	if upper$ (left$ (a$, 18) ) = "TRANSFER-ENCODING:" then isChunked= not 0
	if upper$ (left$ (a$, 15) ) = "CONTENT-LENGTH:" then l= val (mid$ (a$, 16) )
wend

if debug then print #2, string$(70, "-")

if debug then if l <> 0 then print #2, "LENGTH: "; l

if isChunked then if debug then print #2, "CHUNKED" : print #2
if isChunked then goto readchunked

if l <> 0 then goto readfixed

rem Read the content

while not eof (1)
	line input #1, a$
	print a$
wend
goto readend

label readfixed

gosub readblock

goto readend

label readchunked

line input #1, a$
if len (a$) > 0 then if right$ (a$, 1) = chr$ (13) then a$ = left$ (a$, len (a$) - 1)
l= val ("&" + a$)

if debug then print #2, "<--"; a$; "--> CHUNK of "; l; " bytes"

if l = 0 then goto readend

gosub readblock

REM Pass the cr/lf.
line input #1, a$

goto readchunked

label readend

close 1

end

label readblock

repeat
	in$= input$ (l, #1)
	if debug then print #2: print #2, "Block of "; len (in$); " bytes read."
	print in$;
	l= l - len (in$)
until l = 0

return

label no_host

print #2, "ERROR: Host no disponible"
end

rem End of htget.bas
