From 34b25be7d72c929c1461d3c6989016218b82e2ad Mon Sep 17 00:00:00 2001 From: Antoni Sawicki Date: Sat, 25 Apr 2020 23:56:14 -0700 Subject: [PATCH] rename wrpReq variable names to be more readable --- wrp.go | 213 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 107 insertions(+), 106 deletions(-) diff --git a/wrp.go b/wrp.go index 23a45bb..289fa4a 100644 --- a/wrp.go +++ b/wrp.go @@ -54,112 +54,112 @@ type geom struct { // WRP Request type wrpReq struct { - U string // url - W int64 // width - H int64 // height - S float64 // scale - C int64 // #colors - X int64 // mouseX - Y int64 // mouseY - K string // keys to send - F string // Fn buttons - T string // imgtype - o http.ResponseWriter - r *http.Request + url string // url + width int64 // width + height int64 // height + scale float64 // scale + colors int64 // #colors + mouseX int64 // mouseX + mouseY int64 // mouseY + keys string // keys to send + buttons string // Fn buttons + imgType string // imgtype + out http.ResponseWriter + req *http.Request } // Parse HTML Form, Process Input Boxes, Etc. func (w *wrpReq) parseForm() { - w.r.ParseForm() - w.U = w.r.FormValue("url") - if len(w.U) > 1 && !strings.HasPrefix(w.U, "http") { - w.U = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(w.U)) + w.req.ParseForm() + w.url = w.req.FormValue("url") + if len(w.url) > 1 && !strings.HasPrefix(w.url, "http") { + w.url = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(w.url)) } - w.W, _ = strconv.ParseInt(w.r.FormValue("w"), 10, 64) - w.H, _ = strconv.ParseInt(w.r.FormValue("h"), 10, 64) - if w.W < 10 && w.H < 10 { - w.W = defgeom.w - w.H = defgeom.h + w.width, _ = strconv.ParseInt(w.req.FormValue("w"), 10, 64) + w.height, _ = strconv.ParseInt(w.req.FormValue("h"), 10, 64) + if w.width < 10 && w.height < 10 { + w.width = defgeom.w + w.height = defgeom.h } - w.S, _ = strconv.ParseFloat(w.r.FormValue("s"), 64) - if w.S < 0.1 { - w.S = 1.0 + w.scale, _ = strconv.ParseFloat(w.req.FormValue("s"), 64) + if w.scale < 0.1 { + w.scale = 1.0 } - w.C, _ = strconv.ParseInt(w.r.FormValue("c"), 10, 64) - if w.C < 2 || w.C > 256 { - w.C = defgeom.c + w.colors, _ = strconv.ParseInt(w.req.FormValue("c"), 10, 64) + if w.colors < 2 || w.colors > 256 { + w.colors = defgeom.c } - w.K = w.r.FormValue("k") - w.F = w.r.FormValue("Fn") - w.T = w.r.FormValue("t") - if w.T != "gif" && w.T != "png" { - w.T = deftype + w.keys = w.req.FormValue("k") + w.buttons = w.req.FormValue("Fn") + w.imgType = w.req.FormValue("t") + if w.imgType != "gif" && w.imgType != "png" { + w.imgType = deftype } - log.Printf("%s WrpReq from Form: %+v\n", w.r.RemoteAddr, w) + log.Printf("%s WrpReq from Form: %+v\n", w.req.RemoteAddr, w) } // Display WP UI // TODO: make this in to an external template func (w wrpReq) printPage(bgcolor string) { var s string - w.o.Header().Set("Cache-Control", "max-age=0") - w.o.Header().Set("Expires", "-1") - w.o.Header().Set("Pragma", "no-cache") - w.o.Header().Set("Content-Type", "text/html") - fmt.Fprintf(w.o, "\n", version) - fmt.Fprintf(w.o, "\nWRP %s\n\n", w.U, bgcolor) - fmt.Fprintf(w.o, "
\n") - fmt.Fprintf(w.o, "", w.U) - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "W \n", w.W) - fmt.Fprintf(w.o, "H \n", w.H) - fmt.Fprintf(w.o, "S ", w.url) + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "W \n", w.width) + fmt.Fprintf(w.out, "H \n", w.height) + fmt.Fprintf(w.out, "S \n") - fmt.Fprintf(w.o, "T \n") + fmt.Fprintf(w.out, "T \n") - fmt.Fprintf(w.o, "C \n", w.C) - fmt.Fprintf(w.o, "K \n") - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "\n") - fmt.Fprintf(w.o, "

\n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "C \n", w.colors) + fmt.Fprintf(w.out, "K \n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "\n") + fmt.Fprintf(w.out, "
\n") } // Status bar below captured image func (w wrpReq) printFooter(h string, s string) { - fmt.Fprintf(w.o, "\n

"+ + fmt.Fprintf(w.out, "\n

"+ "Web Rendering Proxy Version %s | Shutdown WRP | "+ - "Page Height: %s | Img Size: %s\n\n", w.W, w.H, w.S, w.C, w.T, version, h, s) + "Page Height: %s | Img Size: %s\n\n", w.width, w.height, w.scale, w.colors, w.imgType, version, h, s) } // Process HTTP requests to WRP '/' url func pageServer(out http.ResponseWriter, req *http.Request) { log.Printf("%s Page Request for %s [%+v]\n", req.RemoteAddr, req.URL.Path, req.URL.RawQuery) var w wrpReq - w.r = req - w.o = out + w.req = req + w.out = out w.parseForm() - if len(w.U) > 4 { + if len(w.url) > 4 { w.navigate() w.capture() } else { @@ -172,8 +172,8 @@ func pageServer(out http.ResponseWriter, req *http.Request) { func mapServer(out http.ResponseWriter, req *http.Request) { log.Printf("%s ISMAP Request for %s [%+v]\n", req.RemoteAddr, req.URL.Path, req.URL.RawQuery) w, ok := ismap[req.URL.Path] - w.r = req - w.o = out + w.req = req + w.out = out if !ok { fmt.Fprintf(out, "Unable to find map %s\n", req.URL.Path) log.Printf("Unable to find map %s\n", req.URL.Path) @@ -182,14 +182,14 @@ func mapServer(out http.ResponseWriter, req *http.Request) { if !nodel { defer delete(ismap, req.URL.Path) } - n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.X, &w.Y) + n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.mouseX, &w.mouseY) if err != nil || n != 2 { fmt.Fprintf(out, "n=%d, err=%s\n", n, err) log.Printf("%s ISMAP n=%d, err=%s\n", req.RemoteAddr, n, err) return } log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w) - if len(w.U) > 4 { + if len(w.url) > 4 { w.navigate() w.capture() } else { @@ -242,13 +242,13 @@ func haltServer(out http.ResponseWriter, req *http.Request) { func (w wrpReq) navigate() { var err error // Mouse Click - if w.X > 0 && w.Y > 0 { - log.Printf("%s Mouse Click %d,%d\n", w.r.RemoteAddr, w.X, w.Y) - err = chromedp.Run(ctx, chromedp.MouseClickXY(float64(w.X)/float64(w.S), float64(w.Y)/float64(w.S))) + if w.mouseX > 0 && w.mouseY > 0 { + log.Printf("%s Mouse Click %d,%d\n", w.req.RemoteAddr, w.mouseX, w.mouseY) + err = chromedp.Run(ctx, chromedp.MouseClickXY(float64(w.mouseX)/float64(w.scale), float64(w.mouseY)/float64(w.scale))) // Buttons - } else if len(w.F) > 0 { - log.Printf("%s Button %v\n", w.r.RemoteAddr, w.F) - switch w.F { + } else if len(w.buttons) > 0 { + log.Printf("%s Button %v\n", w.req.RemoteAddr, w.buttons) + switch w.buttons { case "Bk": err = chromedp.Run(ctx, chromedp.NavigateBack()) case "Bs": @@ -265,22 +265,22 @@ func (w wrpReq) navigate() { err = chromedp.Run(ctx, chromedp.KeyEvent("\u0303")) } // Keys - } else if len(w.K) > 0 { - log.Printf("%s Sending Keys: %#v\n", w.r.RemoteAddr, w.K) - err = chromedp.Run(ctx, chromedp.KeyEvent(w.K)) + } else if len(w.keys) > 0 { + log.Printf("%s Sending Keys: %#v\n", w.req.RemoteAddr, w.keys) + err = chromedp.Run(ctx, chromedp.KeyEvent(w.keys)) // Navigate to URL } else { - log.Printf("%s Processing Capture Request for %s\n", w.r.RemoteAddr, w.U) - err = chromedp.Run(ctx, chromedp.Navigate(w.U)) + log.Printf("%s Processing Capture Request for %s\n", w.req.RemoteAddr, w.url) + err = chromedp.Run(ctx, chromedp.Navigate(w.url)) } if err != nil { if err.Error() == "context canceled" { - log.Printf("%s Contex cancelled, try again", w.r.RemoteAddr) - fmt.Fprintf(w.o, "
%s
-- restarting, try again", err) + log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr) + fmt.Fprintf(w.out, "
%s
-- restarting, try again", err) ctx, cancel = chromedp.NewContext(context.Background()) } else { - log.Printf("%s %s", w.r.RemoteAddr, err) - fmt.Fprintf(w.o, "
%s
", err) + log.Printf("%s %s", w.req.RemoteAddr, err) + fmt.Fprintf(w.out, "
%s
", err) } return } @@ -294,9 +294,9 @@ func (w wrpReq) capture() { var h int64 var pngcap []byte chromedp.Run(ctx, - emulation.SetDeviceMetricsOverride(int64(float64(w.W)/w.S), 10, w.S, false), + emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), 10, w.scale, false), chromedp.Sleep(time.Second*2), - chromedp.Location(&w.U), + chromedp.Location(&w.url), chromedp.ComputedStyle("body", &styles, chromedp.ByQuery), chromedp.ActionFunc(func(ctx context.Context) error { _, _, s, err := page.GetLayoutMetrics().Do(ctx) @@ -311,56 +311,57 @@ func (w wrpReq) capture() { fmt.Sscanf(style.Value, "rgb(%d,%d,%d)", &r, &g, &b) } } - log.Printf("%s Landed on: %s, Height: %v\n", w.r.RemoteAddr, w.U, h) + log.Printf("%s Landed on: %s, Height: %v\n", w.req.RemoteAddr, w.url, h) w.printPage(fmt.Sprintf("#%02X%02X%02X", r, g, b)) - if w.H == 0 && h > 0 { - chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.W)/w.S), h+30, w.S, false)) + if w.height == 0 && h > 0 { + chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), h+30, w.scale, false)) } else { - chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.W)/w.S), int64(float64(w.H)/w.S), w.S, false)) + chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), int64(float64(w.height)/w.scale), w.scale, false)) } err = chromedp.Run(ctx, chromedp.CaptureScreenshot(&pngcap)) if err != nil { - log.Printf("%s Failed to capture screenshot: %s\n", w.r.RemoteAddr, err) - fmt.Fprintf(w.o, "
Unable to capture screenshot:
%s
\n", err) + // TODO: process context cancelled here + log.Printf("%s Failed to capture screenshot: %s\n", w.req.RemoteAddr, err) + fmt.Fprintf(w.out, "
Unable to capture screenshot:
%s
\n", err) return } seq := rand.Intn(9999) - imgpath := fmt.Sprintf("/img/%04d.%s", seq, w.T) + imgpath := fmt.Sprintf("/img/%04d.%s", seq, w.imgType) mappath := fmt.Sprintf("/map/%04d.map", seq) ismap[mappath] = w var ssize string var sw, sh int - if w.T == "gif" { + if w.imgType == "gif" { i, err := png.Decode(bytes.NewReader(pngcap)) if err != nil { - log.Printf("%s Failed to decode screenshot: %s\n", w.r.RemoteAddr, err) - fmt.Fprintf(w.o, "
Unable to decode page screenshot:
%s
\n", err) + log.Printf("%s Failed to decode screenshot: %s\n", w.req.RemoteAddr, err) + fmt.Fprintf(w.out, "
Unable to decode page screenshot:
%s
\n", err) return } var gifbuf bytes.Buffer - err = gif.Encode(&gifbuf, i, &gif.Options{NumColors: int(w.C), Quantizer: quantize.MedianCutQuantizer{}}) + err = gif.Encode(&gifbuf, i, &gif.Options{NumColors: int(w.colors), Quantizer: quantize.MedianCutQuantizer{}}) if err != nil { - log.Printf("%s Failed to encode GIF: %s\n", w.r.RemoteAddr, err) - fmt.Fprintf(w.o, "
Unable to encode GIF:
%s
\n", err) + log.Printf("%s Failed to encode GIF: %s\n", w.req.RemoteAddr, err) + fmt.Fprintf(w.out, "
Unable to encode GIF:
%s
\n", err) return } img[imgpath] = gifbuf ssize = fmt.Sprintf("%.1f MB", float32(len(gifbuf.Bytes()))/1024.0/1024.0) sw = i.Bounds().Max.X sh = i.Bounds().Max.Y - log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", w.r.RemoteAddr, imgpath, ssize, w.C, sw, sh) - } else if w.T == "png" { + log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, w.colors, sw, sh) + } else if w.imgType == "png" { pngbuf := bytes.NewBuffer(pngcap) img[imgpath] = *pngbuf cfg, _, _ := image.DecodeConfig(pngbuf) ssize = fmt.Sprintf("%.1f MB", float32(len(pngbuf.Bytes()))/1024.0/1024.0) sw = cfg.Width sh = cfg.Height - log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.r.RemoteAddr, imgpath, ssize, sw, sh) + log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, sw, sh) } - fmt.Fprintf(w.o, "\"Url:", mappath, imgpath, w.U, ssize, sw, sh) + fmt.Fprintf(w.out, "\"Url:", mappath, imgpath, w.url, ssize, sw, sh) w.printFooter(fmt.Sprintf("%d PX", h), ssize) - log.Printf("%s Done with caputure for %s\n", w.r.RemoteAddr, w.U) + log.Printf("%s Done with caputure for %s\n", w.req.RemoteAddr, w.url) } // Main...