2023-12-06 11:59:35 +01:00
|
|
|
/*!
|
|
|
|
* method-override
|
|
|
|
* Copyright(c) 2010 Sencha Inc.
|
|
|
|
* Copyright(c) 2011 TJ Holowaychuk
|
|
|
|
* Copyright(c) 2014 Jonathan Ong
|
|
|
|
* Copyright(c) 2014 Douglas Christopher Wilson
|
|
|
|
* MIT Licensed
|
|
|
|
*/
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
'use strict'
|
|
|
|
|
2023-12-06 11:59:35 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
var debug = require('debug')('method-override')
|
|
|
|
var methods = require('methods')
|
|
|
|
var parseurl = require('parseurl')
|
|
|
|
var querystring = require('querystring')
|
|
|
|
var vary = require('vary')
|
2023-12-06 11:59:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Method Override:
|
|
|
|
*
|
|
|
|
* Provides faux HTTP method support.
|
|
|
|
*
|
|
|
|
* Pass an optional `getter` to use when checking for
|
|
|
|
* a method override.
|
|
|
|
*
|
|
|
|
* A string is converted to a getter that will look for
|
|
|
|
* the method in `req.body[getter]` and a function will be
|
|
|
|
* called with `req` and expects the method to be returned.
|
|
|
|
* If the string starts with `X-` then it will look in
|
|
|
|
* `req.headers[getter]` instead.
|
|
|
|
*
|
|
|
|
* The original method is available via `req.originalMethod`.
|
|
|
|
*
|
|
|
|
* @param {string|function} [getter=X-HTTP-Method-Override]
|
|
|
|
* @param {object} [options]
|
|
|
|
* @return {function}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
module.exports = function methodOverride (getter, options) {
|
|
|
|
var opts = options || {}
|
2023-12-06 11:59:35 +01:00
|
|
|
|
|
|
|
// get the getter fn
|
|
|
|
var get = typeof getter === 'function'
|
|
|
|
? getter
|
|
|
|
: createGetter(getter || 'X-HTTP-Method-Override')
|
|
|
|
|
|
|
|
// get allowed request methods to examine
|
2024-10-04 09:38:27 +02:00
|
|
|
var methods = opts.methods === undefined
|
2023-12-06 11:59:35 +01:00
|
|
|
? ['POST']
|
2024-10-04 09:38:27 +02:00
|
|
|
: opts.methods
|
2023-12-06 11:59:35 +01:00
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
return function methodOverride (req, res, next) {
|
2023-12-06 11:59:35 +01:00
|
|
|
var method
|
|
|
|
var val
|
|
|
|
|
|
|
|
req.originalMethod = req.originalMethod || req.method
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
// validate request is an allowed method
|
2023-12-06 11:59:35 +01:00
|
|
|
if (methods && methods.indexOf(req.originalMethod) === -1) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
val = get(req, res)
|
|
|
|
method = Array.isArray(val)
|
|
|
|
? val[0]
|
|
|
|
: val
|
|
|
|
|
|
|
|
// replace
|
|
|
|
if (method !== undefined && supports(method)) {
|
|
|
|
req.method = method.toUpperCase()
|
2024-10-04 09:38:27 +02:00
|
|
|
debug('override %s as %s', req.originalMethod, req.method)
|
2023-12-06 11:59:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a getter for the given string.
|
|
|
|
*/
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
function createGetter (str) {
|
2023-12-06 11:59:35 +01:00
|
|
|
if (str.substr(0, 2).toUpperCase() === 'X-') {
|
|
|
|
// header getter
|
|
|
|
return createHeaderGetter(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return createQueryGetter(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a getter for the given query key name.
|
|
|
|
*/
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
function createQueryGetter (key) {
|
|
|
|
return function (req, res) {
|
2023-12-06 11:59:35 +01:00
|
|
|
var url = parseurl(req)
|
|
|
|
var query = querystring.parse(url.query || '')
|
|
|
|
return query[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a getter for the given header name.
|
|
|
|
*/
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
function createHeaderGetter (str) {
|
|
|
|
var name = str.toLowerCase()
|
2023-12-06 11:59:35 +01:00
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
return function (req, res) {
|
2023-12-06 11:59:35 +01:00
|
|
|
// set appropriate Vary header
|
|
|
|
vary(res, str)
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
// get header
|
|
|
|
var header = req.headers[name]
|
|
|
|
|
|
|
|
if (!header) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2023-12-06 11:59:35 +01:00
|
|
|
// multiple headers get joined with comma by node.js core
|
2024-10-04 09:38:27 +02:00
|
|
|
var index = header.indexOf(',')
|
|
|
|
|
|
|
|
// return first value
|
|
|
|
return index !== -1
|
|
|
|
? header.substr(0, index).trim()
|
|
|
|
: header.trim()
|
2023-12-06 11:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if node supports `method`.
|
|
|
|
*/
|
|
|
|
|
2024-10-04 09:38:27 +02:00
|
|
|
function supports (method) {
|
|
|
|
return method &&
|
|
|
|
typeof method === 'string' &&
|
|
|
|
methods.indexOf(method.toLowerCase()) !== -1
|
2023-12-06 11:59:35 +01:00
|
|
|
}
|