Skip to content

Commit

Permalink
Support custom endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Jan 10, 2018
1 parent 2f2fa24 commit 8878517
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/WxHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface WxHandlerOptions {
/** Maximum allowed maxFeatures parameter. */
maxFeatures?: number;
encoding?: string;
endpoints?: { [key: string]: boolean | ((req: http.IncomingMessage, res: http.ServerResponse) => void) };
wfs?: {
[ key: string ]: ((state: WxState, ...args: any[]) => any) | undefined,
getCapabilities?: (state: WxState) => WfsGetCapabilities | null,
Expand Down Expand Up @@ -167,7 +168,37 @@ export class WxHandler {
let paramTbl: { [ key: string ]: string } | undefined;

// Parse query string.
if(paramStart >= 0) {
if(paramStart < 0) {
paramStart = reqUrl.length;
paramTbl = {};
}

const endpointStart = reqUrl.lastIndexOf('/', paramStart);

if(endpointStart < 0) {
throw(new WxError(404));
}

// SECURITY: Strip evil characters to avoid injection attacks.
const endpoint = decodeURIComponent(
reqUrl.substr(endpointStart + 1, paramStart - endpointStart - 1)
).replace(/[^A-Za-z]/g, '?');

state.endpoint = endpoint;
state.path = reqUrl.substr(0, endpointStart);

// Check the endpoint.
if(state.options.endpoints) {
const customHandler = state.options.endpoints[endpoint];

if(!customHandler) {
throw(new WxError(404));
} else if(typeof(customHandler) == 'function') {
return(customHandler(state.req, state.res));
}
}

if(!paramTbl) {
// SECURITY: drop unknown parameters,
// and dangerous characters from most parameters.
paramTbl = parseQuery(reqUrl, paramStart + 1, {
Expand All @@ -194,30 +225,11 @@ export class WxHandler {
version: true,
width: true
});
} else {
paramStart = reqUrl.length;
paramTbl = {};
}

state.paramStart = paramStart;
state.paramTbl = paramTbl;

const endpointStart = reqUrl.lastIndexOf('/', paramStart);

if(endpointStart < 0) {
throw(new WxError(404));
}

// SECURITY: Strip evil characters to avoid injection attacks.
const endpoint = decodeURIComponent(
reqUrl.substr(endpointStart + 1, paramStart - endpointStart - 1)
).replace(/[^A-Za-z]/g, '?');

state.endpoint = endpoint;
state.path = reqUrl.substr(0, endpointStart);

// TODO: Maybe check the endpoint.

// SECURITY: Validate service.
const service = (paramTbl['service'] || endpoint).toLowerCase();

Expand Down

0 comments on commit 8878517

Please sign in to comment.