Skip to content

Commit

Permalink
feat: SVG icon-bubbles, at /svg/random-text
Browse files Browse the repository at this point in the history
  • Loading branch information
sharat87 committed Jan 19, 2025
1 parent 3d30d3c commit 66359cd
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 298 deletions.
7 changes: 7 additions & 0 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ <h3 id=client-tuned-responses>Client Tuned Response <a href="#client-tuned-respo
<dt id=html>/html</dt>
<dd>Returns a small HTML document than can trigger XSS, in vulnerable places.</dd>

<dt id=svg>/svg/<span class=var>{text}</span></dt>
<dd>Renders an SVG circle image with fill color determined by the <code>text</code>. The first two letters of the
text are also shown at the center of the circle. Examples:
<img src="svg/bun" style="height:1.5em;vertical-align:middle"> for <code>svg/bun</code>,
<img src="svg/foo" style="height:1.5em;vertical-align:middle"> for <code>svg/foo</code>.
</dd>

<dt id=robots>/robots.txt</dt>
<dd>Returns some robots.txt rules.</dd>

Expand Down
259 changes: 0 additions & 259 deletions assets/svg-logo.svg

This file was deleted.

14 changes: 8 additions & 6 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/sharat87/httpbun/routes/run"
"github.com/sharat87/httpbun/routes/sse"
"github.com/sharat87/httpbun/routes/static"
"github.com/sharat87/httpbun/routes/svg"
"github.com/sharat87/httpbun/util"
"log"
"maps"
Expand Down Expand Up @@ -55,16 +56,17 @@ func GetRoutes() []Route {
"/ip(\\.(?P<format>txt|json))?": handleIp,
}

maps.Copy(routeMap, method.Routes)
maps.Copy(routeMap, headers.Routes)
maps.Copy(routeMap, cache.Routes)
maps.Copy(routeMap, auth.Routes)
maps.Copy(routeMap, redirect.Routes)
maps.Copy(routeMap, mix.Routes)
maps.Copy(routeMap, static.Routes)
maps.Copy(routeMap, cache.Routes)
maps.Copy(routeMap, cookies.Routes)
maps.Copy(routeMap, headers.Routes)
maps.Copy(routeMap, method.Routes)
maps.Copy(routeMap, mix.Routes)
maps.Copy(routeMap, redirect.Routes)
maps.Copy(routeMap, run.Routes)
maps.Copy(routeMap, sse.Routes)
maps.Copy(routeMap, static.Routes)
maps.Copy(routeMap, svg.Routes)

var routes []Route

Expand Down
11 changes: 0 additions & 11 deletions routes/static/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package static

import (
"github.com/sharat87/httpbun/assets"
"github.com/sharat87/httpbun/c"
"github.com/sharat87/httpbun/exchange"
"github.com/sharat87/httpbun/response"
Expand All @@ -12,16 +11,6 @@ var Routes = map[string]exchange.HandlerFn{
"/deny": handleRobotsDeny,
"/robots.txt": handleRobotsTxt,
"/html": handleHtml,
"/image/svg": handleImageSvg,
}

func handleImageSvg(ex *exchange.Exchange) response.Response {
// todo: why isn't this SVG content-type being set by itself, like all the other assets. Is it the extension in the URL?
res := assets.WriteAsset("svg-logo.svg")
res.Header = http.Header{
c.ContentType: []string{"image/svg+xml"},
}
return *res
}

func handleRobotsTxt(ex *exchange.Exchange) response.Response {
Expand Down
31 changes: 31 additions & 0 deletions routes/svg/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package svg

import (
"github.com/sharat87/httpbun/exchange"
"github.com/sharat87/httpbun/response"
"github.com/sharat87/httpbun/util"
"net/http"
"strings"
)

var Routes = map[string]exchange.HandlerFn{
"/svg/(?P<seed>.+)": handleSVGSeeded,
}

func handleSVGSeeded(ex *exchange.Exchange) response.Response {
seed := ex.Field("seed")

color := "#" + util.Md5sum(seed)[:6]

body := `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50%" cy="50%" r="45%" fill="` + color + `" stroke="none" />
<text x="50%" y="53%" text-anchor="middle" dominant-baseline="middle" font-size="36" font-family="sans-serif" fill="` + util.ComputeFgForBg(color) + `">` + strings.ToUpper(seed[:2]) + `</text>
</svg>`

return response.Response{
Header: http.Header{
"Content-Type": []string{"image/svg+xml"},
},
Body: body,
}
}
Loading

0 comments on commit 66359cd

Please sign in to comment.