############################################################################
#
#	File:     htget.icn
#
#	Subject:  Program to get Web file using HTTP protocol
#
#	Author:   Gregg M. Townsend
#
#	Date:     May 15, 2002
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  Htget retrieves the raw text of a file from the world wide web using
#  HTTP protocol.  (Other protocols such as FTP are not supported.)
#
#  usage:  htget [-h | -b] URL
#
#  The URL may be given with or without the "http://" prefix.
#
#  If -h is given, a HEAD request is sent, requesting only information
#  instead of the complete file.
#
#  If -b is given, the header is stripped and the body is copied
#  in binary mode.
#
############################################################################
#
#  Links:  cfunc, options
#
############################################################################
#
#  Requires: UNIX, dynamic loading
#
############################################################################

link cfunc
link options

procedure main(args)
   local opts, req, url, host, port, path, f

   opts := options(args, "hb")
   if \opts["h"] then
      req := "HEAD"
   else
      req := "GET"

   url := \args[1] | stop("usage: ", &progname, " [-h] url")

   url ? {
      ="http:" | ="HTTP:"			# skip optional http:
      tab(many('/'))				# skip optional //
      host := tab(upto(':/') | 0)
      if *host = 0 then
	 host := "localhost"
      if not (=":" & (port := integer(tab(upto('/'))))) then
         port := 80
      if pos(0) then
         path := "/"
      else
         path := tab(0)
   }

   if not (f := tconnect(host, port)) then
       stop ("cannot connect to ", host, ":", port)

   writes(f, req, " ", path, " HTTP/1.0\r\n")
   writes(f, "Host: ", host, "\r\n")
   writes(f, "\r\n")
   flush(f)
   seek(f, 1)

   if \opts["b"] then {
      while *read(f) > 0
      while writes(reads(f, 32768))
      }
   else
      while write(read(f))
end
