Mercurial > psgxs
annotate modules/mod_http.js @ 58:b98e545a94f7 default tip
Always use children instead of tags. Might break something.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 13 Sep 2011 00:54:55 +0200 |
parents | 99bd1d1ac071 |
children |
rev | line source |
---|---|
43
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
1 /* |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
3 * |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
4 * This file is part of PSĜS, a PubSub server written in JavaScript. |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
5 * |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
6 * PSĜS is free software: you can redistribute it and/or modify |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
7 * it under the terms of the GNU Affero General Public License as |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
9 * License. |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
10 * |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
11 * PSĜS is distributed in the hope that it will be useful, |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
14 * GNU Affero General Public License for more details. |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
15 * |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
16 * You should have received a copy of the GNU Affero General Public License |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
18 */ |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
19 |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
20 'use strict'; |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
21 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
22 var sites = {}; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
23 |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 var http = require('http'); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
26 var fs = require('fs'); |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
27 var dir = require('./configuration').pluginsDir; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
28 var files = fs.readdirSync(dir + '/http'); |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 var regex = /^mod_.*\.js/; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
31 var Script = process.binding('evals').Script; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
32 var sandbox = { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
33 config: require('./configuration'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
34 errors: require('./errors'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
35 forms: require('./forms'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
36 makeError: require('./errors').makeError, |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
37 notifs: require('./notifs'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
38 NS: require('./namespaces'), |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
39 require: require, |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
40 storage: require('./storage'), |
47
0d3f18bb1d36
Remove usage of fdsq.js in modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
41 toBare: JID.toBare, |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
42 exports: {} |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
43 }; |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 for (var i in files) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 var file = files[i]; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 if (!regex.test(file)) |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 continue; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
50 fs.readFile(dir + '/http/' + file, function(err, data) { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
51 if (err) { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
52 console.log('ERROR: loading http module “'+file+'”.'); // FIXME: file is always the last file of the list… |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
53 return; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
54 } |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 |
41
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
56 Script.runInNewContext(data, sandbox, dir + '/http/' + file); // FIXME: the same. |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
57 var module = sandbox.exports; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
58 sandbox.exports = {}; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
59 |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
60 for (var j in module) { |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
61 if (typeof sites[j] != 'undefined') |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
62 console.log('WARNING: module http/'+j+' already loaded.'); |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
63 |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
64 sites[j] = module[j]; |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
65 } |
bc717575e66a
Much better handling of modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
33
diff
changeset
|
66 }); |
33
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 } |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 http.createServer(function (req, res) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 var sent = false; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 for (var i in sites) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 var site = sites[i]; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 if (!site.url.test(req.url)) |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 continue; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 sent = site.func(req, res); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 return; |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 } |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 if (!sent) { |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 res.writeHead(404, {'Content-Type': 'text/plain'}); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 res.end('Error 404'); |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 } |
62cbb1c49bc5
Fix publish; add owner metadata field; add HTTP module and Atom HTTP module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 }).listen(8484); |