Add error.html

This commit is contained in:
NI
2019-08-27 17:24:37 +08:00
parent d688e9ec42
commit 8fe5d677ec
6 changed files with 123 additions and 19 deletions

View File

@@ -76,14 +76,9 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "/socket":
err = serveController(h.socketCtl, w, r, clientLogger)
case "/index.html":
fallthrough
case "/error.html":
err = ErrNotFound
default:
if len(r.URL.Path) > 0 {
err = serveStaticData(
if len(r.URL.Path) > 0 && strings.ToUpper(r.Method) == "GET" {
err = serveStaticCacheData(
r.URL.Path[1:],
staticFileExt(r.URL.Path[1:]),
w,

View File

@@ -29,7 +29,5 @@ func serveFailure(
r *http.Request,
l log.Logger,
) error {
w.WriteHeader(err.Code())
return serveStaticPage("error.html", ".html", w, r, l)
return serveStaticPage("error.html", ".html", err.Code(), w, r, l)
}

View File

@@ -29,5 +29,5 @@ type home struct {
}
func (h home) Get(w http.ResponseWriter, r *http.Request, l log.Logger) error {
return serveStaticPage("index.html", ".html", w, r, l)
return serveStaticCachePage("index.html", ".html", w, r, l)
}

View File

@@ -20,6 +20,7 @@ package controller
import (
"mime"
"net/http"
"strconv"
"strings"
"time"
@@ -48,7 +49,7 @@ func staticFileExt(fileName string) string {
return strings.ToLower(fileName[extIdx:])
}
func serveStaticData(
func serveStaticCacheData(
dataName string,
fileExt string,
w http.ResponseWriter,
@@ -59,20 +60,16 @@ func serveStaticData(
return ErrNotFound
}
return serveStaticPage(dataName, fileExt, w, r, l)
return serveStaticCachePage(dataName, fileExt, w, r, l)
}
func serveStaticPage(
func serveStaticCachePage(
dataName string,
fileExt string,
w http.ResponseWriter,
r *http.Request,
l log.Logger,
) error {
if strings.ToUpper(r.Method) != "GET" {
return ErrControllerNotImplemented
}
d, dFound := staticPages[dataName]
if !dFound {
@@ -81,11 +78,13 @@ func serveStaticPage(
selectedData := d.data
selectedDataHash := d.dataHash
selectedLength := len(d.data)
compressEnabled := false
if clientSupportGZIP(r) && d.hasCompressed() {
selectedData = d.compressd
selectedDataHash = d.compressdHash
selectedLength = len(d.compressd)
compressEnabled = true
@@ -123,7 +122,59 @@ func serveStaticPage(
w.Header().Add("Content-Encoding", "gzip")
}
_, wErr := w.Write([]byte(selectedData))
w.Header().Add("Content-Length",
strconv.FormatInt(int64(selectedLength), 10))
_, wErr := w.Write(selectedData)
return wErr
}
func serveStaticPage(
dataName string,
fileExt string,
code int,
w http.ResponseWriter,
r *http.Request,
l log.Logger,
) error {
d, dFound := staticPages[dataName]
if !dFound {
return ErrNotFound
}
selectedData := d.data
selectedLength := len(d.data)
compressEnabled := false
if clientSupportGZIP(r) && d.hasCompressed() {
selectedData = d.compressd
selectedLength = len(d.compressd)
compressEnabled = true
w.Header().Add("Vary", "Accept-Encoding")
}
mimeType := mime.TypeByExtension(fileExt)
if len(mimeType) > 0 {
w.Header().Add("Content-Type", mimeType)
} else {
w.Header().Add("Content-Type", "application/binary")
}
if compressEnabled {
w.Header().Add("Content-Encoding", "gzip")
}
w.Header().Add("Content-Length",
strconv.FormatInt(int64(selectedLength), 10))
w.WriteHeader(code)
_, wErr := w.Write(selectedData)
return wErr
}

39
ui/error.html Normal file
View File

@@ -0,0 +1,39 @@
<!--
// Sshwifty - A Web SSH client
//
// Copyright (C) 2019 Rui NI <nirui@gmx.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Error</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body class="app-error">
<div id="app-loading">
<div id="app-loading-frame">
<div id="app-loading-error">
&times;
</div>
<h1 id="app-loading-title" class="error">
Server was unable to complete the request
</h1>
</div>
</div>
</body>
</html>

View File

@@ -323,6 +323,27 @@ module.exports = {
removeEmptyElements: false
}
}),
new HtmlWebpackPlugin({
filename: "error.html",
inject: true,
template: path.join(__dirname, "ui", "error.html"),
meta: [
{
name: "description",
content: "Connect to a SSH Server from your web browser"
}
],
mobile: true,
lang: "en-US",
minify: {
html5: true,
collapseWhitespace:
process.env.NODE_ENV === "development" ? false : true,
caseSensitive: true,
removeComments: true,
removeEmptyElements: false
}
}),
new ImageminPlugin({
disable: process.env.NODE_ENV !== "production",
pngquant: {