提交 5a56637c 编写于 作者: yanghye's avatar yanghye

修改内置资源XHR代理

上级 2776eeb3
......@@ -41,14 +41,13 @@ type LocalLoadResource struct {
// 本地&内置资源加载配置
// 然后使用 Build() 函数构建对象
type LocalLoadConfig struct {
Enable bool // 设置是否启用本地资源缓存到内存, 默认true: 启用, 禁用时需要调用Disable
EnableCache bool // 启用缓存,将加载过的资源存储到内存中
Domain string // 必须设置的域
Scheme LocalCustomerScheme // 自定义协议, file: 本地磁盘目录加载, fs: 内置到执行程序加载
ResRootDir string // 资源根目录, scheme是file时为本地目录(默认当前程序执行目录) scheme是fs时为resources, 默认:[resources or /current/path]
FS *embed.FS // 内置加载资源对象, scheme是fs时必须填入
Proxy IXHRProxy // 数据请求代理, 在浏览器发送xhr请求时可通过该配置转发, 你可自定义实现该 IXHRProxy 接口
Home string // 默认首页: /index.html, /home.html, /other.html, default: /index.html
Enable bool // 设置是否启用本地资源缓存到内存, 默认true: 启用, 禁用时需要调用Disable函数
Domain string // 必须设置的域
Scheme LocalCustomerScheme // 自定义协议, local: 本地磁盘目录加载, fs: 内置到执行程序加载, 默认 fs
ResRootDir string // 资源根目录, scheme是file时为本地目录(默认当前程序执行目录) scheme是fs时为resources, 默认:[resources or /current/path]
FS *embed.FS // 内置加载资源对象, scheme是fs时必须填入
Proxy IXHRProxy // 数据请求代理, 在浏览器发送xhr请求时可通过该配置转发, 你可自定义实现该 IXHRProxy 接口
Home string // 默认首页: /index.html, /home.html, /other.html, default: /index.html
}
// 请求和响应资源
......@@ -90,8 +89,8 @@ func (m LocalLoadConfig) Build() *LocalLoadConfig {
config.Domain = localDomain
}
// scheme 必须是 file 或 fs
if config.Scheme != LocalCSFile && config.Scheme != LocalCSFS {
config.Scheme = LocalCSFS
if config.Scheme != LcsLocal && config.Scheme != LcsFS {
config.Scheme = LcsFS
}
// 默认使用 /index.html
if config.Home == "" {
......@@ -101,9 +100,9 @@ func (m LocalLoadConfig) Build() *LocalLoadConfig {
}
// 默认的资源目录
if config.ResRootDir == "" {
if config.Scheme == LocalCSFS {
if config.Scheme == LcsFS {
config.ResRootDir = "resources"
} else if config.Scheme == LocalCSFile {
} else if config.Scheme == LcsLocal {
wd, _ := os.Getwd()
config.ResRootDir = wd
}
......@@ -167,11 +166,11 @@ func (m *LocalLoadResource) checkRequest(request *ICefRequest) (*source, bool) {
reqUrl, err := url.Parse(request.URL())
logger.Debug("LocalLoadResource URL:", reqUrl.String(), "RT:", rt)
if err != nil {
logger.Error("LocalLoadResource, scheme invalid should:", LocalCSFS, "or", LocalCSFile)
logger.Error("LocalLoadResource, scheme invalid should:", LcsFS, "or", LcsLocal)
return nil, false
}
if reqUrl.Scheme != string(m.Scheme) {
logger.Error("LocalLoadResource, scheme invalid should:", LocalCSFS, "or", LocalCSFile, "current:", reqUrl.Scheme)
logger.Error("LocalLoadResource, scheme invalid should:", LcsFS, "or", LcsLocal, "current:", reqUrl.Scheme)
return nil, false
}
if reqUrl.Host != m.Domain {
......@@ -183,20 +182,6 @@ func (m *LocalLoadResource) checkRequest(request *ICefRequest) (*source, bool) {
path = m.Home
}
ext := m.ext(path)
/*if ext == "" && rt != RT_XHR {
logger.Error("LocalLoadResource Incorrect resources should: file.[ext](MimeType)")
return nil, false
}*/
// 如果开启缓存,直接在缓存拿指定地址的source
if m.EnableCache {
if s, ok := m.sourceCache[path]; ok {
return s, true
} else {
s = &source{path: path, fileExt: ext, mimeType: m.getMimeType(ext), resourceType: rt}
m.sourceCache[path] = s
return s, true
}
}
return &source{path: path, fileExt: ext, mimeType: m.getMimeType(ext), resourceType: rt}, true
}
......@@ -204,13 +189,13 @@ func (m *LocalLoadResource) checkRequest(request *ICefRequest) (*source, bool) {
func (m *source) readFile() {
// 必须设置文件根目录, scheme是file时, fileRoot为本地文件目录, scheme是fs时, fileRoot为fs的目录名
if localLoadRes.ResRootDir != "" {
if localLoadRes.Scheme == LocalCSFile {
if localLoadRes.Scheme == LcsLocal {
m.bytes, m.err = ioutil.ReadFile(filepath.Join(localLoadRes.ResRootDir, m.path))
// 在本地读取
if m.err != nil {
logger.Error("ReadFile:", m.err.Error())
}
} else if localLoadRes.Scheme == LocalCSFS && localLoadRes.FS != nil {
} else if localLoadRes.Scheme == LcsFS && localLoadRes.FS != nil {
//在fs读取
m.bytes, m.err = localLoadRes.FS.ReadFile(localLoadRes.ResRootDir + m.path)
if m.err != nil {
......@@ -237,13 +222,7 @@ func (m *source) open(request *ICefRequest, callback *ICefCallback) (handleReque
}
} else {
// 如果开启缓存,直接在缓存取
if localLoadRes.EnableCache {
if m.bytes == nil {
m.readFile()
}
} else {
m.readFile()
}
m.readFile()
if m.err == nil {
m.status = 200
m.statusText = "OK"
......@@ -300,7 +279,7 @@ func (m *source) read(dataOut uintptr, bytesToRead int32, callback *ICefResource
i++
}
// 读取到最后不缓存时,清空
if i == 0 && !localLoadRes.EnableCache {
if i == 0 {
m.bytes = nil
}
callback.Cont(int64(i))
......
......@@ -1662,8 +1662,8 @@ const (
type LocalCustomerScheme string
const (
LocalCSFile LocalCustomerScheme = "file" // 本地目录 file://energy/index.html
LocalCSFS LocalCustomerScheme = "fs" // 内置 fs://energy/index.html
LcsLocal LocalCustomerScheme = "local" // 本地目录 local://energy/index.html
LcsFS LocalCustomerScheme = "fs" // 内置 fs://energy/index.html
)
// LocalProxyScheme
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册