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
219
220
221
222
223
224
225
226
227
228
229
230
|
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include <thread.h>
#include <cursor.h>
#include <mouse.h>
#include <keyboard.h>
#include <frame.h>
#include <plumb.h>
#include <html.h>
#include "dat.h"
#include "fns.h"
Url *
urlalloc(Runestr *src, Runestr *post, int m)
{
Url *u;
u = emalloc(sizeof(Url));
copyrunestr(&u->src, src);
if(m==HPost)
copyrunestr(&u->post, post);
u->method = m;
incref(u);
return u;
}
void
urlfree(Url *u)
{
if(u && decref(u)==0){
closerunestr(&u->src);
closerunestr(&u->act);
closerunestr(&u->post);
closerunestr(&u->ctype);
free(u);
}
}
Url *
urldup(Url *a)
{
Url *b;
b = emalloc(sizeof(Url));
b->method = a->method;
copyrunestr(&b->src, &a->src);
copyrunestr(&b->act, &a->act);
copyrunestr(&b->post, &a->post);
copyrunestr(&b->ctype, &a->ctype);
return b;
}
static
Runestr
getattr(int conn, char *s)
{
char buf[BUFSIZE];
int fd, n;
snprint(buf, sizeof(buf), "%s/%d/%s", webmountpt, conn, s);
fd = open(buf, OREAD);
if(fd < 0)
error("can't open attr file");
n = read(fd, buf, sizeof(buf)-1);
if(n < 0)
error("can't read");
close(fd);
buf[n] = '\0';
return (Runestr){runesmprint("%s", buf), n};
}
int
urlopen(Url *u)
{
char buf[BUFSIZE];
int cfd, fd, conn, n;
snprint(buf, sizeof(buf), "%s/clone", webmountpt);
cfd = open(buf, ORDWR);
if(cfd < 0)
error("can't open clone file");
n = read(cfd, buf, sizeof(buf)-1);
if(n <= 0)
error("reading clone");
buf[n] = '\0';
conn = atoi(buf);
snprint(buf, sizeof(buf), "url %S", u->src.r);
if(write(cfd, buf, strlen(buf)) < 0){
// fprint(2, "write: %s: %r\n", buf);
Err:
close(cfd);
return -1;
}
if(u->method==HPost && u->post.r != nil){
snprint(buf, sizeof(buf), "%s/%d/postbody", webmountpt, conn);
fd = open(buf, OWRITE);
if(fd < 0){
// fprint(2, "urlopen: bad query: %s: %r\n", buf);
goto Err;
}
snprint(buf, sizeof(buf), "%S", u->post.r);
if(write(fd, buf, strlen(buf)) < 0)
fprint(2, "urlopen: bad query: %s: %r\n", buf);
close(fd);
}
snprint(buf, sizeof(buf), "%s/%d/body", webmountpt, conn);
fd = open(buf, OREAD);
if(fd < 0){
// fprint(2, "open: %S: %r\n", u->src.r);
goto Err;
}
u->ctype = getattr(conn, "contenttype");
u->act = getattr(conn, "parsed/url");
if(u->act.nr == 0)
copyrunestr(&u->act, &u->src);
close(cfd);
return fd;
}
void
urlcanon(Rune *name){
Rune *s, *t;
Rune **comp, **p, **q;
int rooted;
name = runestrchr(name, L'/')+2;
rooted=name[0]==L'/';
/*
* Break the name into a list of components
*/
comp=emalloc(runestrlen(name)*sizeof(char *));
p=comp;
*p++=name;
for(s=name;;s++){
if(*s==L'/'){
*p++=s+1;
*s='\0';
}
else if(*s=='\0')
break;
}
*p=0;
/*
* go through the component list, deleting components that are empty (except
* the last component) or ., and any .. and its non-.. predecessor.
*/
p=q=comp;
while(*p){
if(runestrcmp(*p, L"")==0 && p[1]!=0
|| runestrcmp(*p, L".")==0)
p++;
else if(runestrcmp(*p, L"..")==0 && q!=comp && runestrcmp(q[-1], L"..")!=0){
--q;
p++;
}
else
*q++=*p++;
}
*q=0;
/*
* rebuild the path name
*/
s=name;
if(rooted) *s++='/';
for(p=comp;*p;p++){
t=*p;
while(*t) *s++=*t++;
if(p[1]!=0) *s++='/';
}
*s='\0';
free(comp);
}
/* this is a HACK */
Rune *
urlcombine(Rune *b, Rune *u)
{
Rune *p, *q, *sep, *s;
Rune endrune[] = { L'?', L'#' };
int i, restore;
if(u == nil)
error("urlcombine: u == nil");
if(validurl(u))
return erunestrdup(u);
if(b==nil || !validurl(b))
error("urlcombine: b==nil || !validurl(b)");
if(runestrncmp(u, L"//", 2) == 0){
q = runestrchr(b, L':');
return runesmprint("%.*S:%S", (int)(q-b), b, u);
}
p = runestrstr(b, L"://")+3;
sep = L"";
q = nil;
if(*u ==L'/')
q = runestrchr(p, L'/');
else if(*u==L'#' || *u==L'?'){
for(i=0; i<nelem(endrune); i++)
if(q = runestrchr(p, endrune[i]))
break;
}else{
sep = L"/";
restore = 0;
s = runestrchr(p, L'?');
if(s != nil){
*s = '\0';
restore = 1;
}
q = runestrrchr(p, L'/');
if(restore)
*s = L'?';
}
if(q == nil)
p = runesmprint("%S%S%S", b, sep, u);
else
p = runesmprint("%.*S%S%S", (int)(q-b), b, sep, u);
urlcanon(p);
return p;
}
|