xref: /plan9-contrib/sys/src/cmd/ip/httpd/init.c (revision d46c239f8612929b7dbade67d0d071633df3a15d)
1 #include <u.h>
2 #include <libc.h>
3 #include "httpd.h"
4 #include "httpsrv.h"
5 
6 void
7 usage(void)
8 {
9 	fprint(2, "usage: httpd [-b inbuf] [-d domain] [-r remoteip] [-w webroot] [-N netdir] [-R reqline] [-L logfd0 logfd1] method version uri [search]\n");
10 	exits("usage");
11 }
12 
13 char	*netdir;
14 char	*webroot;
15 char	*HTTPLOG = "httpd/log";
16 
17 static	HConnect	connect;
18 static	HSPriv		priv;
19 
20 HConnect*
21 init(int argc, char **argv)
22 {
23 	char *s, *vs;
24 
25 	hinit(&connect.hin, 0, Hread);
26 	hinit(&connect.hout, 1, Hwrite);
27 	hmydomain = nil;
28 	connect.replog = writelog;
29 	connect.private = &priv;
30 	priv.remotesys = nil;
31 	priv.remoteserv = nil;
32 	fmtinstall('D', hdatefmt);
33 	fmtinstall('H', httpfmt);
34 	fmtinstall('U', hurlfmt);
35 	netdir = "/net";
36 	ARGBEGIN{
37 	case 'b':
38 		s = ARGF();
39 		if(s != nil)
40 			hload(&connect.hin, s);
41 		break;
42 	case 'd':
43 		hmydomain = ARGF();
44 		break;
45 	case 'r':
46 		priv.remotesys = ARGF();
47 		break;
48 	case 'w':
49 		webroot = ARGF();
50 		break;
51 	case 'N':
52 		netdir = ARGF();
53 		break;
54 	case 'L':
55 		s = ARGF();
56 		if(s == nil)
57 			usage();
58 		logall[0] = strtol(s, nil, 10);
59 		s = ARGF();
60 		if(s == nil)
61 			usage();
62 		logall[1] = strtol(s, nil, 10);
63 		break;
64 	case 'R':
65 		s = ARGF();
66 		if(s == nil)
67 			usage();
68 		snprint((char*)connect.header, sizeof(connect.header), "%s", s);
69 		break;
70 	default:
71 		usage();
72 	}ARGEND
73 
74 	if(priv.remotesys == nil)
75 		priv.remotesys = "unknown";
76 	if(priv.remoteserv == nil)
77 		priv.remoteserv = "unknown";
78 	if(hmydomain == nil)
79 		hmydomain = "unknown";
80 	if(webroot == nil)
81 		webroot = "/usr/web";
82 
83 	/*
84 	 * open all files we might need before castrating namespace
85 	 */
86 	time(nil);
87 	syslog(0, HTTPLOG, nil);
88 
89 	if(argc != 4 && argc != 3)
90 		usage();
91 
92 	connect.req.meth = argv[0];
93 
94 	vs = argv[1];
95 	connect.req.vermaj = 0;
96 	connect.req.vermin = 9;
97 	if(strncmp(vs, "HTTP/", 5) == 0){
98 		vs += 5;
99 		connect.req.vermaj = strtoul(vs, &vs, 10);
100 		if(*vs == '.')
101 			vs++;
102 		connect.req.vermin = strtoul(vs, &vs, 10);
103 	}
104 
105 	connect.req.uri = argv[2];
106 	connect.req.search = argv[3];
107 	connect.head.closeit = 1;
108 	connect.hpos = (uchar*)strchr((char*)connect.header, '\0');
109 	connect.hstop = connect.hpos;
110 	connect.reqtime = time(nil);	/* not quite right, but close enough */
111 	return &connect;
112 }
113