]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/http.go
BLAKE3 instead of BLAKE2
[sgblog.git] / cmd / sgblog / http.go
index 9f39171c4c962943afff958ccb486b8d54ba6999..fd5f7b448ec88bcdb7dc8a15d5b51e01a5575b5a 100644 (file)
@@ -1,6 +1,6 @@
 /*
 SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
@@ -36,13 +36,15 @@ import (
        "strconv"
        "strings"
        "text/template"
+       "time"
 
        "github.com/go-git/go-git/v5"
        "github.com/go-git/go-git/v5/plumbing"
        "github.com/go-git/go-git/v5/plumbing/object"
+       "github.com/vorlif/spreak"
        "go.stargrave.org/sgblog"
        "go.stargrave.org/sgblog/cmd/sgblog/atom"
-       "golang.org/x/crypto/blake2b"
+       "lukechampine.com/blake3"
 )
 
 const (
@@ -58,6 +60,9 @@ var (
                "gopher": {},
                "http":   {},
                "https":  {},
+               "irc":    {},
+               "ircs":   {},
+               "news":   {},
                "telnet": {},
        }
 
@@ -124,7 +129,7 @@ func lineURLizeInTemplate(urlPrefix, line interface{}) string {
 
 func startHeader(etag hash.Hash, gziped bool) string {
        lines := []string{
-               "Content-Type: text/html; charset=UTF-8",
+               "Content-Type: text/html; charset=utf-8",
                "ETag: " + etagString(etag),
        }
        if gziped {
@@ -137,7 +142,7 @@ func startHeader(etag hash.Hash, gziped bool) string {
 
 func makeErr(err error, status int) {
        fmt.Println("Status:", status)
-       fmt.Print("Content-Type: text/plain; charset=UTF-8\n\n")
+       fmt.Print("Content-Type: text/plain; charset=utf-8\n\n")
        fmt.Println(err)
        log.Fatalln(err)
 }
@@ -170,6 +175,7 @@ func serveHTTP() {
        if err != nil {
                log.Fatalln(err)
        }
+       initLocalizer(cfg.Lang)
 
        pathInfo := os.Getenv("PATH_INFO")
        if len(pathInfo) == 0 {
@@ -180,10 +186,7 @@ func serveHTTP() {
                makeErr(err, http.StatusBadRequest)
        }
 
-       etagHash, err := blake2b.New256(nil)
-       if err != nil {
-               panic(err)
-       }
+       etagHash := blake3.New(32, nil)
        for _, s := range []string{
                "SGBLOG",
                sgblog.Version,
@@ -206,9 +209,7 @@ func serveHTTP() {
                cfg.CommentsNotesRef,
                cfg.CommentsEmail,
        }
-       for _, gitURL := range cfg.GitURLs {
-               etagHashForWeb = append(etagHashForWeb, gitURL)
-       }
+       etagHashForWeb = append(etagHashForWeb, cfg.GitURLs...)
 
        headHash, err := initRepo(cfg)
        if err != nil {
@@ -345,6 +346,7 @@ func serveHTTP() {
                }
                os.Stdout.Write([]byte(startHeader(etagHash, gzipWriter != nil)))
                err = TmplHTMLIndex.Execute(out, struct {
+                       T                *spreak.Localizer
                        Version          string
                        Cfg              *Cfg
                        Topic            string
@@ -359,6 +361,7 @@ func serveHTTP() {
                        LogEnded         bool
                        Entries          []TableEntry
                }{
+                       T:                localizer,
                        Version:          sgblog.Version,
                        Cfg:              cfg,
                        Topic:            topic,
@@ -376,6 +379,38 @@ func serveHTTP() {
                if err != nil {
                        makeErr(err, http.StatusInternalServerError)
                }
+       } else if pathInfo == "/twtxt.txt" {
+               commit, err := repo.CommitObject(*headHash)
+               if err != nil {
+                       makeErr(err, http.StatusInternalServerError)
+               }
+               etagHash.Write([]byte("TWTXT POSTS"))
+               etagHash.Write(commit.Hash[:])
+               checkETag(etagHash)
+               repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
+               if err != nil {
+                       makeErr(err, http.StatusInternalServerError)
+               }
+               for i := 0; i < PageEntries; i++ {
+                       commit, err = repoLog.Next()
+                       if err != nil {
+                               break
+                       }
+                       fmt.Fprintf(
+                               out, "%s\t%s\n",
+                               commit.Author.When.Format(time.RFC3339),
+                               msgSplit(commit.Message)[0],
+                       )
+               }
+               os.Stdout.WriteString("Content-Type: text/plain; charset=utf-8\n")
+               os.Stdout.WriteString("ETag: " + etagString(etagHash) + "\n")
+               if gzipWriter != nil {
+                       os.Stdout.WriteString("Content-Encoding: gzip\n")
+                       gzipWriter.Close()
+               }
+               os.Stdout.WriteString("\n")
+               os.Stdout.Write(outBuf.Bytes())
+               return
        } else if pathInfo == "/"+AtomPostsFeed {
                commit, err := repo.CommitObject(*headHash)
                if err != nil {
@@ -397,10 +432,7 @@ func serveHTTP() {
                } else {
                        title = fmt.Sprintf("%s (topic: %s)", cfg.Title, topic)
                }
-               idHasher, err := blake2b.New256(nil)
-               if err != nil {
-                       panic(err)
-               }
+               idHasher := blake3.New(32, nil)
                idHasher.Write([]byte("ATOM POSTS"))
                idHasher.Write([]byte(cfg.AtomId))
                idHasher.Write([]byte(topic))
@@ -441,7 +473,9 @@ func serveHTTP() {
                        }
                        lines := msgSplit(commit.Message)
                        var categories []atom.Category
-                       for _, topic := range sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)) {
+                       for _, topic := range sgblog.ParseTopics(sgblog.GetNote(
+                               repo, topicsTree, commit.Hash,
+                       )) {
                                categories = append(categories, atom.Category{Term: topic})
                        }
                        htmlized := make([]string, 0, len(lines))
@@ -471,6 +505,7 @@ func serveHTTP() {
                if err != nil {
                        makeErr(err, http.StatusInternalServerError)
                }
+               out.Write([]byte(xml.Header))
                out.Write(data)
                goto AtomFinish
        } else if pathInfo == "/"+AtomCommentsFeed {
@@ -481,10 +516,7 @@ func serveHTTP() {
                etagHash.Write([]byte("ATOM COMMENTS"))
                etagHash.Write(commit.Hash[:])
                checkETag(etagHash)
-               idHasher, err := blake2b.New256(nil)
-               if err != nil {
-                       panic(err)
-               }
+               idHasher := blake3.New(32, nil)
                idHasher.Write([]byte("ATOM COMMENTS"))
                idHasher.Write([]byte(cfg.AtomId))
                feed := atom.Feed{
@@ -565,6 +597,7 @@ func serveHTTP() {
                if err != nil {
                        makeErr(err, http.StatusInternalServerError)
                }
+               out.Write([]byte(xml.Header))
                out.Write(data)
                goto AtomFinish
        } else if sha1DigestRe.MatchString(pathInfo[1:]) {
@@ -611,10 +644,7 @@ func serveHTTP() {
                                        body: lines[3:],
                                })
                        }
-                       idHasher, err := blake2b.New256(nil)
-                       if err != nil {
-                               panic(err)
-                       }
+                       idHasher := blake3.New(32, nil)
                        idHasher.Write([]byte("ATOM COMMENTS"))
                        idHasher.Write(commit.Hash[:])
                        feed := atom.Feed{
@@ -671,6 +701,7 @@ func serveHTTP() {
                        if err != nil {
                                makeErr(err, http.StatusInternalServerError)
                        }
+                       out.Write([]byte(xml.Header))
                        out.Write(data)
                        goto AtomFinish
                }
@@ -698,6 +729,7 @@ func serveHTTP() {
 
                os.Stdout.Write([]byte(startHeader(etagHash, gzipWriter != nil)))
                err = TmplHTMLEntry.Execute(out, struct {
+                       T               *spreak.Localizer
                        Version         string
                        Cfg             *Cfg
                        Title           string
@@ -711,6 +743,7 @@ func serveHTTP() {
                        Comments        []CommentEntry
                        Topics          []string
                }{
+                       T:               localizer,
                        Version:         sgblog.Version,
                        Cfg:             cfg,
                        Title:           title,
@@ -738,7 +771,7 @@ func serveHTTP() {
        return
 
 AtomFinish:
-       os.Stdout.WriteString("Content-Type: application/atom+xml; charset=UTF-8\n")
+       os.Stdout.WriteString("Content-Type: application/atom+xml; charset=utf-8\n")
        os.Stdout.WriteString("ETag: " + etagString(etagHash) + "\n")
        if gzipWriter != nil {
                os.Stdout.WriteString("Content-Encoding: gzip\n")