/*
SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2022 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
"html"
"io"
"log"
+ "net/http"
"net/url"
"os"
"strconv"
"strings"
"text/template"
+ "time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"gopher": {},
"http": {},
"https": {},
+ "irc": {},
+ "ircs": {},
+ "news": {},
"telnet": {},
}
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 {
return strings.Join(lines, "\n")
}
-func makeErr(err error) {
- fmt.Print("Content-Type: text/plain; charset=UTF-8\n\n")
+func makeErr(err error, status int) {
+ fmt.Println("Status:", status)
+ fmt.Print("Content-Type: text/plain; charset=utf-8\n\n")
fmt.Println(err)
log.Fatalln(err)
}
log.Fatalln(err)
}
- pathInfo, exists := os.LookupEnv("PATH_INFO")
- if !exists {
+ pathInfo := os.Getenv("PATH_INFO")
+ if len(pathInfo) == 0 {
pathInfo = "/"
}
queryValues, err := url.ParseQuery(os.Getenv("QUERY_STRING"))
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusBadRequest)
}
etagHash, err := blake2b.New256(nil)
headHash, err := initRepo(cfg)
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
if notes, err := repo.Notes(); err == nil {
if offsetRaw, exists := queryValues["offset"]; exists {
offset, err = strconv.Atoi(offsetRaw[0])
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusBadRequest)
}
}
repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
topicsCache, err := getTopicsCache(cfg, repoLog)
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
commitN := 0
topic = t[0]
hashes := topicsCache[topic]
if hashes == nil {
- makeErr(errors.New("no posts with that topic"))
+ makeErr(errors.New("no posts with that topic"), http.StatusBadRequest)
}
if len(hashes) > offset {
hashes = hashes[offset:]
Entries: entries,
})
if err != nil {
- makeErr(err)
+ 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 {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
var topic string
repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
var commits CommitIterNext
if topic == "" {
} else {
topicsCache, err := getTopicsCache(cfg, repoLog)
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
hashes := topicsCache[topic]
if hashes == nil {
- makeErr(errors.New("no posts with that topic"))
+ makeErr(errors.New("no posts with that topic"), http.StatusBadRequest)
}
commits = &HashesIter{hashes}
}
}
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))
}
data, err := xml.MarshalIndent(&feed, "", " ")
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
+ out.Write([]byte(xml.Header))
out.Write(data)
goto AtomFinish
} else if pathInfo == "/"+AtomCommentsFeed {
commit, err := repo.CommitObject(commentsRef.Hash())
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
etagHash.Write([]byte("ATOM COMMENTS"))
etagHash.Write(commit.Hash[:])
}
repoLog, err := repo.Log(&git.LogOptions{From: commentsRef.Hash()})
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
for i := 0; i < PageEntries; i++ {
commit, err = repoLog.Next()
}
fileStats, err := commit.Stats()
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
t, err := commit.Tree()
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
commentedHash := plumbing.NewHash(strings.ReplaceAll(
fileStats[0].Name, "/", "",
}
data, err := xml.MarshalIndent(&feed, "", " ")
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
+ out.Write([]byte(xml.Header))
out.Write(data)
goto AtomFinish
} else if sha1DigestRe.MatchString(pathInfo[1:]) {
commit, err := repo.CommitObject(plumbing.NewHash(pathInfo[1 : 1+sha1.Size*2]))
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusBadRequest)
}
for _, data := range etagHashForWeb {
etagHash.Write([]byte(data))
}
data, err := xml.MarshalIndent(&feed, "", " ")
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
+ out.Write([]byte(xml.Header))
out.Write(data)
goto AtomFinish
}
Topics: sgblog.ParseTopics(topicsRaw),
})
if err != nil {
- makeErr(err)
+ makeErr(err, http.StatusInternalServerError)
}
} else {
- makeErr(errors.New("unknown URL action"))
+ makeErr(errors.New("unknown URL action"), http.StatusNotFound)
}
out.Write([]byte("</body></html>\n"))
if gzipWriter != nil {
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")