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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
implement Message;
include "sys.m";
sys: Sys;
include "string.m";
S : String;
include "bufio.m";
B : Bufio;
Iobuf: import B;
include "message.m";
msg: Message;
msglog: ref Sys->FD;
init(bufio: Bufio, smod: String)
{
sys = load Sys Sys->PATH;
S = smod;
B = bufio;
}
sptab : con " \t";
crlf : con "\r\n";
Msg.newmsg() : ref Msg
{
return ref Msg("", nil, nil, nil, 0);
}
# Read a message header from fd and return a Msg
# the header fields.
# If withprefix is true, read one line first and put it
# in the prefixline field of the Msg (without terminating \r\n)
# Return nil if there is a read error or eof before the
# header is completely read.
Msg.readhdr(io: ref Iobuf, withprefix: int) : (ref Msg, string)
{
m := Msg.newmsg();
l : list of Nameval = nil;
needprefix := withprefix;
for(;;) {
line := getline(io);
n := len line;
if(n == 0) {
if(withprefix && m.prefixline != "")
break;
return(nil, "msg read hdr error: no header");
}
if(line[n-1] != '\n')
return (m, "msg read hdr error: incomplete header");
if(n >= 2 && line[n-2] == '\r')
line = line[0:n-2];
else
line = line[0:n-1];
if(needprefix) {
m.prefixline = line;
needprefix = 0;
}
else {
if(line == "")
break;
if(S->in(line[0], sptab)) {
if(l == nil)
continue;
nv := hd l;
l = Nameval(nv.name, nv.value + " " + S->drop(line, sptab)) :: tl l;
}
else {
(nam, val) := S->splitl(line, ":");
if(val == nil)
continue; # no colon
l = Nameval(S->tolower(nam), S->drop(val[1:], sptab)) :: l;
}
}
}
nh := len l;
if(nh > 0) {
m.fields = array[nh] of Nameval;
for(i := nh-1; i >= 0; i--) {
m.fields[i] = hd l;
l = tl l;
}
}
return (m, "");
}
glbuf := array[300] of byte;
# Like io.gets('\n'), but assume Latin-1 instead of UTF encoding
getline(io: ref Iobuf): string
{
imax := len glbuf - 1;
for(i := 0; i < imax; ) {
c := io.getb();
if(c < 0)
break;
if(c < 128)
glbuf[i++] = byte c;
else
i += sys->char2byte(c, glbuf, i);
if(c == '\n')
break;
if(i == imax) {
imax += 100;
if(imax > 1000)
break; # Header lines aren't supposed to be > 1000
newglbuf := array[imax] of byte;
newglbuf[0:] = glbuf[0:i];
glbuf = newglbuf;
}
}
ans := string glbuf[0:i];
return ans;
}
Bbufsize: con 8000;
# Read the body of the message, assuming the header has been processed.
# If content-length has been specified, read exactly that many bytes
# or until eof; else read until done.
# Return "" if all is OK, else return an error string.
Msg.readbody(m: self ref Msg, io: ref Iobuf) : string
{
(clfnd, cl) := m.fieldval("content-length");
if(clfnd) {
clen := int cl;
if(clen > 0) {
m.body = array[clen] of byte;
n := B->io.read(m.body, clen);
m.bodylen = n;
if(n != clen)
return "short read";
}
}
else {
m.body = array[Bbufsize] of byte;
curlen := 0;
for(;;) {
avail := len m.body - curlen;
if(avail <= 0) {
newa := array[len m.body + Bbufsize] of byte;
if(curlen > 0)
newa[0:] = m.body[0:curlen];
m.body = newa;
avail = Bbufsize;
}
n := B->io.read(m.body[curlen:], avail);
if(n < 0)
return sys->sprint("readbody error %r");
if(n == 0)
break;
else
curlen += n;
}
m.bodylen = curlen;
}
return "";
}
# Look for name (lowercase) in the fields of m
# and (1, field value) if found, or (0,"") if not.
# If multiple fields with the same name exist,
# the value is defined as the comma separated list
# of all such values.
Msg.fieldval(m: self ref Msg, name: string) : (int, string)
{
n := len m.fields;
ans := "";
found := 0;
for(i := 0; i < n; i++) {
if(m.fields[i].name == name) {
v := m.fields[i].value;
if(found)
ans = ans + ", " + v;
else
ans = v;
found = 1;
}
}
return (found, ans);
}
Msg.addhdrs(m: self ref Msg, hdrs: list of Nameval)
{
nh := len hdrs;
if(nh == 0)
return;
onh := len m.fields;
newa := array[nh + onh] of Nameval;
newa[0:] = m.fields;
i := onh;
while(hdrs != nil) {
newa[i++] = hd hdrs;
hdrs = tl hdrs;
}
m.fields = newa;
}
Msg.update(m: self ref Msg, name, value: string)
{
for(i := 0; i < len m.fields; i++)
if(m.fields[i].name == name) {
m.fields[i] = Nameval(name, value);
return;
}
m.addhdrs(Nameval(name, value) :: nil);
}
Msg.header(m: self ref Msg) : string
{
s := "";
for(i := 0; i < len m.fields; i++) {
nv := m.fields[i];
s += nv.name + ": " + nv.value + "\n";
}
return s;
}
Msg.writemsg(m: self ref Msg, io: ref Iobuf) : string
{
n := 0;
if(m.prefixline != nil) {
n = B->io.puts(m.prefixline);
if(n >= 0)
n = B->io.puts(crlf);
}
for(i := 0; i < len m.fields; i++) {
nv := m.fields[i];
if(n >= 0)
n = B->io.puts(nv.name);
if(n >= 0)
n = B->io.puts(": ");
if(n >= 0)
n = B->io.puts(nv.value);
if(n >= 0)
n = B->io.puts(crlf);
}
if(n >= 0)
n = B->io.puts(crlf);
if(n >= 0 && m.bodylen > 0)
n = B->io.write(m.body, m.bodylen);
if(n < 0)
return sys->sprint("msg write error: %r");
B->io.flush();
return "";
}
|