|
10 | 10 | #include <QNetworkRequest> |
11 | 11 | #include <QUrl> |
12 | 12 |
|
| 13 | +namespace { |
| 14 | +// Normalize path like webAO: lowercase and URL-encode each component |
| 15 | +// Uses encodeURI-compatible encoding (preserves safe characters like ! ' ( ) *) |
| 16 | +QString normalizePathForWeb(const QString &path) |
| 17 | +{ |
| 18 | + // Split path into components, lowercase and URL-encode each, then rejoin |
| 19 | + QStringList components = path.split('/'); |
| 20 | + QStringList encoded; |
| 21 | + // Characters that encodeURI does NOT encode (excluding / which we handle via split) |
| 22 | + // These are: A-Za-z0-9 ; , ? : @ & = + $ - _ . ! ~ * ' ( ) # |
| 23 | + // We only pass the non-alphanumeric ones since alphanumerics are never encoded |
| 24 | + const QByteArray safeChars = ";,?:@&=+$-_.!~*'()#"; |
| 25 | + for (const QString &component : components) |
| 26 | + { |
| 27 | + if (component.isEmpty()) |
| 28 | + { |
| 29 | + continue; |
| 30 | + } |
| 31 | + // Lowercase and URL-encode (percent-encode) the component |
| 32 | + QString lowered = component.toLower(); |
| 33 | + QString percentEncoded = QUrl::toPercentEncoding(lowered, safeChars); |
| 34 | + encoded.append(percentEncoded); |
| 35 | + } |
| 36 | + return encoded.join('/'); |
| 37 | +} |
| 38 | +} // namespace |
| 39 | + |
13 | 40 | WebCache::WebCache(AOApplication *parent) |
14 | 41 | : QObject(parent) |
15 | 42 | , ao_app(parent) |
@@ -61,7 +88,9 @@ QString WebCache::getCachedPath(const QString &relativePath) const |
61 | 88 | return QString(); |
62 | 89 | } |
63 | 90 |
|
64 | | - QString localPath = cacheDir() + subdir + relativePath; |
| 91 | + // Use normalized (lowercase) path for cache lookup |
| 92 | + QString normalizedPath = normalizePathForWeb(relativePath); |
| 93 | + QString localPath = cacheDir() + subdir + normalizedPath; |
65 | 94 |
|
66 | 95 | if (!file_exists(localPath)) |
67 | 96 | { |
@@ -123,42 +152,48 @@ void WebCache::resolveOrDownload(const QString &relativePath, const QStringList |
123 | 152 | assetUrl += '/'; |
124 | 153 | } |
125 | 154 |
|
126 | | - // Check if already downloading this path |
127 | | - if (m_pending_downloads.contains(relativePath)) |
128 | | - { |
129 | | - return; |
130 | | - } |
131 | | - |
132 | | - // Check if this path previously failed (don't retry within this session) |
133 | | - if (m_failed_downloads.contains(relativePath)) |
134 | | - { |
135 | | - return; |
136 | | - } |
137 | | - |
138 | 155 | // Get cache subdirectory for this server's asset URL |
139 | 156 | QString subdir = cacheSubdir(); |
140 | 157 | if (subdir.isEmpty()) |
141 | 158 | { |
142 | 159 | return; |
143 | 160 | } |
144 | 161 |
|
145 | | - // Try each suffix |
146 | | - for (const QString &suffix : suffixes) |
| 162 | + // Try each suffix (like webAO tries multiple extensions) |
| 163 | + QStringList effectiveSuffixes = suffixes.isEmpty() ? QStringList{""} : suffixes; |
| 164 | + for (const QString &suffix : effectiveSuffixes) |
147 | 165 | { |
148 | 166 | QString fullPath = relativePath + suffix; |
149 | | - QString localPath = cacheDir() + subdir + fullPath; |
| 167 | + |
| 168 | + // Normalize path like webAO: lowercase and URL-encode |
| 169 | + QString normalizedPath = normalizePathForWeb(fullPath); |
| 170 | + |
| 171 | + // Check if already downloading this path |
| 172 | + if (m_pending_downloads.contains(normalizedPath)) |
| 173 | + { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + // Check if this path previously failed (don't retry within this session) |
| 178 | + if (m_failed_downloads.contains(normalizedPath)) |
| 179 | + { |
| 180 | + continue; // Try next suffix |
| 181 | + } |
| 182 | + |
| 183 | + QString localPath = cacheDir() + subdir + normalizedPath; |
150 | 184 |
|
151 | 185 | // Skip if already cached and not expired |
152 | 186 | if (file_exists(localPath) && !isExpired(localPath)) |
153 | 187 | { |
154 | | - continue; |
| 188 | + return; // Already have a valid cached file |
155 | 189 | } |
156 | 190 |
|
157 | | - QString remoteUrl = assetUrl + fullPath; |
| 191 | + // Construct remote URL with normalized path |
| 192 | + QString remoteUrl = assetUrl + normalizedPath; |
158 | 193 |
|
159 | 194 | // Mark as pending and start download |
160 | | - m_pending_downloads.insert(relativePath, true); |
161 | | - startDownload(remoteUrl, localPath, relativePath); |
| 195 | + m_pending_downloads.insert(normalizedPath, true); |
| 196 | + startDownload(remoteUrl, localPath, normalizedPath); |
162 | 197 | return; // Only try one suffix at a time |
163 | 198 | } |
164 | 199 | } |
|
0 commit comments