forked from zamronypj/fano
/
MultipartFormDataParserImpl.pas
512 lines (478 loc) · 19.2 KB
/
MultipartFormDataParserImpl.pas
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
{*!
* Fano Web Framework (https://fanoframework.github.io)
*
* @link https://github.com/fanoframework/fano
* @copyright Copyright (c) 2018 Zamrony P. Juhara
* @license https://github.com/fanoframework/fano/blob/master/LICENSE (MIT)
*}
unit MultipartFormDataParserImpl;
interface
{$MODE OBJFPC}
{$H+}
uses
Classes,
ListIntf,
DependencyContainerIntf,
MultipartFormDataParserIntf,
EnvironmentIntf,
UploadedFileCollectionWriterIntf,
UploadedFileCollectionWriterFactoryIntf;
type
(*!----------------------------------------------
* basic implementation having capability as
* parse multipart/form-data request
*
* @author Zamrony P. Juhara <zamronypj@yahoo.com>
*-----------------------------------------------*)
TMultipartFormDataParser = class(TInterfacedObject, IMultipartFormDataParser)
private
uploadedFilesFactory : IUploadedFileCollectionWriterFactory;
(*!----------------------------------------------
* extract boundary from multipart/form-data content type
* header
*-------- --------------------------------------
* @param contentType content type header
* @return boundary of multipart/form-data
*------------------------------------------------
* For example if we have header as follows: *
* Content-Type: multipart/form-data; boundary=xxx12345678
*
* contentType parameter will contain string
* 'multipart/form-data; boundary=xxx12345678'
*
* this method will return string 'xxx12345678'
*
*-----------------------------------------------*)
function getBoundary(const contentType : string) : string;
(*!----------------------------------------
* parse data in string store parsed data in body request parameter
* and uploaded files (if any).
*------------------------------------------
* @param actualData actual data to parse
* @param body instance of IList that will store
* parsed body parameter
* @param uploadedFiles instance of uploaded file collection
*-------------------------------------------
* Example of actualData
*
* Content-Disposition: form-data; name="text"
* \r\n
* text default
*------------------------------------------*)
procedure parseData(
const actualData : string;
const body : IList;
const uploadedFiles : IUploadedFileCollectionWriter
);
procedure splitDataByBoundaryAndParse(
const multipartData : string;
const boundary : string;
const body : IList;
const uploadedFiles : IUploadedFileCollectionWriter
);
(*!----------------------------------------
* add data payload to file upload (if any)
*------------------------------------------
* @param headerPart header data of file upload
* @param dataPart file upload data
* @param varName name of file upload
* @param originalFilename original filename as send by browser (may be empty)
* @param uploadedFiles instance of uploaded file collection
*------------------------------------------*)
procedure addFileUpload(
const headerPart : string;
const dataPart : string;
const varName : string;
const originalFilename : string;
const uploadedFiles : IUploadedFileCollectionWriter
);
(*!-------------------------------------------------------------
* extract name from Content-Disposition
*--------------------------------------------------------------
* @param start starting index for extracting name
* @param header header part
* @return name
*--------------------------------------------------------------
* If we have header contains following line
*
* Content-Disposition: form-data; name="file2"; filename="a.html"
* Content-Type: text/html
*
* start will contains value 32, start index of 'name="' substring
* from pos() function
* then this method will return string file2
*--------------------------------------------------------------*)
function extractName(const start: integer; const header : string) : string;
(*!-------------------------------------------------------------
* extract variable name from Content-Disposition
*--------------------------------------------------------------
* @param header header part
* @return name
* @throws EInvalidRequest if name is not found
*--------------------------------------------------------------
* If we have header contains following line
*
* Content-Disposition: form-data; name="file2"; filename="a.html"
* Content-Type: text/html
*
* then this method will return string file2 or raise exception
* if name is not found
*--------------------------------------------------------------*)
function extractVariableName(const headerPart : string) : string;
public
(*!----------------------------------------
* constructor
*------------------------------------------
* @param factory factory instance to build uploaded file
* collection instance
*------------------------------------------*)
constructor create(
const factory : IUploadedFileCollectionWriterFactory
);
(*!----------------------------------------
* destructor
*------------------------------------------*)
destructor destroy(); override;
(*!----------------------------------------
* Read POST data and parse
* it and store parsed data in body request parameter
* and uploaded files (if any). If not file upload
* then TNullUploadedFileCollection instance is return
*------------------------------------------
* @param contentType Content-Type request header
* @param postData POST data from web server
* @param body instance of IList that will store
* parsed body parameter
* @param uploadedFiles instance of uploaded file collection
* @return current instance
*-------------------------------------------
* Example of POST data with boundary of 'xxx12345678' s
*
* --xxx12345678
* Content-Disposition: form-data; name="text"
* \r\n
* text default
* --xxx12345678
* Content-Disposition: form-data; name="file1"; filename="a.txt"
* Content-Type: text/plain
* \r\n
* Content of a.txt.
*
* --xxx12345678
* Content-Disposition: form-data; name="file2"; filename="a.html"
* Content-Type: text/html
* \r\n
* <!DOCTYPE html><title>Content of a.html.</title>
*
* --xxx12345678--
* @link : https://stackoverflow.com/questions/4238809/example-of-multipart-form-data
*------------------------------------------*)
function parse(
const contentType : string;
const postData : string;
const body : IList;
out uploadedFiles : IUploadedFileCollectionWriter
) : IMultipartFormDataParser;
end;
implementation
uses
sysutils,
KeyValueTypes,
EInvalidRequestImpl;
resourcestring
sErrInvalidBoundary = 'Invalid multipart/form-data boundary';
sErrInvalidMultipartFormDataName = 'Invalid multipart/form-data name';
(*!----------------------------------------
* constructor
*------------------------------------------
* @param factory factory instance to build uploaded file
* collection instance
*------------------------------------------*)
constructor TMultipartFormDataParser.create(
const factory : IUploadedFileCollectionWriterFactory
);
begin
uploadedFilesFactory := factory;
end;
(*!----------------------------------------
* destructor
*------------------------------------------*)
destructor TMultipartFormDataParser.destroy();
begin
inherited destroy();
uploadedFilesFactory := nil;
end;
(*!----------------------------------------------
* extract boundary from multipart/form-data content type
* header
*-------- --------------------------------------
* @param contentType content type header
* @return boundary of multipart/form-data
*------------------------------------------------
* For example if we have contentType
* 'multipart/form-data; boundary=xxx12345678'
* it will return string 'xxx12345678'
*-----------------------------------------------*)
function TMultipartFormDataParser.getBoundary(const contentType : string) : string;
const BOUNDARY = 'boundary=';
const BOUNDARY_LEN = 9;
var boundaryPos : integer;
begin
boundaryPos := pos(BOUNDARY, contentType);
if (boundaryPos = 0) then
begin
//boundary string not present. Something is not right
raise EInvalidRequest.create(sErrInvalidBoundary);
end;
//boundaryPos will point at beginning of 'boundary=..'
//offset it to point to beginning of actual boundary
boundaryPos := boundaryPos + BOUNDARY_LEN;
//read boundary value
result := copy(
contentType,
boundaryPos,
length(contentType) - boundaryPos + 1
);
end;
(*!-------------------------------------------------------------
* extract name from Content-Disposition
*--------------------------------------------------------------
* @param start starting index for extracting name
* @param header header part
* @return name
*--------------------------------------------------------------
* If we have header contains following line
*
* Content-Disposition: form-data; name="file2"; filename="a.html"
* Content-Type: text/html
*
* start will contains value 32, start index of 'name="' substring
* from pos() function
* then this method will return string file2
*--------------------------------------------------------------*)
function TMultipartFormDataParser.extractName(const start: integer; const header : string) : string;
var i, len:integer;
begin
len := length(header);
i := start;
result := '';
while (header[i] <> '"') and (i<len) do
begin
result:= result + header[i];
inc(i);
end;
end;
(*!-------------------------------------------------------------
* extract variable name from Content-Disposition
*--------------------------------------------------------------
* @param header header part
* @return name
* @throws EInvalidRequest if name is not found
*--------------------------------------------------------------
* If we have header contains following line
*
* Content-Disposition: form-data; name="file2"; filename="a.html"
* Content-Type: text/html
*
* then this method will return string file2 or raise exception
* if name is not found
*--------------------------------------------------------------*)
function TMultipartFormDataParser.extractVariableName(const headerPart : string) : string;
var posName : integer;
begin
posName := pos('name="', headerPart);
if (posName = 0) then
begin
//name must be exists, if not, something is very wrong
raise EInvalidRequest.create(sErrInvalidMultipartFormDataName);
end;
//6= length of 'name="'
result := extractName(posName + 6, headerPart);
end;
(*!----------------------------------------
* add data payload to file upload (if any)
*------------------------------------------
* @param headerPart header data of file upload
* @param dataPart file upload data
* @param varName name of file upload
* @param originalFilename original filename as send by browser (may be empty)
* @param uploadedFiles instance of uploaded file collection
*------------------------------------------*)
procedure TMultipartFormDataParser.addFileUpload(
const headerPart : string;
const dataPart : string;
const varName : string;
const originalFilename : string;
const uploadedFiles : IUploadedFileCollectionWriter
);
var posContentType : int64;
contentType : string;
begin
//extract contentType (if any)
contentType := 'application/octet-stream';
posContentType := pos('content-type:', lowerCase(headerPart));
if (posContentType > 0) then
begin
//13=length of 'content-type:'
contentType := copy(headerPart, posContentType + 13, length(headerPart));
contentType := trim(contentType.split([';'])[0]);
end;
if (length(dataPart) > 0)then
begin
//if we get here then, form upload contain file input and
//at least a file is uploaded
uploadedFiles.add(
varName,
dataPart,
contentType,
originalFilename
);
end;
end;
(*!----------------------------------------
* parse data in string store parsed data in body request parameter
* and uploaded files (if any).
*------------------------------------------
* @param actualData actual data to parse
* @param body instance of IList that will store
* parsed body parameter
* @param uploadedFiles instance of uploaded file collection
*-------------------------------------------
* Example of actualData
*
* Content-Disposition: form-data; name="text"
* \n\n
* text default
*
* or
* Content-Disposition: form-data; name="photo"; filename="shell300x300.jpg"
* Content-Type: image/jpeg
* \n\n
* [binary data]
*------------------------------------------*)
procedure TMultipartFormDataParser.parseData(
const actualData : string;
const body : IList;
const uploadedFiles : IUploadedFileCollectionWriter
);
var headerPart, dataPart, varName, originalFilename : string;
crlfSeparatorPos, posFilename : integer;
param : PKeyValue;
begin
//split header and data.
//need to copy actualData because delete() replace string in place
//which will trigger compiler error because we mark it as const
dataPart := actualData;
crlfSeparatorPos := pos(#13#10#13#10, dataPart);
//copy header part and its crlf
headerPart := copy(dataPart, 1, crlfSeparatorPos + 2);
//remove header part including crlf that separate header and data
//so dataPart now consist of solely data payload
delete(dataPart, 1, crlfSeparatorPos + 2 + 1);
//for multipart/form-data, 'Content-Disposition' always 'form-data'
//so we can just simply read 'name'
varName := extractVariableName(headerPart);
posFilename := pos('filename="', headerPart);
if (posFilename > 0) then
begin
//if we get here it means we handle a file upload
//10= length of 'filename="'
//ExtractFilename is required to strip any directory information
//See Section 2.3 of RFC 2183
originalFilename := extractFilename(extractName(posFilename + 10, headerPart));
addFileUpload(headerPart, dataPart, varName, originalFilename, uploadedFiles);
end else
begin
//if we get here it means we handle ordinary input
new(param);
param^.key := varName;
param^.value := dataPart;
body.add(varName, param);
end;
end;
(*!----------------------------------------
* split raw POST data by boundary and parse
* them
*------------------------------------------
* @param multipartData raw POST data from client browser
* @param boundary multipart/form-data boundary id string
* @param body instance of IList that will store
* parsed body parameter
* @param uploadedFiles instance of uploaded file collection
*------------------------------------------*)
procedure TMultipartFormDataParser.splitDataByBoundaryAndParse(
const multipartData : string;
const boundary : string;
const body : IList;
const uploadedFiles : IUploadedFileCollectionWriter
);
var payloads : TStringArray;
indx, len : integer;
begin
payloads := multipartData.split(
['--' + boundary ],
TStringSplitOptions.ExcludeEmpty
);
//RFC 7578 requires last boundary format --<boundary>-- so
//last buffer will contain -- because we split with --<boundary>
//as delimiter, we will ignore last array element
len := length(payloads);
for indx := 0 to len-2 do
begin
//delete first CRLF
delete(payloads[indx], 1, 2);
//delete Last CRLF
delete(payloads[indx], length(payloads[indx])-1, 2);
parseData(payloads[indx], body, uploadedFiles);
end;
end;
(*!----------------------------------------
* Read POST data in standard input and parse
* it and store parsed data in body request parameter
* and uploaded files (if any).
*------------------------------------------
* @param env CGI environment variable
* @param body instance of IList that will store
* parsed body parameter
* @param uploadedFiles instance of uploaded file collection
* @return current instance
*------------------------------------------
* Example of POST data with boundary of 'xxx12345678' s
*
* --xxx12345678
* Content-Disposition: form-data; name="text"
* \r\n
* text default
* --xxx12345678
* Content-Disposition: form-data; name="file1"; filename="a.txt"
* Content-Type: text/plain
* \r\n
* Content of a.txt.
*
* --xxx12345678
* Content-Disposition: form-data; name="file2"; filename="a.html"
* Content-Type: text/html
* \r\n
* <!DOCTYPE html><title>Content of a.html.</title>
*
* --xxx12345678--
* @link : https://stackoverflow.com/questions/4238809/example-of-multipart-form-data
* @link : https://tools.ietf.org/html/rfc7578
*------------------------------------------*)
function TMultipartFormDataParser.parse(
const contentType : string;
const postData : string;
const body : IList;
out uploadedFiles : IUploadedFileCollectionWriter
) : IMultipartFormDataParser;
begin
uploadedFiles := uploadedFilesFactory.createCollectionWriter();
splitDataByBoundaryAndParse(
postData,
getBoundary(contentType),
body,
uploadedFiles
);
result := self;
end;
end.