Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from isaacs/master
Ported to JS
- Loading branch information
Showing
30 changed files
with
872 additions
and
603 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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. |
This file was deleted.
Oops, something went wrong.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |
This file was deleted.
Oops, something went wrong.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |
This file was deleted.
Oops, something went wrong.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |
This file was deleted.
Oops, something went wrong.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |
Oops, something went wrong.