Beispiel #1
0
internals.payload = function (request, next) {

    if (request.method === 'get' ||
        request.method === 'head') {            // When route.method is '*'

        return next();
    }

    Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload, function (err, parsed) {

        request.mime = parsed.mime;
        request.payload = parsed.payload || null;

        if (!err) {
            return next();
        }

        var failAction = request.route.settings.payload.failAction;         // failAction: 'error', 'log', 'ignore'
        if (failAction !== 'ignore') {
            request._log(['payload', 'error'], err);
        }

        if (failAction === 'error') {
            return next(err);
        }

        return next();
    });
};
Beispiel #2
0
internals.payload = async function (request) {

    if (request.method === 'get' ||
        request.method === 'head') {            // When route.method is '*'

        return;
    }

    if (request._expectContinue) {
        request.raw.res.writeContinue();
    }

    try {
        const { payload, mime } = await Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload);

        request._isPayloadPending = !!(payload && payload._readableState);
        request.mime = mime;
        request.payload = payload;
    }
    catch (err) {
        if (request._isPayloadPending) {
            await internals.drain(request);
            request._isPayloadPending = false;
        }
        else {
            request._isPayloadPending = true;
        }

        request.mime = err.mime;
        request.payload = null;

        return request._core.toolkit.failAction(request, request.route.settings.payload.failAction, err, { tags: ['payload', 'error'] });
    }
};
Beispiel #3
0
        const read = function (err, data) {

            if (err) {
                return reply();
            }

            const stream = Wreck.toReadableStream(data);
            stream.headers = request.headers;

            Subtext.parse(stream, null, options, copy);
        };
Beispiel #4
0
internals.parseBufferPayload = function (stream, wl, fn) {

    const _validate = function (err, parsed) {

        if (err) {
            return fn(err);
        }

        const payload = parsed.payload;
        const items = _.keys(payload);
        const iterator = internals.validateFormFields(payload, wl);

        async.each(items, iterator, fn);
    };

    const options = {
        output: 'data',
        parse: true
    };

    return Subtext.parse(stream, null, options, _validate);
};
Beispiel #5
0
internals.payload = function (request, next) {

    if (request.method === 'get' ||
        request.method === 'head') {            // When route.method is '*'

        return next();
    }

    var onParsed = function (err, parsed) {

        request.mime = parsed.mime;
        request.payload = parsed.payload || null;

        if (!err) {
            return next();
        }

        var failAction = request.route.settings.payload.failAction;         // failAction: 'error', 'log', 'ignore'
        if (failAction !== 'ignore') {
            request._log(['payload', 'error'], err);
        }

        if (failAction === 'error') {
            return next(err);
        }

        return next();
    };

    Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload, function (err, parsed) {

        if (!err ||
            !request._isPayloadPending) {

            request._isPayloadPending = false;
            return onParsed(err, parsed);
        }

        // Flush out any pending request payload not consumed due to errors

        var stream = request.raw.req;

        var read = function () {

            stream.read();
        };

        var end = function () {

            stream.removeListener('readable', read);
            stream.removeListener('error', end);
            stream.removeListener('end', end);

            request._isPayloadPending = false;
            return onParsed(err, parsed);
        };

        stream.on('readable', read);
        stream.once('error', end);
        stream.once('end', end);
    });
};
Beispiel #6
0
internals.payload = function (request, next) {

    if (request.method === 'get' ||
        request.method === 'head') {            // When route.method is '*'

        return next();
    }

    if (request._expectContinue) {
        request.raw.res.writeContinue();
    }

    const onParsed = (err, parsed) => {

        request.mime = parsed.mime;
        request.payload = (parsed.payload === undefined ? null : parsed.payload);

        if (!err) {
            return next();
        }

        // failAction: 'error', 'log', 'ignore', function (request, reply, error)

        const failAction = request.route.settings.payload.failAction;
        if (failAction === 'ignore') {
            return next();
        }

        request._log(['payload', 'error'], err);

        // Log only

        if (failAction === 'log') {
            return next();
        }

        // Return error

        if (typeof failAction !== 'function') {
            return next(err);
        }

        // Custom handler

        request._protect.run(next, (exit) => {

            const reply = request.server._replier.interface(request, request.route.realm, {}, exit);
            return failAction(request, reply, err);
        });
    };

    Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload, (err, parsed) => {

        if (!err ||
            !request._isPayloadPending) {

            request._isPayloadPending = !!(err || (parsed.payload && parsed.payload._readableState));
            return onParsed(err, parsed);
        }

        // Flush out any pending request payload not consumed due to errors

        Streams.drain(request.raw.req, () => {

            request._isPayloadPending = false;
            return onParsed(err, parsed);
        });
    });
};