diff --git a/www/admin/UrlRequests.target.php b/www/admin/UrlRequests.target.php index c127b97d..ad53829f 100644 --- a/www/admin/UrlRequests.target.php +++ b/www/admin/UrlRequests.target.php @@ -1,4 +1,4 @@ - true, ]); +/** + * build return json + * + * @param array $http_headers + * @param string $body + * @return string + */ +function buildContent(array $http_headers, string $body): string +{ + return Json::jsonConvertArrayTo([ + 'HEADERS' => $http_headers, + "REQUEST_TYPE" => $_SERVER['REQUEST_METHOD'], + "PARAMS" => $_GET, + "BODY" => Json::jsonConvertToArray($body) + ]); +} + $http_headers = array_filter($_SERVER, function ($value, $key) { if (str_starts_with($key, 'HTTP_')) { return true; } }, ARRAY_FILTER_USE_BOTH); +header("Content-Type: application/json; charset=UTF-8"); + +// if the header has Authorization and RunAuthTest then exit with 401 +if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RUNAUTHTEST'])) { + header("HTTP/1.1 401 Unauthorized"); + print buildContent($http_headers, '["code": 401, "content": {"Error" => "Not Authorized"}]'); + exit; +} + $file_get = file_get_contents('php://input') ?: '{"Error" => "file_get_contents failed"}'; // str_replace('\"', '"', trim($file_get, '"')); $log->debug('SERVER', $log->prAr($_SERVER)); $log->debug('HEADERS', $log->prAr($http_headers)); +$log->debug('REQUEST TYPE', $_SERVER['REQUEST_METHOD']); $log->debug('GET', $log->prAr($_GET)); $log->debug('POST', $log->prAr($_POST)); $log->debug('PHP-INPUT', $log->prAr($file_get)); -header("Content-Type: application/json; charset=UTF-8"); - -print Json::jsonConvertArrayTo([ - 'HEADERS' => $http_headers, - "PARAMS" => $_GET, - "BODY" => Json::jsonConvertToArray($file_get), -]); +print buildContent($http_headers, $file_get); $log->debug('[END]', '=========================================>'); diff --git a/www/admin/class_test.url-requests.curl.php b/www/admin/class_test.url-requests.curl.php index 5b6b9ec7..f657e7de 100644 --- a/www/admin/class_test.url-requests.curl.php +++ b/www/admin/class_test.url-requests.curl.php @@ -204,6 +204,9 @@ try { 'default-remove-array-part-alt' => ['c', 'd', 'e'], 'default-overwrite' => 'will be overwritten', 'default-add' => 'will be added', + ], + 'query' => [ + 'global-p' => 'glob' ] ]); print "CONFIG:
" . print_r($uc->getConfig(), true) . "
"; @@ -217,13 +220,16 @@ try { print "CONFIG:
" . print_r($uc->getConfig(), true) . "
"; $data = $uc->request( 'get', - 'UrlRequests.target.php?other=get_a', + 'UrlRequests.target.php', [ 'headers' => [ 'call-header' => 'call-get', 'default-header' => 'overwrite-uc-get', 'X-Foo' => ['bar', 'baz'], - ] + ], + 'query' => [ + 'other' => 'get_a', + ], ] ); print "[uc] _GET RESPONSE, nothing set:
" . print_r($data, true) . "
"; @@ -234,6 +240,55 @@ try { print "Exception:
" . print_r(json_decode($e->getMessage(), true), true) . "

"; } +print "
"; +try { + $uc = new Curl([ + "base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/', + "exception_on_not_authorized" => false, + "headers" => [ + "Authorization" => "schmalztiegel", + "RunAuthTest" => "yes", + ] + ]); + $response = $uc->get('UrlRequests.target.php'); + print "AUTH REQUEST:
" . print_r($response, true) . "
"; + print "[uc] SENT URL: " . $uc->getUrlSent() . "
"; + print "[uc] SENT URL PARSED:
" . print_r($uc->getUrlParsedSent(), true) . "
"; + print "[uc] SENT HEADERS:
" . print_r($uc->getHeadersSent(), true) . "
"; +} catch (Exception $e) { + print "Exception:
" . print_r(json_decode($e->getMessage(), true), true) . "

"; +} +print "AUTH REQUEST WITH EXCEPTION:
"; +try { + $uc = new Curl([ + "base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/', + "exception_on_not_authorized" => true, + "headers" => [ + "Authorization" => "schmalztiegel", + "RunAuthTest" => "yes", + ] + ]); + $response = $uc->get('UrlRequests.target.php'); + print "AUTH REQUEST:
" . print_r($response, true) . "
"; + print "[uc] SENT URL: " . $uc->getUrlSent() . "
"; + print "[uc] SENT URL PARSED:
" . print_r($uc->getUrlParsedSent(), true) . "
"; + print "[uc] SENT HEADERS:
" . print_r($uc->getHeadersSent(), true) . "
"; +} catch (Exception $e) { + print "Exception:
" . print_r(json_decode($e->getMessage(), true), true) . "

"; +} + +print "
"; +$uc = new Curl([ + "base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/', + "headers" => [ + "header-one" => "one" + ] +]); +$response = $uc->get('UrlRequests.target.php', ["headers" => null, "query" => ["test" => "one-test"]]); +print "AUTH REQUEST:
" . print_r($response, true) . "
"; +print "[uc] SENT URL: " . $uc->getUrlSent() . "
"; +print "[uc] SENT URL PARSED:
" . print_r($uc->getUrlParsedSent(), true) . "
"; +print "[uc] SENT HEADERS:
" . print_r($uc->getHeadersSent(), true) . "
"; print "";