Skip to content

Commit 683f576

Browse files
author
Christian Cordiviola
committed
few fixes
1 parent 441ba33 commit 683f576

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

batchtool/wpt_batch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def RunBatch(options):
103103
submitted_urls = set(id_url_dict.values())
104104
for url in requested_urls:
105105
if url not in submitted_urls:
106-
logging.warn('URL submission failed: %s', url)
106+
logging.warning('URL submission failed: %s', url)
107107

108108
pending_test_ids = id_url_dict.keys()
109109
if not os.path.isdir(options.outputdir):
@@ -127,13 +127,13 @@ def RunBatch(options):
127127
if test_status == '200':
128128
completed_test_ids.append(test_id)
129129
else:
130-
logging.warn('Tests failed with status $s: %s', test_status, test_id)
130+
logging.warning('Tests failed with status $s: %s', test_status, test_id)
131131
test_results = wpt_batch_lib.GetXMLResult(completed_test_ids,
132132
server_url=options.server)
133133
result_test_ids = set(test_results.keys())
134134
for test_id in completed_test_ids:
135135
if test_id not in result_test_ids:
136-
logging.warn('The XML failed to retrieve: %s', test_id)
136+
logging.warning('The XML failed to retrieve: %s', test_id)
137137

138138
for test_id, dom in test_results.iteritems():
139139
SaveTestResult(options.outputdir, id_url_dict[test_id], test_id,

www/addresses.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
require_once __DIR__ . '/common.inc';
77

88
$user_api_key = $request_context->getApiKeyInUse();
9-
if (strlen($user_api_key)) {
9+
if (!empty($user_api_key)) {
1010
$keys_file = SETTINGS_PATH . '/keys.ini';
1111
if (file_exists(SETTINGS_PATH . '/common/keys.ini')) {
1212
$keys_file = SETTINGS_PATH . '/common/keys.ini';

www/getLocations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function LoadLocations($isPaid = false)
202202
$locations = array();
203203
$loc = LoadLocationsIni();
204204
$user_api_key = $request_context->getApiKeyInUse();
205-
if (strlen($user_api_key)) {
205+
if (!empty($user_api_key)) {
206206
foreach ($loc as $name => $location) {
207207
if (isset($location['browser']) && isset($location['noapi'])) {
208208
unset($loc[$name]);
@@ -215,7 +215,7 @@ function LoadLocations($isPaid = false)
215215
}
216216
}
217217
}
218-
$isPaid = !is_null($request_context->getUser()) && $request_context->getUser()->isPaid();
218+
$isPaid = !is_null($request_context->getUser()) && $request_context->getUser()->isPaid();
219219
$includePaid = $isPaid || $admin;
220220

221221
FilterLocations($loc, $includePaid);

www/src/RequestContext.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RequestContext
2525
private ?BannerMessageManager $banner_message_manager;
2626
// Should use an enum, TODO
2727
private string $environment;
28-
private string $api_key_in_use;
28+
private ?string $api_key_in_use;
2929

3030
private string $user_api_key_header = 'X-WPT-API-KEY';
3131

@@ -49,7 +49,7 @@ public function __construct(array $global_request, array $server = [], array $op
4949
$this->host = $options['host'] ?? Util::getSetting('host', "");
5050

5151
$this->environment = Environment::$Production;
52-
$this->api_key_in_use = "";
52+
$this->api_key_in_use = null;
5353
}
5454

5555
public function getRaw(): array
@@ -160,19 +160,20 @@ public function getEnvironment(): string
160160
* */
161161
public function getApiKeyInUse(): string
162162
{
163-
if (empty($this->api_key_in_use)) {
164-
$user_api_key_header = $this->user_api_key_header;
165-
$request_headers = getallheaders();
166-
$matching_headers = array_filter($request_headers, function ($k) use ($user_api_key_header) {
167-
return strtolower($k) == strtolower($user_api_key_header);
168-
}, ARRAY_FILTER_USE_KEY);
169-
if (!empty($matching_headers)) {
170-
$user_api_key = array_values($matching_headers)[0];
171-
}
172-
173-
$this->api_key_in_use = $user_api_key;
163+
if ($this->api_key_in_use == null) {
164+
$this->api_key_in_use = $this->readApiKey();
174165
}
175-
176166
return $this->api_key_in_use;
177167
}
168+
169+
private function readApiKey()
170+
{
171+
$request_headers = getallheaders();
172+
foreach ($request_headers as $k => $value) {
173+
if (strtolower($k) == strtolower($this->user_api_key_header)) {
174+
return trim($value);
175+
}
176+
}
177+
return '';
178+
}
178179
}

www/usage.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727

2828
<?php
2929
$user_api_key = $request_context->getApiKeyInUse();
30-
if (strlen($user_api_key)) {
31-
$key = trim($user_api_key);
30+
if (!empty($user_api_key)) {
3231
$keys_file = SETTINGS_PATH . '/keys.ini';
3332
if (file_exists(SETTINGS_PATH . '/common/keys.ini')) {
3433
$keys_file = SETTINGS_PATH . '/common/keys.ini';
@@ -38,7 +37,7 @@
3837
}
3938
$keys = parse_ini_file($keys_file, true);
4039

41-
if ($admin && $key == 'all') {
40+
if ($admin && $user_api_key == 'all') {
4241
if (!isset($_REQUEST['days'])) {
4342
$days = 1;
4443
}
@@ -66,7 +65,7 @@
6665
}
6766
$used = array();
6867
foreach ($keys as $key => &$keyUser) {
69-
$u = isset($usage[$key]) ? (int)$usage[$key] : 0;
68+
$u = isset($usage[$key]) ? (int) $usage[$key] : 0;
7069
if ($u) {
7170
$used[] = array('used' => $u, 'description' => $keyUser['description'], 'contact' => $keyUser['contact'], 'limit' => $keyUser['limit']);
7271
}
@@ -101,9 +100,9 @@
101100
}
102101
}
103102
} else {
104-
if (isset($keys[$key])) {
103+
if (isset($keys[$user_api_key])) {
105104
$out = array();
106-
$limit = (int)@$keys[$key]['limit'];
105+
$limit = (int) @$keys[$user_api_key]['limit'];
107106
if (!$json) {
108107
echo "<table class=\"table\"><tr><th>Date</th><th>Used</th><th>Limit</th></tr>";
109108
}
@@ -114,7 +113,7 @@
114113
$used = 0;
115114
if (is_file($keyfile)) {
116115
$usage = json_decode(file_get_contents($keyfile), true);
117-
$used = (int)@$usage[$key];
116+
$used = (int) @$usage[$user_api_key];
118117
}
119118
$date = $targetDate->format("Y/m/d");
120119
if ($json) {
@@ -128,9 +127,9 @@
128127
echo '</table>';
129128
}
130129

131-
$limit = (int)$keys[$key]['limit'];
132-
if (isset($usage[$key])) {
133-
$used = (int)$usage[$key];
130+
$limit = (int) $keys[$user_api_key]['limit'];
131+
if (isset($usage[$user_api_key])) {
132+
$used = (int) $usage[$user_api_key];
134133
} else {
135134
$used = 0;
136135
}

0 commit comments

Comments
 (0)