Skip to content
This repository was archived by the owner on Mar 23, 2020. It is now read-only.

Commit fdc106b

Browse files
committed
fix index.html mimetype bug,add overwrite checkbox;update build.sh;
1 parent cf35a68 commit fdc106b

2 files changed

Lines changed: 98 additions & 26 deletions

File tree

build.sh

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,107 @@ GOOS_GOARCH_array=( aix_ppc64 android_386 android_amd64 android_arm android_arm6
1212
appname=`\basename $(\pwd)`
1313
outputpath=./bin
1414
package=''
15+
declare -i verbose=0
1516

1617
until [ $# -eq 0 ]; do
1718
case "$1" in
1819
--help|-h)
19-
echo "usage: bash ${0##*/} [--package|-p <package-name>] [--name|-n <app-name>] [--output|-o <output-path>]"
20+
printf "usage: bash ${0##*/} [--verbose|-v] [--package|-p <package-name>] [--name|-n <app-name>] [--output|-o <output-path>]\n"
2021
exit 0
2122
;;
23+
--verbose|-v)
24+
verbose=1
25+
;;
2226
--package|-n)
2327
shift
2428
if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then
25-
echo "ERROR: package argument error: $1"
29+
printf "ERROR: package argument error: $1\n"
2630
exit 1
2731
fi
2832
package=$1
2933
;;
3034
--name|-n)
3135
shift
3236
if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then
33-
echo "ERROR: name argument error: $1"
37+
printf "ERROR: name argument error: $1\n"
3438
exit 1
3539
fi
3640
appname=$1
3741
;;
3842
--output|-o)
3943
shift
4044
if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then
41-
echo "ERROR: output argument error: $1"
45+
printf "ERROR: output argument error: $1\n"
4246
exit 1
4347
fi
4448
outputpath=$1
4549
;;
4650
*)
47-
echo "ERROR: arguments error. please run \"bash ${0##*/} --help\" to get usage"
51+
printf "ERROR: arguments error. please run \"bash ${0##*/} --help\" to get usage\n"
4852
exit 1
4953
;;
5054
esac
5155
shift
5256
done
5357

5458
if [ -z "$appname" ]; then
55-
echo "ERROR: please input an app-name by --name"
59+
printf "ERROR: please input an app-name by --name\n"
5660
exit 2
5761
fi
5862

5963
if [ -z "$outputpath" ]; then
60-
echo "ERROR: please input an output-path by --output"
64+
printf "ERROR: please input an output-path by --output\n"
6165
exit 2
6266
fi
6367

64-
mkdir -p $outputpath
68+
mkdir -p "$outputpath"
6569
if [ $? -ne 0 ]; then
66-
echo "ERROR: creating $outputpath failed: $?"
70+
printf "ERROR: create $outputpath failed: $?\n"
6771
exit 2
6872
fi
6973

7074
cat << EOF
7175
Package: $package
7276
AppName: $appname
7377
Output: $outputpath
74-
7578
EOF
7679

80+
if [ ! -f go.mod ]; then
81+
printf "\nCreating go.mod ...\n"
82+
go mod init $appname
83+
if [ $? -ne 0 ]; then
84+
printf "Error: $?\n"
85+
exit 2
86+
fi
87+
fi
88+
7789
declare -i count=${#GOOS_GOARCH_array[*]}
78-
declare -i num=1
90+
declare -i num=0
7991
for target in "${GOOS_GOARCH_array[@]}"; do
92+
let num+=1
8093
output=$outputpath/${appname}_${target}
81-
if [ "${target%%_*}"="windows" ]; then
82-
output="$output.exe"
83-
fi
84-
echo "($num/$count) building $output ..."
85-
env GOOS=${target%%_*} GOARCH=${target##*_} go build -o $output $package
94+
mygoos=${target%%_*}
95+
mygoarch=${target##*_}
96+
myverbose=''
97+
mycgo=''
98+
if [ $verbose -eq 1 ]; then
99+
myverbose='-v'
100+
fi
101+
case "$mygoos" in
102+
windows)
103+
output="$output.exe"
104+
;;
105+
android)
106+
mycgo='CGO_ENABLED=1'
107+
;;
108+
esac
109+
printf "\n($num/$count) Building $output ...\n"
110+
env GOOS=$mygoos GOARCH=$mygoarch `printf "$mycgo"` go build $myverbose -ldflags "-s -w" -o $output $package
86111
file $output
87-
let num+=1
88-
done
112+
done
113+
114+
if [ -t 1 ]; then
115+
printf "\nDone. Press any key to exit..."
116+
read -n1
117+
printf "\n"
118+
fi

main.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path/filepath"
1212
"strings"
1313
"sync"
14+
"time"
1415

1516
"github.com/fatih/color"
1617
"github.com/valyala/fasthttp"
@@ -19,7 +20,7 @@ import (
1920

2021
const (
2122
// Version information
22-
Version = "SimpleHttpServer v1.3-beta.1"
23+
Version = "SimpleHttpServer v1.3-beta.2"
2324
// HTTPProxy returns HTTP_PROXY
2425
HTTPProxy = "HTTP_PROXY"
2526
// HTTPSProxy returns HTTPS_PROXY
@@ -289,6 +290,10 @@ func main() {
289290
fs.PathNotFound = func(ctx *fasthttp.RequestCtx) {
290291
fallbackpath := filepath.Join(v, config.Fallback)
291292
if _, err := os.Stat(fallbackpath); err == nil {
293+
mimeType := staticFileGetMimeType(filepath.Ext(fallbackpath))
294+
if len(mimeType) > 0 {
295+
ctx.SetContentType(mimeType)
296+
}
292297
ctx.SendFile(fallbackpath)
293298
} else {
294299
ctx.Error(fasthttp.StatusMessage(fasthttp.StatusNotFound), fasthttp.StatusNotFound)
@@ -341,6 +346,11 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
341346
}
342347

343348
// router
349+
// log print
350+
if config.Verbose {
351+
statusCode := ctx.Response.StatusCode()
352+
go logInfo(statusCode, "%d | %s | %s | %s\n", statusCode, ctx.RemoteIP(), ctx.Method(), ctx.Path())
353+
}
344354
switch string(ctx.Method()) {
345355
case "POST":
346356
switch string(ctx.Path()) {
@@ -359,12 +369,6 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
359369
default:
360370
ctx.Error(fasthttp.StatusMessage(fasthttp.StatusNotFound), fasthttp.StatusNotFound)
361371
}
362-
363-
// log print
364-
if config.Verbose {
365-
statusCode := ctx.Response.StatusCode()
366-
go logInfo(statusCode, "%d | %s | %s | %s\n", statusCode, ctx.RemoteIP(), ctx.Method(), ctx.Path())
367-
}
368372
}
369373

370374
func fsHandler(ctx *fasthttp.RequestCtx) {
@@ -417,6 +421,10 @@ func dirHandler(path string, ctx *fasthttp.RequestCtx) bool {
417421
indexfile := filepath.Join(localpath, v)
418422
if fi, err := os.Stat(indexfile); err == nil {
419423
if !fi.IsDir() {
424+
mimeType := staticFileGetMimeType(filepath.Ext(indexfile))
425+
if len(mimeType) > 0 {
426+
ctx.SetContentType(mimeType)
427+
}
420428
ctx.SendFile(indexfile)
421429
return true
422430
}
@@ -446,6 +454,7 @@ func dirHandler(path string, ctx *fasthttp.RequestCtx) bool {
446454
uploadhtml = fmt.Sprintf(`<form enctype="multipart/form-data" action="/upload" method="post">`+
447455
`<input name="files[]" type="file" multiple>`+
448456
`<input type="submit" value="Upload" onclick="this.disabled=true;this.value='Sending...';"/>`+
457+
`<input type="checkbox" name="o" value="true">Overwrite`+
449458
`<input type="hidden" id="r" name="r" value="%s">`+
450459
`<input type="hidden" id="p" name="p" value="%s"></form>`,
451460
ctx.RequestURI(), localpath)
@@ -488,6 +497,7 @@ func uploadHandle(ctx *fasthttp.RequestCtx) {
488497
}
489498

490499
var uri, path string
500+
isOverwrite := false
491501
if r, ok := form.Value["r"]; ok && len(r) == 1 {
492502
uri = r[0]
493503
}
@@ -502,11 +512,36 @@ func uploadHandle(ctx *fasthttp.RequestCtx) {
502512
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
503513
return
504514
}
515+
if o, ok := form.Value["o"]; ok && len(o) == 1 {
516+
isOverwrite = o[0] == "true"
517+
}
505518

506519
for _, v := range form.File {
507520
for _, header := range v {
508521
fn := filepath.Join(path, header.Filename)
509-
fasthttp.SaveMultipartFile(header, fn)
522+
if !isOverwrite && fileOrDirIsExist(fn) {
523+
for index := 1; index <= MaxInt; index++ {
524+
ext := filepath.Ext(fn)
525+
newfn := fmt.Sprintf("%s_%s_%d%s",
526+
strings.TrimSuffix(fn, ext),
527+
time.Now().Format("20060102150405"),
528+
index,
529+
ext)
530+
if !fileOrDirIsExist(newfn) {
531+
fn = newfn
532+
break
533+
}
534+
if index == MaxInt {
535+
ctx.Error("Sorry, can not create unique filename for "+fn, fasthttp.StatusInternalServerError)
536+
return
537+
}
538+
}
539+
}
540+
logInfo(0, "%s | Saving file %s", ctx.RemoteIP(), fn)
541+
err := fasthttp.SaveMultipartFile(header, fn)
542+
if err != nil {
543+
logInfo(fasthttp.StatusInternalServerError, "Save %s failed: %s", fn, err.Error())
544+
}
510545
}
511546
}
512547
ctx.Redirect(uri, fasthttp.StatusOK)
@@ -519,6 +554,13 @@ func dirIsExist(path string) bool {
519554
return false
520555
}
521556

557+
func fileOrDirIsExist(path string) bool {
558+
if _, err := os.Stat(path); err == nil {
559+
return true
560+
}
561+
return false
562+
}
563+
522564
func basicAuth(ctx *fasthttp.RequestCtx) (username, password string, ok bool) {
523565
auth := ctx.Request.Header.Peek("Authorization")
524566
if auth == nil {

0 commit comments

Comments
 (0)