From: Markus Triska Date: Sat, 13 Jun 2020 07:32:08 +0000 (+0200) Subject: ADDED: library(http/http_open), opening HTTP and HTTPS streams for reading X-Git-Tag: v0.8.127~26^2 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=19acd608a5b53348716aac2db644f8da629182d4;p=scryer-prolog.git ADDED: library(http/http_open), opening HTTP and HTTPS streams for reading Example: ?- http_open("https://github.com/mthom/scryer-prolog", Stream, []), length(Ls, 10), maplist(get_char(Stream), Ls). Yielding: %@ Stream = '$stream'(0x7fba4c59ef10), Ls = "\n\n\n\n\n []. +list([L|Ls]) --> [L], list(Ls). + +handle_response('2', _, Stream, Stream). % ok +handle_response('3', HeaderLines, Stream0, Stream) :- % redirect + close(Stream0), + once((member(Line, HeaderLines), + phrase(("Location: ",list(Location),"\r\n"), Line))), + http_open(Location, Stream, []). + +% Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF + +read_header_lines(Stream, Hs) :- + read_line_to_chars(Stream, Cs, []), + ( Cs == "" -> Hs = [] + ; Cs == "\r\n" -> Hs = [] + ; Hs = [Cs|Rest], + read_header_lines(Stream, Rest) + ). + +chars_host_url(Cs, Host, [/|Us]) :- + ( phrase((list(Hs),"/",list(Us)), Cs) -> + true + ; Hs = Cs, + Us = [] + ), + atom_chars(Host, Hs). + +connect(https, Host, Stream) :- + socket_client_open(Host:443, Stream, [tls(true)]). +connect(http, Host, Stream) :- + socket_client_open(Host:80, Stream, []). +