-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.php
110 lines (94 loc) · 2.92 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
$respondWithJsonAndExit = function (array $responsePayload): void {
$responsePayloadString = json_encode($responsePayload);
echo $responsePayloadString;
file_put_contents('php://stdout', sprintf("Responding with: %s\n", $responsePayloadString));
exit;
};
if ($_SERVER['REQUEST_URI'] === '/reject/with-33-percent-chance') {
if (rand(0, 2) === 0) {
$respondWithJsonAndExit([
'id' => 'rejecting-unlucky-ones',
'action' => 'reject',
'responseStatusCode' => 403,
'rejectionErrorCode' => 'M_FORBIDDEN',
'rejectionErrorMessage' => 'Rejecting it via a hook delivered from a REST service',
]);
}
$respondWithJsonAndExit([
'id' => 'allowing-lucky-ones',
'action' => 'pass.unmodified',
]);
}
if ($_SERVER['REQUEST_URI'] === '/reject/forbidden') {
$respondWithJsonAndExit([
'id' => 'rejection-response-hook',
'action' => 'reject',
'responseStatusCode' => 403,
'rejectionErrorCode' => 'M_FORBIDDEN',
'rejectionErrorMessage' => 'Rejecting it via a hook delivered from a REST service',
]);
}
if ($_SERVER['REQUEST_URI'] === '/inject-something-into-request') {
$respondWithJsonAndExit([
'id' => 'injection-request-hook',
'action' => 'pass.modifiedRequest',
"injectJSONIntoRequest" => [
'customKey' => 'value',
],
'injectHeadersIntoRequest' => [
'X-Custom-Header' => 'Header-Value',
],
]);
}
if ($_SERVER['REQUEST_URI'] === '/inject-something-into-response') {
$respondWithJsonAndExit([
'id' => 'injection-response-hook',
'action' => 'pass.modifiedResponse',
"injectJSONIntoResponse" => [
'customKey' => 'value',
],
'injectHeadersIntoResponse' => [
'X-Custom-Header' => 'Header-Value',
],
]);
}
if ($_SERVER['REQUEST_URI'] === '/respond-with-something') {
// We could read the request (and possibly response) information here,
// and act depending on that.
//
// See how we do it for the `/dump` handler for an example.
$respondWithJsonAndExit([
'id' => 'respond-directly',
'action' => 'respond',
"responseStatusCode" => 200,
'responsePayload' => [
'message' => 'This response is coming from the REST service',
],
]);
}
if ($_SERVER['REQUEST_URI'] === '/dump') {
$payload = file_get_contents('php://input');
file_put_contents('php://stdout', sprintf("Request: %s\n", print_r($_SERVER, true)));
file_put_contents('php://stdout', sprintf("Payload: %s\n", $payload));
// The payload may or may not be JSON.
// So errors below may mean malformed JSON input, or (in rare cases) something different than JSON.
$json = json_decode($payload);
if (json_last_error() != JSON_ERROR_NONE) {
file_put_contents('php://stdout', sprintf(
"Payload parsing error (%s): %s\n",
json_last_error(),
json_last_error_msg(),
));
} else {
file_put_contents('php://stdout', "Payload JSON parsing: OK\n");
}
$respondWithJsonAndExit([
'id' => 'passed-after-dump',
'action' => 'pass.unmodified',
]);
}
$respondWithJsonAndExit([
'id' => 'default',
'action' => 'pass.unmodified',
]);