Browse Source

Whoops, .gitignore was avoiding these

master
Teran McKinney 2 years ago
parent
commit
d2bea2e950
  1. 1
      .gitignore
  2. 14
      cmd/burnpaste/ishex.go
  3. 36
      cmd/burnpaste/ishex_test.go
  4. 54
      cmd/burnpaste/main.go

1
.gitignore

@ -1 +0,0 @@
burnpaste

14
cmd/burnpaste/ishex.go

@ -0,0 +1,14 @@
package main
import (
"strings"
)
func isHex(hexString string) bool {
for _, character := range hexString {
if !strings.Contains("abcdef01234567890", string(character)) {
return false
}
}
return true
}

36
cmd/burnpaste/ishex_test.go

@ -0,0 +1,36 @@
package main
import (
"log"
"testing"
)
func TestIsHex(t *testing.T) {
hex := "aaaaa"
if isHex(hex) {
log.Printf("%s is indeed hex.", hex)
} else {
t.Errorf("%s is hex but we think it is not.", hex)
}
hex = "01234567890abcdef"
if isHex(hex) {
log.Printf("%s is indeed hex.", hex)
} else {
t.Errorf("%s is hex but we think it is not.", hex)
}
hex = "01234567890abcdefg"
if isHex(hex) == true {
t.Errorf("%s is not hex but we think it is.", hex)
} else {
log.Printf("%s is indeed not hex.", hex)
}
hex = "."
if isHex(hex) == true {
t.Errorf("%s is not hex but we think it is.", hex)
} else {
log.Printf("%s is indeed not hex.", hex)
}
}

54
cmd/burnpaste/main.go

@ -0,0 +1,54 @@
// burnpaste...
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/teran-mckinney/burnpaste"
)
func main() {
http.HandleFunc("/read/", func(w http.ResponseWriter, r *http.Request) {
var err error
path_parts := strings.Split(r.URL.Path, "/")
path := path_parts[len(path_parts)-1]
if len(path) != 64 {
log.Println("Error: Path too short.")
goto error
}
// This will filter out ".." so should make this safe.
if isHex(path) == false {
log.Println("Error: Path not hex.")
goto error
}
http.ServeFile(w, r, path)
err = os.Remove(path)
if err == nil {
log.Printf("read: %s\n", path)
}
return
error:
http.Error(w, "paste id must be 64 hex characters.", 400)
return
})
http.HandleFunc("/write", func(w http.ResponseWriter, r *http.Request) {
data := []byte(r.FormValue("data"))
path := burnpaste.Hash(data)
ioutil.WriteFile(path, []byte(data), 0600)
fmt.Fprintf(w, path)
log.Printf("write: %s\n", path)
})
var port = ":2323"
if len(os.Args) == 2 {
port = os.Args[1]
}
log.Fatal(http.ListenAndServe(port, nil))
}
Loading…
Cancel
Save