aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-01-23 18:43:37 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-01-23 18:43:37 +0100
commitef736c377953405fbbfa0c22b9a65a0abfe5edf3 (patch)
tree39ca25a56b29bb336ea4bb83dbe718c8b8d2b6ba
parent93a5ed54ad9a0bf53f61554a890c4d5ceb29ac28 (diff)
Stop using m4(1) macros
-rw-r--r--Makefile9
-rw-r--r--macros.m44
-rw-r--r--mpaste.go38
3 files changed, 31 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index e038050..8f89b33 100644
--- a/Makefile
+++ b/Makefile
@@ -4,16 +4,13 @@ MANDIR = /usr/share/man/man1
target = mpaste
all: ${target}
-${target}: macros.m4 mpaste.go
- m4 macros.m4 mpaste.go >tmp.go
- go build tmp.go
- mv tmp ${target}
- rm tmp.go
+${target}: mpaste.go
+ go build
docs:
>/dev/null command -v gzip && gzip -c9 mpaste.1 >${MANDIR}/mpaste.1.gz || \
cp mpaste.1 ${MANDIR}
clean:
- rm -rf ${target} tmp.go counter files/
+ rm -rf ${target} counter files/
.PHONY: clean
diff --git a/macros.m4 b/macros.m4
deleted file mode 100644
index b898e1f..0000000
--- a/macros.m4
+++ /dev/null
@@ -1,4 +0,0 @@
-define(`m4_len', defn(`len'))
-undefine(`len')
-define(`WRITEHEADER',
- `w.WriteHeader($1); ifelse(eval(m4_len($2) == 0), 0, fmt.Fprintln(w, $2);) return;')
diff --git a/mpaste.go b/mpaste.go
index a43381a..16a1cef 100644
--- a/mpaste.go
+++ b/mpaste.go
@@ -50,6 +50,15 @@ func die(e interface{}) {
os.Exit(1)
}
+func writeHeader(w http.ResponseWriter, h int, s string) {
+ w.WriteHeader(h)
+ if s == "" {
+ fmt.Fprintln(w, http.StatusText(h))
+ } else {
+ fmt.Fprintln(w, s)
+ }
+}
+
func removeExt(s string) string {
return strings.TrimSuffix(s, path.Ext(s))
}
@@ -134,12 +143,14 @@ func syntaxHighlighting(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadFile(filePrefix + removeExt(r.URL.Path[1:]))
if err != nil {
w.Header().Set("Content-Type", "text/plain")
- WRITEHEADER(http.StatusNotFound, "404 page not found")
+ writeHeader(w, http.StatusNotFound, "")
+ return
}
iterator, err := lexer.Tokenise(nil, string(data))
if err != nil {
- WRITEHEADER(http.StatusInternalServerError, "Failed to tokenize output")
+ writeHeader(w, http.StatusInternalServerError, "Failed to tokenize output")
+ return
}
tw, err := strconv.Atoi(r.URL.Query().Get("tabs"))
@@ -149,7 +160,8 @@ func syntaxHighlighting(w http.ResponseWriter, r *http.Request) {
formatter := html.New(html.Standalone(true), html.WithClasses(true),
html.WithLineNumbers(true), html.LineNumbersInTable(true), html.TabWidth(tw))
if err := formatter.Format(w, style, iterator); err != nil {
- WRITEHEADER(http.StatusInternalServerError, "Failed to format output")
+ writeHeader(w, http.StatusInternalServerError, "Failed to format output")
+ return
}
}
@@ -160,7 +172,8 @@ func endpoint(w http.ResponseWriter, r *http.Request) {
case urlHomepage:
http.ServeFile(w, r, indexFile)
case urlInvalid:
- WRITEHEADER(http.StatusNotFound, "404 page not found")
+ writeHeader(w, http.StatusNotFound, "")
+ return
case urlSyntax:
w.Header().Set("Content-Type", "text/html")
syntaxHighlighting(w, r)
@@ -170,13 +183,15 @@ func endpoint(w http.ResponseWriter, r *http.Request) {
}
case http.MethodPost:
if secretKey != "" && !validateToken(r) {
- WRITEHEADER(http.StatusForbidden, "Invalid API key")
+ writeHeader(w, http.StatusForbidden, "")
+ return
}
file, _, err := r.FormFile("data")
defer file.Close()
if err != nil {
- WRITEHEADER(http.StatusInternalServerError, "Failed to parse form")
+ writeHeader(w, http.StatusInternalServerError, "Failed to parse form")
+ return
}
mutex.Lock()
@@ -186,15 +201,18 @@ func endpoint(w http.ResponseWriter, r *http.Request) {
nfile, err := os.Create(fname)
defer nfile.Close()
if err != nil {
- WRITEHEADER(http.StatusInternalServerError, "Failed to create file")
+ writeHeader(w, http.StatusInternalServerError, "Failed to create file")
+ return
}
if _, err = io.Copy(nfile, file); err != nil {
- WRITEHEADER(http.StatusInternalServerError, "Failed to write file")
+ writeHeader(w, http.StatusInternalServerError, "Failed to write file")
+ return
}
if err = os.WriteFile(counterFile, []byte(strconv.Itoa(counter+1)), 0644); err != nil {
- WRITEHEADER(http.StatusInternalServerError, "Failed to update counter")
+ writeHeader(w, http.StatusInternalServerError, "Failed to update counter")
+ return
}
w.WriteHeader(http.StatusOK)
@@ -202,7 +220,7 @@ func endpoint(w http.ResponseWriter, r *http.Request) {
counter++
default:
- WRITEHEADER(http.StatusMethodNotAllowed, "Only GET and POST requests are supported")
+ writeHeader(w, http.StatusMethodNotAllowed, "Only GET and POST requests are supported")
}
}