Changeset 11 in code for trunk/morty.go


Ignore:
Timestamp:
Oct 25, 2016, 9:01:01 PM (9 years ago)
Author:
asciimoo
Message:

[enh] enhanced main page

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r10 r11  
    134134
    135135        if requestURI == nil {
    136                 p.breakOnError(ctx, errors.New(`missing "mortyurl" URL parameter`))
     136                p.serveMainPage(ctx, nil)
    137137                return
    138138        }
     
    140140        if p.Key != nil {
    141141                if !verifyRequestURI(requestURI, requestHash, p.Key) {
    142                         p.breakOnError(ctx, errors.New("invalid hash"))
     142                        p.serveMainPage(ctx, errors.New(`invalid "mortyhash" parameter`))
    143143                        return
    144144                }
     
    147147        parsedURI, err := url.Parse(string(requestURI))
    148148
    149         if p.breakOnError(ctx, err) {
     149        if err != nil {
     150                p.serveMainPage(ctx, err)
    150151                return
    151152        }
     
    176177        }
    177178
    178         if p.breakOnError(ctx, CLIENT.DoTimeout(req, resp, p.RequestTimeout)) {
     179        err = CLIENT.DoTimeout(req, resp, p.RequestTimeout)
     180
     181        if err != nil {
     182                p.serveMainPage(ctx, err)
    179183                return
    180184        }
     
    201205
    202206        if contentType == nil {
    203                 p.breakOnError(ctx, errors.New("invalid content type"))
     207                p.serveMainPage(ctx, errors.New("invalid content type"))
    204208                return
    205209        }
     
    212216                var err error
    213217                responseBody, err = charmap.ISO8859_2.NewDecoder().Bytes(resp.Body())
    214                 if p.breakOnError(ctx, err) {
     218                if err != nil {
     219                        p.serveMainPage(ctx, err)
    215220                        return
    216221                }
     
    232237
    233238func appRequestHandler(ctx *fasthttp.RequestCtx) bool {
     239        // serve robots.txt
    234240        if bytes.Equal(ctx.Path(), []byte("/robots.txt")) {
    235241                ctx.SetContentType("text/plain")
     
    237243                return true
    238244        }
     245
    239246        return false
    240247}
     
    527534}
    528535
    529 func (p *Proxy) breakOnError(ctx *fasthttp.RequestCtx, err error) bool {
    530         if err == nil {
    531                 return false
    532         }
    533         log.Println("error:", err)
    534         ctx.SetStatusCode(404)
     536func (p *Proxy) serveMainPage(ctx *fasthttp.RequestCtx, err error) {
    535537        ctx.SetContentType("text/html")
    536538        ctx.Write([]byte(`<!doctype html>
    537539<head>
    538 <title>MortyError</title>
     540<title>MortyProxy</title>
     541<style>
     542body { font-family: 'Garamond', 'Georgia', serif; text-align: center; color: #444; background: #FAFAFA; margin: 0; padding: 0; font-size: 1.1em; }
     543input { border: 1px solid #888; padding: 0.3em; color: #444; background: #FFF; font-size: 1.1em; }
     544a { text-decoration: none; #2980b9; }
     545h1, h2 { font-weight: 200; margin-bottom: 2rem; }
     546h1 { font-size: 3em; }
     547.footer { position: absolute; bottom: 2em; width: 100%; }
     548.footer p { font-size: 0.8em; }
     549
     550</style>
    539551</head>
    540 <body><h2>Error!</h2>`))
    541         ctx.Write([]byte("<h3>"))
    542         ctx.Write([]byte(html.EscapeString(err.Error())))
    543         ctx.Write([]byte("</h3>"))
     552<body>
     553        <h1>MortyProxy</h1>`))
     554        if err != nil {
     555                ctx.SetStatusCode(404)
     556                log.Println("error:", err)
     557                ctx.Write([]byte("<h2>Error: "))
     558                ctx.Write([]byte(html.EscapeString(err.Error())))
     559                ctx.Write([]byte("</h2>"))
     560        } else {
     561                ctx.SetStatusCode(200)
     562        }
    544563        if p.Key == nil {
    545564                ctx.Write([]byte(`
     
    548567        <input type="submit" value="go" />
    549568</form>`))
     569        } else {
     570                ctx.Write([]byte(`<h3>Warning! This instance does not support direct URL opening.</h3>`))
    550571        }
    551572        ctx.Write([]byte(`
     573<div class="footer">
     574        <p>Morty rewrites web pages to exclude malicious HTML tags and CSS/HTML attributes. It also replaces external resource references to prevent third-party information leaks.<br />
     575        <a href="https://github.com/asciimoo/morty">view on github</a>
     576        </p>
     577</div>
    552578</body>
    553579</html>`))
    554         return true
    555580}
    556581
Note: See TracChangeset for help on using the changeset viewer.