Skip to content

Commit

Permalink
Merge pull request #2 from isaacs/master
Browse files Browse the repository at this point in the history
Ported to JS
  • Loading branch information
federomero committed Apr 24, 2012
2 parents b256a3e + 2fc3d2f commit 4ed759d
Show file tree
Hide file tree
Showing 30 changed files with 872 additions and 603 deletions.
31 changes: 0 additions & 31 deletions .gitignore

This file was deleted.

27 changes: 27 additions & 0 deletions LICENSE
@@ -0,0 +1,27 @@
Original "Negotiator" program Copyright Federico Romero
Port to JavaScript Copyright Isaac Z. Schlueter

All rights reserved.

MIT License

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
27 changes: 0 additions & 27 deletions examples/accept.coffee

This file was deleted.

47 changes: 47 additions & 0 deletions examples/accept.js
@@ -0,0 +1,47 @@
(function() {
var Negotiator, availableMediaTypes, http, key, representations, server, val;

Negotiator = require('../lib/negotiator').Negotiator;

http = require('http');

representations = {
'text/html': '<h1>Hello world!</h1>',
'text/plain': 'Hello World!',
'application/json': JSON.stringify({
hello: 'world!'
})
};

availableMediaTypes = (function() {
var _results;
_results = [];
for (key in representations) {
val = representations[key];
_results.push(key);
}
return _results;
})();

server = http.createServer(function(req, res) {
var mediaType, negotiator;
negotiator = new Negotiator(req);
console.log("Accept: " + req.headers['accept']);
console.log("Preferred: " + (negotiator.preferredMediaTypes()));
console.log("Possible: " + (negotiator.preferredMediaTypes(availableMediaTypes)));
mediaType = negotiator.preferredMediaType(availableMediaTypes);
console.log("Selected: " + mediaType);
if (mediaType) {
res.writeHead(200, {
'Content-Type': mediaType
});
return res.end(representations[mediaType]);
} else {
res.writeHead(406);
return res.end();
}
});

server.listen(8080);

}).call(this);
31 changes: 0 additions & 31 deletions examples/charset.coffee

This file was deleted.

52 changes: 52 additions & 0 deletions examples/charset.js
@@ -0,0 +1,52 @@
(function() {
var Buffer, Iconv, Negotiator, availableCharsets, http, iconv, key, message, messages, server, val;

Negotiator = require('../lib/negotiator').Negotiator;

http = require('http');

Buffer = require('buffer').Buffer;

Iconv = require('iconv').Iconv;

iconv = new Iconv('UTF-8', 'ISO-8859-1');

message = "ë";

messages = {
'utf-8': message,
'iso-8859-1': iconv.convert(new Buffer(message))
};

availableCharsets = (function() {
var _results;
_results = [];
for (key in messages) {
val = messages[key];
_results.push(key);
}
return _results;
})();

server = http.createServer(function(req, res) {
var charset, negotiator;
negotiator = new Negotiator(req);
console.log("Accept-Charset: " + req.headers['accept-charset']);
console.log("Preferred: " + (negotiator.preferredCharsets()));
console.log("Possible: " + (negotiator.preferredCharsets(availableCharsets)));
charset = negotiator.preferredCharset(availableCharsets);
console.log("Selected: " + charset);
if (charset) {
res.writeHead(200, {
'Content-Type': "text/html; charset=" + charset
});
return res.end(messages[charset]);
} else {
res.writeHead(406);
return res.end();
}
});

server.listen(8080);

}).call(this);
31 changes: 0 additions & 31 deletions examples/encoding.coffee

This file was deleted.

48 changes: 48 additions & 0 deletions examples/encoding.js
@@ -0,0 +1,48 @@
(function() {
var Negotiator, gbuf, http, messages;

Negotiator = require('../lib/negotiator').Negotiator;

http = require('http');

gbuf = require('gzip-buffer');

messages = {
identity: 'Hello World'
};

gbuf.gzip(messages.identity, function(zipped) {
var availableEncodings, key, server, val;
messages.gzip = zipped;
availableEncodings = (function() {
var _results;
_results = [];
for (key in messages) {
val = messages[key];
_results.push(key);
}
return _results;
})();
console.log(availableEncodings);
server = http.createServer(function(req, res) {
var encoding, negotiator;
negotiator = new Negotiator(req);
console.log("Accept-Encoding: " + req.headers['accept-encoding']);
console.log("Preferred: " + (negotiator.preferredEncodings()));
console.log("Possible: " + (negotiator.preferredEncodings(availableEncodings)));
encoding = negotiator.preferredEncoding(availableEncodings);
console.log("Selected: " + encoding);
if (encoding) {
res.writeHead(200, {
'Content-Encoding': encoding
});
return res.end(messages[encoding]);
} else {
res.writeHead(406);
return res.end();
}
});
return server.listen(8080);
});

}).call(this);
26 changes: 0 additions & 26 deletions examples/language.coffee

This file was deleted.

44 changes: 44 additions & 0 deletions examples/language.js
@@ -0,0 +1,44 @@
(function() {
var Negotiator, availableLanguages, http, key, messages, server, val;

Negotiator = require('../lib/negotiator').Negotiator;

http = require('http');

messages = {
es: "¡Hola Mundo!",
en: "Hello World!"
};

availableLanguages = (function() {
var _results;
_results = [];
for (key in messages) {
val = messages[key];
_results.push(key);
}
return _results;
})();

server = http.createServer(function(req, res) {
var language, negotiator;
negotiator = new Negotiator(req);
console.log("Accept-Language: " + req.headers['accept-language']);
console.log("Preferred: " + (negotiator.preferredLanguages()));
console.log("Possible: " + (negotiator.preferredLanguages(availableLanguages)));
language = negotiator.preferredLanguage(availableLanguages);
console.log("Selected: " + language);
if (language) {
res.writeHead(200, {
'Content-Language': language
});
return res.end(messages[language]);
} else {
res.writeHead(406);
return res.end();
}
});

server.listen(8080);

}).call(this);

0 comments on commit 4ed759d

Please sign in to comment.