Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
336 lines
11 KiB
Go
336 lines
11 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/xml"
|
|
stdhtml "html"
|
|
"html/template"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/net/html"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func publicPeriodicalArticlePath(target string) bool {
|
|
parts := strings.Split(strings.TrimPrefix(target, "/"), "/")
|
|
if len(parts) != 3 && len(parts) != 4 {
|
|
return false
|
|
}
|
|
date := strings.Join(parts[:3], "-")
|
|
d, err := time.Parse("2006-01-02", date)
|
|
return err == nil && d.Year() > 0 && path.Clean(target) == target && !strings.ContainsAny(target, "\\?#") && (len(parts) == 3 || parts[3] != "")
|
|
}
|
|
|
|
func projectedArticleHref(prefix, target string) string {
|
|
if !strings.HasPrefix(target, strings.TrimSuffix(prefix, "/")+"/") {
|
|
return ""
|
|
}
|
|
relative := strings.TrimPrefix(target, strings.TrimSuffix(prefix, "/"))
|
|
if publicPeriodicalArticlePath(relative) {
|
|
return cityPageHref(relative)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *Server) renderPublicPeriodical(w http.ResponseWriter, r *http.Request, kind, month, selected, sourceHost string, projection *store.PeriodicalProjection, entries []store.PeriodicalEntry, revisions map[string]store.Revision, article, feed bool) {
|
|
domain := s.domainUUIDForRequest(r)
|
|
site := s.getWikiName(domain)
|
|
items := make([]periodicalItem, 0, len(entries))
|
|
for _, e := range entries {
|
|
items = append(items, periodicalItem{PeriodicalEntry: e, Href: projectedArticleHref(projection.Prefix, e.Path)})
|
|
}
|
|
if feed {
|
|
s.renderPeriodicalRSS(w, r, site, sourceHost, projection.Source, items, revisions)
|
|
return
|
|
}
|
|
data := map[string]any{"Context": s.BuildRequestContext(r), "Locale": s.getLocale(domain), "Title": site, "Kind": kind, "SiteTitle": site, "BasePath": "/", "FilterAction": "/v/" + kind}
|
|
articleIndex := -1
|
|
if article {
|
|
sourcePath := strings.TrimSuffix(projection.Prefix, "/") + r.URL.Path
|
|
for i := range items {
|
|
if items[i].Path == sourcePath {
|
|
articleIndex = i
|
|
break
|
|
}
|
|
}
|
|
if articleIndex < 0 {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
item := items[articleIndex]
|
|
v := revisions[item.Path]
|
|
// Recheck the exact current public revision before opening its bytes.
|
|
current, err := s.store.AnonymousRevision(r.Context(), projection.Source, item.Path, []string{v.Language})
|
|
if err != nil || current.UUID != v.UUID {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
reader, err := s.store.OpenDocumentContext(r.Context(), v.DocumentHash)
|
|
if err != nil {
|
|
http.Error(w, "published document unavailable", 503)
|
|
return
|
|
}
|
|
body, err := io.ReadAll(io.LimitReader(reader, maxProjectionInput+1))
|
|
reader.Close()
|
|
if err != nil {
|
|
http.Error(w, "published document unavailable", 503)
|
|
return
|
|
}
|
|
sourceRequest := r.Clone(r.Context())
|
|
sourceRequest.Host = sourceHost
|
|
sourceRequest, err = s.withPublicNavigation(sourceRequest)
|
|
if err != nil {
|
|
http.Error(w, "publication unavailable", 503)
|
|
return
|
|
}
|
|
if strings.HasPrefix(v.ContentType, "text/html") {
|
|
_, data["Description"] = socialHTML(string(body))
|
|
safe := s.safeHTMLProjection(sourceRequest, string(body), item.Path, true)
|
|
data["HTML"] = template.HTML(projectedPeriodicalHTML(safe, item.Path, sourceHost, items, ""))
|
|
} else if strings.HasPrefix(v.ContentType, "text/") {
|
|
data["Text"] = string(body)
|
|
} else {
|
|
http.Error(w, "unsupported article type", 415)
|
|
return
|
|
}
|
|
data["PublicMediaOrigin"] = s.socialOrigin(sourceRequest)
|
|
data["Article"] = item
|
|
data["Title"] = item.Title
|
|
data["Canonical"] = s.socialOrigin(r) + item.Href
|
|
month, selected = item.Date[:7], item.Date
|
|
if articleIndex > 0 {
|
|
data["Newer"] = items[articleIndex-1]
|
|
}
|
|
if articleIndex+1 < len(items) {
|
|
data["Older"] = items[articleIndex+1]
|
|
}
|
|
}
|
|
filterMonth := month != "" || kind == "diary"
|
|
if month == "" {
|
|
month = time.Now().In(s.getLocation(projection.Source)).Format("2006-01")
|
|
}
|
|
first, _ := time.Parse("2006-01", month)
|
|
counts := map[string]int{}
|
|
filtered, inMonth := []periodicalItem{}, []periodicalItem{}
|
|
for _, item := range items {
|
|
if strings.HasPrefix(item.Date, month+"-") {
|
|
counts[item.Date]++
|
|
if len(inMonth) < 31 && (articleIndex < 0 || item.Path != items[articleIndex].Path) {
|
|
inMonth = append(inMonth, item)
|
|
}
|
|
}
|
|
if selected != "" && item.Date != selected {
|
|
continue
|
|
}
|
|
if filterMonth && !strings.HasPrefix(item.Date, month+"-") {
|
|
continue
|
|
}
|
|
filtered = append(filtered, item)
|
|
}
|
|
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
|
offset = max(0, min(offset, len(filtered)))
|
|
end := min(offset+20, len(filtered))
|
|
data["Entries"] = filtered[offset:end]
|
|
pageLink := func(n int) string {
|
|
q := r.URL.Query()
|
|
q.Set("offset", strconv.Itoa(n))
|
|
return periodicalURL(r.URL.Path, q)
|
|
}
|
|
if offset > 0 {
|
|
data["Previous"] = pageLink(max(0, offset-20))
|
|
}
|
|
if end < len(filtered) {
|
|
data["Next"] = pageLink(end)
|
|
}
|
|
days := []periodicalDay{}
|
|
for i := 0; i < int(first.Weekday()); i++ {
|
|
days = append(days, periodicalDay{})
|
|
}
|
|
for d := first; d.Month() == first.Month(); d = d.AddDate(0, 0, 1) {
|
|
date := d.Format("2006-01-02")
|
|
days = append(days, periodicalDay{Number: d.Day(), Count: counts[date], Selected: date == selected, Href: periodicalDatePath(kind, month, date)})
|
|
}
|
|
monthLink := func(delta int) string {
|
|
d := first.AddDate(0, delta, 0)
|
|
if d.Year() < 1 || d.Year() > 9999 {
|
|
d = first
|
|
}
|
|
return periodicalDatePath(kind, d.Format("2006-01"), "")
|
|
}
|
|
data["Days"], data["Month"], data["MonthEntries"] = days, month, inMonth
|
|
data["PreviousMonth"], data["NextMonth"] = monthLink(-1), monthLink(1)
|
|
data["FilterMonth"] = month
|
|
if r.URL.Path == "/" && kind == "blog" {
|
|
data["FilterMonth"] = ""
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
s.render(w, "public_periodical.gohtml", data)
|
|
}
|
|
|
|
// Rewrite only already-sanitized links. Known public articles stay on the blog;
|
|
// all other source-relative links retain their original source-domain meaning.
|
|
func projectedPeriodicalHTML(body, sourcePath, sourceHost string, items []periodicalItem, absoluteOrigin string) string {
|
|
doc, err := html.Parse(strings.NewReader(body))
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
local := map[string]string{}
|
|
for _, item := range items {
|
|
local[item.Path] = item.Href
|
|
}
|
|
base := &url.URL{Scheme: "https", Host: sourceHost, Path: sourcePath}
|
|
var visit func(*html.Node)
|
|
visit = func(n *html.Node) {
|
|
for i := range n.Attr {
|
|
a := &n.Attr[i]
|
|
if a.Key != "href" && a.Key != "src" {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(a.Val, "#") {
|
|
if absoluteOrigin != "" {
|
|
a.Val = absoluteOrigin + local[sourcePath] + a.Val
|
|
}
|
|
continue
|
|
}
|
|
u, err := url.Parse(a.Val)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
u = base.ResolveReference(u)
|
|
if a.Key == "href" && u.Host == sourceHost && local[u.Path] != "" {
|
|
mapped, _ := url.Parse(local[u.Path])
|
|
mapped.RawQuery = u.RawQuery
|
|
mapped.Fragment = u.Fragment
|
|
a.Val = mapped.String()
|
|
if absoluteOrigin != "" {
|
|
a.Val = absoluteOrigin + a.Val
|
|
}
|
|
} else {
|
|
a.Val = u.String()
|
|
}
|
|
}
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
visit(c)
|
|
}
|
|
}
|
|
visit(doc)
|
|
var out strings.Builder
|
|
var emit func(*html.Node)
|
|
emit = func(n *html.Node) {
|
|
if n.Type == html.ElementNode && n.Data == "body" {
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
_ = html.Render(&out, c)
|
|
}
|
|
return
|
|
}
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
emit(c)
|
|
}
|
|
}
|
|
emit(doc)
|
|
return out.String()
|
|
}
|
|
|
|
type periodicalRSSItem struct {
|
|
Title string `xml:"title"`
|
|
Link string `xml:"link"`
|
|
GUID string `xml:"guid"`
|
|
Date string `xml:"pubDate"`
|
|
Content periodicalRSSContent `xml:"content:encoded"`
|
|
}
|
|
type periodicalRSSContent struct {
|
|
HTML string `xml:",cdata"`
|
|
}
|
|
type periodicalRSS struct {
|
|
XMLName xml.Name `xml:"rss"`
|
|
Version string `xml:"version,attr"`
|
|
ContentNamespace string `xml:"xmlns:content,attr"`
|
|
Channel struct {
|
|
Title string `xml:"title"`
|
|
Link string `xml:"link"`
|
|
Description string `xml:"description"`
|
|
Items []periodicalRSSItem `xml:"item"`
|
|
} `xml:"channel"`
|
|
}
|
|
|
|
func (s *Server) renderPeriodicalRSS(w http.ResponseWriter, r *http.Request, site, sourceHost, sourceDomain string, items []periodicalItem, revisions map[string]store.Revision) {
|
|
origin := s.socialOrigin(r)
|
|
if origin == "" {
|
|
http.Error(w, "feed origin unavailable", 503)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
if r.Method == "HEAD" {
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
|
defer cancel()
|
|
sourceRequest := r.Clone(ctx)
|
|
sourceRequest.Host = sourceHost
|
|
sourceRequest, err := s.withPublicNavigation(sourceRequest)
|
|
if err != nil {
|
|
http.Error(w, "publication unavailable", 503)
|
|
return
|
|
}
|
|
feed := periodicalRSS{Version: "2.0", ContentNamespace: "http://purl.org/rss/1.0/modules/content/"}
|
|
feed.Channel.Title = site
|
|
feed.Channel.Link = origin + "/"
|
|
feed.Channel.Description = site
|
|
contentBytes := 0
|
|
for _, item := range items[:min(20, len(items))] {
|
|
v := revisions[item.Path]
|
|
current, err := s.store.AnonymousRevision(ctx, sourceDomain, item.Path, []string{v.Language})
|
|
if err != nil || current.UUID != v.UUID {
|
|
http.Error(w, "publication changed; retry", 503)
|
|
return
|
|
}
|
|
reader, err := s.store.OpenDocumentContext(r.Context(), v.DocumentHash)
|
|
if err != nil {
|
|
http.Error(w, "published document unavailable", 503)
|
|
return
|
|
}
|
|
raw, err := io.ReadAll(io.LimitReader(reader, maxProjectionInput+1))
|
|
reader.Close()
|
|
if err != nil || len(raw) > maxProjectionInput {
|
|
http.Error(w, "feed document exceeds rendering budget", 503)
|
|
return
|
|
}
|
|
content := ""
|
|
if strings.HasPrefix(v.ContentType, "text/html") {
|
|
content = projectedPeriodicalHTML(s.safeHTMLProjection(sourceRequest, string(raw), item.Path, true), item.Path, sourceHost, items, origin)
|
|
} else if strings.HasPrefix(v.ContentType, "text/") {
|
|
content = "<pre>" + stdhtml.EscapeString(string(raw)) + "</pre>"
|
|
}
|
|
if ctx.Err() != nil {
|
|
http.Error(w, "feed rendering timed out", 503)
|
|
return
|
|
}
|
|
contentBytes += len(content)
|
|
if contentBytes > 8<<20 {
|
|
http.Error(w, "feed exceeds rendering budget", 503)
|
|
return
|
|
}
|
|
link := origin + item.Href
|
|
feed.Channel.Items = append(feed.Channel.Items, periodicalRSSItem{Title: item.Title, Link: link, GUID: link, Date: time.UnixMilli(v.CreatedAtMS).UTC().Format(time.RFC1123Z), Content: periodicalRSSContent{HTML: content}})
|
|
}
|
|
body, err := xml.Marshal(feed)
|
|
if err != nil {
|
|
http.Error(w, "feed unavailable", 503)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
if r.Method == "HEAD" {
|
|
return
|
|
}
|
|
_, _ = io.WriteString(w, xml.Header)
|
|
_, _ = w.Write(body)
|
|
}
|