Commit aa03e8c8e0

aa03e8c8e02357cfe5f94188fdab3c79846cebae

parent: 0a69a8facd

Verified · cmc

cmc <hello@cleberg.net> · 2026-09-12 01:23 UTC

httpd: budget snippet rendering, fix a dead branch and a lost failure flash

snippetPage now fills Content only for the owner and spends
maxRenderBytes across the page's files in order, so a snippet with
many or oversized files no longer highlights every byte for every
viewer; a file that does not fit gets a raw link instead. snippetAction
always dispatched through runControlStdinCode, since an empty stdin
string already reads as empty either way, and a failed delete now
lands back on the snippet's own page with the message rather than
silently redirecting to the list. highlight gets a sibling,
highlightPlain, without linkable line-number ids, since a page with
several highlighted files repeated id="L1" per file.

Ref #195

Layout: unified · split

internal/httpd/snippets.go +35 −19
@@ -66,13 +66,18 @@ func (s *Server) snippetsPage(w http.ResponseWriter, r *http.Request) {
66} 66}
67 67
68type snippetFileView struct { 68type snippetFileView struct {
69 Name string 69 Name string
70 Size int64 70 Size int64
71 Lines int 71 Lines int
72 Content string 72 Content string
73 HTML template.HTML 73 HTML template.HTML
74 TooLarge bool
74} 75}
75 76
77// snippetPage highlights files up to a shared budget across the page: a
78// snippet with many or large files does not make one request highlight
79// megabytes of markup. Content is filled only for the owner, whose edit
80// textarea needs the raw text regardless of the budget.
76func (s *Server) snippetPage(w http.ResponseWriter, r *http.Request) { 81func (s *Server) snippetPage(w http.ResponseWriter, r *http.Request) {
77 sn, viewer, ok := s.snippetScope(w, r) 82 sn, viewer, ok := s.snippetScope(w, r)
78 if !ok { 83 if !ok {
@@ -83,13 +88,25 @@ func (s *Server) snippetPage(w http.ResponseWriter, r *http.Request) {
83 http.Error(w, "internal error", http.StatusInternalServerError) 88 http.Error(w, "internal error", http.StatusInternalServerError)
84 return 89 return
85 } 90 }
91 canWrite := policy.CanWriteSnippet(viewer, sn)
92 budget := int64(maxRenderBytes)
86 views := make([]snippetFileView, 0, len(files)) 93 views := make([]snippetFileView, 0, len(files))
87 for _, f := range files { 94 for _, f := range files {
88 lines := bytes.Count(f.Content, []byte("\n")) 95 lines := bytes.Count(f.Content, []byte("\n"))
89 if len(f.Content) > 0 && f.Content[len(f.Content)-1] != '\n' { 96 if len(f.Content) > 0 && f.Content[len(f.Content)-1] != '\n' {
90 lines++ 97 lines++
91 } 98 }
92 views = append(views, snippetFileView{f.Name, f.Size, lines, string(f.Content), highlight(f.Name, f.Content)}) 99 view := snippetFileView{Name: f.Name, Size: f.Size, Lines: lines}
100 if canWrite {
101 view.Content = string(f.Content)
102 }
103 if f.Size <= budget {
104 view.HTML = highlightPlain(f.Name, f.Content)
105 budget -= f.Size
106 } else {
107 view.TooLarge = true
108 }
109 views = append(views, view)
93 } 110 }
94 s.render(w, "snippet.html", struct { 111 s.render(w, "snippet.html", struct {
95 basePage 112 basePage
@@ -98,7 +115,7 @@ func (s *Server) snippetPage(w http.ResponseWriter, r *http.Request) {
98 Files []snippetFileView 115 Files []snippetFileView
99 CanWrite bool 116 CanWrite bool
100 Notice string 117 Notice string
101 }{s.baseFor(viewer), sn.OwnerName, sn, views, policy.CanWriteSnippet(viewer, sn), s.takeFlash(w, r)}) 118 }{s.baseFor(viewer), sn.OwnerName, sn, views, canWrite, s.takeFlash(w, r)})
102} 119}
103 120
104// snippetRaw serves one file as text, inert on the forge's origin. 121// snippetRaw serves one file as text, inert on the forge's origin.
@@ -147,28 +164,27 @@ func (s *Server) snippetNewSubmit(w http.ResponseWriter, r *http.Request, u stor
147 http.Redirect(w, r, "/"+u.Username+"/-/snippets/"+out.ID, http.StatusSeeOther) 164 http.Redirect(w, r, "/"+u.Username+"/-/snippets/"+out.ID, http.StatusSeeOther)
148} 165}
149 166
150// snippetAction runs a write on the snippet in the URL and returns to 167// snippetAction runs a write on the snippet in the URL and returns to its
151// its page with the message, or to the list after a delete. A snippet 168// page with the message, or to dest (the list, for a delete) on success.
152// the viewer may not read is the 404 page, as on every read. 169// A snippet the viewer may not read is the 404 page, as on every read.
153func (s *Server) snippetAction(w http.ResponseWriter, r *http.Request, u store.User, argv []string, stdin string, dest string) { 170func (s *Server) snippetAction(w http.ResponseWriter, r *http.Request, u store.User, argv []string, stdin string, dest string) {
154 sn, _, ok := s.snippetScope(w, r) 171 sn, _, ok := s.snippetScope(w, r)
155 if !ok { 172 if !ok {
156 return 173 return
157 } 174 }
175 page := "/" + sn.OwnerName + "/-/snippets/" + sn.PublicID
158 if dest == "" { 176 if dest == "" {
159 dest = "/" + sn.OwnerName + "/-/snippets/" + sn.PublicID 177 dest = page
160 } 178 }
161 back := func(w http.ResponseWriter, r *http.Request, msg string) { 179 back := func(w http.ResponseWriter, r *http.Request, msg string) {
162 s.setFlash(w, msg) 180 s.setFlash(w, msg)
163 http.Redirect(w, r, dest, http.StatusSeeOther) 181 to := dest
164 } 182 if msg != "" {
165 var msg string 183 to = page
166 var code int 184 }
167 if stdin == "" { 185 http.Redirect(w, r, to, http.StatusSeeOther)
168 _, msg, code = s.runControlCode(u, argv)
169 } else {
170 msg, code = s.runControlStdinCode(u, argv, stdin)
171 } 186 }
187 msg, code := s.runControlStdinCode(u, argv, stdin)
172 if code == protocol.ExitDenied { 188 if code == protocol.ExitDenied {
173 http.Error(w, msg, http.StatusForbidden) 189 http.Error(w, msg, http.StatusForbidden)
174 return 190 return
internal/httpd/web.go +16 −1
@@ -910,7 +910,22 @@ var chromaFormatter = html.New(html.WithClasses(true),
910 html.WithLineNumbers(true), html.LineNumbersInTable(false), 910 html.WithLineNumbers(true), html.LineNumbersInTable(false),
911 html.WithLinkableLineNumbers(true, "L")) 911 html.WithLinkableLineNumbers(true, "L"))
912 912
913// chromaFormatterPlain is chromaFormatter without linkable line numbers,
914// for a page that highlights more than one file: linkable ids are
915// per-file line numbers, so several files on one page would repeat
916// id="L1", id="L2", ...
917var chromaFormatterPlain = html.New(html.WithClasses(true),
918 html.WithLineNumbers(true), html.LineNumbersInTable(false))
919
913func highlight(filePath string, data []byte) template.HTML { 920func highlight(filePath string, data []byte) template.HTML {
921 return highlightWith(chromaFormatter, filePath, data)
922}
923
924func highlightPlain(filePath string, data []byte) template.HTML {
925 return highlightWith(chromaFormatterPlain, filePath, data)
926}
927
928func highlightWith(formatter *html.Formatter, filePath string, data []byte) template.HTML {
914 lexer := lexers.Match(filePath) 929 lexer := lexers.Match(filePath)
915 if lexer == nil { 930 if lexer == nil {
916 lexer = lexers.Fallback 931 lexer = lexers.Fallback
@@ -920,7 +935,7 @@ func highlight(filePath string, data []byte) template.HTML {
920 return template.HTML("<pre>" + template.HTMLEscapeString(string(data)) + "</pre>") 935 return template.HTML("<pre>" + template.HTMLEscapeString(string(data)) + "</pre>")
921 } 936 }
922 var buf bytes.Buffer 937 var buf bytes.Buffer
923 if err := chromaFormatter.Format(&buf, styles.Get(lightStyle), iterator); err != nil { 938 if err := formatter.Format(&buf, styles.Get(lightStyle), iterator); err != nil {
924 return template.HTML("<pre>" + template.HTMLEscapeString(string(data)) + "</pre>") 939 return template.HTML("<pre>" + template.HTMLEscapeString(string(data)) + "</pre>")
925 } 940 }
926 return template.HTML(buf.String()) 941 return template.HTML(buf.String())
internal/web/static/style.css +1 −1
@@ -1499,7 +1499,7 @@ table.tree td.name.dir a { color: var(--accent); }
1499p.clone { margin: 0 0 var(--sp-3); } 1499p.clone { margin: 0 0 var(--sp-3); }
1500p.filefacts { color: var(--muted); font-size: var(--fs-1); margin: 0 0 var(--sp-3); } 1500p.filefacts { color: var(--muted); font-size: var(--fs-1); margin: 0 0 var(--sp-3); }
1501.pathbar .actions { font-size: var(--fs-1); color: var(--muted); } 1501.pathbar .actions { font-size: var(--fs-1); color: var(--muted); }
1502.snippetfile { margin-bottom: 1.5rem } 1502.snippetfile { margin-bottom: var(--sp-5); }
1503 1503
1504/* merge request: a two-column split, so state has somewhere to live that 1504/* merge request: a two-column split, so state has somewhere to live that
1505 is not a run-on sentence under the title */ 1505 is not a run-on sentence under the title */
internal/web/templates/snippet.html +1 −1
@@ -12,7 +12,7 @@
12 <span class="actions"><a href="/{{$.Owner}}/-/snippets/{{$.Snippet.PublicID}}/raw/{{.Name}}">raw</a></span> 12 <span class="actions"><a href="/{{$.Owner}}/-/snippets/{{$.Snippet.PublicID}}/raw/{{.Name}}">raw</a></span>
13</div> 13</div>
14<p class="filefacts">{{.Lines}} lines · {{.Size}} bytes</p> 14<p class="filefacts">{{.Lines}} lines · {{.Size}} bytes</p>
15<div class="code">{{.HTML}}</div> 15{{if .TooLarge}}<p class="empty-note">too large to render here — <a href="/{{$.Owner}}/-/snippets/{{$.Snippet.PublicID}}/raw/{{.Name}}">raw</a></p>{{else}}<div class="code">{{.HTML}}</div>{{end}}
16{{if $.CanWrite}}<details class="editbox"><summary>edit {{.Name}}</summary> 16{{if $.CanWrite}}<details class="editbox"><summary>edit {{.Name}}</summary>
17<form method="post" action="/{{$.Owner}}/-/snippets/{{$.Snippet.PublicID}}/file" class="commentform"> 17<form method="post" action="/{{$.Owner}}/-/snippets/{{$.Snippet.PublicID}}/file" class="commentform">
18<input type="hidden" name="name" value="{{.Name}}"> 18<input type="hidden" name="name" value="{{.Name}}">