Skip to content

Commit

Permalink
fix(server): fixes issue where OPTIONS requests would return METHOD_N…
Browse files Browse the repository at this point in the history
…OT_ALLOWED
  • Loading branch information
wessberg committed Jan 18, 2019
1 parent 90e98d2 commit 0ebc4ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
],
"devDependencies": {
"@wessberg/di-compiler": "^2.0.2",
"@wessberg/rollup-plugin-ts": "1.1.17",
"@wessberg/rollup-plugin-ts": "1.1.18",
"@wessberg/scaffold": "^1.0.5",
"@wessberg/ts-config": "^0.0.34",
"standard-changelog": "^2.0.6",
"tslint": "^5.12.1",
"typescript": "^3.2.2",
"typescript": "^3.2.4",
"useragent-generator": "^1.1.0"
},
"dependencies": {
Expand All @@ -70,7 +70,7 @@
"@types/semver": "^5.5.0",
"@webcomponents/custom-elements": "^1.2.1",
"@webcomponents/shadycss": "^1.8.0",
"@webcomponents/shadydom": "^1.4.1",
"@webcomponents/shadydom": "^1.4.2",
"@webcomponents/template": "^1.4.0",
"@wessberg/browserslist-generator": "1.0.3",
"@wessberg/di": "^2.0.3",
Expand All @@ -84,8 +84,8 @@
"blob-polyfill": "^3.0.20180112",
"chalk": "^2.4.2",
"console-polyfill": "^0.3.0",
"core-js": "^3.0.0-beta.8",
"core-js-builder": "^3.0.0-beta.8",
"core-js": "^3.0.0-beta.9",
"core-js-builder": "^3.0.0-beta.9",
"devcert": "^1.0.0",
"events-polyfill": "^2.1.0",
"file-type": "^10.7.0",
Expand Down Expand Up @@ -123,7 +123,7 @@
"web-animations-js": "^2.3.1",
"whatwg-fetch": "^3.0.0",
"xhr-polyfill": "^0.1.8",
"zone.js": "^0.8.27"
"zone.js": "^0.8.28"
},
"main": "./dist/index.js",
"module": "./dist/index.js",
Expand Down
33 changes: 32 additions & 1 deletion src/server/request-handler/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Response} from "../i-response";
import {printRequest} from "../../util/request-util/request-util";
import {RequestHandlerOptions} from "./request-handler-options";
import {constants} from "http2";
import {METHOD_NOT_ALLOWED, NOT_FOUND, OK} from "http-status-codes";
import {RegisteredControllers} from "../../controller/controller/registered-controllers";
import {IStaticController} from "../../controller/static/i-static-controller";

Expand All @@ -27,10 +28,14 @@ export class RequestHandler implements IRequestHandler {
switch (options.request.method) {
case "GET":
return await this.handleGetRequest(options);
case "OPTIONS":
return await this.handleOptionsRequest(options);
default:
return {
body: `Only GET requests are supported`,
statusCode: constants.HTTP_STATUS_METHOD_NOT_ALLOWED,
statusCode: options.request.http2
? constants.HTTP_STATUS_METHOD_NOT_ALLOWED
: METHOD_NOT_ALLOWED,
contentType: "text/plain"
};
}
Expand All @@ -54,4 +59,30 @@ export class RequestHandler implements IRequestHandler {
// Fall back to the index route
return await this.staticController.onIndexRequested(options.request);
}

/**
* Handles the given OPTIONS request
* @param {RequestHandlerOptions} options
* @returns {Promise<Response>}
*/
private async handleOptionsRequest (options: RequestHandlerOptions): Promise<Response> {

// Find the first matched controller
for (const controller of this.controllers) {
const match = controller.match(options.request);
if (match != null) {
return {
statusCode: options.request.http2
? constants.HTTP_STATUS_OK
: OK
};
}
}

return {
statusCode: options.request.http2
? constants.HTTP_STATUS_NOT_FOUND
: NOT_FOUND
};
}
}

0 comments on commit 0ebc4ef

Please sign in to comment.