支持headers注入

master
宇天 2022-04-25 23:37:27 +08:00
parent 7bd86c3a89
commit 325239bddc
3 changed files with 33 additions and 13 deletions

View File

@ -19,8 +19,12 @@
```json ```json
{ {
"port": 23333, // [可选] 默认使用 23333 端口, 如果被使用了, 会向上查找可用端口 "enabled": true, // 这里配置为 true 才会启动web服务
"root": ".", // [可选] web服务的根目录, 默认为当前项目的根目录, 可填写相对项目根目录的路径。 "port": 23333, // 【可选】 默认使用 23333 端口, 如果被使用了, 会向上查找可用端口
"enabled": true // 这里配置为 true 才会启动web服务 "root": ".", // 【可选】 web服务的根目录, 默认为当前项目的根目录, 可填写相对项目根目录的路径。
"headers": { // 【可选】支持注入一些头信息, 方便特殊需要的调试
"cache-control": "no-store" // 默认为 no-store
...
}
} }
``` ```

View File

@ -39,6 +39,13 @@ const MIME_TYPES = {
other: 'application/octet-stream' other: 'application/octet-stream'
} }
const COMMON_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Cache-Control': 'no-store',
'X-Powered-By': 'VS Code simple.http'
}
MIME_TYPES.htm = MIME_TYPES.html MIME_TYPES.htm = MIME_TYPES.html
MIME_TYPES.jpeg = MIME_TYPES.jpg MIME_TYPES.jpeg = MIME_TYPES.jpg
MIME_TYPES.tif = MIME_TYPES.tiff MIME_TYPES.tif = MIME_TYPES.tiff
@ -71,20 +78,19 @@ function createServer() {
let stat = fs.stat(file) let stat = fs.stat(file)
let ext = pathname.split('.').pop() let ext = pathname.split('.').pop()
res.setHeader('Access-Control-Allow-Origin', '*') for (let k in COMMON_HEADERS) {
res.setHeader('Access-Control-Allow-Headers', '*') res.setHeader(k, COMMON_HEADERS[k])
res.setHeader('Cache-Control', 'no-store') }
res.setHeader('X-Powered-By', 'VS Code simple.http')
if (stat.isFile()) { if (stat.isFile()) {
res.setHeader('Accept-Ranges', 'bytes') res.setHeader('accept-ranges', 'bytes')
res.setHeader('Content-Type', MIME_TYPES[ext] || MIME_TYPES.other) res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.other)
res.setHeader('Content-Length', stat.size) res.setHeader('content-length', stat.size)
res.writeHead(200, 'OK') res.writeHead(200, 'OK')
fs.origin.createReadStream(file).pipe(res) fs.origin.createReadStream(file).pipe(res)
} else { } else {
res.setHeader('Content-Type', MIME_TYPES.html) res.setHeader('content-type', MIME_TYPES.html)
res.setHeader('Content-Length', 0) res.setHeader('content-length', 0)
res.writeHead(404, 'Not Found') res.writeHead(404, 'Not Found')
res.end('') res.end('')
} }
@ -117,6 +123,16 @@ function __init__() {
root = join(root, conf.root) root = join(root, conf.root)
} }
if (conf.headers) {
for (let k in conf.headers) {
let _k = k.toLowerCase()
if (['accept-ranges', 'content-type', 'content-length'].contains(_k)) {
continue
}
COMMON_HEADERS[_k] = conf.headers[k]
}
}
if (conf.enabled) { if (conf.enabled) {
enabled = true enabled = true
port = conf.port || 23333 port = conf.port || 23333

View File

@ -2,7 +2,7 @@
"name": "simple-http", "name": "simple-http",
"displayName": "simple http", "displayName": "simple http",
"description": "🔥 简单的http服务器, 方便临时调试html", "description": "🔥 简单的http服务器, 方便临时调试html",
"version": "1.1.0", "version": "1.2.0",
"publisher": "yutent", "publisher": "yutent",
"author": "Yutent [@yutent]", "author": "Yutent [@yutent]",
"icon": "logo.png", "icon": "logo.png",