From 5d5b7b413f87df6c57f8073ec9d6793e4b402589 Mon Sep 17 00:00:00 2001 From: asciimoo Date: Tue, 14 Jul 2020 21:22:08 +0000 Subject: [PATCH] [enh] use html template for injected html snippets git-svn-id: file:///srv/svn/repo/yukari/trunk@121 f3bd38d9-da89-464d-a02a-eb04e43141b5 --- morty.go | 91 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 31 deletions(-) diff --git a/morty.go b/morty.go index 6def9a7..9983ddb 100644 --- a/morty.go +++ b/morty.go @@ -9,6 +9,7 @@ import ( "errors" "flag" "fmt" + "html/template" "io" "log" "mime" @@ -186,31 +187,18 @@ type RequestConfig struct { BaseURL *url.URL } -var HTML_FORM_EXTENSION string = `` +type HTMLBodyExtParam struct { + BaseURL string + HasMortyKey bool +} -var HTML_BODY_EXTENSION string = ` - -
-
- - Morty Proxy - - This is a proxified and sanitized view of the page, visit original site. -
-
- -` +type HTMLFormExtParam struct { + BaseURL string + MortyKey string +} +var HTML_FORM_EXTENSION *template.Template +var HTML_BODY_EXTENSION *template.Template var HTML_HEAD_CONTENT_TYPE string = ` @@ -255,6 +243,37 @@ func init() { FaviconBase64 := "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII" FAVICON_BYTES, _ = base64.StdEncoding.DecodeString(FaviconBase64) + var err error + HTML_FORM_EXTENSION, err = template.New("html_form_extension").Parse( + ``) + if err != nil { + panic(err) + } + HTML_BODY_EXTENSION, err = template.New("html_body_extension").Parse(` + +
+
+ + Morty Proxy + + This is a proxified and sanitized view of the page, visit original site. +
+
+ +`) + if err != nil { + panic(err) + } } func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) { @@ -490,12 +509,9 @@ func popRequestParam(ctx *fasthttp.RequestCtx, paramName []byte) []byte { if param == nil { param = ctx.PostArgs().PeekBytes(paramName) - if param != nil { - ctx.PostArgs().DelBytes(paramName) - } - } else { - ctx.QueryArgs().DelBytes(paramName) + ctx.PostArgs().DelBytes(paramName) } + ctx.QueryArgs().DelBytes(paramName) return param } @@ -639,8 +655,12 @@ func sanitizeHTML(rc *RequestConfig, out io.Writer, htmlDoc []byte) { if rc.Key != nil { key = hash(urlStr, rc.Key) } - fmt.Fprintf(out, HTML_FORM_EXTENSION, urlStr, key) - + err := HTML_FORM_EXTENSION.Execute(out, HTMLFormExtParam{urlStr, key}) + if err != nil { + if DEBUG { + fmt.Println("failed to inject body extension", err) + } + } } case html.EndTagToken: @@ -648,7 +668,16 @@ func sanitizeHTML(rc *RequestConfig, out io.Writer, htmlDoc []byte) { writeEndTag := true switch string(tag) { case "body": - fmt.Fprintf(out, HTML_BODY_EXTENSION, rc.BaseURL.String(), rc.BaseURL.String()) + p := HTMLBodyExtParam{rc.BaseURL.String(), false} + if len(rc.Key) > 0 { + p.HasMortyKey = true + } + err := HTML_BODY_EXTENSION.Execute(out, p) + if err != nil { + if DEBUG { + fmt.Println("failed to inject body extension", err) + } + } case "style": state = STATE_DEFAULT case "noscript":