Debian Codegraph @

awklib/eg/network/coreserv.awk

raw
1 # CGI Library and core of a web server
2 #
3 # Juergen Kahrs, [email protected]
4 # with Arnold Robbins, [email protected]
5 # September 2000
6
7 # Global arrays
8 # GETARG --- arguments to CGI GET command
9 # MENU --- menu items (path names)
10 # PARAM --- parameters of form x=y
11
12 # Optional variable MyHost contains host address
13 # Optional variable MyPort contains port number
14 # Needs TopHeader, TopDoc, TopFooter
15 # Sets MyPrefix, HttpService, Status, Reason
16
17 BEGIN {
18 if (MyHost == "") {
19 "uname -n" | getline MyHost
20 close("uname -n")
21 }
22 if (MyPort == 0) MyPort = 8080
23 HttpService = "/inet/tcp/" MyPort "/0/0"
24 MyPrefix = "http://" MyHost ":" MyPort
25 SetUpServer()
26 while ("awk" != "complex") {
27 # header lines are terminated this way
28 RS = ORS = "\r\n"
29 Status = 200 # this means OK
30 Reason = "OK"
31 Header = TopHeader
32 Document = TopDoc
33 Footer = TopFooter
34 if (GETARG["Method"] == "GET") {
35 HandleGET()
36 } else if (GETARG["Method"] == "HEAD") {
37 # not yet implemented
38 } else if (GETARG["Method"] != "") {
39 print "bad method", GETARG["Method"]
40 }
41 Prompt = Header Document Footer
42 print "HTTP/1.0", Status, Reason |& HttpService
43 print "Connection: Close" |& HttpService
44 print "Pragma: no-cache" |& HttpService
45 len = length(Prompt) + length(ORS)
46 print "Content-length:", len |& HttpService
47 print ORS Prompt |& HttpService
48 # ignore all the header lines
49 while ((HttpService |& getline) > 0)
50 continue
51 # stop talking to this client
52 close(HttpService)
53 # wait for new client request
54 HttpService |& getline
55 # do some logging
56 print systime(), strftime(), $0
57 CGI_setup($1, $2, $3)
58 }
59 }
60
61 function CGI_setup(method, uri, version, i)
62 {
63 delete GETARG
64 delete MENU
65 delete PARAM
66 GETARG["Method"] = method
67 GETARG["URI"] = uri
68 GETARG["Version"] = version
69
70 i = index(uri, "?")
71 if (i > 0) { # is there a "?" indicating a CGI request?
72 split(substr(uri, 1, i-1), MENU, "[/:]")
73 split(substr(uri, i+1), PARAM, "&")
74 for (i in PARAM) {
75 PARAM[i] = _CGI_decode(PARAM[i])
76 j = index(PARAM[i], "=")
77 GETARG[substr(PARAM[i], 1, j-1)] = \
78 substr(PARAM[i], j+1)
79 }
80 } else { # there is no "?", no need for splitting PARAMs
81 split(uri, MENU, "[/:]")
82 }
83 for (i in MENU) # decode characters in path
84 if (i > 4) # but not those in host name
85 MENU[i] = _CGI_decode(MENU[i])
86 }
87 function _CGI_decode(str, hexdigs, i, pre, code1, code2,
88 val, result)
89 {
90 hexdigs = "123456789abcdef"
91
92 i = index(str, "%")
93 if (i == 0) # no work to do
94 return str
95
96 do {
97 pre = substr(str, 1, i-1) # part before %xx
98 code1 = substr(str, i+1, 1) # first hex digit
99 code2 = substr(str, i+2, 1) # second hex digit
100 str = substr(str, i+3) # rest of string
101
102 code1 = tolower(code1)
103 code2 = tolower(code2)
104 val = index(hexdigs, code1) * 16 \
105 + index(hexdigs, code2)
106
107 result = result pre sprintf("%c", val)
108 i = index(str, "%")
109 } while (i != 0)
110 if (length(str) > 0)
111 result = result str
112 return result
113 }