-
-
Notifications
You must be signed in to change notification settings - Fork 680
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* improve code style * test Array parameters support * add example on how to use multipartParser as a standalone module * add example for multiple and arrays * Update multiple.js * update tc for multiple values
- Loading branch information
1 parent
18d10d1
commit 036a809
Showing
7 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { MultipartParser } = require('../lib/multipart_parser.js'); | ||
|
||
|
||
const multipartParser = new MultipartParser(); | ||
|
||
// hand crafted multipart | ||
const boundary = '--abcxyz'; | ||
const next = '\r\n'; | ||
const formData = 'Content-Disposition: form-data; '; | ||
const buffer = Buffer.from( | ||
`${boundary}${next}${formData}name="text"${next}${next}text ...${next}${next}${boundary}${next}${formData}name="z"${next}${next}text inside z${next}${next}${boundary}${next}${formData}name="file1"; filename="a.txt"${next}Content-Type: text/plain${next}${next}Content of a.txt.${next}${next}${boundary}${next}${formData}name="file2"; filename="a.html"${next}Content-Type: text/html${next}${next}<!DOCTYPE html><title>Content of a.html.</title>${next}${next}${boundary}--` | ||
); | ||
|
||
const logAnalyzed = (buffer, start, end) => { | ||
if (buffer && start && end) { | ||
console.log(String(buffer.slice(start, end))) | ||
} | ||
}; | ||
|
||
// multipartParser.onPartBegin | ||
// multipartParser.onPartEnd | ||
|
||
// multipartParser.on('partData', logAnalyzed) // non supported syntax | ||
multipartParser.onPartData = logAnalyzed; | ||
multipartParser.onHeaderField = logAnalyzed; | ||
multipartParser.onHeaderValue = logAnalyzed; | ||
multipartParser.initWithBoundary(boundary.substring(2)); | ||
|
||
|
||
const bytesParsed = multipartParser.write(buffer); | ||
const error = multipartParser.end(); | ||
|
||
if (error) { | ||
console.error(error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var common = require('../test/common'); | ||
var http = require('http'), | ||
util = require('util'), | ||
os = require('os'), | ||
formidable = common.formidable, | ||
port = common.port, | ||
server; | ||
|
||
server = http.createServer(function(req, res) { | ||
if (req.url === '/') { | ||
res.writeHead(200, {'content-type': 'text/html'}); | ||
res.end( | ||
`<form action="/upload" enctype="multipart/form-data" method="post"> | ||
<label>simple<input type="text" name="simple"></label><br> | ||
<label>array text 0<input type="text" name="atext[]"></label><br> | ||
<label>array text 1<input type="text" name="atext[]"></label><br> | ||
<label>file simple<input type="file" name="filesimple"></label><br> | ||
<label>file attribute multiple<input type="file" name="multiplefile" multiple></label><br> | ||
<label>file html array0<input type="file" name="filearray[]"></label><br> | ||
<label>file html array1<input type="file" name="filearray[]"></label><br> | ||
<label>file html array and mulitple0<input type="file" name="mfilearray[]" multiple></label><br> | ||
<label>file html array and mulitple1<input type="file" name="mfilearray[]" multiple></label><br> | ||
<br> | ||
<button>Upload</button> | ||
</form>` | ||
); | ||
} else if (req.url === '/upload') { | ||
var form = new formidable.IncomingForm({multiples: true}); | ||
|
||
form.uploadDir = os.tmpdir(); | ||
|
||
form.parse(req, function (error, fields, files) { | ||
res.writeHead(200, {'content-type': 'text/plain'}); | ||
res.write('received fields:\n\n '+util.inspect(fields)); | ||
res.write('\n\n'); | ||
res.end('received files:\n\n '+util.inspect(files)); | ||
}) | ||
} else { | ||
res.writeHead(404, {'content-type': 'text/plain'}); | ||
res.end('404'); | ||
} | ||
}); | ||
server.listen(port); | ||
|
||
console.log('listening on http://localhost:'+port+'/'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters