summaryrefslogtreecommitdiff
path: root/ipl/progs/htget.icn
blob: 09746a031154ef71ffe078d4456d431febf5335f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
############################################################################
#
#	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