diff --git a/src/html/client.html b/src/html/client.html index 6191a0c8..48ea3ece 100644 --- a/src/html/client.html +++ b/src/html/client.html @@ -10,7 +10,7 @@ - + diff --git a/src/server/node/server.mjs b/src/server/node/server.mjs index d12ae8df..e7e95491 100644 --- a/src/server/node/server.mjs +++ b/src/server/node/server.mjs @@ -154,6 +154,9 @@ class WebServer extends EventEmitter { var url = req.url if (url === '/qrcode'){ + + // qrcode image + res.setHeader('Content-Type', 'image/svg+xml') let str = this.qrcode, buf = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from(str) : new Buffer(str) @@ -162,6 +165,8 @@ class WebServer extends EventEmitter { } else if (url === '/' || url.indexOf('/?') === 0) { + // index.html + var ip = req.connection.remoteAddress.replace('::ffff:', '') var clientOptions = {} @@ -184,99 +189,93 @@ class WebServer extends EventEmitter { `)) .pipe(res) + } else if (url.indexOf('__APP_THEME__.css') != -1) { + + // user theme(s) + + res.setHeader('Content-Type', 'text/css') + + var etag = settings.read('theme') ? theme.hash : 'empty-theme' + res.setHeader('Cache-Control', 'no-cache') + res.setHeader('ETag', etag) + if (req.headers['if-none-match'] && req.headers['if-none-match'] === etag) { + res.statusCode = 304 + res.end() + return + } + + if (settings.read('theme')) { + let str = theme.get(), + buf = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from(str) : new Buffer(str) + res.write(buf) + } else { + res.write('') + } + res.end() + + } else if (url.match(/^\/+__APP_DIR__\//)) { + + // osc asset files + + try { + res.sendFile(path.resolve(__dirname + '/../../' + url.replace('__APP_DIR__/', ''))) + return true + } catch(e) {} + + + console.error(`(ERROR, HTTP) File not found: ${url}`) + + if (url.match(/\/client\/index.*\.js/)) { + console.warn('(WARNING) It seems a client attempted to load an older version of the app,', + 'this may occur when using a homescreen bookmark on mobile devices.', + 'If that\'s the case, please delete it and browse to the app\'s url again.') + } + + res.writeHead(404) + res.end() + + } else { - if ( - url.match(/^\/+__APP_DIR__\//) - ) { + // user files - // osc asset files + // windows absolute path fix + url = url.replace('_:_', ':') // escaped drive colon + url = url.replace(/^\/([^/]*:)/, '$1') // strip leading slash - if (url.indexOf('theme.css') != -1) { + if (url.match(/.(svg|jpg|jpeg|png|apng|gif|webp|tiff|xbm|bmp|ico|ttf|otf|woff|woff2|html|css|js)(\?[0-9]*)?$/i)) { - res.setHeader('Content-Type', 'text/css') + // Resolution order: session path, theme path, absolute path + var id + if (req.headers.cookie) { + id = (req.headers.cookie.match(/client_id=([^;]+)/) || [])[1] + } - var etag = settings.read('theme') ? theme.hash : 'empty-theme' - res.setHeader('Cache-Control', 'no-cache') - res.setHeader('ETag', etag) - if (req.headers['if-none-match'] && req.headers['if-none-match'] === etag) { - res.statusCode = 304 - res.end() - return - } - - if (settings.read('theme')) { - let str = theme.get(), - buf = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from(str) : new Buffer(str) - res.write(buf) - } else { - res.write('') - } - res.end() - - } else { + var resolvedPath = this.resolvePath(decodeURI(url), id) + if (resolvedPath) { try { - res.sendFile(path.resolve(__dirname + '/../../' + url.replace('__APP_DIR__/', ''))) + res.sendFile(resolvedPath) return true } catch(e) {} + } - + if (!this.ignoredNotFound.includes(url)) { console.error(`(ERROR, HTTP) File not found: ${url}`) - - if (url.match(/\/client\/index.*\.js/)) { - console.warn('(WARNING) It seems a client attempted to load an older version of the app,', - 'this may occur when using a homescreen bookmark on mobile devices.', - 'If that\'s the case, please delete it and browse to the app\'s url again.') - } - - res.writeHead(404) - res.end() - } + res.writeHead(404) + res.end() + + } else if (url.includes('/osc-ping')) { + this.httpCheck(true) } else { - - // user files - - // windows absolute path fix - url = url.replace('_:_', ':') // escaped drive colon - url = url.replace(/^\/([^/]*:)/, '$1') // strip leading slash - - if (url.match(/.(svg|jpg|jpeg|png|apng|gif|webp|tiff|xbm|bmp|ico|ttf|otf|woff|woff2|html|css|js)(\?[0-9]*)?$/i)) { - - // Resolution order: session path, theme path, absolute path - var id - if (req.headers.cookie) { - id = (req.headers.cookie.match(/client_id=([^;]+)/) || [])[1] - } - - var resolvedPath = this.resolvePath(decodeURI(url), id) - - if (resolvedPath) { - try { - res.sendFile(resolvedPath) - return true - } catch(e) {} - } - - if (!this.ignoredNotFound.includes(url)) { - console.error(`(ERROR, HTTP) File not found: ${url}`) - } - - res.writeHead(404) - res.end() - - } else if (url.includes('/osc-ping')) { - this.httpCheck(true) - } else { - res.writeHead(403) - res.end() - } - + res.writeHead(403) + res.end() } } + } httpCheck(ok, error) {