2 SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
3 Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
38 "github.com/go-git/go-git/v5"
39 "github.com/go-git/go-git/v5/plumbing"
40 "github.com/go-git/go-git/v5/plumbing/object"
41 "go.stargrave.org/sgblog"
42 "go.stargrave.org/sgblog/cmd/sgblog/atom"
43 "golang.org/x/crypto/blake2b"
47 AtomPostsFeed = "feed.atom"
48 AtomCommentsFeed = "comments.atom"
49 TmplHTMLIndex = `<html>
51 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
52 <meta name="generator" content="SGBlog {{.Version}}">
53 <title>{{.Cfg.Title}} {{if .Topic}}(topic: {{.Topic}}) {{end}}({{.Offset}}-{{.OffsetNext}})</title>
54 {{with .Cfg.CSS}}<link rel="stylesheet" type="text/css" href="{{.}}">{{end}}
55 {{with .Cfg.Webmaster}}<link rev="made" href="mailto:{{.}}">{{end}}
56 {{range .Cfg.GitURLs}}<link rel="vcs-git" href="{{.}}" title="Git repository">{{end}}
57 <link rel="top" href="{{.Cfg.URLPrefix}}/" title="top">
58 <link rel="alternate" title="Posts feed" href="{{.Cfg.AtomBaseURL}}{{.Cfg.URLPrefix}}/{{.AtomPostsFeed}}{{if .Topic}}?topic={{.Topic}}{{end}}" type="application/atom+xml">
59 {{if .CommentsEnabled}}<link rel="alternate" title="Comments feed" href="{{.Cfg.AtomBaseURL}}{{.Cfg.URLPrefix}}/{{.AtomCommentsFeed}}" type="application/atom+xml">{{end}}
60 {{if .Offset}}<link rel="prev" href="{{.Cfg.URLPrefix}}/?offset={{.OffsetPrev}}{{if .Topic}}&topic={{.Topic}}{{end}}" title="prev">{{end}}
61 {{if not .LogEnded}}<link rel="next" href="{{.Cfg.URLPrefix}}/?offset={{.OffsetNext}}{{if .Topic}}&topic={{.Topic}}{{end}}" title="next">{{end}}
64 {{with .Cfg.AboutURL}}[<a href="{{.}}">about</a>]{{end}}
66 {{if .Offset}}[<a href="{{.Cfg.URLPrefix}}/?offset={{.OffsetPrev}}{{if .Topic}}&topic={{.Topic}}{{end}}">prev</a>]{{end}}
67 {{if not .LogEnded}}[<a href="{{.Cfg.URLPrefix}}/?offset={{.OffsetNext}}{{if .Topic}}&topic={{.Topic}}{{end}}">next</a>]{{end}}
71 Topics: [<tt><a href="{{$Cfg.URLPrefix}}/">ALL</a></tt>]
72 {{range .Topics}}[<tt><a href="{{$Cfg.URLPrefix}}?topic={{.}}">{{.}}</a></tt>]
75 {{- $TopicsEnabled := .TopicsEnabled -}}
76 {{- $datePrev := "0001-01-01" -}}
79 <th>N</th><th>When</th><th>Title</th>
80 <th size="5%"><a title="Lines">L</a></th>
81 <th size="5%"><a title="Comments">C</a></th>
83 {{if .TopicsEnabled}}<th>Topics</th>{{end}}
86 {{- $dateCur := .Commit.Author.When.Format "2006-01-02" -}}
87 {{- if ne $dateCur $datePrev -}}
88 <tr><td colspan={{if $TopicsEnabled}}7{{else}}7{{end}}><center><tt>{{$dateCur}}</tt></center></td></tr>
89 {{- $datePrev = $dateCur -}}
93 <td><tt>{{.Commit.Author.When.Format "15:04"}}</tt></td>
94 <td><a href="{{$Cfg.URLPrefix}}/{{.Commit.Hash.String}}">{{.Title}}</a></td>
95 <td>{{.LinesNum}}</td>
96 <td>{{if .CommentsNum}}{{.CommentsNum}}{{else}} {{end}}</td>
97 <td>{{if .DomainURLs}}{{range .DomainURLs}} {{.}} {{end}}{{else}} {{end}}</td>
98 {{if $TopicsEnabled}}<td>{{if .Topics}}{{range .Topics}} <a href="{{$Cfg.URLPrefix}}/?topic={{.}}">{{.}}</a> {{end}}{{else}} {{end}}</td>{{end}}
101 {{template "links" .}}
105 TmplHTMLEntry = `{{$Cfg := .Cfg}}<html>
107 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
108 <meta name="generator" content="SGBlog {{.Version}}">
109 <title>{{.Title}} ({{.When}})</title>
110 {{with .Cfg.CSS}}<link rel="stylesheet" type="text/css" href="{{.}}">{{end}}
111 {{with .Cfg.Webmaster}}<link rev="made" href="mailto:{{.}}">{{end -}}
112 {{- range .Cfg.GitURLs}}
113 <link rel="vcs-git" href="{{.}}" title="Git repository">{{end}}
114 <link rel="top" href="{{.Cfg.URLPrefix}}/" title="top">
115 <link rel="alternate" title="Comments feed" href="{{.AtomCommentsURL}}" type="application/atom+xml">
116 {{if .Parent}}<link rel="prev" href="{{.Cfg.URLPrefix}}/{{.Parent}}" title="prev">{{end}}
119 {{with .Cfg.AboutURL}}[<a href="{{.}}">about</a>]{{end}}
120 [<a href="{{.Cfg.URLPrefix}}/">index</a>]
121 {{if .Parent}}[<a href="{{.Cfg.URLPrefix}}/{{.Parent}}">prev</a>]{{end}}
122 [<tt><a title="When">{{.When}}</a></tt>]
123 [<tt><a title="What">{{.Commit.Hash.String}}</a></tt>]
127 Topics: {{range .Topics}}[<tt><a href="{{$Cfg.URLPrefix}}?topic={{.}}">{{.}}</a></tt>]{{end}}
133 {{range .Lines}}{{. | lineURLize $Cfg.URLPrefix}}
137 {{if .NoteLines}}Note:<pre>
138 {{range .NoteLines}}{{. | lineURLize $Cfg.URLPrefix}}
142 {{if .Cfg.CommentsEmail}}[<a href="mailto:{{.Cfg.CommentsEmail}}?subject={{.TitleEscaped}}">leave comment</a>]{{end}}
144 <dl>{{range $idx, $comment := .Comments}}
145 <dt><a name="comment{{$idx}}"><a href="#comment{{$idx}}">comment {{$idx}}</a>:</dt>
147 {{range $comment.HeaderLines}}{{.}}
148 {{end}}{{range $comment.BodyLines}}{{. | lineURLize $Cfg.URLPrefix}}
158 renderableSchemes = map[string]struct{}{
168 type TableEntry struct {
169 Commit *object.Commit
180 type CommentEntry struct {
185 func makeA(href, text string) string {
186 return `<a href="` + href + `">` + text + `</a>`
189 func etagString(etag hash.Hash) string {
190 return `"` + hex.EncodeToString(etag.Sum(nil)) + `"`
193 func urlParse(what string) *url.URL {
194 if u, err := url.ParseRequestURI(what); err == nil {
195 if _, exists := renderableSchemes[u.Scheme]; exists {
202 func lineURLize(urlPrefix, line string) string {
203 cols := strings.Split(html.EscapeString(line), " ")
204 for i, col := range cols {
205 if u := urlParse(col); u != nil {
206 cols[i] = makeA(col, col)
209 cols[i] = sha1DigestRe.ReplaceAllString(col, makeA(urlPrefix+"/$1", "$1"))
211 return strings.Join(cols, " ")
214 func lineURLizeInTemplate(urlPrefix, line interface{}) string {
215 return lineURLize(urlPrefix.(string), line.(string))
218 func startHeader(etag hash.Hash, gziped bool) string {
220 "Content-Type: text/html; charset=UTF-8",
221 "ETag: " + etagString(etag),
224 lines = append(lines, "Content-Encoding: gzip")
226 lines = append(lines, "")
227 lines = append(lines, "")
228 return strings.Join(lines, "\n")
231 func makeErr(err error) {
232 fmt.Print("Content-Type: text/plain; charset=UTF-8\n\n")
237 func checkETag(etag hash.Hash) {
238 ifNoneMatch := os.Getenv("HTTP_IF_NONE_MATCH")
239 if ifNoneMatch != "" && ifNoneMatch == etagString(etag) {
240 fmt.Printf("Status: 304\nETag: %s\n\n", ifNoneMatch)
245 func bytes2uuid(b []byte) string {
248 raw[6] = (raw[6] & 0x0F) | uint8(4<<4) // version 4
249 return fmt.Sprintf("%x-%x-%x-%x-%x", raw[0:4], raw[4:6], raw[6:8], raw[8:10], raw[10:])
252 type CommitIterNext interface {
253 Next() (*object.Commit, error)
257 cfgPath := os.Getenv("SGBLOG_CFG")
259 log.Fatalln("SGBLOG_CFG is not set")
261 cfg, err := readCfg(cfgPath)
266 pathInfo, exists := os.LookupEnv("PATH_INFO")
270 queryValues, err := url.ParseQuery(os.Getenv("QUERY_STRING"))
275 etagHash, err := blake2b.New256(nil)
279 for _, s := range []string{
290 if _, err = etagHash.Write([]byte(s)); err != nil {
294 etagHashForWeb := []string{
298 cfg.CommentsNotesRef,
301 for _, gitURL := range cfg.GitURLs {
302 etagHashForWeb = append(etagHashForWeb, gitURL)
305 headHash, err := initRepo(cfg)
310 if notes, err := repo.Notes(); err == nil {
311 var notesRef *plumbing.Reference
312 var commentsRef *plumbing.Reference
313 notes.ForEach(func(ref *plumbing.Reference) error {
314 switch string(ref.Name()) {
315 case "refs/notes/commits":
317 case cfg.CommentsNotesRef:
323 if commentsCommit, err := repo.CommitObject(notesRef.Hash()); err == nil {
324 notesTree, _ = commentsCommit.Tree()
327 if commentsRef != nil {
328 if commentsCommit, err := repo.CommitObject(commentsRef.Hash()); err == nil {
329 commentsTree, _ = commentsCommit.Tree()
334 var outBuf bytes.Buffer
337 var gzipWriter *gzip.Writer
338 acceptEncoding := os.Getenv("HTTP_ACCEPT_ENCODING")
339 for _, encoding := range strings.Split(acceptEncoding, ", ") {
340 if encoding == "gzip" {
341 gzipWriter = gzip.NewWriter(&outBuf)
348 if offsetRaw, exists := queryValues["offset"]; exists {
349 offset, err = strconv.Atoi(offsetRaw[0])
354 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
358 topicsCache, err := getTopicsCache(cfg, repoLog)
362 repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
368 var commits CommitIterNext
370 if t, exists := queryValues["topic"]; exists {
372 hashes := topicsCache[topic]
374 makeErr(errors.New("no posts with that topic"))
376 if len(hashes) > offset {
377 hashes = hashes[offset:]
380 commits = &HashesIter{hashes}
382 for i := 0; i < offset; i++ {
383 if _, err = repoLog.Next(); err != nil {
391 entries := make([]TableEntry, 0, PageEntries)
393 for _, data := range etagHashForWeb {
394 etagHash.Write([]byte(data))
396 etagHash.Write([]byte("INDEX"))
397 etagHash.Write([]byte(topic))
398 for i := 0; i < PageEntries; i++ {
399 commit, err := commits.Next()
404 etagHash.Write(commit.Hash[:])
405 commentsRaw := sgblog.GetNote(repo, commentsTree, commit.Hash)
406 etagHash.Write(commentsRaw)
407 topicsRaw := sgblog.GetNote(repo, topicsTree, commit.Hash)
408 etagHash.Write(topicsRaw)
409 entries = append(entries, TableEntry{
411 CommentsRaw: commentsRaw,
412 TopicsRaw: topicsRaw,
417 for i, entry := range entries {
420 lines := msgSplit(entry.Commit.Message)
421 entry.Title = lines[0]
422 entry.LinesNum = len(lines) - 2
423 for _, line := range lines[2:] {
428 entry.DomainURLs = append(entry.DomainURLs, makeA(line, u.Host))
430 entry.CommentsNum = len(sgblog.ParseComments(entry.CommentsRaw))
431 entry.Topics = sgblog.ParseTopics(entry.TopicsRaw)
434 offsetPrev := offset - PageEntries
438 tmpl := template.Must(template.New("index").Parse(TmplHTMLIndex))
439 os.Stdout.Write([]byte(startHeader(etagHash, gzipWriter != nil)))
440 err = tmpl.Execute(out, struct {
448 AtomCommentsFeed string
455 Version: sgblog.Version,
458 TopicsEnabled: topicsTree != nil,
459 Topics: topicsCache.Topics(),
460 CommentsEnabled: commentsTree != nil,
461 AtomPostsFeed: AtomPostsFeed,
462 AtomCommentsFeed: AtomCommentsFeed,
464 OffsetPrev: offsetPrev,
465 OffsetNext: offset + PageEntries,
472 } else if pathInfo == "/"+AtomPostsFeed {
473 commit, err := repo.CommitObject(*headHash)
479 if t, exists := queryValues["topic"]; exists {
483 etagHash.Write([]byte("ATOM POSTS"))
484 etagHash.Write([]byte(topic))
485 etagHash.Write(commit.Hash[:])
491 title = fmt.Sprintf("%s (topic: %s)", cfg.Title, topic)
493 idHasher, err := blake2b.New256(nil)
497 idHasher.Write([]byte("ATOM POSTS"))
498 idHasher.Write([]byte(cfg.AtomId))
499 idHasher.Write([]byte(topic))
502 ID: "urn:uuid:" + bytes2uuid(idHasher.Sum(nil)),
503 Updated: atom.Time(commit.Author.When),
506 Href: cfg.AtomBaseURL + cfg.URLPrefix + "/" + AtomPostsFeed,
508 Author: &atom.Person{Name: cfg.AtomAuthor},
511 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
515 var commits CommitIterNext
519 topicsCache, err := getTopicsCache(cfg, repoLog)
523 hashes := topicsCache[topic]
525 makeErr(errors.New("no posts with that topic"))
527 commits = &HashesIter{hashes}
530 for i := 0; i < PageEntries; i++ {
531 commit, err = commits.Next()
535 lines := msgSplit(commit.Message)
536 var categories []atom.Category
537 for _, topic := range sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)) {
538 categories = append(categories, atom.Category{Term: topic})
540 htmlized := make([]string, 0, len(lines))
541 htmlized = append(htmlized, "<pre>")
542 for _, l := range lines[2:] {
543 htmlized = append(htmlized, lineURLize(cfg.AtomBaseURL+cfg.URLPrefix, l))
545 htmlized = append(htmlized, "</pre>")
546 feed.Entry = append(feed.Entry, &atom.Entry{
548 ID: "urn:uuid:" + bytes2uuid(commit.Hash[:]),
551 Href: cfg.AtomBaseURL + cfg.URLPrefix + "/" + commit.Hash.String(),
553 Published: atom.Time(commit.Author.When),
554 Updated: atom.Time(commit.Author.When),
555 Summary: &atom.Text{Type: "text", Body: lines[0]},
558 Body: strings.Join(htmlized, "\n"),
560 Category: categories,
563 data, err := xml.MarshalIndent(&feed, "", " ")
569 } else if pathInfo == "/"+AtomCommentsFeed {
570 commit, err := repo.CommitObject(commentsRef.Hash())
574 etagHash.Write([]byte("ATOM COMMENTS"))
575 etagHash.Write(commit.Hash[:])
577 idHasher, err := blake2b.New256(nil)
581 idHasher.Write([]byte("ATOM COMMENTS"))
582 idHasher.Write([]byte(cfg.AtomId))
584 Title: cfg.Title + " comments",
585 ID: "urn:uuid:" + bytes2uuid(idHasher.Sum(nil)),
586 Updated: atom.Time(commit.Author.When),
589 Href: cfg.AtomBaseURL + cfg.URLPrefix + "/" + AtomCommentsFeed,
591 Author: &atom.Person{Name: cfg.AtomAuthor},
593 repoLog, err := repo.Log(&git.LogOptions{From: commentsRef.Hash()})
597 for i := 0; i < PageEntries; i++ {
598 commit, err = repoLog.Next()
602 fileStats, err := commit.Stats()
606 t, err := commit.Tree()
610 commentedHash := plumbing.NewHash(strings.ReplaceAll(
611 fileStats[0].Name, "/", "",
613 commit, err = repo.CommitObject(commentedHash)
617 comments := sgblog.ParseComments(sgblog.GetNote(repo, t, commentedHash))
618 if len(comments) == 0 {
621 commentN := strconv.Itoa(len(comments) - 1)
622 lines := strings.Split(comments[len(comments)-1], "\n")
623 from := strings.TrimPrefix(lines[0], "From: ")
624 date := strings.TrimPrefix(lines[1], "Date: ")
625 htmlized := make([]string, 0, len(lines))
626 htmlized = append(htmlized, "<pre>")
627 for _, l := range lines[2:] {
628 htmlized = append(htmlized, lineURLize(cfg.AtomBaseURL+cfg.URLPrefix, l))
630 htmlized = append(htmlized, "</pre>")
632 idHasher.Write([]byte("COMMENT"))
633 idHasher.Write(commit.Hash[:])
634 idHasher.Write([]byte(commentN))
635 feed.Entry = append(feed.Entry, &atom.Entry{
637 "Comment %s for \"%s\" by %s",
638 commentN, msgSplit(commit.Message)[0], from,
640 Author: &atom.Person{Name: from},
641 ID: "urn:uuid:" + bytes2uuid(idHasher.Sum(nil)),
644 Href: strings.Join([]string{
645 cfg.AtomBaseURL, cfg.URLPrefix, "/",
646 commit.Hash.String(), "#comment", commentN,
649 Published: atom.TimeStr(strings.Replace(date, " ", "T", -1)),
650 Updated: atom.TimeStr(strings.Replace(date, " ", "T", -1)),
653 Body: strings.Join(htmlized, "\n"),
657 data, err := xml.MarshalIndent(&feed, "", " ")
663 } else if sha1DigestRe.MatchString(pathInfo[1:]) {
664 commit, err := repo.CommitObject(plumbing.NewHash(pathInfo[1 : 1+sha1.Size*2]))
668 for _, data := range etagHashForWeb {
669 etagHash.Write([]byte(data))
671 etagHash.Write([]byte("ENTRY"))
672 etagHash.Write(commit.Hash[:])
673 atomCommentsURL := strings.Join([]string{
674 cfg.AtomBaseURL, cfg.URLPrefix, "/",
675 commit.Hash.String(), "/", AtomCommentsFeed,
677 commentsRaw := sgblog.GetNote(repo, commentsTree, commit.Hash)
678 etagHash.Write(commentsRaw)
679 topicsRaw := sgblog.GetNote(repo, topicsTree, commit.Hash)
680 etagHash.Write(topicsRaw)
681 if strings.HasSuffix(pathInfo, AtomCommentsFeed) {
682 etagHash.Write([]byte("ATOM COMMENTS"))
684 type Comment struct {
690 commentsRaw := sgblog.ParseComments(commentsRaw)
692 if len(commentsRaw) > PageEntries {
693 toSkip = len(commentsRaw) - PageEntries
695 comments := make([]Comment, 0, len(commentsRaw)-toSkip)
696 for i := len(commentsRaw) - 1; i >= toSkip; i-- {
697 lines := strings.Split(commentsRaw[i], "\n")
698 from := strings.TrimPrefix(lines[0], "From: ")
699 date := strings.TrimPrefix(lines[1], "Date: ")
700 comments = append(comments, Comment{
703 date: strings.Replace(date, " ", "T", 1),
707 idHasher, err := blake2b.New256(nil)
711 idHasher.Write([]byte("ATOM COMMENTS"))
712 idHasher.Write(commit.Hash[:])
714 Title: fmt.Sprintf("\"%s\" comments", msgSplit(commit.Message)[0]),
715 ID: "urn:uuid:" + bytes2uuid(idHasher.Sum(nil)),
716 Link: []atom.Link{{Rel: "self", Href: atomCommentsURL}},
717 Author: &atom.Person{Name: cfg.AtomAuthor},
719 if len(comments) > 0 {
720 feed.Updated = atom.TimeStr(comments[0].date)
722 feed.Updated = atom.Time(commit.Author.When)
724 for _, comment := range comments {
726 idHasher.Write([]byte("COMMENT"))
727 idHasher.Write(commit.Hash[:])
728 idHasher.Write([]byte(comment.n))
729 htmlized := make([]string, 0, len(comment.body))
730 htmlized = append(htmlized, "<pre>")
731 for _, l := range comment.body {
734 lineURLize(cfg.AtomBaseURL+cfg.URLPrefix, l),
737 htmlized = append(htmlized, "</pre>")
738 feed.Entry = append(feed.Entry, &atom.Entry{
739 Title: fmt.Sprintf("Comment %s by %s", comment.n, comment.from),
740 Author: &atom.Person{Name: comment.from},
741 ID: "urn:uuid:" + bytes2uuid(idHasher.Sum(nil)),
744 Href: strings.Join([]string{
747 commit.Hash.String(),
748 "#comment", comment.n,
751 Published: atom.TimeStr(
752 strings.Replace(comment.date, " ", "T", -1),
754 Updated: atom.TimeStr(
755 strings.Replace(comment.date, " ", "T", -1),
759 Body: strings.Join(htmlized, "\n"),
763 data, err := xml.MarshalIndent(&feed, "", " ")
770 notesRaw := sgblog.GetNote(repo, notesTree, commit.Hash)
771 etagHash.Write(notesRaw)
774 lines := msgSplit(commit.Message)
776 when := commit.Author.When.Format(sgblog.WhenFmt)
778 if len(commit.ParentHashes) > 0 {
779 parent = commit.ParentHashes[0].String()
781 commentsParsed := sgblog.ParseComments(commentsRaw)
782 comments := make([]CommentEntry, 0, len(commentsParsed))
783 for _, comment := range commentsParsed {
784 lines := strings.Split(comment, "\n")
785 comments = append(comments, CommentEntry{lines[:3], lines[3:]})
787 var notesLines []string
788 if len(notesRaw) > 0 {
789 notesLines = strings.Split(string(notesRaw), "\n")
792 tmpl := template.New("entry")
793 tmpl = tmpl.Funcs(template.FuncMap{"lineURLize": lineURLizeInTemplate})
794 tmpl = template.Must(tmpl.Parse(TmplHTMLEntry))
795 os.Stdout.Write([]byte(startHeader(etagHash, gzipWriter != nil)))
796 err = tmpl.Execute(out, struct {
802 AtomCommentsURL string
804 Commit *object.Commit
807 Comments []CommentEntry
810 Version: sgblog.Version,
813 TitleEscaped: url.PathEscape(fmt.Sprintf("Re: %s (%s)", title, commit.Hash)),
815 AtomCommentsURL: atomCommentsURL,
819 NoteLines: notesLines,
821 Topics: sgblog.ParseTopics(topicsRaw),
827 makeErr(errors.New("unknown URL action"))
829 out.Write([]byte("</body></html>\n"))
830 if gzipWriter != nil {
833 os.Stdout.Write(outBuf.Bytes())
837 os.Stdout.WriteString("Content-Type: application/atom+xml; charset=UTF-8\n")
838 os.Stdout.WriteString("ETag: " + etagString(etagHash) + "\n")
839 if gzipWriter != nil {
840 os.Stdout.WriteString("Content-Encoding: gzip\n")
843 os.Stdout.WriteString("\n")
844 os.Stdout.Write(outBuf.Bytes())