watson / ipp-printer

An IPP printer written in Node.js
MIT License
555 stars 80 forks source link

Allow user to define a function to check any operation before continuing #9

Closed dresende closed 7 years ago

dresende commented 8 years ago

This is just a suggestion, you're free to decide if you like it or not.

This creates an option (optional..) called operation with a prototype similar to express (req, res, next) that is called before any operation and enables the developer to inspect the HTTP request and decide if it should continue (next) or respond in another way (using res).

Here's a stupid example:

var printer = new Printer({
    name      : "My Awesome Printer",
    operation : function (req, res, next) {
        if (!req.headers.authorization) {
            console.log("no guests! you need to auth!");

            // it could be DIGEST instead of BASIC
            res.writeHead(401, "Not Authorized", {
                "WWW-Authenticate": "Basic realm=\"My Awesome Printer\""
            });
            return res.end();
        } else {
            console.log("I don't know who you are, I have to base64 decode but at least you typed a username");
        }

        return next();
    }
});
dresende commented 8 years ago

The operation next() could have an error argument but I don't know IPP error constants and that could be done later on if you like.