Removed unused functions

This commit is contained in:
Seednode 2022-09-09 14:14:54 -05:00
parent 51ab23a234
commit 4a484eed83
3 changed files with 6 additions and 121 deletions

View File

@ -5,65 +5,11 @@ Copyright © 2022 Seednode <seednode@seedno.de>
package cmd
import (
"bufio"
"os"
"strings"
)
type Exit struct{ Code int }
func Chunks(s string, chunkSize int) []string {
if len(s) == 0 {
return nil
}
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string = make([]string, 0, (len(s)-1)/chunkSize+1)
currentLen := 0
currentStart := 0
for i := range s {
if currentLen == chunkSize {
chunks = append(chunks, s[currentStart:i])
currentLen = 0
currentStart = i
}
currentLen++
}
chunks = append(chunks, s[currentStart:])
return chunks
}
func CollectOutputs(output chan string, outputs chan<- []string) {
o := []string{}
for r := range output {
o = append(o, r)
}
outputs <- o
close(outputs)
}
func FirstN(s string, n int) string {
i := 0
for j := range s {
if i == n {
return s[:j]
}
i++
}
return s
}
func HandleExit() {
if e := recover(); e != nil {
if exit, ok := e.(Exit); ok == true {
@ -72,31 +18,3 @@ func HandleExit() {
panic(e)
}
}
func ScanFile(dbfile string) (func(), *bufio.Scanner, error) {
readFile, err := os.Open(dbfile)
if err != nil {
return func() {}, nil, err
}
fileScanner := bufio.NewScanner(readFile)
buffer := make([]byte, 0, 64*1024)
fileScanner.Buffer(buffer, 1024*1024)
return func() { _ = readFile.Close() }, fileScanner, nil
}
func Strip(s string) string {
var result strings.Builder
for i := 0; i < len(s); i++ {
b := s[i]
if ('a' <= b && b <= 'z') ||
('A' <= b && b <= 'Z') ||
('0' <= b && b <= '9') {
result.WriteByte(b)
}
}
return result.String()
}

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
)
var Version = "0.1.0"
var Version = "0.2.0"
func init() {
rootCmd.AddCommand(versionCmd)

View File

@ -6,7 +6,6 @@ package cmd
import (
"errors"
"fmt"
"io"
"log"
"net/http"
@ -48,35 +47,9 @@ func generatePageHtml(w http.ResponseWriter, paths []string) error {
return nil
}
func statusNotFound(w http.ResponseWriter, filePath string) error {
fmt.Println("Client requested non-existent file " + filePath + ".")
func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) error {
request := r.RequestURI
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "txt/plain")
htmlBody := "File not found."
_, err := io.WriteString(w, htmlBody)
if err != nil {
return err
}
return nil
}
func statusForbidden(w http.ResponseWriter, filePath string) error {
fmt.Println("Client requested forbidden file " + filePath + ".")
w.WriteHeader(http.StatusForbidden)
w.Header().Set("Content-Type", "txt/plain")
htmlBody := "Access denied."
_, err := io.WriteString(w, htmlBody)
if err != nil {
return err
}
return nil
}
func serveStaticFile(w http.ResponseWriter, request string, paths []string) error {
filePath, err := url.QueryUnescape(request)
if err != nil {
return err
@ -91,20 +64,14 @@ func serveStaticFile(w http.ResponseWriter, request string, paths []string) erro
}
if matchesPrefix == false {
err := statusNotFound(w, filePath)
if err != nil {
return err
}
http.NotFound(w, &r)
return nil
}
_, err = os.Stat(filePath)
if errors.Is(err, os.ErrNotExist) {
err := statusNotFound(w, filePath)
if err != nil {
return err
}
http.NotFound(w, &r)
return nil
} else if !errors.Is(err, os.ErrNotExist) && err != nil {
@ -129,7 +96,7 @@ func servePageHandler(paths []string) http.HandlerFunc {
log.Fatal(err)
}
} else {
err := serveStaticFile(w, r.RequestURI, paths)
err := serveStaticFile(w, *r, paths)
if err != nil {
log.Fatal(err)
}