Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/middleware/nonReadRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function createMiddleware() {
// Handle anything but read operations *before* the serveIndex middleware
// as it will reject them with a 405 (Method not allowed) instead of 404 like our old tooling
if (req.method !== "GET" && req.method !== "HEAD" && req.method !== "OPTIONS") {
res.status(404).end(`Cannot ${req.method} ${req.path}`);
res.status(404).end(`Cannot ${req.method} ${req.url}`);
} else {
next();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/middleware/serveIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ function createContent(path, resourceInfos) {
*/
function createMiddleware({resourceCollections}) {
return function serveIndex(req, res, next) {
log.verbose("\n Listing index of " +req.path);
let glob = req.path + (req.path.endsWith("/") ? "*" : "/*");
log.verbose("\n Listing index of " +req.url);
let glob = req.url + (req.url.endsWith("/") ? "*" : "/*");
resourceCollections.combo.byGlob(glob, {nodir: false}).then((resources) => {
if (!resources || resources.length == 0) { // Not found
next();
Expand All @@ -151,7 +151,7 @@ function createMiddleware({resourceCollections}) {
});

let resourceInfos = createResourceInfos(resources);
res.end(createContent(req.path, resourceInfos));
res.end(createContent(req.url, resourceInfos));
}).catch((err) => {
next(err);
});
Expand Down
6 changes: 4 additions & 2 deletions lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function isFresh(req, res) {
*/
function createMiddleware({resourceCollections}) {
return function serveResources(req, res, next) {
resourceCollections.combo.byPath(req.path).then(function(resource) {
const url = req.url.split("?")[0];

resourceCollections.combo.byPath(url).then(function(resource) {
if (!resource) { // Not found
next();
return;
Expand Down Expand Up @@ -65,7 +67,7 @@ function createMiddleware({resourceCollections}) {
if (resource._project) {
stream = stream.pipe(replaceStream("${version}", resource._project.version));
} else {
log.verbose("Project missing from resource %s", req.path);
log.verbose("Project missing from resource %s", req.url);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/serveThemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function createMiddleware({resourceCollections}) {
});

return function theme(req, res, next) {
/* req.path examples:
/* req.url examples:
/resources/sap/ui/core/themes/sap_belize/library.css
*/

Expand All @@ -37,7 +37,7 @@ function createMiddleware({resourceCollections}) {
3 => -RTL.css suffix
4 => -parameters.json suffix
*/
const themeReq = themeRequest.exec(req.path);
const themeReq = themeRequest.exec(req.url);
if (!themeReq) {
next();
return;
Expand Down
66 changes: 36 additions & 30 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const express = require("express");
const compression = require("compression");
const fastify = require("fastify");
const cors = require("cors");
const portscanner = require("portscanner");

Expand Down Expand Up @@ -50,7 +49,17 @@ function serve(tree, {port, changePortIfInUse = false, h2 = false, key, cert, ac
combo
};

const app = express();
let serverConfiguration = {};
if (h2) {
serverConfiguration.http2 = true;
serverConfiguration.https = {
allowHTTP1: true, // fallback support for HTTP1
key,
cert
};
}

const app = fastify(serverConfiguration);

const oCspConfig = {
allowDynamicPolicySelection: true,
Expand Down Expand Up @@ -79,7 +88,7 @@ function serve(tree, {port, changePortIfInUse = false, h2 = false, key, cert, ac
};
app.use(csp("sap-ui-xx-csp-policy", oCspConfig));

app.use(compression());
// app.use(compression());
app.use(cors());

app.use("/discovery", discovery({resourceCollections}));
Expand All @@ -96,17 +105,29 @@ function serve(tree, {port, changePortIfInUse = false, h2 = false, key, cert, ac
app.use(nonReadRequests({resourceCollections}));
app.use(serveIndex({resourceCollections}));

if (h2) {
return _addSsl({app, h2, key, cert});
}
app.setNotFoundHandler((request, reply) => {
reply.code(404).type("text/html; charset=utf-8").send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot ${request.method} ${request.url}</pre>
</body>
</html>
`);
});

return app;
}).then((app) => {
return _listen(app, port, changePortIfInUse, acceptRemoteConnections).then(function({port, server}) {
return _listen(app, port, changePortIfInUse, acceptRemoteConnections).then(function({port}) {
return {
h2,
port,
close: function(callback) {
server.close(callback);
app.close(callback);
}
};
});
Expand Down Expand Up @@ -156,32 +177,17 @@ function _listen(app, port, changePortIfInUse, acceptRemoteConnections) {
}

options.port = foundPort;
const server = app.listen(options, function() {
resolve({port: options.port, server});
});

server.on("error", function(err) {
reject(err);
app.listen(options.port, options.host, function(err) {
if (err) {
reject(err);
} else {
resolve({port: options.port});
}
});
});
});
}

/**
* Adds SSL support to an express application
*
* @param {Object} app The original express application
* @param {string} key Path to private key to be used for https
* @param {string} cert Path to certificate to be used for for https
* @returns {Object} The express application with SSL support
* @private
*/
function _addSsl({app, key, cert}) {
// Using spdy as http2 server as the native http2 implementation
// from Node v8.4.0 doesn't seem to work with express
return require("spdy").createServer({cert, key}, app);
}

module.exports = {
serve: serve
};
Loading