Permalink
Browse files

FIX shortUrl domain

  • Loading branch information...
naholyr committed Jan 16, 2011
1 parent 394eaa3 commit bc84a40b5330158bb0fed2dae3826889151adf3e
Showing with 28 additions and 24 deletions.
  1. +27 −24 lib/301.tl.js
  2. +1 −0 views/home.ejs
View
@@ -138,7 +138,7 @@ exports.routes.err500 = function(err, req, res, next) {
// POST : curl -d 'longUrl=<my url>&custom=<my key>...' 'http://301.tl'
// JSON : curl -d '{"longUrl":"<my url>","custom":"<my key>"...}' -H 'Content-Type: application/json'
function encodeLongUrl(req, res, next) {
- var host = req.header('x-forwarded-host') || req.host
+ addDomainInfo(req)
var params = (req.method == 'POST' ? req.body : req.query) || {}
if (params.longUrl) {
if (req.header('Origin')) {
@@ -150,14 +150,14 @@ function encodeLongUrl(req, res, next) {
if (err) {
exports.error(err, req, res)
}
- var shortUrl = 'http://' + host + '/' + key
+ var shortUrl = 'http://' + req.domain + '/' + key
var status = _new ? 201 : 200
if (req.is('text')) {
res.send('shortUrl=' + shortUrl + '\nlongUrl=' + url, {'Content-Type':'text/plain'}, status)
} else if (req.is('json')) {
res.send(JSON.stringify({"shortUrl":shortUrl,"longUrl":url}), {'Content-Type':'application/json'}, status)
} else { // default = html
- res.send('<a href="' + shortUrl +'" title="' + url + '">' + host + '/<strong>' + key + '</strong></a>', {'Content-Type':'text/html'}, status)
+ res.send('<a href="' + shortUrl +'" title="' + url + '">' + req.domain + '/<strong>' + key + '</strong></a>', {'Content-Type':'text/html'}, status)
}
})
}
@@ -172,35 +172,32 @@ function home(req, res, next) {
if (req.method == 'OPTIONS') {
sendCORSHeaders(req, res)
res.send('', {}, 200)
- return;
- }
- var host = req.headers['x-forwarded-host'] || req.host
- // Host "api.301.tl"
- if (host.match(/^api\./)) {
- if (req.query.longUrl) {
- res.header('Access-Control-Allow-Origin', '*')
- res.header('Access-Control-Allow-Methods', 'GET, POST')
- encodeLongUrl(req, res, function(err) {
- req.params.error = err
+ } else {
+ addDomainInfo(req)
+ // Host "api.301.tl"
+ if (req.host.match(/^api\./)) {
+ if (req.query.longUrl) {
+ res.header('Access-Control-Allow-Origin', '*')
+ res.header('Access-Control-Allow-Methods', 'GET, POST')
+ encodeLongUrl(req, res, function(err) {
+ req.params.error = err
+ homeAPI(req, res, next)
+ })
+ } else {
homeAPI(req, res, next)
- })
- } else {
- homeAPI(req, res, next)
+ }
+ }
+ // Other hosts "301.tl" or "www.301.tl" -> main homepage
+ else {
+ res.render('home', {locals: {host: req.host}})
}
- }
- // Other hosts "301.tl" or "www.301.tl" -> main homepage
- else {
- server = req.headers['x-forwarded-server'] || host.replace(/^[^\/]+([^\/\.]+\.[^\/\.]+)(\/.*?)$/, '$1')
- res.render('home', {locals: {host: server}})
}
}
// API homepage
function homeAPI(req, res, next) {
- var host = req.headers['x-forwarded-host'] || req.host,
- server = req.headers['x-forwarded-server'] || host.replace(/^[^\/]+([^\/\.]+\.[^\/\.]+)(\/.*?)$/, '$1')
res.render('home-api', {locals: {
- host: server,
+ host: req.domain,
error: (req.params && req.params.error) ? (req.params.error.message || req.params.error) : false,
}})
}
@@ -229,3 +226,9 @@ function sendCORSHeaders(req, res) {
res.header('Access-Control-Allow-Methods', 'GET, POST')
res.header('Access-Control-Allow-Headers', 'Content-Type')
}
+
+// Add "host" and "domain" attributes to request
+function addDomainInfo(req) {
+ req.host = req.headers['x-forwarded-host'] || req.host
+ req.domain = req.host.replace(/^([^\/\.]+\.)*([^\/\.]+\.[^\/\.]+)(\/.*)?$/, '$2')
+}
View
@@ -48,6 +48,7 @@ $('#F_shorten').submit(function(e) {
},
success: function(res, status, req) {
$('#shortUrl').attr({href:res.shortUrl, title:res.longUrl}).html(res.shortUrl);
+ $('#f_longUrl').val(res.longUrl);
$('#shortUrl_container').show();
},
type: 'POST',

0 comments on commit bc84a40

Please sign in to comment.