Skip to content

Commit 66359cd

Browse files
committed
feat: SVG icon-bubbles, at /svg/random-text
1 parent 3d30d3c commit 66359cd

File tree

7 files changed

+68
-298
lines changed

7 files changed

+68
-298
lines changed

assets/index.html

+7
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ <h3 id=client-tuned-responses>Client Tuned Response <a href="#client-tuned-respo
207207
<dt id=html>/html</dt>
208208
<dd>Returns a small HTML document than can trigger XSS, in vulnerable places.</dd>
209209

210+
<dt id=svg>/svg/<span class=var>{text}</span></dt>
211+
<dd>Renders an SVG circle image with fill color determined by the <code>text</code>. The first two letters of the
212+
text are also shown at the center of the circle. Examples:
213+
<img src="svg/bun" style="height:1.5em;vertical-align:middle"> for <code>svg/bun</code>,
214+
<img src="svg/foo" style="height:1.5em;vertical-align:middle"> for <code>svg/foo</code>.
215+
</dd>
216+
210217
<dt id=robots>/robots.txt</dt>
211218
<dd>Returns some robots.txt rules.</dd>
212219

assets/svg-logo.svg

-259
This file was deleted.

routes/routes.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/sharat87/httpbun/routes/run"
1818
"github.com/sharat87/httpbun/routes/sse"
1919
"github.com/sharat87/httpbun/routes/static"
20+
"github.com/sharat87/httpbun/routes/svg"
2021
"github.com/sharat87/httpbun/util"
2122
"log"
2223
"maps"
@@ -55,16 +56,17 @@ func GetRoutes() []Route {
5556
"/ip(\\.(?P<format>txt|json))?": handleIp,
5657
}
5758

58-
maps.Copy(routeMap, method.Routes)
59-
maps.Copy(routeMap, headers.Routes)
60-
maps.Copy(routeMap, cache.Routes)
6159
maps.Copy(routeMap, auth.Routes)
62-
maps.Copy(routeMap, redirect.Routes)
63-
maps.Copy(routeMap, mix.Routes)
64-
maps.Copy(routeMap, static.Routes)
60+
maps.Copy(routeMap, cache.Routes)
6561
maps.Copy(routeMap, cookies.Routes)
62+
maps.Copy(routeMap, headers.Routes)
63+
maps.Copy(routeMap, method.Routes)
64+
maps.Copy(routeMap, mix.Routes)
65+
maps.Copy(routeMap, redirect.Routes)
6666
maps.Copy(routeMap, run.Routes)
6767
maps.Copy(routeMap, sse.Routes)
68+
maps.Copy(routeMap, static.Routes)
69+
maps.Copy(routeMap, svg.Routes)
6870

6971
var routes []Route
7072

routes/static/handler.go

-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package static
22

33
import (
4-
"github.com/sharat87/httpbun/assets"
54
"github.com/sharat87/httpbun/c"
65
"github.com/sharat87/httpbun/exchange"
76
"github.com/sharat87/httpbun/response"
@@ -12,16 +11,6 @@ var Routes = map[string]exchange.HandlerFn{
1211
"/deny": handleRobotsDeny,
1312
"/robots.txt": handleRobotsTxt,
1413
"/html": handleHtml,
15-
"/image/svg": handleImageSvg,
16-
}
17-
18-
func handleImageSvg(ex *exchange.Exchange) response.Response {
19-
// todo: why isn't this SVG content-type being set by itself, like all the other assets. Is it the extension in the URL?
20-
res := assets.WriteAsset("svg-logo.svg")
21-
res.Header = http.Header{
22-
c.ContentType: []string{"image/svg+xml"},
23-
}
24-
return *res
2514
}
2615

2716
func handleRobotsTxt(ex *exchange.Exchange) response.Response {

routes/svg/handler.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package svg
2+
3+
import (
4+
"github.com/sharat87/httpbun/exchange"
5+
"github.com/sharat87/httpbun/response"
6+
"github.com/sharat87/httpbun/util"
7+
"net/http"
8+
"strings"
9+
)
10+
11+
var Routes = map[string]exchange.HandlerFn{
12+
"/svg/(?P<seed>.+)": handleSVGSeeded,
13+
}
14+
15+
func handleSVGSeeded(ex *exchange.Exchange) response.Response {
16+
seed := ex.Field("seed")
17+
18+
color := "#" + util.Md5sum(seed)[:6]
19+
20+
body := `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
21+
<circle cx="50%" cy="50%" r="45%" fill="` + color + `" stroke="none" />
22+
<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>
23+
</svg>`
24+
25+
return response.Response{
26+
Header: http.Header{
27+
"Content-Type": []string{"image/svg+xml"},
28+
},
29+
Body: body,
30+
}
31+
}

0 commit comments

Comments
 (0)