summaryrefslogtreecommitdiffstats
path: root/LayoutTests/http/tests/resources/network-simulator.php
blob: 486002813b5df28c13f36c0153ea7fc0064d3e20 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
require_once 'portabilityLayer.php';

// This script acts as a stateful proxy for retrieving files. When the state is set to
// offline, it simulates a network error by redirecting to itself.

if (!sys_get_temp_dir()) {
    echo "FAIL: No temp dir was returned.\n";
    exit();
}

function setState($newState, $file)
{
    file_put_contents($file, $newState);
}

function getState($file)
{
    if (!file_exists($file)) {
        return "Uninitialized";
    }
    return file_get_contents($file);
}

function contentType($path)
{
    if (preg_match("/\.html$/", $path))
        return "text/html";
    if (preg_match("/\.manifest$/", $path))
        return "text/cache-manifest";
    if (preg_match("/\.js$/", $path))
        return "text/javascript";
    if (preg_match("/\.xml$/", $path))
        return "application/xml";
    if (preg_match("/\.xhtml$/", $path))
        return "application/xhtml+xml";
    if (preg_match("/\.svg$/", $path))
        return "application/svg+xml";
    if (preg_match("/\.xsl$/", $path))
        return "application/xslt+xml";
    if (preg_match("/\.gif$/", $path))
        return "image/gif";
    if (preg_match("/\.jpg$/", $path))
        return "image/jpeg";
    if (preg_match("/\.png$/", $path))
        return "image/png";
    return "text/plain";
}

function generateNoCacheHTTPHeader()
{
    header("Expires: Thu, 01 Dec 2003 16:00:00 GMT");
    header("Cache-Control: no-cache, no-store, must-revalidate");
    header("Pragma: no-cache");
}

function generateResponse($path)
{
    global $stateFile;
    $state = getState($stateFile);
    if ($state == "Offline") {
        header('HTTP/1.1 307 Temporary Redirect');
        # Simulate a network error by redirecting to self.
        header('Location: ' . $_SERVER['REQUEST_URI']);
    } else {
        // A little securuty checking can't hurt.
        if (strstr($path, ".."))
            exit;

        if ($path[0] == '/')
            $path = '..' . $path;

        generateNoCacheHTTPHeader();    

        if (file_exists($path)) {
            header("Last-Modified: " . gmdate("D, d M Y H:i:s T", filemtime($path)));
            header("Content-Type: " . contentType($path));
        
            print file_get_contents($path);
        } else {
            header('HTTP/1.1 404 Not Found');
        }
    }
}

function handleIncreaseResourceCountCommand($path)
{
    $resourceCountFile = sys_get_temp_dir() . "/resource-count";
    $resourceCount = getState($resourceCountFile);
    $pieces = explode(" ", $resourceCount);
    $count = 0;
    if (count($pieces) == 2 && $pieces[0] == $path) {
        $count = 1 + $pieces[1];
    } else {
        $count = 1;
    }
    file_put_contents($resourceCountFile, $path . " " . $count);
    generateResponse($path);
}

function handleResetResourceCountCommand()
{
    $resourceCountFile = sys_get_temp_dir() . "/resource-count";
    file_put_contents($resourceCountFile, 0);
    generateNoCacheHTTPHeader();
    header('HTTP/1.1 200 OK');
}

function handleGetResourceCountCommand($path)
{
    $resourceCountFile = sys_get_temp_dir() . "/resource-count";
    $resourceCount = getState($resourceCountFile);
    $pieces = explode(" ", $resourceCount);
    generateNoCacheHTTPHeader();
    header('HTTP/1.1 200 OK');
    if (count($pieces) == 2 && $pieces[0] == $path) {
        echo $pieces[1];
    } else {
        echo 0;
    }
}

function handleStartResourceRequestsLog()
{
    $resourceLogFile = sys_get_temp_dir() . "/resource-log";
    file_put_contents($resourceLogFile,  "");
}

function handleClearResourceRequestsLog()
{
    $resourceLogFile = sys_get_temp_dir() . "/resource-log";
    file_put_contents($resourceLogFile, "");
}

function handleGetResourceRequestsLog()
{
    $resourceLogFile = sys_get_temp_dir() . "/resource-log";

    generateNoCacheHTTPHeader();
    header("Content-Type: text/plain");

    print file_get_contents($resourceLogFile);
}

function handleLogResourceRequest($path)
{
    $resourceLogFile = sys_get_temp_dir() . "/resource-log";
    
    $newData = "\n".$path;
    // Documentation says that appends are atomic.
    file_put_contents($resourceLogFile, $newData, FILE_APPEND);
    generateResponse($path);
}

$stateFile = sys_get_temp_dir() . "/network-simulator-state";
$command = $_GET['command'];
if ($command) {
    if ($command == "connect")
        setState("Online", $stateFile);
    else if ($command == "disconnect")
        setState("Offline", $stateFile);
    else if ($command == "increase-resource-count")
        handleIncreaseResourceCountCommand($_GET['path']);
    else if ($command == "reset-resource-count")
        handleResetResourceCountCommand();
    else if ($command == "get-resource-count")
        handleGetResourceCountCommand($_GET['path']);
    else if ($command == "start-resource-request-log")
        handleStartResourceRequestsLog();
    else if ($command == "clear-resource-request-log")
        handleClearResourceRequestsLog();
    else if ($command == "get-resource-request-log")
        handleGetResourceRequestsLog();
    else if ($command == "log-resource-request")
        handleLogResourceRequest($_GET['path']);
    else
        echo "Unknown command: " . $command . "\n";
    exit();
}

$requestedPath = $_GET['path'];
generateResponse($requestedPath);
?>