github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_error.go (about) 1 package nodes 2 3 import ( 4 "github.com/TeaOSLab/EdgeCommon/pkg/configutils" 5 "github.com/iwind/TeaGo/lists" 6 "github.com/iwind/TeaGo/types" 7 "net/http" 8 "strings" 9 ) 10 11 const httpStatusPageTemplate = `<!DOCTYPE html> 12 <html lang="en"> 13 <head> 14 <title>${status} ${statusMessage}</title> 15 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 16 <style> 17 address { line-height: 1.8; } 18 </style> 19 </head> 20 <body> 21 22 <h1>${status} ${statusMessage}</h1> 23 <p>${message}</p> 24 25 <address>Connection: ${remoteAddr} (Client) -> ${serverAddr} (Server)</address> 26 <address>Request ID: ${requestId}.</address> 27 28 </body> 29 </html>` 30 31 func (this *HTTPRequest) write404() { 32 this.writeCode(http.StatusNotFound, "", "") 33 } 34 35 func (this *HTTPRequest) writeCode(statusCode int, enMessage string, zhMessage string) { 36 if this.doPage(statusCode) { 37 return 38 } 39 40 var pageContent = configutils.ParseVariables(httpStatusPageTemplate, func(varName string) (value string) { 41 switch varName { 42 case "status": 43 return types.String(statusCode) 44 case "statusMessage": 45 return http.StatusText(statusCode) 46 case "message": 47 var acceptLanguages = this.RawReq.Header.Get("Accept-Language") 48 if len(acceptLanguages) > 0 { 49 var index = strings.Index(acceptLanguages, ",") 50 if index > 0 { 51 var firstLanguage = acceptLanguages[:index] 52 if firstLanguage == "zh-CN" { 53 return zhMessage 54 } 55 } 56 } 57 return enMessage 58 } 59 return this.Format("${" + varName + "}") 60 }) 61 62 this.ProcessResponseHeaders(this.writer.Header(), statusCode) 63 this.writer.WriteHeader(statusCode) 64 65 _, _ = this.writer.Write([]byte(pageContent)) 66 } 67 68 func (this *HTTPRequest) write50x(err error, statusCode int, enMessage string, zhMessage string, canTryStale bool) { 69 if err != nil { 70 this.addError(err) 71 } 72 73 // 尝试从缓存中恢复 74 if canTryStale && 75 this.cacheCanTryStale && 76 this.web.Cache.Stale != nil && 77 this.web.Cache.Stale.IsOn && 78 (len(this.web.Cache.Stale.Status) == 0 || lists.ContainsInt(this.web.Cache.Stale.Status, statusCode)) { 79 var ok = this.doCacheRead(true) 80 if ok { 81 return 82 } 83 } 84 85 // 显示自定义页面 86 if this.doPage(statusCode) { 87 return 88 } 89 90 // 内置HTML模板 91 var pageContent = configutils.ParseVariables(httpStatusPageTemplate, func(varName string) (value string) { 92 switch varName { 93 case "status": 94 return types.String(statusCode) 95 case "statusMessage": 96 return http.StatusText(statusCode) 97 case "requestId": 98 return this.requestId 99 case "message": 100 var acceptLanguages = this.RawReq.Header.Get("Accept-Language") 101 if len(acceptLanguages) > 0 { 102 var index = strings.Index(acceptLanguages, ",") 103 if index > 0 { 104 var firstLanguage = acceptLanguages[:index] 105 if firstLanguage == "zh-CN" { 106 return "网站出了一点小问题,原因:" + zhMessage + "。" 107 } 108 } 109 } 110 return "The site is unavailable now, cause: " + enMessage + "." 111 } 112 return this.Format("${" + varName + "}") 113 }) 114 115 this.ProcessResponseHeaders(this.writer.Header(), statusCode) 116 this.writer.WriteHeader(statusCode) 117 118 _, _ = this.writer.Write([]byte(pageContent)) 119 }