ariya/phantomjs — pull request #288 by n1k0 (2012-07-01 21:11:40+00:00)
Title: Added WebServerResponse::writeBinary to allow sending binary contents

This is my poor — but working — attempt at making possible to serve binary files from the built-in phantomjs webserver. This possibly fixes [issue 505](http://code.google.com/p/phantomjs/issues/detail?id=505).

I'm really new to C++, so please forgive the more than probably crappy code.

Sample usage:

```javascript
var fs = require('fs');
var server = require('webserver').create();
var service = server.listen(54321, function(request, response) {
var pageFile = require('fs').workingDirectory + request.url;
if (!fs.isFile(pageFile)) {
response.statusCode = 404;
response.write("404 - NOT FOUND");
} else {
response.statusCode = 200;
if (/\.png$/.test(pageFile.toLowerCase())) {
var image = fs.read(pageFile, 'b');
response.headers = {
'Accept-Ranges': 'bytes',
'Content-Type': 'image/png',
'Content-Length': image.length
};
response.writeBinary(image);
} else {
response.write(fs.read(pageFile));
}
}
response.close();
});
```

This server code works for me on OSX Lion, I don't know for other platforms.

Note: I tried to add a `mode` char arg to the `write()` method, but… well, C++ is not my thing =)