Example #1
0
    return function(req, res, next) {
        if (req.method != 'GET' && req.method != 'HEAD') return next();
        Logger.debugi("Before", req.url);
        var url = Url.parse(req.url);
        var pathname = QueryString.unescape(url.pathname);
        var resolved = PK.resolvePath('', pathname);

        // Now put it all back
        //url.pathname = QueryString.escape(resolved);
        url.pathname = resolved;
        req.url = Url.format(url);

        // Pass through to the next layer
        Logger.debugi("After", req.url);
        return next();
    };
Example #2
0
exports.validate = function(req,res,next,meta,target) {
    Logger.debug("validate");
    function goodParam(param, name) {
        if (!param) {
            Logger.error("Probably FATAL: in controllerHelpers.validate the param",name,"is missing :(");
            Logger.logStack();
            if (res) {
                res.writeHead(500,{});
                res.end();
            }
            return false;
        }
        return true;
    };
    
    if (!goodParam(req,"req") || !goodParam(res,"res") || !goodParam(next,"next") ||
        !goodParam(meta,"meta") || !goodParam(target,"target")) return;
    var toCheck = meta.params;

    var errorHandler = meta.errorHandler;
    if ((typeof errorHandler) === "string") {
        if (errorHandler === "json") {
            errorHandler = exports.sendJSONError;
        }
    }
    if (!errorHandler) {
        errorHandler = exports.sendJSONError;
    }

    // See if they are requesting meta data
    if (req.param("meta")) {
        Logger.debug("Sending meta information");
        return exports.sendJSON(res, meta);
    }

    // Are there parameters needed?
    if (!toCheck) {
        // Nothing to do, pass it on
        Logger.debugi("No parameters to check, target is",target);

        return target(req,res,next,meta);
    }

    // TODO: Enhance this checking so that for the early requests with redirect_uri's we can
    // send the error responses as part of the redirect rather than as JSON in return to the
    // original request. JSON is right once the API is up and running, but when getting the
    // access tokens we actually want to redirect to the URI (as long as there is one).
    // Check for the presence of each required parameter
    for (key in toCheck) {
        Logger.debug("Checking key", key);
        var value = req.param(key);

        var keyDesc = toCheck[key];
        if (keyDesc.required) {
            Logger.debugi("  it is required, value=",value);
            if (!value) {
                var error = "Required parameter is missing";
                var description = "The required parameter '" + key + "' is missing.";
                return errorHandler(req, res, error, description, 400);
            }
        } else {
            Logger.debugi("  - is not required");
        }
        
        if (value) {
            // If it is there, it has to match the other validation
            if (keyDesc.minLength) {
                if (value.length < keyDesc.minLength) {
                    var error = "Parameter to short";
                    var description = "The parameter '" + key + "' must be at least "+keyDesc.minLength+" characters long.";
                    return errorHandler(req, res, error, description, 400);
                }
            }
            
            if (keyDesc.validation) {
                if (!keyDesc.validation.test(value)) {
                    var error = "Bad format";
                    var description = "The parameter '" + key + "' was not in the required format.";
                    return errorHandler(req, res, error, description, 400);                    
                }
            }
        }
    }

    // We have all the right params, so do the request
    // TODO: It might not be horrible to either wrap the target at this point in a try/catch so
    // we can format exceptions as JSON or maybe we just need to do that in a middleware error handler.
    return target(req,res,next,meta);  
}
Example #3
0
    return function(req, res, next) {
        
        // Bail if there is nothing to check
        if (!meta) {
            return next();
        }

        // Are there parameters needed?
        var toCheck = meta.params;
        if (!toCheck) {
            // Nothing to do, pass it on
            Logger.debugi("No parameters to check");
            return next();
        }
        
        Logger.debugi("Checking params ",toCheck);

        // TODO: Enhance this checking so that for the early requests with redirect_uri's we can
        // send the error responses as part of the redirect rather than as JSON in return to the
        // original request. JSON is right once the API is up and running, but when getting the
        // access tokens we actually want to redirect to the URI (as long as there is one).
        // Check for the presence of each required parameter
        for (key in toCheck) {
            Logger.debug("Checking key", key);
            var value = req.param(key);

            var keyDesc = toCheck[key];
            if (keyDesc.required) {
                Logger.debugi("  it is required, value=",value);
                if (!value) {
                    return res.send({
                          error: "Required parameter is missing"
                        , description: "The required parameter '" + key + "' is missing."
                    }, 400);
                }
            } else {
                Logger.debugi("  - is not required");
            }

            if (value) {
                // If it is there, it has to match the other validation
                if (keyDesc.minLength) {
                    if (value.length < keyDesc.minLength) {
                        return res.send({
                              error: "Parameter to short"
                            , description: "The parameter '" + key + "' must be at least "+keyDesc.minLength+" characters long."                            
                        }, 400);
                    }
                }

                if (keyDesc.validation) {
                    if (!keyDesc.validation.test(value)) {
                        return res.send({
                              error: "Bad format"
                            , description: "The parameter '" + key + "' was not in the required format."
                        }, 400);
                    }
                }
            }
        }

        // We have all the right params, so do the request
        
        next();
    };
Example #4
0
exports.register = function(app, name, controller, options) {
    var meta = controller.meta;
    if (!options) options = {};
    
    for (key in meta) {
        var data = meta[key];

        // Logger.info("name='"+name+"' data.endpoint='"+data.endpoint+"' name.length="+name.length);                
        var endpoint = (name.length > 0) ? ("/" + name) : "";
        if (data.exactEndpoint) {
            endpoint = data.exactEndpoint;
        } else if (data.endpoint) {
            endpoint += "/" + data.endpoint;
        }
        
        // Safety
        if (endpoint.length===0) endpoint = "/";

        Logger.info("endpoint = ",endpoint);

        var methods;
        if (data.methods) {
            // Have to make sure they are lowercase
            methods = [];
            for(var ix = 0; ix<data.methods.length; ix++) {
                methods.push(data.methods[ix].toLowerCase())
            }
        } else {
            // By default, everything
            methods = ["get", "post", "put", "delete"];
        }
        
        // The suffix is added unless configured not to
        var stack = [endpoint];
        if (data.preSessionHandler) stack.push(data.preSessionHandler);
        if (options.beforeFilter) addFilters(stack, data, options.beforeFilter);
        
        if (data.middleware) {
            // If an explict set of middleware is defined, use it. This can be
            // either an array or a single function
            if ((typeof data.middleware) === "object") {
                // Logger.debug("Adding middleware from array");
                for(var ix=0; ix<data.middleware.length; ix++) {
                    Logger.debugi("typeof ",data.middleware[ix])
                    stack.push(data.middleware[ix]);
                }
            } else {
                // Logger.debug("Adding single middleware");
                stack.push(data.middleware);
            }
        } else {
            // Otherwise use convention to find the middleware
            stack.push(controller[key]);            
        }
        
        if (options.afterFilter) addFilters(stack, data, options.afterFilter);

        Logger.debugi("Stack=",stack);
        for(var ix = 0; ix<methods.length; ix++) {
            var method = methods[ix];
            app[method].apply(app, stack);
        }
    }  
}
Example #5
0
Queue.prototype.streamTimeout = function() {
    Logger.debugi("streamTimeout");

}
Example #6
0
Queue.prototype.streamEnd = function() {
    Logger.debugi("streamEnd");

}