I made a simplke add that detects whether http body is base64 endoded:
const connect = require('connect');
const base64RegExp = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/;
const isBase64 = (str) => base64RegExp.test(str)
const app = connect();
app.use(function(req,res,next){
var body = [];
req.on('data',(data) =>
{
console.log('Getting Body');
body.push(data)
});
req.on('end',() => {
body = body.toString();
res.setHeader("Content-Type",'text/plain');
res.writeHead(200);
if(isBase64(body)){
res.end("BOdy base64 encoded\n");
} else {
res.end("BOdy not base64 encoded\n");
}
});
});
app.listen(8090);
But once I do:
base64 ~/Εικόνες/IMG_20230124_114954.jpg | curl --request POST --data @- http://127.0.0.1:8090
I get:
BOdy not base64 encoded
Meaning that the app, fails to recognize a base64 encoded string as base64 enbcoded string. Any idea why fails to do so?
I tried to use the approach mentioned upon this answer
Approach 1
Furthermore I tried to use:
if(isBase64(body.replace("\n","").replace("\r",""))){
res.end("BOdy base64 encoded\n");
} else {
res.end("BOdy not base64 encoded\n");
}
Still I get same result.
approach 2:
I also tried this to fix it:
const connect = require('connect');
// const base64RegExp = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/;
const isBase64 = (str) => Buffer.from(str.replace("\n","").replace("\r",""), 'base64').toString('base64') === str
const app = connect();
app.use(function(req,res,next){
var body = [];
req.on('data',(data) =>
{
console.log('Getting Body');
body.push(data)
});
req.on('end',() => {
body = body.toString();
res.setHeader("Content-Type",'text/plain');
res.writeHead(200);
if(isBase64(body.replace("\n","").replace("\r",""))){
res.end("BOdy base64 encoded\n");
} else {
res.end("BOdy not base64 encoded\n");
}
});
});
app.listen(8090);
Same result despite sending base64 encoded data