summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/httpd/netlib_history.c
blob: d24806225c49e869a04e8d9ed7cc88dcf8145e5a (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "httpd.h"
#include "httpsrv.h"

Hio *HO;
int diffb;

enum{ DAY = 24*60*60 };

void
lastbefore(ulong t, char *f, char *b)
{
	Tm *tm;
	Dir *dir;
	int try;
	ulong t0, mtime;

	t0 = t;
	for(try=0; try<10; try++) {
		tm = localtime(t);
		t -= DAY;
		sprint(b,"%.4d/%.2d%.2d/netlib/pub/%s",tm->year+1900,tm->mon+1,tm->mday,f);
		dir = dirstat(b);
		if(dir == nil)
			continue;
		mtime = dir->mtime;
		free(dir);
		if(mtime > t0)
			continue;
		return;
	}
	strcpy(b, "filenotfound");
}

// create explicit file for diff, which otherwise would create a
// mode 0600 file that it couldn't read (because running as none)
void
gunzip(char *f, char *tmp)
{
	int fd = open(tmp, OWRITE);

	if(fd < 0)  // can't happen
		return;
	switch(fork()){
	case 0:
		dup(fd, 1);
		close(fd);
		close(0);
		execl("/bin/gunzip", "gunzip", "-c", f, nil);
		hprint(HO, "can't exec gunzip: %r\n");
		break;
	case -1:
		hprint(HO, "fork failed: %r\n");
	default:
		while(waitpid() != -1)
			;
		break;
	}
	close(fd);
}

void
netlibhistory(char *file)
{
	char buf[500], pair[2][500], tmpf[2][30], *f;
	int toggle = 0, started = 0, limit;
	Dir *dir;
	ulong otime, dt;
	int i, fd, tmpcnt;

	if(strncmp(file, "../", 3) == 0 || strstr(file, "/../") ||
		strlen(file) >= sizeof(buf) - strlen("1997/0204/netlib/pub/0"))
		return;
	limit = 50;
	if(diffb){
		limit = 10;
		// create two tmp files for gunzip
		for(i = 0, tmpcnt = 0; i < 2 && tmpcnt < 20; tmpcnt++){
			snprint(tmpf[i], sizeof(tmpf[0]), "/tmp/d%x", tmpcnt);
			if(access(buf, AEXIST) == 0)
				continue;
			fd = create(tmpf[i], OWRITE, 0666);
			if(fd < 0)
				goto done;
			close(fd);
			i++;
		}
	}
	otime = time(0);
	hprint(HO,"<UL>\n");
	while(limit--){
		lastbefore(otime, file, buf);
		dir = dirstat(buf);
		if(dir == nil)
			goto done;
		dt = DAY/2;
		while(otime <= dir->mtime){
			lastbefore(otime-dt, file, buf);
			free(dir);
			dir = dirstat(buf);
			if(dir == nil)
				goto done;
			dt += DAY/2;
		}
		f = pair[toggle];
		strcpy(f, buf);
		if(diffb && strcmp(f+strlen(f)-3, ".gz") == 0){
			gunzip(f, tmpf[toggle]);
			strcpy(f, tmpf[toggle]);
		}
		if(diffb && started){
			hprint(HO, "<PRE>\n");
			hflush(HO);
			switch(fork()){
			case 0:
				execl("/bin/diff", "diff", "-nb",
					pair[1-toggle], pair[toggle], nil);
				hprint(HO, "can't exec diff: %r\n");
				break;
			case -1:
				hprint(HO, "fork failed: %r\n");
				break;
			default:
				while(waitpid() != -1)
					;
				break;
			}
			hprint(HO, "</PRE>\n");
		}
		hprint(HO,"<LI><A HREF=\"/historic/%s\">%s</A> %lld bytes\n",
			buf, 4+asctime(gmtime(dir->mtime)), dir->length);
		if(diffb)
			hprint(HO," <FONT SIZE=-1>(%s)</FONT>\n", pair[toggle]);
		toggle = 1-toggle;
		started = 1;
		otime = dir->mtime;
		free(dir);
	}
	hprint(HO,"<LI>...\n");
done:
	hprint(HO,"</UL>\n");
	if(diffb){
		remove(tmpf[0]);
		remove(tmpf[1]);
	}
}

int
send(HConnect *c)
{
	char *file, *s;
	HSPairs *q;

	if(strcmp(c->req.meth, "GET") != 0 && strcmp(c->req.meth, "HEAD") != 0)
		return hunallowed(c, "GET, HEAD");
	if(c->head.expectother || c->head.expectcont)
		return hfail(c, HExpectFail, nil);
	if(c->req.search == nil || !*c->req.search)
		return hfail(c, HNoData, "netlib_history");
	s = c->req.search;
	while((s = strchr(s, '+')) != nil)
		*s++ = ' ';
	file = nil;
	for(q = hparsequery(c, hstrdup(c, c->req.search)); q; q = q->next){
		if(strcmp(q->s, "file") == 0)
			file = q->t;
		else if(strcmp(q->s, "diff") == 0)
			diffb = 1;
	}
	if(file == nil)
		return hfail(c, HNoData, "netlib_history missing file field");
	logit(c, "netlib_hist %s%s", file, diffb?" DIFF":"");

	if(c->req.vermaj){
		hokheaders(c);
		hprint(HO, "Content-type: text/html\r\n");
		hprint(HO, "\r\n");
	}
	if(strcmp(c->req.meth, "HEAD") == 0){
		writelog(c, "Reply: 200 netlib_history 0\n");
		hflush(HO);
		exits(nil);
	}

	hprint(HO, "<HEAD><TITLE>%s history</TITLE></HEAD>\n<BODY>\n",file);
	hprint(HO, "<H2>%s history</H2>\n",file);
	hprint(HO, "<I>Netlib's copy of %s was changed\n", file);
	hprint(HO, "on the dates shown.  <BR>Click on the date link\n");
	hprint(HO, "to retrieve the corresponding version.</I>\n");
	if(diffb){
		hprint(HO, "<BR><I>Lines beginning with &lt; are for the\n");
		hprint(HO, "newer of the two versions.</I>\n");
	}

	if(chdir("/usr/web/historic") < 0)
		hprint(HO, "chdir failed: %r\n");
	netlibhistory(file);

	hprint(HO, "<BR><A HREF=\"http://cm.bell-labs.com/who/ehg\">Eric Grosse</A>\n");
	hprint(HO, "</BODY></HTML>\n");
	hflush(HO);
	writelog(c, "Reply: 200 netlib_history %ld %ld\n", HO->seek, HO->seek);
	return 1;
}

void
main(int argc, char **argv)
{
	HConnect *c;

	c = init(argc, argv);
	HO = &c->hout;
	if(hparseheaders(c, HSTIMEOUT) >= 0)
		send(c);
	exits(nil);
}