home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / htget.icn < prev    next >
Text File  |  2001-10-04  |  2KB  |  83 lines

  1. ############################################################################
  2. #
  3. #    File:     htget.icn
  4. #
  5. #    Subject:  Program to get Web file using HTTP protocol
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     September 27, 2001
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Htget retrieves the raw text of a file from the world wide web using
  18. #  HTTP protocol.  (Other protocols such as FTP are not supported.)
  19. #
  20. #  usage:  htget [-h | -b] URL
  21. #
  22. #  The URL may be given with or without the "http://" prefix.
  23. #
  24. #  If -h is given, a HEAD request is sent, requesting only information
  25. #  instead of the complete file.
  26. #
  27. #  If -b is given, the header is stripped and the body is copied
  28. #  in binary mode.
  29. #
  30. ############################################################################
  31. #
  32. #  Links:  cfunc, options
  33. #
  34. ############################################################################
  35. #
  36. #  Requires: UNIX, dynamic loading
  37. #
  38. ############################################################################
  39.  
  40. link cfunc
  41. link options
  42.  
  43. procedure main(args)
  44.    local opts, req, url, host, port, path, f
  45.  
  46.    opts := options(args, "hb")
  47.    if \opts["h"] then
  48.       req := "HEAD"
  49.    else
  50.       req := "GET"
  51.  
  52.    url := \args[1] | stop("usage: ", &progname, " [-h] url")
  53.  
  54.    url ? {
  55.       ="http:" | ="HTTP:"            # skip optional http:
  56.       tab(many('/'))                # skip optional //
  57.       host := tab(upto(':/') | 0)
  58.       if *host = 0 then
  59.      host := "localhost"
  60.       if not (=":" & (port := integer(tab(upto('/'))))) then
  61.          port := 80
  62.       if pos(0) then
  63.          path := "/"
  64.       else
  65.          path := tab(0)
  66.    }
  67.  
  68.    if not (f := tconnect(host, port)) then
  69.        stop ("cannot connect to ", host, ":", port)
  70.  
  71.    writes(f, req, " ", path, " HTTP/1.0\r\n")
  72.    writes(f, "\r\n")
  73.    flush(f)
  74.    seek(f, 1)
  75.  
  76.    if \opts["b"] then {
  77.       while *read(f) > 0
  78.       while writes(reads(f, 32768))
  79.       }
  80.    else
  81.       while write(read(f))
  82. end
  83.