Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit e10f21f

Browse files
committed
Serve default favicon, allow disabling logs
1 parent 7194eae commit e10f21f

File tree

5 files changed

+91
-15
lines changed

5 files changed

+91
-15
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Flags:
4444
-k, --key string Key file (default "/home/maetthu/.config/dirhttps/key.pem")
4545
-l, --listen string Listen address (default ":8443")
4646
--no-cors Disable CORS handling
47+
--no-favicon Disable default favicon delivered when no favicon.ico is present in current directory
48+
-q, --quiet Don't log requests to STDOUT
4749
--version version for dirhttps
4850
```
4951

@@ -64,3 +66,10 @@ $ dirhttps
6466
$ dirhttps -l :1234
6567
$ dirhttps -l 127.0.0.2:8443
6668
```
69+
70+
71+
## License
72+
73+
The default favicon served by dirhttps is generated from the [Font Awesome "code" icon](https://fontawesome.com/icons/code?style=solid) by [FontIcon](https://github.com/devgg/FontIcon) and is [licensed under the CC BY 4.0 License](https://fontawesome.com/license/free).
74+
75+
dirhttps is licensed under the MIT License.

cmd/root.go

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/maetthu/dirhttps/internal/lib/version"
6-
"github.com/mitchellh/go-homedir"
7-
"github.com/rs/cors"
8-
"github.com/spf13/cobra"
95
"log"
106
"net/http"
117
"net/http/httputil"
128
"os"
139
"path/filepath"
10+
11+
"github.com/maetthu/dirhttps/internal/lib/static"
12+
"github.com/maetthu/dirhttps/internal/lib/version"
13+
"github.com/mitchellh/go-homedir"
14+
"github.com/rs/cors"
15+
"github.com/spf13/cobra"
1416
)
1517

1618
const (
@@ -19,9 +21,9 @@ const (
1921
)
2022

2123
var rootCmd = &cobra.Command{
22-
Use: "dirhttps",
23-
Short: "Serving contents of current directory by HTTPS.",
24-
Args: cobra.NoArgs,
24+
Use: "dirhttps",
25+
Short: "Serving contents of current directory by HTTPS.",
26+
Args: cobra.NoArgs,
2527
Version: fmt.Sprintf("%s -- %s", version.Version, version.Commit),
2628
Run: func(cmd *cobra.Command, args []string) {
2729

@@ -39,11 +41,11 @@ var rootCmd = &cobra.Command{
3941

4042
certAvailable := true
4143

42-
if _, err := os.Stat(certFile); err != nil {
44+
if _, err := os.Stat(certFile); err != nil {
4345
certAvailable = false
4446
}
4547

46-
if _, err := os.Stat(keyFile); err != nil {
48+
if _, err := os.Stat(keyFile); err != nil {
4749
certAvailable = false
4850
}
4951

@@ -53,7 +55,7 @@ var rootCmd = &cobra.Command{
5355
"Store a certificate to \"%s\" and a key file to \"%s\" or provide the --cert and --key flags",
5456
certFile,
5557
keyFile,
56-
)
58+
)
5759
}
5860

5961
dir, err := os.Getwd()
@@ -71,13 +73,23 @@ var rootCmd = &cobra.Command{
7173
log.Printf("Listening for HTTPS connections on %s", addr)
7274
log.Printf("Serving from directory %s", dir)
7375

74-
handler := logger(http.FileServer(http.Dir(dir)))
76+
handler := http.FileServer(http.Dir(dir))
77+
78+
// favicon
79+
if disableFavicon, _ := cmd.Flags().GetBool("no-favicon"); !disableFavicon {
80+
handler = favicon(handler)
81+
}
82+
83+
// logger
84+
if quiet, _ := cmd.Flags().GetBool("quiet"); !quiet {
85+
handler = logger(handler)
86+
}
7587

7688
// permit some CORS stuff
7789
if disableCORS, _ := cmd.Flags().GetBool("no-cors"); !disableCORS {
7890
handler = cors.New(cors.Options{
7991
AllowCredentials: true,
80-
AllowOriginFunc: func(origin string) bool {return true},
92+
AllowOriginFunc: func(origin string) bool { return true },
8193
}).Handler(handler)
8294
}
8395

@@ -103,7 +115,7 @@ func Execute() {
103115
}
104116
}
105117

106-
func init(){
118+
func init() {
107119
home, err := homedir.Dir()
108120

109121
if err != nil {
@@ -120,13 +132,42 @@ func init(){
120132
rootCmd.Flags().StringP("listen", "l", ":8443", "Listen address")
121133

122134
rootCmd.Flags().Bool("cache", false, "Enable client side caching")
123-
rootCmd.Flags().BoolP("dump", "d",false, "Dump client request headers to STDOUT")
135+
rootCmd.Flags().BoolP("dump", "d", false, "Dump client request headers to STDOUT")
136+
rootCmd.Flags().BoolP("quiet", "q", false, "Don't log requests to STDOUT")
124137
rootCmd.Flags().Bool("no-cors", false, "Disable CORS handling")
138+
rootCmd.Flags().Bool(
139+
"no-favicon",
140+
false,
141+
"Disable default favicon delivered when no favicon.ico is present in current directory",
142+
)
143+
}
144+
145+
func favicon(handler http.Handler) http.Handler {
146+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
147+
if r.RequestURI != "/favicon.ico" {
148+
handler.ServeHTTP(w, r)
149+
return
150+
}
151+
152+
i, err := os.Stat("favicon.ico")
153+
154+
if !os.IsNotExist(err) && !i.IsDir() {
155+
handler.ServeHTTP(w, r)
156+
return
157+
}
158+
159+
w.Header().Add("Content-Type", "image/vnd.microsoft.icon")
160+
_, err = w.Write(static.Favicon)
161+
162+
if err != nil {
163+
log.Print(err)
164+
}
165+
})
125166
}
126167

127168
func logger(handler http.Handler) http.Handler {
128169
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
129-
log.Printf( "%s \"%s %s %s\"\n", r.RemoteAddr, r.Method, r.URL, r.Proto)
170+
log.Printf("%s \"%s %s %s\"\n", r.RemoteAddr, r.Method, r.URL, r.Proto)
130171
handler.ServeHTTP(w, r)
131172
})
132173
}

helper/dump.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
)
9+
10+
func main() {
11+
f, err := os.Open("favicon.ico")
12+
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
17+
b, err := ioutil.ReadAll(f)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
22+
fmt.Printf("%#v", b)
23+
}

helper/favicon.ico

14.7 KB
Binary file not shown.

internal/lib/static/favicon.go

Lines changed: 3 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)