Skip to content

Commit

Permalink
write more test
Browse files Browse the repository at this point in the history
  • Loading branch information
sters committed Dec 12, 2019
1 parent 2a8459c commit e6132c3
Show file tree
Hide file tree
Showing 9 changed files with 629 additions and 185 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/sters/onstatic
go 1.13

require (
github.com/morikuni/failure v0.12.0
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413
gopkg.in/src-d/go-billy.v4 v4.3.2
gopkg.in/src-d/go-git.v4 v4.13.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/morikuni/failure v0.12.0 h1:bTddS2uYzvhSpVuFG5jgG4x4hlIM88C+0Rowz6NtyZ0=
github.com/morikuni/failure v0.12.0/go.mod h1:+IjvKCz9B/D4BQrTzYLwERdWyMkGJdu+q5gri9dWecg=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
6 changes: 4 additions & 2 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package http
import (
"net"
"net/http"

"github.com/morikuni/failure"
)

// Server for http
Expand All @@ -17,12 +19,12 @@ type Server struct {
func (s *Server) Run() error {
ln, err := net.Listen("tcp", "127.0.0.1:"+s.port)
if err != nil {
return err
return failure.Wrap(err)
}
s.ln = ln

if err := s.http.Serve(s.ln); err != nil && err != http.ErrServerClosed {
return err
return failure.Wrap(err)
}

return nil
Expand Down
79 changes: 40 additions & 39 deletions onstatic/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -104,67 +103,69 @@ func handleAll(res http.ResponseWriter, req *http.Request) {
}

cleanedPath := path.Clean(req.URL.Path)
if cleanedPath[0] == '/' {
cleanedPath = cleanedPath[1:]
}

pathes := strings.Split(cleanedPath, "/")
if len(pathes) == 0 {
if len(pathes) <= 1 {
res.WriteHeader(http.StatusNotFound)
return
}

var ignoreContains = []string{
"/.", "/internal", "/bin/",
}
for _, c := range ignoreContains {
if strings.Contains(cleanedPath, c) {
res.WriteHeader(http.StatusNotFound)
return
}
}

var ignoreSuffix = []string{
"/LICENSE", "/Makefile", "/README.md", "/README", "/id_rsa",
".bin", ".exe", ".dll",
".zip", ".gz", ".tar", ".db",
".json", ".conf",
}
for _, s := range ignoreSuffix {
if strings.HasSuffix(cleanedPath, s) {
res.WriteHeader(http.StatusNotFound)
return
}
}

requestFilePath := strings.Replace(cleanedPath, "/"+pathes[0], "", 1)
if len(pathes) == 1 {
requestFilePath = "index.html"
}

fullpath := filepath.Clean(filepath.Join(getRepositoryDirectoryPath(pathes[0]), requestFilePath))
if strings.Contains(fullpath, "/.") ||
!strings.HasPrefix(fullpath, getRepositoriesDir()) ||
strings.Replace(fullpath, getRepositoriesDir(), "", 1) == "" {
if hasIgnoreContents(cleanedPath) || hasIgnoreSuffix(cleanedPath) {
res.WriteHeader(http.StatusNotFound)
return
}

if s, err := os.Stat(fullpath); err != nil || s.IsDir() {
requestFilePath := strings.Replace(cleanedPath, pathes[0], "", 1)
fs := fsNew(getRepositoryDirectoryPath(pathes[0]))
if s, err := fs.Stat(requestFilePath); err != nil || s.IsDir() {
res.WriteHeader(http.StatusNotFound)
return
}

f, err := os.Open(fullpath)
f, err := fs.Open(requestFilePath)
if err != nil {
res.WriteHeader(http.StatusNotFound)
return
}
defer f.Close()

res.Header().Set("Content-Type", guessContentType(requestFilePath))
res.WriteHeader(http.StatusOK)
if _, err := io.Copy(res, f); err != nil {
log.Println(err)
res.WriteHeader(http.StatusNotFound)
return
}
}

res.Header().Add("Content-Type", guessContentType(fullpath))
res.WriteHeader(http.StatusOK)
func hasIgnoreContents(p string) bool {
var ignoreContains = []string{
"/.", "/internal", "/bin/",
}
for _, c := range ignoreContains {
if strings.Contains(p, c) {
return true
}
}
return false
}

func hasIgnoreSuffix(p string) bool {
var ignoreSuffix = []string{
"/LICENSE", "/Makefile", "/README.md", "/README", "/id_rsa",
".bin", ".exe", ".dll",
".zip", ".gz", ".tar", ".db",
".json", ".conf",
}
for _, s := range ignoreSuffix {
if strings.HasSuffix(p, s) {
return true
}
}

return false
}

func guessContentType(path string) string {
Expand Down
Loading

0 comments on commit e6132c3

Please sign in to comment.