├── lib ├── refresh.js └── simplexml.js ├── capture.gz ├── LICENSE ├── capture.log ├── provider.js ├── README.markdown └── test.js /lib/refresh.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /capture.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creationix/refresh/HEAD/capture.gz -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Tim Caswell 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | 9 | -------------------------------------------------------------------------------- /capture.log: -------------------------------------------------------------------------------- 1 | tim@TimBook:~$ mount_webdav -i http://127.0.0.1:8080/ webdav 2 | Username: admin 3 | Password: 4 | tim@TimBook:~$ cd webdav/ 5 | tim@TimBook:~/webdav$ ls 6 | README.md* _user/ dev/ index.html* sling-logo.png* sling.css* templates/ 7 | _group/ apps/ devwidgets/ sites/ sling-test/ system/ var/ 8 | tim@TimBook:~/webdav$ touch TEST_FILE 9 | tim@TimBook:~/webdav$ vi TEST_FILE 10 | tim@TimBook:~/webdav$ ls 11 | README.md* _group/ apps/ devwidgets/ sites/ sling-test/ system/ var/ 12 | TEST_FILE* _user/ dev/ index.html* sling-logo.png* sling.css* templates/ 13 | tim@TimBook:~/webdav$ ls -lh 14 | total 70 15 | -rwx------ 1 tim staff 812B Feb 23 09:35 README.md* 16 | -rwx------ 1 tim staff 13B Feb 23 15:04 TEST_FILE* 17 | drwx------ 1 tim staff 2.0K Feb 23 15:06 _group/ 18 | drwx------ 1 tim staff 2.0K Feb 23 15:06 _user/ 19 | drwx------ 1 tim staff 2.0K Feb 23 15:06 apps/ 20 | drwx------ 1 tim staff 2.0K Feb 23 15:06 dev/ 21 | drwx------ 1 tim staff 2.0K Feb 23 15:06 devwidgets/ 22 | -rwx------ 1 tim staff 3.0K Feb 23 09:34 index.html* 23 | drwx------ 1 tim staff 2.0K Feb 23 15:06 sites/ 24 | -rwx------ 1 tim staff 6.1K Feb 23 09:34 sling-logo.png* 25 | drwx------ 1 tim staff 2.0K Feb 23 15:06 sling-test/ 26 | -rwx------ 1 tim staff 3.2K Feb 23 09:34 sling.css* 27 | drwx------ 1 tim staff 2.0K Feb 23 15:06 system/ 28 | drwx------ 1 tim staff 2.0K Feb 23 15:06 templates/ 29 | drwx------ 1 tim staff 2.0K Feb 23 15:06 var/ 30 | tim@TimBook:~/webdav$ mv TEST_FILE TEST_FILE2 31 | tim@TimBook:~/webdav$ rm TEST_FILE 32 | rm: TEST_FILE: No such file or directory 33 | tim@TimBook:~/webdav$ rm TEST_FILE2 34 | tim@TimBook:~/webdav$ cd .. 35 | tim@TimBook:~$ umount webdav 36 | -------------------------------------------------------------------------------- /provider.js: -------------------------------------------------------------------------------- 1 | var data = []; 2 | 3 | function find(path) { 4 | if (path === '/' || path === '') { 5 | return data; 6 | } 7 | return path.replace(/[^a-z\/]/g, '').split('/').reduce(function (data, name) { 8 | if (name === "") return data; 9 | return data[name]; 10 | }, data); 11 | } 12 | 13 | module.exports = { 14 | getProp: function (path, name) { 15 | var obj = find(path); 16 | if (name === "getlastmodified") { 17 | return (new Date()).toUTCString(); 18 | } 19 | if (name === "resourcetype") { 20 | return "container"; 21 | } 22 | if (obj.hasOwnProperty(name)) { 23 | return obj[name]; 24 | } 25 | }, 26 | get: function (path) { 27 | var pos = path.lastIndexOf('/'); 28 | var name = path.substr(pos + 1); 29 | path = path.substr(0, pos); 30 | var parent = find(path); 31 | if (parent && parent.hasOwnProperty(name)) { 32 | return parent[name]; 33 | } 34 | }, 35 | put: function (path, contents) { 36 | var pos = path.lastIndexOf('/'); 37 | var name = path.substr(pos + 1); 38 | path = path.substr(0, pos); 39 | var parent = find(path); 40 | if (parent && !parent.hasOwnProperty(name)) { 41 | parent[name] = contents; 42 | return parent[name]; 43 | } 44 | }, 45 | makeCol: function (path) { 46 | path = path.substr(0, path.length - 1); 47 | var pos = path.lastIndexOf('/'); 48 | var name = path.substr(pos + 1); 49 | path = path.substr(0, pos); 50 | var parent = find(path); 51 | if (parent && !parent.hasOwnProperty(name)) { 52 | parent[name] = []; 53 | return parent[name]; 54 | } 55 | }, 56 | del: function (path) { 57 | path = path.substr(0, path.length - 1); 58 | var pos = path.lastIndexOf('/'); 59 | var name = path.substr(pos + 1); 60 | path = path.substr(0, pos); 61 | var parent = find(path); 62 | if (parent && parent.hasOwnProperty(name)) { 63 | delete parent[name]; 64 | return true; 65 | } 66 | }, 67 | data: data 68 | } 69 | 70 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # Refresh REST/Webdav for node 2 | 3 | This project will be a module that exposes ANY data source as HTTP. The goals are as follows: 4 | 5 | - Implement GET/PUT/POST/DELETE as typically done in web apps 6 | - Implement enough of WebDav to enable mounting read/write in Finder 7 | - Further implement WebDav for other clients as time allows and need arises. 8 | 9 | The main goal is to make a simple resource provider for web-apps and other http clients. This will work with, but not be tied to [node-persistence][] 10 | 11 | ## RESTful Web Service HTTP methods 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 |
ResourceGETPUTPOSTDELETE
Collection URI, such as http://example.com/resources/List the members of the collection, complete with their member URIs for further navigation. For example, list all the cars for sale.Meaning defined as "replace the entire collection with another collection".Create a new entry in the collection where the ID is assigned automatically by the collection. The ID created is usually included as part of the data returned by this operation.Meaning defined as "delete the entire collection".
Element URI, such as http://example.com/resources/7HOU57YRetrieve a representation of the addressed member of the collection expressed in an appropriate MIME typeUpdate the addressed member of the collection or create it with the specified ID.Treats the addressed member as a collection in its own right and creates a new subordinate of it.Delete the addressed member of the collection.
39 | 40 | ## WebDav Notes 41 | 42 | WebDav clients aren't good about following the spec, here are some notes. 43 | 44 | 45 | 46 | [node-persistence]: http://github.com/creationix/node-persistence -------------------------------------------------------------------------------- /lib/simplexml.js: -------------------------------------------------------------------------------- 1 | // Mini XML parser. Just enough for WebDav 2 | 3 | var Matchers = { 4 | header: /^<\?xml[^?]*\?>[\s\n]*/, 5 | open: /^<([a-z:]+)(\s+[a-z:]+="(?:[^"]+|\\")")*>[\s\n]*/i, 6 | both: /^<([a-z:]+)(\s+[a-z:]+="(?:[^"]+|\\")")*\s*\/>[\s\n]*/i, 7 | close: /^<\/([a-z:]+)>[\s\n]*/i, 8 | plain: /^[\s\n]*([^<]+)[\s\n]*/ 9 | }; 10 | 11 | function find_match(part) { 12 | var name, match; 13 | for (name in Matchers) { 14 | if (match = part.match(Matchers[name])) { 15 | match.name = name; 16 | return match; 17 | } 18 | } 19 | throw new Error("XML Parse Error: " + JSON.stringify(part.substr(0,20))); 20 | } 21 | 22 | exports.parse = function parse(xml) { 23 | var pos = 0, 24 | length = xml.length; 25 | tree = {}; 26 | stack = [], 27 | current = tree; 28 | // Left trim whitespace 29 | xml = xml.replace(/^[\s\n]*/, ''); 30 | while (pos < length) { 31 | var match = find_match(xml.substr(pos)); 32 | pos += match[0].length 33 | if (match[1]) { 34 | var tag = match[1].replace(/^.*:/, ''); 35 | } 36 | switch (match.name) { 37 | case "open": 38 | current[tag] = {} 39 | stack.push(current); 40 | current = current[tag]; 41 | break; 42 | case "both": 43 | current[tag] = {}; 44 | break; 45 | case "close": 46 | current = stack.pop(); 47 | break; 48 | case "plain": 49 | current._ = match[1]; 50 | break; 51 | } 52 | } 53 | return tree; 54 | } 55 | 56 | 57 | function render(json) { 58 | if (json instanceof Array) { 59 | return json.map(render).join("\n"); 60 | } 61 | if (typeof json === 'object') { 62 | return Object.keys(json).map(function (key) { 63 | var value = json[key]; 64 | if (value === {} || value === undefined) { 65 | return ""; 66 | } 67 | return "" + render(value) + ""; 68 | }).join("\n"); 69 | } 70 | return json; 71 | 72 | } 73 | 74 | exports.render = function (json) { 75 | return '\n' + render(json); 76 | }; 77 | 78 | // var xml = '\n\ 79 | // \n\ 80 | // \n\ 81 | // \n\ 82 | // \n\ 83 | // \n\ 84 | // \n\ 85 | // \n\ 86 | // '; 87 | // 88 | // var json = { 89 | // propfind: { 90 | // prop: { 91 | // bigbox: null, 92 | // author: null, 93 | // DingALing: null, 94 | // Random: null 95 | // } 96 | // } 97 | // }; 98 | // 99 | // process.mixin(require('sys')); 100 | // p(exports.parse(xml)); -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var Xml = require('./lib/simplexml'); 2 | var source = require('./provider'); 3 | var PORT = 6543; 4 | var HOST = "127.0.0.1"; 5 | 6 | var tree = { 7 | people: [ 8 | {name: "Tim", age: 27}, 9 | {name: "Jack", age: 3} 10 | ], 11 | colors: [0xff8800, 0x223322] 12 | } 13 | 14 | var http = require('http'); 15 | process.mixin(require('sys')); 16 | 17 | // "OPTIONS, GET, HEAD, POST, TRACE, PROPFIND, PROPPATCH, MKCOL, COPY, PUT, DELETE, MOVE, LOCK, UNLOCK, BIND, REBIND, UNBIND, VERSION-CONTROL" 18 | var Handlers = { 19 | OPTIONS: function (req, res, body) { 20 | res.writeHeader(200, { 21 | Dav: "1,2", 22 | Allow: Object.keys(Handlers).join(', '), 23 | }); 24 | res.close(); 25 | }, 26 | PROPFIND: function (req, res, body) { 27 | var results = {}; 28 | var xml = Xml.parse(body); 29 | Object.keys(xml.propfind.prop).forEach(function (name) { 30 | var value = source.getProp(req.url, name); 31 | var group = "HTTP/1.1 200 OK"; 32 | if (value === undefined) { 33 | var group = "HTTP/1.1 404 Not Found"; 34 | } 35 | if (!results[group]) { 36 | results[group] = {}; 37 | } 38 | results[group][name] = value; 39 | }) 40 | var output = Xml.render({ 41 | multistatus: { 42 | response: { 43 | href: address + req.url, 44 | propstat: Object.keys(results).map(function (group) { 45 | return { 46 | prop: results[group], 47 | status: group 48 | } 49 | }) 50 | } 51 | } 52 | }); 53 | puts(output); 54 | res.writeHeader(207, "Multi Status", { 55 | "Content-Type": 'text/xml; charset="utf-8"', 56 | "Content-length": output.length 57 | }); 58 | res.write(output); 59 | res.close(); 60 | }, 61 | MKCOL: function (req, res, body) { 62 | if (source.makeCol(req.url)) { 63 | res.writeHeader(201, "Created", {}); 64 | res.close(); 65 | } else { 66 | res.writeHeader(409, "Conflict", {}); 67 | res.close(); 68 | } 69 | }, 70 | DELETE: function (req, res, body) { 71 | // TODO: Implement 72 | if (source.del(req.url)) { 73 | res.writeHeader(204, "No Content", {}); 74 | res.close(); 75 | } else { 76 | res.writeHeader(404, "Not Found", {}); 77 | res.close(); 78 | } 79 | }, 80 | PUT: function (req, res, body) { 81 | if (source.put(req.url, { mime: req.headers['content-type'], content: body})) { 82 | res.writeHeader(201, "Created", {}); 83 | res.close(); 84 | } else { 85 | res.writeHeader(409, "Conflict", {}); 86 | res.close(); 87 | } 88 | }, 89 | GET: function (req, res, body) { 90 | var data; 91 | if (data = source.get(req.url)) { 92 | res.writeHeader(200, { 93 | "Content-Type": data.mime, 94 | "Content-Length": data.content.length 95 | }); 96 | res.write(data.content); 97 | res.close(); 98 | } else { 99 | res.writeHeader(404, {}); 100 | res.close(); 101 | } 102 | }, 103 | POST: function () {}, 104 | }; 105 | 106 | http.createServer(function (req, res) { 107 | puts("\"" + req.method + " " + req.url + " HTTP/" + req.httpVersionMajor + "." + req.httpVersionMinor + "\""); 108 | p(source.data); 109 | var close = res.close; 110 | res.close = function () { 111 | // Common Log Format (mostly) 112 | puts(req.connection.remoteAddress + " - - [" + (new Date()).toUTCString() + "] \"" + req.method + " " + req.url + " HTTP/" + req.httpVersionMajor + "." + req.httpVersionMinor + "\" " + res.statusCode + " " + res.output[0].length + " \"" + (req.headers['referrer'] || "") + "\" \"" + req.headers["user-agent"] + "\""); 113 | return close.apply(res, arguments); 114 | } 115 | var writeHeader = res.writeHeader; 116 | res.writeHeader = function (code) { 117 | res.statusCode = code; 118 | return writeHeader.apply(res, arguments); 119 | } 120 | function get_body(callback) { 121 | var content = ''; 122 | req.addListener('data', function (chunk) { 123 | content += chunk; 124 | }); 125 | req.addListener('end', function () { 126 | callback(content); 127 | }); 128 | } 129 | if (Handlers[req.method]) { 130 | get_body(function (body) { 131 | Handlers[req.method](req, res, body); 132 | }); 133 | } else { 134 | get_body(function (body) { 135 | p(req); 136 | p(body); 137 | res.writeHeader(500, {'Content-Type': 'text/plain'}); 138 | res.write(body); 139 | res.close(); 140 | }); 141 | } 142 | }).listen(6543); 143 | 144 | var address = "http://" + HOST + ":" + PORT; 145 | puts("SERVER STARTED ON " + address); 146 | 147 | process.addListener("uncaughtException", function (err) { 148 | error(err.stack); 149 | }); --------------------------------------------------------------------------------