Skip to content

Commit

Permalink
Add error.html
Browse files Browse the repository at this point in the history
  • Loading branch information
NI committed Aug 27, 2019
1 parent d688e9e commit 8fe5d67
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 19 deletions.
9 changes: 2 additions & 7 deletions application/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions application/controller/failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion application/controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
67 changes: 59 additions & 8 deletions application/controller/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package controller
import (
"mime"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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 changes: 39 additions & 0 deletions ui/error.html
Original file line number Diff line number Diff line change
@@ -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>
21 changes: 21 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 8fe5d67

Please sign in to comment.