lingxia-lxapp 0.6.1

LxApp (lightweight application) container and runtime for LingXia framework
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use super::uri as lx_uri;
use crate::plugin;
use http::{Request, Response, StatusCode, Uri};
use lingxia_platform::traits::app_runtime::AppRuntime;
use lingxia_webview::WebResourceResponse;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use crate::error;
use crate::error::LxAppError;
use crate::lxapp::LxApp;
use crate::page::PageInstance;

enum ResolvedLxAsset {
    FilePath(PathBuf),
    BuiltinAsset(String),
}

impl LxApp {
    /// Handler for lx:// scheme requests to access static app assets (images, CSS, JS, etc.)
    /// HTML files are handled separately through generate_page_html and load_data
    pub fn handle_lingxia_request(
        &self,
        page: &PageInstance,
        req: Request<Vec<u8>>,
    ) -> Option<WebResourceResponse> {
        let uri = req.uri();
        match uri.host() {
            Some(lx_uri::HOST_ASSETS) => {
                return self.handle_sdk_asset(uri);
            }
            Some(lx_uri::HOST_LXAPP)
            | Some(lx_uri::HOST_PLUGIN)
            | Some(lx_uri::HOST_TEMP)
            | Some(lx_uri::HOST_USER_CACHE)
            | Some(lx_uri::HOST_USER_DATA) => {}
            Some(other) => {
                error!("Unknown lx host: {}", other).with_appid(self.appid.clone());
                return Some(self.create_error_response(
                    StatusCode::BAD_REQUEST,
                    "Unknown Host",
                    &format!("Unknown lx host '{}'.", other),
                ));
            }
            None => {
                error!("lx request missing host: {}", uri).with_appid(self.appid.clone());
                return Some(self.create_error_response(
                    StatusCode::BAD_REQUEST,
                    "Invalid URL",
                    "The URL is missing a host component and cannot be processed.",
                ));
            }
        }

        let asset_path = match self.resolve_lx_uri(page, uri) {
            Ok(ResolvedLxAsset::BuiltinAsset(relative)) => {
                return self.handle_builtin_lxapp_asset(&relative);
            }
            Ok(ResolvedLxAsset::FilePath(path)) => path,
            Err(e) => {
                error!("resolve_lx_uri failed for {}: {}", uri.path(), e)
                    .with_appid(self.appid.clone());
                return Some(self.create_error_response(
                    StatusCode::NOT_FOUND,
                    "Asset Not Found",
                    &format!("The requested asset '{}' could not be found.", uri.path()),
                ));
            }
        };

        let metadata = match fs::metadata(&asset_path) {
            Ok(meta) => meta,
            Err(e) => {
                error!(
                    "Failed to read asset metadata: {} - {}",
                    asset_path.display(),
                    e
                )
                .with_appid(self.appid.clone());
                return Some(self.create_error_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "Asset Error",
                    "Failed to read asset metadata.",
                ));
            }
        };

        let file_len = metadata.len();
        let mime_type = Self::infer_mime_type(uri.path());

        let mut builder = Response::builder()
            .status(StatusCode::OK)
            .header("Content-Type", mime_type)
            .header("Access-Control-Allow-Origin", "null");

        if let Ok(value) = http::HeaderValue::from_str(&file_len.to_string()) {
            builder = builder.header("Content-Length", value);
        }

        let response = builder.body(()).unwrap_or_else(|_| {
            Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(())
                .expect("Failed to build fallback empty response")
        });

        let (parts, _) = response.into_parts();
        self.touch_user_cache_access_time(&asset_path);
        Some((parts, asset_path).into())
    }

    fn handle_sdk_asset(&self, uri: &Uri) -> Option<WebResourceResponse> {
        let path = uri.path().trim_start_matches('/');
        if path.is_empty() {
            return Some(self.create_error_response(
                StatusCode::BAD_REQUEST,
                "Invalid Asset Path",
                "Asset path is empty.",
            ));
        }
        if lx_uri::has_invalid_segment(path) {
            return Some(self.create_error_response(
                StatusCode::BAD_REQUEST,
                "Invalid Asset Path",
                "Asset path contains invalid segments.",
            ));
        }

        let mut reader = match self.runtime.read_asset(path) {
            Ok(reader) => reader,
            Err(e) => {
                error!("Failed to read sdk asset {}: {}", path, e).with_appid(self.appid.clone());
                return Some(self.create_error_response(
                    StatusCode::NOT_FOUND,
                    "Asset Not Found",
                    &format!("The requested asset '{}' could not be found.", path),
                ));
            }
        };

        let mut data = Vec::new();
        if let Err(e) = reader.read_to_end(&mut data) {
            error!("Failed to read sdk asset {}: {}", path, e).with_appid(self.appid.clone());
            return Some(self.create_error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                "Asset Read Error",
                "Failed to read asset data.",
            ));
        }

        let mime_type = Self::infer_mime_type(path);
        let mut builder = Response::builder()
            .status(StatusCode::OK)
            .header("Content-Type", mime_type)
            .header("Access-Control-Allow-Origin", "null");

        if let Ok(value) = http::HeaderValue::from_str(&data.len().to_string()) {
            builder = builder.header("Content-Length", value);
        }

        let response = builder.body(()).unwrap_or_else(|_| {
            Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(())
                .expect("Failed to build fallback empty response")
        });

        let (parts, _) = response.into_parts();
        Some((parts, data).into())
    }

    fn resolve_lx_uri(
        &self,
        page: &PageInstance,
        uri: &Uri,
    ) -> Result<ResolvedLxAsset, LxAppError> {
        match uri.host() {
            Some(lx_uri::HOST_LXAPP) => {
                if matches!(
                    self.bundle_source,
                    crate::lxapp::LxAppBundleSource::BuiltinAssets { .. }
                ) {
                    self.resolve_lxapp_relative_path(page, uri)
                        .map(ResolvedLxAsset::BuiltinAsset)
                } else {
                    self.resolve_lxapp_uri(page, uri)
                        .map(ResolvedLxAsset::FilePath)
                }
            }
            Some(lx_uri::HOST_PLUGIN) => self
                .resolve_plugin_uri(page, uri)
                .map(ResolvedLxAsset::FilePath),
            Some(lx_uri::HOST_TEMP) => self
                .resolve_lx_path_uri(
                    &lx_uri::LxUri::from_str(&uri.to_string())
                        .map_err(|_| LxAppError::ResourceNotFound(uri.to_string()))?,
                )
                .map(ResolvedLxAsset::FilePath),
            Some(lx_uri::HOST_USER_CACHE) => self
                .resolve_user_dir_uri(uri, &self.user_cache_dir)
                .map(ResolvedLxAsset::FilePath),
            Some(lx_uri::HOST_USER_DATA) => self
                .resolve_user_dir_uri(uri, &self.user_data_dir)
                .map(ResolvedLxAsset::FilePath),
            _ => Err(LxAppError::ResourceNotFound(uri.to_string())),
        }
    }

    fn handle_builtin_lxapp_asset(&self, relative_path: &str) -> Option<WebResourceResponse> {
        let asset_root = self.bundle_source.builtin_asset_root()?;
        let asset_path = format!(
            "{}/{}",
            asset_root.trim_end_matches('/'),
            relative_path.trim_start_matches('/')
        );

        let mut reader = match self.runtime.read_asset(&asset_path) {
            Ok(reader) => reader,
            Err(e) => {
                error!("Failed to read builtin lxapp asset {}: {}", asset_path, e)
                    .with_appid(self.appid.clone());
                return Some(self.create_error_response(
                    StatusCode::NOT_FOUND,
                    "Asset Not Found",
                    &format!(
                        "The requested asset '{}' could not be found.",
                        relative_path
                    ),
                ));
            }
        };

        let mut data = Vec::new();
        if let Err(e) = reader.read_to_end(&mut data) {
            error!("Failed to read builtin lxapp asset {}: {}", asset_path, e)
                .with_appid(self.appid.clone());
            return Some(self.create_error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                "Asset Read Error",
                "Failed to read asset data.",
            ));
        }

        let mime_type = Self::infer_mime_type(relative_path);
        let mut builder = Response::builder()
            .status(StatusCode::OK)
            .header("Content-Type", mime_type)
            .header("Access-Control-Allow-Origin", "null");

        if let Ok(value) = http::HeaderValue::from_str(&data.len().to_string()) {
            builder = builder.header("Content-Length", value);
        }

        let response = builder.body(()).unwrap_or_else(|_| {
            Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(())
                .expect("Failed to build fallback empty response")
        });

        let (parts, _) = response.into_parts();
        Some((parts, data).into())
    }

    fn resolve_user_dir_uri(&self, uri: &Uri, base_dir: &Path) -> Result<PathBuf, LxAppError> {
        let decoded_path = lx_uri::decode_lx_path(uri.path());
        let rel = decoded_path.trim_matches('/');
        if rel.is_empty()
            || lx_uri::has_invalid_segment(rel)
            || rel.contains(':')
            || rel.contains('\\')
        {
            return Err(LxAppError::ResourceNotFound(uri.to_string()));
        }

        let absolute = base_dir.join(rel);
        let absolute_str = absolute.to_string_lossy();
        self.resolve_accessible_path(absolute_str.as_ref())
    }

    fn resolve_lxapp_uri(&self, page: &PageInstance, uri: &Uri) -> Result<PathBuf, LxAppError> {
        let relative = self.resolve_lxapp_relative_path(page, uri)?;
        if let Ok(local_path) = self.resolve_accessible_path(&relative) {
            return Ok(local_path);
        }

        let absolute_path = format!("/{}", relative);
        if let Ok(local_path) = self.resolve_accessible_path(&absolute_path) {
            return Ok(local_path);
        }

        Err(LxAppError::ResourceNotFound(uri.to_string()))
    }

    fn resolve_lxapp_relative_path(
        &self,
        _page: &PageInstance,
        uri: &Uri,
    ) -> Result<String, LxAppError> {
        let decoded_path = lx_uri::decode_lx_path(uri.path());
        if Path::new(&decoded_path).is_absolute() {
            if let Ok(local_path) = self.resolve_accessible_path(&decoded_path) {
                let relative = local_path
                    .strip_prefix(&self.lxapp_dir)
                    .map(|path| path.to_string_lossy().replace('\\', "/"))
                    .unwrap_or_else(|_| decoded_path.trim_start_matches('/').to_string());
                return Ok(relative);
            }
        }
        let raw_path = decoded_path.trim_start_matches('/');
        // Support both:
        // - lx://lxapp/<appid>/<path> (explicit)
        // - lx://lxapp/<path>         (implicit appid = current page's appid)
        //
        // The implicit form enables common web patterns like <img src="/public/logo.png">,
        // which resolve (via URL rules) to lx://lxapp/public/logo.png (appid omitted).
        let (first, rest) = raw_path.split_once('/').unwrap_or(("", raw_path));
        let normalized = if first == self.appid.as_str() {
            rest.trim_matches('/')
        } else {
            raw_path.trim_matches('/')
        };
        if normalized.is_empty() {
            return Err(LxAppError::ResourceNotFound(uri.to_string()));
        }
        if lx_uri::has_invalid_segment(normalized) {
            return Err(LxAppError::ResourceNotFound(uri.to_string()));
        }
        let normalized = normalized.to_string();

        Ok(normalized)
    }

    fn resolve_plugin_uri(&self, _page: &PageInstance, uri: &Uri) -> Result<PathBuf, LxAppError> {
        let decoded_path = lx_uri::decode_lx_path(uri.path());
        let raw_path = decoded_path.trim_start_matches('/');
        let (plugin_name, rest) = raw_path
            .split_once('/')
            .ok_or_else(|| LxAppError::ResourceNotFound(uri.to_string()))?;

        let normalized = rest.trim_matches('/');
        if normalized.is_empty() {
            return Err(LxAppError::ResourceNotFound(uri.to_string()));
        }
        if lx_uri::has_invalid_segment(normalized) {
            return Err(LxAppError::ResourceNotFound(uri.to_string()));
        }

        let last_err = match plugin::resolve_plugin_resource_path(
            &self.runtime,
            &self.config.plugins,
            plugin_name,
            normalized,
        ) {
            Ok(local_path) => return Ok(local_path),
            Err(e) => e,
        };

        Err(last_err)
    }

    fn infer_mime_type(path: &str) -> &'static str {
        if path.ends_with(".js") {
            "application/javascript"
        } else if path.ends_with(".css") {
            "text/css"
        } else if path.ends_with(".png") {
            "image/png"
        } else if path.ends_with(".jpg") || path.ends_with(".jpeg") {
            "image/jpeg"
        } else if path.ends_with(".gif") {
            "image/gif"
        } else if path.ends_with(".svg") {
            "image/svg+xml"
        } else if path.ends_with(".webp") {
            "image/webp"
        } else if path.ends_with(".ico") {
            "image/x-icon"
        } else if path.ends_with(".json") {
            "application/json"
        } else if path.ends_with(".woff") {
            "font/woff"
        } else if path.ends_with(".woff2") {
            "font/woff2"
        } else if path.ends_with(".ttf") {
            "font/ttf"
        } else if path.ends_with(".mp3") {
            "audio/mpeg"
        } else if path.ends_with(".wav") {
            "audio/wav"
        } else if path.ends_with(".mp4") {
            "video/mp4"
        } else {
            "application/octet-stream"
        }
    }

    fn touch_user_cache_access_time(&self, path: &Path) {
        if path.starts_with(&self.user_cache_dir) && path.exists() {
            crate::cache::touch_access_time(path);
        }
    }

    fn sanitize_for_filename(value: &str) -> String {
        value
            .chars()
            .map(|c| match c {
                'a'..='z' | 'A'..='Z' | '0'..='9' => c,
                _ => '_',
            })
            .collect()
    }
    /// Create a simple centered error response
    pub(crate) fn create_error_response(
        &self,
        status: StatusCode,
        title: &str,
        message: &str,
    ) -> WebResourceResponse {
        let html_content = format!(
            r#"<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{}</title>
    <style>
        body {{ font-family: system-ui, sans-serif; margin: 0; padding: 20px; background: #f5f5f5; display: flex; justify-content: center; align-items: center; min-height: 100vh; }}
        .error {{ background: white; border-radius: 8px; padding: 40px; text-align: center; max-width: 500px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }}
        .code {{ font-size: 48px; font-weight: bold; color: #e74c3c; margin-bottom: 16px; }}
        .title {{ font-size: 24px; font-weight: 600; color: #2c3e50; margin-bottom: 16px; }}
        .message {{ font-size: 16px; color: #7f8c8d; line-height: 1.5; }}
    </style>
</head>
<body>
    <div class="error">
        <div class="code">{}</div>
        <div class="title">{}</div>
        <div class="message">{}</div>
    </div>
</body>
</html>"#,
            title,
            status.as_u16(),
            title,
            message
        );

        let mut target_dir = self.user_cache_dir.join("webview_errors");
        if let Err(e) = fs::create_dir_all(&target_dir) {
            error!("Failed to prepare error directory: {}", e).with_appid(self.appid.clone());
            target_dir = std::env::temp_dir().join("lingxia-webview-errors");
            let _ = fs::create_dir_all(&target_dir);
        }

        let file_name = format!(
            "{}_{}.html",
            status.as_u16(),
            Self::sanitize_for_filename(title)
        );
        let mut file_path = target_dir.join(file_name);

        if let Err(e) = fs::write(&file_path, html_content.as_bytes()) {
            error!(
                "Failed to write error response file ({}): {}",
                file_path.display(),
                e
            )
            .with_appid(self.appid.clone());
            let unique_suffix = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_millis())
                .unwrap_or(0);
            file_path = target_dir.join(format!(
                "{}_{}_{}.html",
                status.as_u16(),
                Self::sanitize_for_filename(title),
                unique_suffix
            ));
            let _ = fs::write(&file_path, html_content.as_bytes());
        }

        let file_len = fs::metadata(&file_path)
            .map(|meta| meta.len())
            .unwrap_or_else(|_| html_content.len() as u64);

        let mut builder = Response::builder()
            .status(status)
            .header("Content-Type", "text/html; charset=utf-8");

        if let Ok(value) = http::HeaderValue::from_str(&file_len.to_string()) {
            builder = builder.header("Content-Length", value);
        }

        let response = builder.body(()).unwrap_or_else(|_| {
            Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(())
                .expect("Failed to build fallback error response")
        });

        let (parts, _) = response.into_parts();
        (parts, file_path).into()
    }
}