Add error.html
This commit is contained in:
@@ -76,14 +76,9 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
case "/socket":
|
case "/socket":
|
||||||
err = serveController(h.socketCtl, w, r, clientLogger)
|
err = serveController(h.socketCtl, w, r, clientLogger)
|
||||||
|
|
||||||
case "/index.html":
|
|
||||||
fallthrough
|
|
||||||
case "/error.html":
|
|
||||||
err = ErrNotFound
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if len(r.URL.Path) > 0 {
|
if len(r.URL.Path) > 0 && strings.ToUpper(r.Method) == "GET" {
|
||||||
err = serveStaticData(
|
err = serveStaticCacheData(
|
||||||
r.URL.Path[1:],
|
r.URL.Path[1:],
|
||||||
staticFileExt(r.URL.Path[1:]),
|
staticFileExt(r.URL.Path[1:]),
|
||||||
w,
|
w,
|
||||||
|
|||||||
@@ -29,7 +29,5 @@ func serveFailure(
|
|||||||
r *http.Request,
|
r *http.Request,
|
||||||
l log.Logger,
|
l log.Logger,
|
||||||
) error {
|
) error {
|
||||||
w.WriteHeader(err.Code())
|
return serveStaticPage("error.html", ".html", err.Code(), w, r, l)
|
||||||
|
|
||||||
return serveStaticPage("error.html", ".html", w, r, l)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,5 +29,5 @@ type home struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h home) Get(w http.ResponseWriter, r *http.Request, l log.Logger) error {
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ func staticFileExt(fileName string) string {
|
|||||||
return strings.ToLower(fileName[extIdx:])
|
return strings.ToLower(fileName[extIdx:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveStaticData(
|
func serveStaticCacheData(
|
||||||
dataName string,
|
dataName string,
|
||||||
fileExt string,
|
fileExt string,
|
||||||
w http.ResponseWriter,
|
w http.ResponseWriter,
|
||||||
@@ -59,20 +60,16 @@ func serveStaticData(
|
|||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return serveStaticPage(dataName, fileExt, w, r, l)
|
return serveStaticCachePage(dataName, fileExt, w, r, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveStaticPage(
|
func serveStaticCachePage(
|
||||||
dataName string,
|
dataName string,
|
||||||
fileExt string,
|
fileExt string,
|
||||||
w http.ResponseWriter,
|
w http.ResponseWriter,
|
||||||
r *http.Request,
|
r *http.Request,
|
||||||
l log.Logger,
|
l log.Logger,
|
||||||
) error {
|
) error {
|
||||||
if strings.ToUpper(r.Method) != "GET" {
|
|
||||||
return ErrControllerNotImplemented
|
|
||||||
}
|
|
||||||
|
|
||||||
d, dFound := staticPages[dataName]
|
d, dFound := staticPages[dataName]
|
||||||
|
|
||||||
if !dFound {
|
if !dFound {
|
||||||
@@ -81,11 +78,13 @@ func serveStaticPage(
|
|||||||
|
|
||||||
selectedData := d.data
|
selectedData := d.data
|
||||||
selectedDataHash := d.dataHash
|
selectedDataHash := d.dataHash
|
||||||
|
selectedLength := len(d.data)
|
||||||
compressEnabled := false
|
compressEnabled := false
|
||||||
|
|
||||||
if clientSupportGZIP(r) && d.hasCompressed() {
|
if clientSupportGZIP(r) && d.hasCompressed() {
|
||||||
selectedData = d.compressd
|
selectedData = d.compressd
|
||||||
selectedDataHash = d.compressdHash
|
selectedDataHash = d.compressdHash
|
||||||
|
selectedLength = len(d.compressd)
|
||||||
|
|
||||||
compressEnabled = true
|
compressEnabled = true
|
||||||
|
|
||||||
@@ -123,7 +122,59 @@ func serveStaticPage(
|
|||||||
w.Header().Add("Content-Encoding", "gzip")
|
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
|
return wErr
|
||||||
}
|
}
|
||||||
|
|||||||
39
ui/error.html
Normal file
39
ui/error.html
Normal 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">
|
||||||
|
×
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h1 id="app-loading-title" class="error">
|
||||||
|
Server was unable to complete the request
|
||||||
|
</h1>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -323,6 +323,27 @@ module.exports = {
|
|||||||
removeEmptyElements: false
|
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({
|
new ImageminPlugin({
|
||||||
disable: process.env.NODE_ENV !== "production",
|
disable: process.env.NODE_ENV !== "production",
|
||||||
pngquant: {
|
pngquant: {
|
||||||
|
|||||||
Reference in New Issue
Block a user