-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
298 lines (253 loc) · 7.43 KB
/
Copy pathindex.js
File metadata and controls
298 lines (253 loc) · 7.43 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const MAX_HEADER_SIZE = 16 * 1024;
const HEADER_TERMINATOR = new Uint8Array([13, 10, 13, 10]);
class Request {
constructor(uri, opts) {
this._uri = uri;
this._opts = opts;
}
get body() {
return this._opts.body;
}
get headers() {
return new Headers(this._opts.headers);
}
get method() {
return this._opts.method;
}
get url() {
return this._uri;
}
}
class Server {
constructor() {
this._encoder = new TextEncoder();
this._decoder = new TextDecoder('utf-8');
}
async serve(listener, callback) {
this._domain = listener.getDomain();
const connStreamReader = listener.connectionStream.getReader();
while (true) {
const { value: conn, done } = await connStreamReader.read();
if (done) {
break;
}
this.handleConn(conn, callback).catch((error) => {
console.error('http-js connection error:', error);
});
}
}
async handleConn(conn, callback) {
const reader = conn.readable.getReader();
const { headerBytes, bodyStart } = await readHeaders(reader);
const headerText = this._decoder.decode(headerBytes);
const headerLines = headerText.split('\r\n');
const requestLine = headerLines.shift().split(' ');
if (requestLine.length !== 3) {
throw new Error('Invalid HTTP request line');
}
const [method, path] = requestLine;
/** @type {HeadersInit} */
const headers = {};
for (const header of headerLines) {
const separator = header.indexOf(':');
if (separator < 1) {
throw new Error('Invalid HTTP header');
}
const name = header.slice(0, separator).trim().toLowerCase();
const value = header.slice(separator + 1).trim();
headers[name] = headers[name] ? `${headers[name]}, ${value}` : value;
}
const contentLength = parseContentLength(headers['content-length']);
const body = contentLength > 0
? requestBody(reader, bodyStart, contentLength)
: null;
const request = new Request(`https://${this._domain}${path}`, {
method,
headers,
body,
});
const response = await callback(request);
if (!(response instanceof Response)) {
throw new Error('HTTP handler must return a Response');
}
await this._sendResponse(conn, response, method === 'HEAD');
}
async _sendResponse(conn, response, omitBody) {
const statusText = response.statusText || defaultStatusText(response.status);
let headerText = `HTTP/1.1 ${response.status} ${statusText}\r\n`;
for (const [name, value] of response.headers.entries()) {
headerText += `${name}: ${value}\r\n`;
}
headerText += 'Connection: close\r\n\r\n';
const writer = conn.writable.getWriter();
await writer.write(this._encoder.encode(headerText));
writer.releaseLock();
if (!omitBody && response.body !== null) {
await response.body.pipeTo(conn.writable);
}
else {
const closeWriter = conn.writable.getWriter();
await closeWriter.close();
}
}
}
async function readHeaders(reader) {
let buffered = new Uint8Array(0);
while (true) {
const { value, done } = await reader.read();
if (done) {
throw new Error('Connection closed before HTTP headers finished');
}
const next = new Uint8Array(buffered.byteLength + value.byteLength);
next.set(buffered);
next.set(value, buffered.byteLength);
buffered = next;
const headerEnd = indexOf(buffered, HEADER_TERMINATOR);
if (headerEnd >= 0) {
return {
headerBytes: buffered.slice(0, headerEnd),
bodyStart: buffered.slice(headerEnd + HEADER_TERMINATOR.byteLength),
};
}
if (buffered.byteLength > MAX_HEADER_SIZE) {
throw new Error('HTTP headers too large');
}
}
}
function requestBody(reader, firstChunk, contentLength) {
let bytesRead = 0;
let pending = firstChunk;
return new ReadableStream({
async pull(controller) {
if (bytesRead >= contentLength) {
controller.close();
return;
}
let chunk;
if (pending.byteLength > 0) {
chunk = pending;
pending = new Uint8Array(0);
}
else {
const result = await reader.read();
if (result.done) {
controller.error(new Error('Connection closed before HTTP body finished'));
return;
}
chunk = result.value;
}
const remaining = contentLength - bytesRead;
if (chunk.byteLength > remaining) {
chunk = chunk.slice(0, remaining);
}
bytesRead += chunk.byteLength;
controller.enqueue(chunk);
if (bytesRead >= contentLength) {
controller.close();
}
},
});
}
function directoryTreeHandler(dirTree, opt) {
return async (request) => {
const url = new URL(request.url);
let file;
try {
file = await dirTree.openFile(url.pathname);
}
catch {
return new Response('Not found', {
status: 404,
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
});
}
let sendFile = file;
let statusCode = 200;
const headers = { ...((opt && opt.headers) || {}) };
const rangeHeader = request.headers.get('range');
if (rangeHeader) {
let range;
try {
range = parseRangeHeader(rangeHeader, file.size);
}
catch {
return new Response(null, {
status: 416,
headers: { 'Content-Range': `bytes */${file.size}` },
});
}
sendFile = file.slice(range.start, range.end + 1);
headers['Content-Range'] = `bytes ${range.start}-${range.end}/${file.size}`;
statusCode = 206;
}
headers['Accept-Ranges'] = 'bytes';
headers['Content-Type'] = file.type || 'application/octet-stream';
headers['Content-Length'] = String(sendFile.size);
return new Response(sendFile.stream(), {
status: statusCode,
headers,
});
};
}
function parseRangeHeader(headerText, maxSize) {
const match = /^bytes=(\d*)-(\d*)$/.exec(headerText.trim());
if (!match || maxSize <= 0 || (!match[1] && !match[2])) {
throw new Error('Invalid range');
}
let start;
let end;
if (!match[1]) {
const suffixLength = Number(match[2]);
if (!Number.isSafeInteger(suffixLength) || suffixLength <= 0) {
throw new Error('Invalid range');
}
start = Math.max(0, maxSize - suffixLength);
end = maxSize - 1;
}
else {
start = Number(match[1]);
end = match[2] ? Number(match[2]) : maxSize - 1;
}
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) ||
start < 0 || start >= maxSize || end < start) {
throw new Error('Invalid range');
}
return { start, end: Math.min(end, maxSize - 1) };
}
function parseContentLength(value) {
if (value === undefined) {
return 0;
}
const length = Number(value);
if (!Number.isSafeInteger(length) || length < 0) {
throw new Error('Invalid Content-Length');
}
return length;
}
function indexOf(haystack, needle) {
outer: for (let i = 0; i <= haystack.byteLength - needle.byteLength; i++) {
for (let j = 0; j < needle.byteLength; j++) {
if (haystack[i + j] !== needle[j]) {
continue outer;
}
}
return i;
}
return -1;
}
function defaultStatusText(status) {
const statuses = {
200: 'OK',
206: 'Partial Content',
400: 'Bad Request',
404: 'Not Found',
416: 'Range Not Satisfiable',
500: 'Internal Server Error',
};
return statuses[status] || '';
}
export {
Server,
parseRangeHeader,
directoryTreeHandler,
};