1#!/usr/local/bin/lua52 2 3local cqueues = require("cqueues") 4local socket = require("cqueues.socket") 5local host, port = ... 6 7host = host or "google.com" 8port = tonumber(port or 80) 9 10local http = socket.connect(host, port) 11 12if port == 443 then 13 http:starttls() 14end 15 16local cq = cqueues.new() 17 18cq:wrap(function() 19 http:write("GET / HTTP/1.0\n") 20 http:write(string.format("Host: %s:%d\n\n", host, port)) 21 22 local status = http:read() 23 print("!", status) 24 25 for ln in http:lines"*h" do 26 print("|", ln) 27 end 28 29 local empty = http:read"*L" 30 31 if empty and empty:match"^[^\r\n]" then 32 http:unget(empty) 33 end 34 35 print"~" 36 37 for ln in http:lines"*L" do 38 io.stdout:write(ln) 39 end 40 41 http:close() 42end) 43 44while not cq:empty() do 45 local ok, err = cq:step() 46 47 if not ok then 48 error("cqueue: " .. err) 49 end 50end 51 52