应用程序/x"/>
我如何检查数据是否是正确的应用程序/x
我正在尝试检查发布数据是否是使用 nodejs 进行 www-urlencoded 的正确形式:
const connect = require('connect');
const { request } = require('node:http');
const app = connect();
/**
* Check if valis url encoded.
* For now I set the value as true
* @param {String} body
* @returns Boolean
*/
const isValidFormUrlEncoded = (body) => {
return true;
}
app.use(function(req,res,next){
req.id = 1;
var body = [];
req.on('data',(data) =>
{
console.log('Getting Body');
body.push(data)
});
req.on('end',() => {
body = Buffer.concat(body).toString();
if(
!body ||
request.header['content-type'] != 'application/x-www-form-urlencoded' ||
(request.header['content-type'] != 'application/x-www-form-urlencoded' && isValidFormUrlEncoded(body) )
){
next();
} else {
res.setHeader("Content-Type",'text/plain');
res.writeHead(400,{'X-val':3});
res.end("Body not form-url endoded.")
}
});
});
app.use(function(req,res){
console.log("id:",req.id);
res.setHeader("Content-Type",'text/plain');
res.writeHead(200,{'X-val':3});
res.end("jhello");
});
app.listen(8090)
但我不知道如何验证
application/x-www-form-urlencoded
的身体。
我想使用
node:querystring
包。但即使 body 无效,它也会解析任何内容。
例如我试过:
const querystring = require("node:querystring");
let parsedData = querystring.parse("Hello");
console.log(parsedData);
parsedData = querystring.parse("Hello Big Brother = d2232332r3*cdw How are you");
console.log(parsedData);
console.log(parsedData);
我得到:
node stackoverflow/connect_expirement.js
[Object: null prototype] { Hello: '' }
[Object: null prototype] { 'Hello Big Brother': 'd2232332r3*cdw How are you' }
Bot 我认为无效但
querystring
最好强制匹配/partse 即使字符串无效。关于如何检查正文是否是有效的 url 编码字符串的任何想法?
编辑1
我试图通过正则表达式来做:
const regex2 = new RegExp('^(([a-zA-Z1-9])+(\[(1-9)*\])?=[a-zA-Z1-9%]+&?)*([a-zA-Z1-9])+(\[?(1-9)*\]?)=[a-zA-Z1-9%]+$');
但是尽管这些测试似乎有效:
console.log(regex2.test('panties')); // false - works fine
console.log(regex2.test('hello=value&am=i')); // true - works fine
但是这些似乎失败了,我希望正则表达式匹配但不匹配:
console.log(regex2.test('pleas=help&me=plz&var[]=true&var[]=false'));
console.log(regex2.test('pleas=help&me=plz&var[]=true&var[2]=false&var['blahblah']=ipsum'));
我希望从上面的这个中获得真正的价值。但我不明白。所以我还是卡住了。
我也试过这个:
我根本没有得到预期的结果。
回答如下:最佳工作代码是:
const connect = require('connect');
const app = connect();
/**
* Check if valis url encoded.
* For now I set the value as true
* @param {String} body
* @returns Boolean
*/
const isValidFormUrlEncoded = (body) => {
return /^(?:(?:\w+)(?:\[(?:\d*|'[^']*')\])?=[\w%]*(?:&|$))*$/.test(body);
}
app.use(function(req,res,next){
req.id = 1;
var body = [];
req.on('data',(data) =>
{
console.log('Getting Body');
body.push(data)
});
req.on('end',() => {
console.log(req.headers);
body = Buffer.concat(body).toString();
console.log(body);
if(
!body ||
req.headers['content-type'] != 'application/x-www-form-urlencoded' ||
(req.headers['content-type'] == 'application/x-www-form-urlencoded' && isValidFormUrlEncoded(body) )
){
next();
} else {
res.setHeader("Content-Type",'text/plain');
res.writeHead(400,{'X-val':3});
res.end("Body not form-url endoded.")
}
});
});
app.use(function(req,res){
console.log("id:",req.id);
res.setHeader("Content-Type",'text/plain');
res.writeHead(200,{'X-val':3});
res.end("jhello");
});
app.listen(8090)
正如您所见,测试使用了正则表达式: https://regex101/r/CiJafp/4
可以使用:
/^(?:(?:\w+)(?:\[(?:\d*|'[^']*')\])?=[\w%]*(?:&|$))*$/.test(body)
请记住,正则表达式使用
//
而不是new Regexp()
进行注释。
更多推荐
我如何检查数据是否是正确的应用程序/x
发布评论