HTTP Range Request: Tải Từng Phần Tệp Với Mã HTTP 206 Partial Content
Cơ chế hoạt động của HTTP Range Request giúp tua video, tiếp tục tải file dở dang và hỗ trợ các trình tăng tốc download đa luồng.
HTTP Range Requests (RFC 7233) cho phép client tải xuống các phạm vi byte cụ thể của tài nguyên. Đây là công nghệ nền tảng hỗ trợ thanh seek video, trình quản lý tải xuống đa luồng (IDM, aria2) và tải xuống có thể tiếp tục.
Vòng Đời HTTP 206 Partial Content
- Client thăm dò: Client thực hiện request
HEAD. Máy chủ trả vềAccept-Ranges: bytes. - Range request: Client gửi
Range: bytes=0-1048575(yêu cầu 1MB đầu tiên). - Partial response: Máy chủ trả về HTTP 206 Partial Content với:
Content-Range: bytes 0-1048575/10485760Content-Length: 1048576
Kiểm thử HTTP byte range request bằng curl với file PDF mẫu 10MB của chúng tôi.
Tải File Mẫu 10MB PDF →Kiểm Thử Byte Range Với cURL
# Yêu cầu chỉ 500 byte đầu tiên (để kiểm tra magic header)
curl -r 0-499 -i "https://filedummy.dev/api/download?type=pdf&size=10mb"
Triển Khai Range Request Trong Node.js
import fs from 'fs';
import { IncomingMessage, ServerResponse } from 'http';
export function handleRangeRequest(req: IncomingMessage, res: ServerResponse, filePath: string) {
const stat = fs.statSync(filePath);
const total = stat.size;
const range = req.headers.range;
if (!range) {
res.writeHead(200, {
'Content-Length': total,
'Content-Type': 'application/pdf',
'Accept-Ranges': 'bytes',
});
fs.createReadStream(filePath).pipe(res);
return;
}
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : total - 1;
const chunksize = end - start + 1;
res.writeHead(206, {
'Content-Range': `bytes ${start}-${end}/${total}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'application/pdf',
});
const stream = fs.createReadStream(filePath, { start, end });
stream.pipe(res);
}
Nguyễn Đại Long
Tác GiảBackend Lead • Chuyên gia Kiến trúc Hệ thống Phân tán & Lưu trữ Đám mây
Hơn 4 năm kinh nghiệm thiết kế các hệ thống xử lý tệp tải lên thông lượng lớn, tối ưu hóa cơ sở dữ liệu và hạ tầng phân tán Cloudflare R2 / AWS S3. Người sáng lập FileDummy và Mạng lưới Hệ sinh thái NDL.
Bài viết này có hữu ích không?
Bấm Thích để ủng hộ tác giả và giúp bài viết lan tỏa tới cộng đồng lập trình viên.
Thảo Luận Kỹ Thuật & Đóng Góp Ý Kiến (0)
Chia sẻ kết quả benchmark, phản hồi các trường hợp biên hoặc đặt câu hỏi chuyên môn.
Chưa có bình luận nào. Hãy là lập trình viên đầu tiên bắt đầu cuộc thảo luận!
Câu Hỏi Thường Gặp (FAQ)
What is the HTTP Range request header?▾
The Range header asks the server to return only a specific byte range. Format: Range: bytes=start-end (e.g., bytes=0-1023 for the first 1024 bytes). The server responds with 206 Partial Content and the Content-Range header indicating the returned bytes.
How does video streaming use HTTP Range requests?▾
HTML5 video elements use range requests to seek without downloading the entire video. When you drag the seek bar, the browser sends a Range request for the bytes at that timestamp — this is why you can seek before a video is fully buffered.
How do I check if a server supports range requests?▾
Look for Accept-Ranges: bytes in the HEAD response. Run curl -I https://example.com/file.pdf and check for the header. All major cloud storage services (S3, R2, CloudFront) support ranges natively.