Issues (3)

src/Resources/fake_agent.php (1 issue)

1
<?php
2
3
/**
4
 * Examples of payload.
5
 *
6
 * GET.
7
 * Request: GET {"host": "host1.com", "request_uri": "/foo", "user_agent": "redirection-io-client/0.0.1", "referer": "http://host0.com", "use_json": true}
8
 * Response: {"status_code":301,"location":"/bar"}
9
 *
10
 * LOG.
11
 * Request: LOG {"status_code": 301, "host": "host1.com", "request_uri": "/foo", "user_agent": "redirection-io-client/0.0.1", "referer": "http://host0.com", "use_json": true}
12
 * Response: ok
13
 */
14
set_time_limit(0);
15
16
$projectKey = 'szio2389-bfdz-51e8-8468-02dcop129501:ep6a4805-eo6z-dzo6-aeb0-8c1lbmo40242';
17
$socketType = isset($_SERVER['RIO_SOCKET_TYPE']) ? $_SERVER['RIO_SOCKET_TYPE'] : 'AF_INET';
18
$socketPath = isset($_SERVER['RIO_SOCKET_PATH']) ? $_SERVER['RIO_SOCKET_PATH'] : sys_get_temp_dir().'/fake_agent.sock';
19
$ip = isset($_SERVER['RIO_HOST']) ? $_SERVER['RIO_HOST'] : 'localhost';
20
$port = isset($_SERVER['RIO_PORT']) ? $_SERVER['RIO_PORT'] : 3100;
21
$timeout = 1000000; // seconds
22
$matcher = [
23
    ['/foo', '/bar', 301],
24
    ['/baz', '/qux', 302],
25
    ['/quux', '/corge', 307],
26
    ['/uier', '/grault', 308],
27
    ['/garply', '', 410],
28
];
29
30
switch ($socketType) {
31
    case 'AF_INET':
32
        $local_socket = "tcp://$ip:$port";
33
        break;
34
    case 'AF_UNIX':
35
        $local_socket = "unix://$socketPath";
36
        break;
37
    default:
38
        echo 'Please set a `RIO_SOCKET_TYPE` env var to `AF_INET` or `AF_UNIX`';
39
        exit(1);
40
}
41
42
@unlink($socketPath);
43
if (!$socket = stream_socket_server($local_socket, $errNo, $errMsg)) {
44
    echo "Couldn't create stream_socket_server: [$errNo] $errMsg";
45
    exit(1);
46
}
47
48
echo "Fake agent started on $local_socket\n";
49
50
while (true) {
51
    $client = stream_socket_accept($socket, $timeout);
52
53
    if (!$client) {
54
        continue;
55
    }
56
57
    $readPart = function ($client) {
58
        $buffer = '';
59
        while (true) {
60
            if (stream_get_meta_data($client)['eof']) {
61
                break;
62
            }
63
64
            $char = fread($client, 1);
65
66
            if (false === $char) {
67
                return false;
68
            }
69
70
            // On timeout char is empty
71
            if ('' === $char) {
72
                return false;
73
            }
74
75
            if ("\0" === $char) {
76
                return $buffer;
77
            }
78
79
            $buffer .= $char;
80
        }
81
    };
82
83
    while (true) {
84
        $cmdName = $readPart($client);
85
86
        if (false === $cmdName) {
87
            fclose($client);
88
            break;
89
        }
90
91
        $cmdData = $readPart($client);
92
93
        if (false === $cmdData) {
94
            fclose($client);
95
            break;
96
        }
97
98
        if ('MATCH' === $cmdName || 'MATCH_WITH_RESPONSE' === $cmdName) {
99
            findRedirect($client, $cmdData, $matcher, $projectKey);
100
        } elseif ('LOG' === $cmdName) {
101
            logRedirect($client);
102
        } else {
103
            echo "Unknown command: '$cmdName'\n";
104
            fclose($client);
105
106
            break;
107
        }
108
    }
109
110
    fclose($client);
111
}
112
113
fclose($socket);
114
115
function findRedirect($client, $cmdData, $matcher, $projectKey)
116
{
117
    $req = json_decode($cmdData, true);
118
119
    $nbMatchers = count($matcher);
120
121
    $res = json_encode([
122
        'status_code' => 0,
123
        'location' => '',
124
    ]);
125
126
    for ($i = 0; $i < $nbMatchers; ++$i) {
127
        if ($matcher[$i][0] === $req['request_uri'] && $projectKey === $req['project_id']) {
128
            $res = json_encode([
129
                'status_code' => $matcher[$i][2],
130
                'location' => $matcher[$i][1],
131
            ]);
132
133
            break;
134
        }
135
    }
136
137
    $writed = fwrite($client, $res."\0", strlen($res) + 1);
138
139
    var_dump($writed);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($writed) looks like debug code. Are you sure you do not want to remove it?
Loading history...
140
}
141
142
function logRedirect($client)
143
{
144
}
145