Skip to content

Commit c774f33

Browse files
committed
refacto : move proxy echo feature to as guzzle middleware
1 parent c5f86c9 commit c774f33

File tree

2 files changed

+101
-74
lines changed

2 files changed

+101
-74
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Lizmap\Request;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use Lizmap\App;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class EchoMiddleWare
11+
{
12+
/**
13+
* encoded "__echo__&".
14+
*/
15+
public const ENCODED_ECHO_PARAM = '%5F%5Fecho%5F%5F=&';
16+
17+
/**
18+
* check if $body contains a '__echo__=&' param.
19+
*
20+
* @return bool
21+
*/
22+
public function hasEchoInBody(string $body)
23+
{
24+
return strstr($body, self::ENCODED_ECHO_PARAM);
25+
}
26+
27+
/**
28+
* Try to find the content stored in log for a previous request.
29+
*
30+
* @param string $url request URL
31+
* @param string $body request body
32+
*/
33+
public function getEchoFromRequest(string $url, string $body): string
34+
{
35+
// md5 hash to search in the file
36+
$md5ToSearch = md5($url.'|'.str_replace(self::ENCODED_ECHO_PARAM, '', $body));
37+
38+
$logPath = \jApp::logPath('echoproxy.log');
39+
if (is_file($logPath)) {
40+
// retrieve the 50 last lines
41+
$nLastLines = preg_split("/\r\n|\n|\r/", App\FileTools::tail($logPath, 50));
42+
// key : md5 , value : usefull content
43+
foreach ($nLastLines as $line) {
44+
$words = explode("\t", $line);
45+
if (count($words) > 4
46+
&& $md5ToSearch == $words[3]) {
47+
// return the Url submitted to server
48+
return $words[4];
49+
}
50+
}
51+
52+
return 'unfound '.$md5ToSearch;
53+
}
54+
55+
return 'unfound echoproxy.log';
56+
}
57+
58+
/**
59+
* Log the URL and its body in the 'echoproxy' log file
60+
* We add a md5 hash of the string to help retrieving it later
61+
* NOTE : currently we log only the url & body, thus it doesn't really need to be logged
62+
* because the same url & body are needed to retreive the content
63+
* but the function will be useful when it will log additionnal content.
64+
*/
65+
public function logRequestToEcho(string $url, string $body)
66+
{
67+
$md5 = md5($url.'|'.$body);
68+
\jLog::log($md5."\t".$url.'?'.$body, 'echoproxy');
69+
}
70+
71+
public function __invoke(callable $handler)
72+
{
73+
return function (RequestInterface $request, array $options) use ($handler) {
74+
if ($this->hasEchoInBody($request->getBody())) {
75+
$content = $this->getEchoFromRequest($request->getUri(), $request->getBody());
76+
77+
$promise = $handler($request, $options);
78+
79+
return $promise->then(
80+
function (ResponseInterface $response) use ($content) {
81+
// We do not perform the request, but return the content previously logged
82+
return new Response(
83+
200,
84+
array('Content-Type' => 'text/json'),
85+
$content
86+
);
87+
}
88+
);
89+
}
90+
// All requests are logged
91+
$this->logRequestToEcho($request->getUri(), $request->getBody());
92+
93+
// invoke the initial handler
94+
return $handler($request, $options);
95+
};
96+
}
97+
}

lizmap/modules/lizmap/lib/Request/Proxy.php

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -387,23 +387,6 @@ protected static function sendRequest($url, $options)
387387
{
388388
list($url, $options) = self::buildHeaders($url, $options);
389389

390-
// check is the env variable is set
391-
if (getenv('ECHO_OGC_ORIGINAL_REQUEST')) {
392-
// did the request has to be echoed ?
393-
if (self::hasEchoInBody($options['body'])) {
394-
$content = self::getEchoFromRequest($url, $options['body']);
395-
396-
// We do not perform the request, but return the content previously logged
397-
return new Response(
398-
200,
399-
array('Content-Type' => 'text/json'),
400-
$content,
401-
);
402-
}
403-
// All requests are logged
404-
self::logRequestToEcho($url, $options['body']);
405-
}
406-
407390
if ($options['referer']) {
408391
$options['headers']['Referer'] = $options['referer'];
409392
}
@@ -420,6 +403,10 @@ protected static function sendRequest($url, $options)
420403
$stack = HandlerStack::create();
421404
$stack->push(new CacheMiddleware($strategy));
422405

406+
// check is the env variable is set
407+
if (getenv('ECHO_OGC_ORIGINAL_REQUEST')) {
408+
$stack->push(new EchoMiddleWare());
409+
}
423410
// Create Client
424411
$client = new Client(array(
425412
// You can set any number of default request options.
@@ -902,63 +889,6 @@ public static function clearLayerCache($repository, $project, $layer)
902889

903890
return true;
904891
}
905-
906-
/**
907-
* check if $body contains a '__echo__=&' param.
908-
*
909-
* @return bool
910-
*/
911-
public static function hasEchoInBody(string $body)
912-
{
913-
$encodedEchoParam = '%5F%5Fecho%5F%5F=&';
914-
915-
return strstr($body, $encodedEchoParam);
916-
}
917-
918-
/**
919-
* Log the URL and its body in the 'echoproxy' log file
920-
* We add a md5 hash of the string to help retrieving it later
921-
* NOTE : currently we log only the url & body, thus it doesn't really need to be logged
922-
* because the same url & body are needed to retreive the content
923-
* but the function will be useful when it will log additionnal content.
924-
*/
925-
public static function logRequestToEcho(string $url, string $body)
926-
{
927-
$md5 = md5($url.'|'.$body);
928-
\jLog::log($md5."\t".$url.'?'.$body, 'echoproxy');
929-
}
930-
931-
/**
932-
* return the content that was logged for the (url, body) params
933-
* using a md5 hash to search it in the log file.
934-
*
935-
* @see logRequestToEcho()
936-
*/
937-
public static function getEchoFromRequest(string $url, string $body): string
938-
{
939-
$encodedEchoParam = '%5F%5Fecho%5F%5F=&';
940-
// md5 hash to search in the file
941-
$md5ToSearch = md5($url.'|'.str_replace($encodedEchoParam, '', $body));
942-
943-
$logPath = \jApp::logPath('echoproxy.log');
944-
if (is_file($logPath)) {
945-
// retrieve the 50 last lines
946-
$nLastLines = preg_split("/\r\n|\n|\r/", App\FileTools::tail($logPath, 50));
947-
// key : md5 , value : usefull content
948-
$md5Assoc = array();
949-
foreach ($nLastLines as $line) {
950-
$words = explode("\t", $line);
951-
if (count($words) > 4
952-
&& $md5ToSearch == $words[3]) {
953-
return $words[4];
954-
}
955-
}
956-
957-
return 'unfound '.$md5ToSearch;
958-
}
959-
960-
return 'unfound echoproxy.log';
961-
}
962892
}
963893

964894
/*

0 commit comments

Comments
 (0)