Skip to content

Instantly share code, notes, and snippets.

@defeo
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defeo/5df5fe5365a947150a7d to your computer and use it in GitHub Desktop.
Save defeo/5df5fe5365a947150a7d to your computer and use it in GitHub Desktop.
Reflector, tutorial for Applications Web & Sécurité (http://defeo.lu/aws)
var express = require('express');
var bodyP = require('body-parser');
var cookieP = require('cookie-parser');
var app = express();
// Configuration des middlewares
app
.use(bodyP.urlencoded( { extended: false } ))
.use(cookieP());
function to_html(obj) {
var out = "";
for (var x in obj) {
out += x + ": " + obj[x] + "<br>";
}
return out;
};
app.get('/query_string', function(req, res) {
res.send(to_html(req.query));
});
app.get('/form_data', function(req, res) {
res.send(to_html(req.body));
});
app.get('/headers', function(req, res) {
res.send(to_html(req.headers) + "<br><br>" + to_html(req.cookies));
});
app.get('/', function(req, res) {
var out = "";
var objects = { "Query string" : req.query,
"Request body" : req.body,
"Headers" : req.headers,
"Cookies" : req.cookies };
for (var obj in objects) {
out += "<h2>" + obj + "</h2><p>" + to_html(objects[obj]) + "</p>";
}
res.send(out);
});
app.listen(8080);
<?php
require_once 'vendor/autoload.php';
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
$app = new Application();
// Configuration
$app['debug'] = true;
$qs = function(Request $req) {
$out = "";
foreach ($req->query as $a => $b) {
$out .= "$a: $b<br>";
}
return $out;
};
$fd = function(Request $req) {
$out = "";
foreach ($req->request as $a => $b) {
$out .= "$a: $b<br>";
}
return $out;
};
$hd = function(Request $req) {
$out = "";
foreach ($req->headers as $a => $b) {
$out .= "$a: $b[0]<br>";
}
$out .= "<br><br>";
foreach ($req->cookies as $a => $b) {
$out .= "$a: $b<br>";
}
return $out;
};
$app->get('/query_string', $qs);
$app->post('/form_data', $fd);
$app->match('/headers', $hd);
$app->match('/', function(Request $req) use ($qs, $fd, $hd) {
$res = "<h2>Query string</h2> <p>" . $qs($req) . "</p>";
$res .= "<h2>Request body</h2> <p>" . $fd($req) . "</p>";
$res .= "<h2>Headers & Cookies</h2> <p>" . $hd($req) . "</p>";
return $res;
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment