purplefox/vert.x — pull request #235 by nemecec (2012-06-22 21:58:02+00:00)
Title: netty-based support for handling multipart/form-data

How does it work?
=============

It introduces a new `DefaultHttpServerRequest` subclass: `PostHttpServerRequest`. This class has methods `HttpMultipartFormDataHandler singleElementHandler(String name, HttpDataHandler handler)` and `void elementHandler(HttpMultipartFormElementHandler elementHandler)` - these can be used to register callback "hooks".

At the moment, JavaScript wrapper API is missing, I could add that if you could give me some pointers.
Also, unit test is missing, I could add that if you could give me some (more) pointers. :-)

More details: handling specific (named) HTTP data
=========

`singleElementHandler` method allows you to register a callback hook for `HttpData` fields with specific name (`HttpData` can be attribute or file upload).

Typical (imagined) usage, with JavaScript:
```javascript
server.requestHandler(function(req) {
if (req.path == '/upload') {
req.singleElementHandler("file", function(data) {
req.pause();
var filename = vertx.generateUUID() + ".uploaded";
vertx.FileSystem.open(filename, function(err, file) {
var pump = new vertx.Pump(data, file.getWriteStream());
data.endHandler(function() {
file.close(function() {
stdout.println("Uploaded " + pump.getBytesPumped() + " bytes to " + filename);
});
});
pump.start();
req.resume();
});
});
} else {
req.response.statusCode = 404;
req.response.end;
}
});
```
The basic idea is to allow handling only the part of the request that the user is interested in and ignore the rest. And do that in direct streaming mode, no messing with temporary files.

More details: handling all HTTP data
=========

`elementHandler` method allows you to register a callback hook for all `HttpData` fields, regardless of their name.

Typical (imagined) usage, with JavaScript:
```javascript
server.requestHandler(function(req) {
if (req.path == '/upload') {
req.elementHandler(function(data) {
if (data.name != 'file') return;
req.pause();
var filename = vertx.generateUUID() + ".uploaded";
vertx.FileSystem.open(filename, function(err, file) {
var pump = new vertx.Pump(data, file.getWriteStream());
data.endHandler(function() {
file.close(function() {
stdout.println("Uploaded " + pump.getBytesPumped() + " bytes to " + filename);
});
});
pump.start();
req.resume();
});
});
} else {
req.response.statusCode = 404;
req.response.end;
}
});
```

The basic idea is to allow handling all HTTP data, no matter what name it has. And do that in direct streaming mode, no messing with temporary files.