-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathBlobBatchHandler.ts
More file actions
583 lines (518 loc) · 18.7 KB
/
Copy pathBlobBatchHandler.ts
File metadata and controls
583 lines (518 loc) · 18.7 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import { URLBuilder } from "@azure/ms-rest-js";
import IAccountDataStore from "../../common/IAccountDataStore";
import { OAuthLevel } from "../../common/models";
import IExtentStore from "../../common/persistence/IExtentStore";
import AccountSASAuthenticator from "../authentication/AccountSASAuthenticator";
import BlobSASAuthenticator from "../authentication/BlobSASAuthenticator";
import BlobSharedKeyAuthenticator from "../authentication/BlobSharedKeyAuthenticator";
import BlobTokenAuthenticator from "../authentication/BlobTokenAuthenticator";
import IAuthenticator from "../authentication/IAuthenticator";
import PublicAccessAuthenticator from "../authentication/PublicAccessAuthenticator";
import BlobStorageContext from "../context/BlobStorageContext";
import StorageError from "../errors/StorageError";
import StorageErrorFactory from "../errors/StorageErrorFactory";
import Operation from "../generated/artifacts/operation";
import Context from "../generated/Context";
import MiddlewareError from "../generated/errors/MiddlewareError";
import IHandlers from "../generated/handlers/IHandlers";
import IRequest, { HttpMethod } from "../generated/IRequest";
import IResponse from "../generated/IResponse";
import deserializerMiddleware from "../generated/middleware/deserializer.middleware";
import dispatchMiddleware from "../generated/middleware/dispatch.middleware";
import endMiddleware from "../generated/middleware/end.middleware";
import errorMiddleware from "../generated/middleware/error.middleware";
import HandlerMiddlewareFactory from "../generated/middleware/HandlerMiddlewareFactory";
import serializerMiddleware from "../generated/middleware/serializer.middleware";
import ILogger from "../generated/utils/ILogger";
import AuthenticationMiddlewareFactory from "../middlewares/AuthenticationMiddlewareFactory";
import { internalBlobStorageContextMiddleware } from "../middlewares/blobStorageContext.middleware";
import IBlobMetadataStore from "../persistence/IBlobMetadataStore";
import { DEFAULT_CONTEXT_PATH, HTTP_HEADER_DELIMITER, HTTP_LINE_ENDING } from "../utils/constants";
import AppendBlobHandler from "./AppendBlobHandler";
import { BlobBatchSubRequest } from "./BlobBatchSubRequest";
import { BlobBatchSubResponse } from "./BlobBatchSubResponse";
import BlobHandler from "./BlobHandler";
import BlockBlobHandler from "./BlockBlobHandler";
import ContainerHandler from "./ContainerHandler";
import PageBlobHandler from "./PageBlobHandler";
import PageBlobRangesManager from "./PageBlobRangesManager";
import ServiceHandler from "./ServiceHandler";
type SubRequestNextFunction = (err?: any) => void;
type SubRequestHandler = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => any;
type SubRequestErrorHandler = (err: any, req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => any;
export class BlobBatchHandler {
private handlers: IHandlers;
private authenticators: IAuthenticator[];
private handlePipeline: SubRequestHandler[];
private errorHandler: SubRequestErrorHandler;
private operationFinder: SubRequestHandler[];
constructor(
private readonly accountDataStore: IAccountDataStore,
private readonly oauth: OAuthLevel | undefined,
private readonly metadataStore: IBlobMetadataStore,
private readonly extentStore: IExtentStore,
private readonly logger: ILogger,
private readonly loose: boolean,
private readonly disableProductStyle?: boolean
) {
const subRequestContextMiddleware = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
const urlbuilder = URLBuilder.parse(req.getUrl());
internalBlobStorageContextMiddleware(
new BlobStorageContext(locals, DEFAULT_CONTEXT_PATH),
req,
res,
urlbuilder.getHost()!,
urlbuilder.getPath()!,
next,
true,
this.disableProductStyle,
this.loose
);
};
const subRequestDispatchMiddleware = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
dispatchMiddleware(
new Context(locals, DEFAULT_CONTEXT_PATH, req, res),
req,
next,
this.logger
);
};
// AuthN middleware, like shared key auth or SAS auth
const authenticationMiddlewareFactory = new AuthenticationMiddlewareFactory(
this.logger
);
this.authenticators = [
new PublicAccessAuthenticator(this.metadataStore, logger),
new BlobSharedKeyAuthenticator(this.accountDataStore, logger),
new AccountSASAuthenticator(
this.accountDataStore,
this.metadataStore,
logger
),
new BlobSASAuthenticator(
this.accountDataStore,
this.metadataStore,
logger
)
];
if (this.oauth !== undefined) {
this.authenticators.push(
new BlobTokenAuthenticator(this.accountDataStore, this.oauth, logger)
);
}
const subRequestAuthenticationMiddleware = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
const context = new BlobStorageContext(locals, DEFAULT_CONTEXT_PATH);
authenticationMiddlewareFactory.authenticate(
context,
req,
res,
this.authenticators
).then(pass => {
// TODO: To support public access, we need to modify here to reject request later in handler
if (pass) {
next();
} else {
next(
StorageErrorFactory.getAuthorizationFailure(context.contextId!)
);
}
})
.catch(errorInfo =>
next(errorInfo));
};
const subRequestDeserializeMiddleware = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
deserializerMiddleware(
new Context(locals, DEFAULT_CONTEXT_PATH, req, res),
req,
next,
this.logger
);
};
this.handlers = {
appendBlobHandler: new AppendBlobHandler(
this.metadataStore,
this.extentStore,
this.logger,
this.loose
),
blobHandler: new BlobHandler(
this.metadataStore,
this.extentStore,
this.logger,
this.loose,
new PageBlobRangesManager()
),
blockBlobHandler: new BlockBlobHandler(
this.metadataStore,
this.extentStore,
this.logger,
this.loose
),
containerHandler: new ContainerHandler(
this.accountDataStore,
this.oauth,
this.metadataStore,
this.extentStore,
this.logger,
this.loose
),
pageBlobHandler: new PageBlobHandler(
this.metadataStore,
this.extentStore,
this.logger,
this.loose,
new PageBlobRangesManager()
),
serviceHandler: new ServiceHandler(
this.accountDataStore,
this.oauth,
this.metadataStore,
this.extentStore,
this.logger,
this.loose
)
};
const handlerMiddlewareFactory = new HandlerMiddlewareFactory(
this.handlers,
this.logger
);
const subRequestHandlerMiddleware = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
handlerMiddlewareFactory.createHandlerMiddleware()(
new Context(locals, DEFAULT_CONTEXT_PATH, req, res),
next
);
};
const subRequestSerializeMiddleWare = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
serializerMiddleware(
new Context(locals, DEFAULT_CONTEXT_PATH, req, res),
res,
next,
this.logger
);
};
const subRequestErrorMiddleWare = (err: any, req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
errorMiddleware(
new Context(locals, DEFAULT_CONTEXT_PATH, req, res),
err,
req,
res,
next,
this.logger
);
};
const subRequestEndMiddleWare = (req: IRequest, res: IResponse, locals: any, next: SubRequestNextFunction) => {
endMiddleware(
new Context(locals, DEFAULT_CONTEXT_PATH, req, res),
res,
this.logger
);
next();
};
this.handlePipeline = [
subRequestContextMiddleware,
subRequestDispatchMiddleware,
subRequestAuthenticationMiddleware,
subRequestDeserializeMiddleware,
subRequestHandlerMiddleware,
subRequestSerializeMiddleWare,
subRequestEndMiddleWare
];
this.operationFinder = [
subRequestContextMiddleware,
subRequestDispatchMiddleware,
];
this.errorHandler = subRequestErrorMiddleWare;
}
private async streamToBuffer2(
stream: NodeJS.ReadableStream,
buffer: Buffer,
encoding?: BufferEncoding
): Promise<number> {
let pos = 0; // Position in stream
const bufferSize = buffer.length;
return new Promise<number>((resolve, reject) => {
stream.on("readable", () => {
let chunk = stream.read();
if (!chunk) {
return;
}
if (typeof chunk === "string") {
chunk = Buffer.from(chunk, encoding);
}
if (pos + chunk.length > bufferSize) {
reject(new Error(`Stream exceeds buffer size. Buffer size: ${bufferSize}`));
return;
}
// Efficiently copy chunk data without allocation
if (Buffer.isBuffer(chunk)) {
(chunk as any).copy(buffer, pos);
} else {
// For Uint8Array or other typed arrays
(buffer as any).set(chunk, pos);
}
pos += chunk.length;
});
stream.on("end", () => {
resolve(pos);
});
stream.on("error", reject);
});
}
private async requestBodyToString(body: NodeJS.ReadableStream): Promise<string> {
let buffer = Buffer.alloc(4 * 1024 * 1024);
const responseLength = await this.streamToBuffer2(
body,
buffer
);
// Slice the buffer to trim the empty ending.
buffer = buffer.slice(0, responseLength);
return buffer.toString();
}
private async getSubRequestOperation(request: IRequest): Promise<Operation> {
const subRequestHandlePipeline = this.operationFinder;
const fakeResponse = new BlobBatchSubResponse(0, "HTTP/1.1");
return new Promise((resolve, reject) => {
const locals: any = {};
let i = 0;
const next = (error: any) => {
if (error) {
reject(error);
}
else {
++i;
if (i < subRequestHandlePipeline.length) {
subRequestHandlePipeline[i](request, fakeResponse, locals, next);
}
else {
resolve((new Context(locals, DEFAULT_CONTEXT_PATH, request, fakeResponse)).operation!);
}
}
};
subRequestHandlePipeline[i](
request,
fakeResponse,
locals,
next
);
});
}
private async parseSubRequests(
commonRequestId: string,
perRequestPrefix: string,
batchRequestEnding: string,
subRequestPathPrefix: string,
request: IRequest,
body: string): Promise<BlobBatchSubRequest[]> {
const requestAll = body.split(batchRequestEnding);
const response1 = requestAll[0]; // string after ending is useless
const response2 = response1.split(perRequestPrefix);
const subRequests = response2.slice(1);
const blobBatchSubRequests: BlobBatchSubRequest[] = [];
let previousOperation: Operation | undefined;
for (const subRequest of subRequests) {
const requestLines = subRequest.split(`${HTTP_LINE_ENDING}`);
// Content-Encoding
// Content-Type
// Content-ID
// empty line
// Operation infos
if (requestLines.length < 5) throw new Error("Bad request");
// Get Content_ID
let lineIndex = 0;
let content_id: number | undefined;
while (lineIndex < requestLines.length) {
if (requestLines[lineIndex] === '') break;
const header = requestLines[lineIndex].split(HTTP_HEADER_DELIMITER, 2);
if (header.length !== 2) throw new Error("Bad Request");
if (header[0].toLocaleLowerCase() === "content-id") {
content_id = parseInt(header[1], 10);
}
++lineIndex;
}
if (content_id === undefined) throw new Error("Bad request");
// "DELETE /container166063791875402779/blob0 HTTP/1.1"
++lineIndex;
const operationInfos = requestLines[lineIndex].split(" ");
if (operationInfos.length < 3) throw new Error("Bad request");
const requestPath = operationInfos[1].startsWith("/") ? operationInfos[1] : "/" + operationInfos[1];
if (!requestPath.startsWith(subRequestPathPrefix)) {
throw new Error("Request from a different container");
}
const url = `${request.getEndpoint()}${requestPath}`;
const method = operationInfos[0] as HttpMethod;
const blobBatchSubRequest = new BlobBatchSubRequest(content_id!, url, method, operationInfos[2], {});
++lineIndex;
while (lineIndex < requestLines.length) {
if (requestLines[lineIndex] === '') break; // Last line
const header = requestLines[lineIndex].split(HTTP_HEADER_DELIMITER, 2);
if (header.length !== 2) throw new Error("Bad Request");
blobBatchSubRequest.setHeader(header[0], header[1]);
++lineIndex;
}
const operation = await this.getSubRequestOperation(blobBatchSubRequest);
if (operation !== Operation.Blob_Delete && operation !== Operation.Blob_SetTier) {
throw new Error("Not supported operation");
}
if (previousOperation === undefined) {
previousOperation = operation;
}
else if (operation !== previousOperation!) {
throw new StorageError(
400,
"AllBatchSubRequestsShouldBeSameApi",
"All batch subrequests should be the same api.",
commonRequestId
);
}
blobBatchSubRequests.push(blobBatchSubRequest);
}
if (blobBatchSubRequests.length === 0) {
throw new Error("Bad Request");
}
return blobBatchSubRequests;
}
private serializeSubResponse(
subResponsePrefix: string,
responseEnding: string,
subResponses: BlobBatchSubResponse[]): string {
let responseBody = "";
subResponses.forEach(subResponse => {
responseBody += subResponsePrefix,
responseBody += "Content-Type: application/http" + HTTP_LINE_ENDING;
if (subResponse.content_id !== undefined) {
responseBody += "Content-ID" + HTTP_HEADER_DELIMITER + subResponse.content_id.toString() + HTTP_LINE_ENDING;
}
responseBody += HTTP_LINE_ENDING;
responseBody += subResponse.protocolWithVersion + " " + subResponse.getStatusCode().toString() + " "
+ subResponse.getStatusMessage() + HTTP_LINE_ENDING;
const headers = subResponse.getHeaders();
for (const key of Object.keys(headers)) {
responseBody += key + HTTP_HEADER_DELIMITER + headers[key] + HTTP_LINE_ENDING;
}
const bodyContent = subResponse.getBodyContent();
if (bodyContent !== "") {
responseBody += HTTP_LINE_ENDING + bodyContent + HTTP_LINE_ENDING;
}
responseBody += HTTP_LINE_ENDING;
});
responseBody += responseEnding;
return responseBody;
}
public async submitBatch(
body: NodeJS.ReadableStream,
requestBatchBoundary: string,
subRequestPathPrefix: string,
batchRequest: IRequest,
context: Context
): Promise<string> {
const perRequestPrefix = `--${requestBatchBoundary}${HTTP_LINE_ENDING}`;
const batchRequestEnding = `--${requestBatchBoundary}--`
const requestBody = await this.requestBodyToString(body);
let subRequests: BlobBatchSubRequest[] | undefined;
let error: any | undefined;
try {
subRequests = await this.parseSubRequests(
context.contextId!,
perRequestPrefix,
batchRequestEnding,
subRequestPathPrefix,
batchRequest,
requestBody);
} catch (err) {
if ((err instanceof MiddlewareError)
&& err.hasOwnProperty("storageErrorCode")
&& err.hasOwnProperty("storageErrorMessage")
&& err.hasOwnProperty("storageRequestID")) {
error = err;
}
else {
error = new StorageError(
400,
"InvalidInput",
"One of the request inputs is not valid.",
context.contextId!
);
}
}
const subResponses: BlobBatchSubResponse[] = [];
if (subRequests && subRequests.length > 256) {
error = new StorageError(
400,
"ExceedsMaxBatchRequestCount",
"The batch operation exceeds maximum number of allowed subrequests.",
context.contextId!
);
}
if (error) {
this.logger.error(
`BlobBatchHandler: ${error.message}`,
context.contextId
);
const errorResponse = new BlobBatchSubResponse(undefined, "HTTP/1.1");
await this.HandleOneFailedRequest(error, batchRequest, errorResponse);
subResponses.push(errorResponse);
}
else {
for (const subRequest of subRequests!) {
this.logger.info(
`BlobBatchHandler: starting on subrequest ${subRequest.content_id}`,
context.contextId
);
const subResponse = new BlobBatchSubResponse(subRequest.content_id, subRequest.protocolWithVersion);
await this.HandleOneSubRequest(subRequest,
subResponse);
subResponses.push(subResponse);
this.logger.info(
`BlobBatchHandler: completed on subrequest ${subRequest.content_id} ${subResponse.getHeader("x-ms-request-id")}`,
context.contextId
);
}
}
return this.serializeSubResponse(perRequestPrefix, batchRequestEnding, subResponses);
}
private HandleOneSubRequest(request: IRequest,
response: IResponse): Promise<void> {
const subRequestHandlePipeline = this.handlePipeline;
const subRequestErrorHandler = this.errorHandler;
let completed: boolean = false;
return new Promise((resolve, reject) => {
const locals: any = {};
let i = 0;
const next = (error: any) => {
if (completed) {
resolve();
return;
}
if (error) {
subRequestErrorHandler(error, request, response, locals, next);
completed = true;
}
else {
++i;
if (i < subRequestHandlePipeline.length) {
subRequestHandlePipeline[i](request, response, locals, next);
}
else {
resolve();
}
}
};
subRequestHandlePipeline[i](
request,
response,
locals,
next
);
});
}
private HandleOneFailedRequest(
err: any,
request: IRequest,
response: IResponse
): Promise<void> {
const subRequestErrorHandler = this.errorHandler;
return new Promise((resolve, reject) => {
subRequestErrorHandler(err, request, response, {}, resolve);
});
}
}