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
|
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <plumb.h>
#include <9p.h>
#include "dat.h"
#include "fns.h"
static int plumbsendfd;
static int plumbwebfd;
static Channel *plumbchan;
static void plumbwebproc(void*);
static void plumbwebthread(void*);
static void plumbsendproc(void*);
void
plumbinit(void)
{
plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
plumbwebfd = plumbopen("web", OREAD|OCEXEC);
}
void
plumbstart(void)
{
plumbchan = chancreate(sizeof(Plumbmsg*), 0);
proccreate(plumbwebproc, nil, STACK);
threadcreate(plumbwebthread, nil, STACK);
}
static void
plumbwebthread(void*)
{
char *base;
Plumbmsg *m;
for(;;){
m = recvp(plumbchan);
if(m == nil)
threadexits(nil);
base = plumblookup(m->attr, "baseurl");
if(base == nil)
base = m->wdir;
plumburl(m->data, base);
plumbfree(m);
}
}
static void
plumbwebproc(void*)
{
Plumbmsg *m;
for(;;){
m = plumbrecv(plumbwebfd);
sendp(plumbchan, m);
if(m == nil)
threadexits(nil);
}
}
static void
addattr(Plumbmsg *m, char *name, char *value)
{
Plumbattr *a;
a = malloc(sizeof(Plumbattr));
a->name = name;
a->value = value;
a->next = m->attr;
m->attr = a;
}
static void
freeattrs(Plumbmsg *m)
{
Plumbattr *a, *next;
a = m->attr;
while(a != nil) {
next = a->next;
free(a);
a = next;
}
}
static struct
{
char *ctype;
char *ext;
}
ctypes[] =
{
{ "application/msword", "doc" },
{ "application/pdf", "pdf" },
{ "application/postscript", "ps" },
{ "application/rtf", "rtf" },
{ "image/gif", "gif" },
{ "image/jpeg", "jpg" },
{ "image/png", "png" },
{ "image/ppm", "ppm" },
{ "image/tiff", "tiff" },
{ "text/html", "html" },
{ "text/plain", "txt" },
{ "text/xml", "xml" },
};
void
replumb(Client *c)
{
int i;
Plumbmsg *m;
char name[128], *ctype, *ext, *p;
if(!c->plumbed)
return;
m = emalloc(sizeof(Plumbmsg));
m->src = "webfs";
m->dst = nil;
m->wdir = "/";
m->type = "text";
m->attr = nil;
addattr(m, "url", c->url->url);
ctype = c->contenttype;
ext = nil;
if(ctype != nil) {
addattr(m, "content-type", ctype);
for(i = 0; i < nelem(ctypes); i++) {
if(strcmp(ctype, ctypes[i].ctype) == 0) {
ext = ctypes[i].ext;
break;
}
}
}
if(ext == nil) {
p = strrchr(c->url->url, '/');
if(p != nil)
p = strrchr(p+1, '.');
if(p != nil && strlen(p) <= 5)
ext = p+1;
else
ext = "txt"; /* punt */
}
c->ext = ext;
if(0)fprint(2, "content type %s -> extension .%s\n", ctype, ext);
m->ndata = snprint(name, sizeof name, "/mnt/web/%d/body.%s", c->num, ext);
m->data = estrdup(name);
proccreate(plumbsendproc, m, STACK); /* separate proc to avoid a deadlock */
}
static void
plumbsendproc(void *x)
{
Plumbmsg *m;
m = x;
plumbsend(plumbsendfd, m);
freeattrs(m);
free(m->data);
free(m);
}
|