Files
EdgeNode/internal/nodes/http_request_error.go

54 lines
1.4 KiB
Go
Raw Normal View History

2020-09-26 08:07:07 +08:00
package nodes
import (
2021-12-16 17:27:21 +08:00
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types"
2020-09-26 08:07:07 +08:00
"net/http"
)
2020-09-26 11:22:21 +08:00
func (this *HTTPRequest) write404() {
2020-09-26 08:07:07 +08:00
if this.doPage(http.StatusNotFound) {
return
}
this.processResponseHeaders(http.StatusNotFound)
this.writer.WriteHeader(http.StatusNotFound)
_, _ = this.writer.Write([]byte("404 page not found: '" + this.URL() + "'" + " (Request Id: " + this.requestId + ")"))
2020-09-26 08:07:07 +08:00
}
2021-12-12 11:48:01 +08:00
func (this *HTTPRequest) writeCode(code int) {
if this.doPage(code) {
return
}
this.processResponseHeaders(code)
this.writer.WriteHeader(code)
_, _ = this.writer.Write([]byte(types.String(code) + " " + http.StatusText(code) + ": '" + this.URL() + "'" + " (Request Id: " + this.requestId + ")"))
2021-12-12 11:48:01 +08:00
}
2021-12-16 17:27:21 +08:00
func (this *HTTPRequest) write50x(err error, statusCode int, canTryStale bool) {
2020-09-27 15:26:06 +08:00
if err != nil {
this.addError(err)
}
2021-12-16 17:27:21 +08:00
// 尝试从缓存中恢复
if canTryStale &&
this.cacheCanTryStale &&
this.web.Cache.Stale != nil &&
this.web.Cache.Stale.IsOn &&
(len(this.web.Cache.Stale.Status) == 0 || lists.ContainsInt(this.web.Cache.Stale.Status, statusCode)) {
ok := this.doCacheRead(true)
if ok {
return
}
}
// 显示自定义页面
2020-09-26 08:07:07 +08:00
if this.doPage(statusCode) {
return
}
this.processResponseHeaders(statusCode)
this.writer.WriteHeader(statusCode)
_, _ = this.writer.Write([]byte(types.String(statusCode) + " " + http.StatusText(statusCode) + " (Request Id: " + this.requestId + ")"))
}