Product knowledge · Advanced · Filesystem boundary

Một path trong URL không phải quyền đọc filesystem.

Dùng static mount cho public assets, SPA fallback cho navigation và confined file response cho download đã được application authorize. Ba surface này chia sẻ delivery engine nhưng không chia sẻ trust policy.

Trang này hướng dẫn tải xuống: server → client. sendfile là một cách gửi tệp đã có trong response. Muốn nhận tệp client → server, dùng các bài tải lên. Xem cách tải tiếp tệp lớn.

01 · Three file surfaces

Chọn theo authority, không theo extension

SurfaceAi chọn fileDùng cho
Static mountDeployment/app declaration grant một public root.JS, CSS, images, fonts và public built assets.
SPA fallbackRoute policy trả index cho navigation path chưa match.Client-side routes; không thay API 404.
File responseApplication handler authorize rồi chọn relative file trong authority.Private report, invoice hoặc user-owned download.

Core sở hữu resolution, validators, ranges, pressure và delivery. Application sở hữu authorization/business mapping. Deployment sở hữu asset bytes, publication và replacement.

02 · Route precedence

Explicit application route thắng static fallback

GET /api/orders/7       → explicit API route
GET /assets/app.js      → static mount
GET /app/customer/7     → SPA index fallback
GET /api/missing        → API 404/refusal, not SPA HTML

Giữ API namespace rõ. Fallback chỉ dành cho navigation đã chọn; không nuốt typo, authorization failure hoặc missing API rồi trả HTML 200.

03 · Public static root

Mount chỉ chứa thứ thực sự public

04 · SPA fallback

Navigation fallback không phải business router

Accept

Navigation path thuộc frontend scope và cần index để client router tiếp quản.

Reject

API path, file request, method không phù hợp hoặc request không chấp nhận HTML.

Cache

Index và hashed assets có lifecycle/cache policy khác nhau; deployment chọn versioning.

Security

Fallback không bypass handler authorization hoặc biến private file thành public asset.

05 · Authorized file response

Authorize business object trước, chọn file sau

1

Parse business identity

Ví dụ invoice ID; không coi request path là OS path.

2

Authenticate + authorize

Application quyết định principal có quyền đọc object.

3

Resolve trong authority

Map object sang relative path/application file reference đã confined.

4

Return file intent

Core validate, account và deliver; handler không đọc whole file thành byte array nếu không cần.

Không làm: nối ?path=/etc/passwd, filename hoặc decoded capture trực tiếp vào filesystem root.

06 · Validators and ranges

Client cache và partial read có terminal riêng

200
Full representation với validator/length facts theo contract.
206
Satisfiable byte range; response có Content-Range tương ứng.
304
Validator match; không có representation body mới.
416
Range không thỏa mãn; không silently trả full file.

Dùng runtime validator/range contract; không reimplement header parser trong handler. Test full, partial, cached, missing, invalid path và interrupted download.

07 · Delivery mechanism

File response là contract; sendfile chỉ là eligible path

Plaintext HTTP/1.1 đủ điều kiện có thể dùng native sendfile path. TLS, HTTP/2, HTTP/3 hoặc platform khác dùng bounded delivery path phù hợp. Core chọn mechanism/fallback; connector không dựng socket/file loop riêng.

Không hứa zero-copy

Cache, storage, protocol, kernel và client speed vẫn quyết định copies/CPU.

Không tắt TLS

Security boundary quan trọng hơn một fast path; mechanism không phải application knob.

Không suy performance

Successful download không chứng minh syscall/backend; cần matching-host evidence.

Không nhầm upload

Sendfile không parse multipart, chọn destination hoặc persist incoming bytes.

Range tiếp tục tải xuống một response; next_offset tiếp tục tải lên một tệp đang gửi. Xem cách tải tiếp lên server.

08 · Failure law

Refuse theo boundary, không lộ filesystem detail

FailureOwner action
Outside authority / invalid pathFail closed; không normalize thành file khác.
Not authorizedApplication trả public-safe refusal; không tiết lộ existence/path.
Missing/changed fileMap theo application/API contract; giữ operator detail riêng.
Pressure/timeout/disconnectRelease transfer state; không retry/download vô hạn trong connector.
Close refusalGiữ owner và filesystem dependencies tới khi work converge an toàn.

09 · Integration checklist

Trước khi xuống API ngôn ngữ