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

Commit

Permalink
Serve default favicon, allow disabling logs
Browse files Browse the repository at this point in the history
  • Loading branch information
maetthu committed Apr 18, 2019
1 parent 7194eae commit e10f21f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 15 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Flags:
-k, --key string Key file (default "/home/maetthu/.config/dirhttps/key.pem")
-l, --listen string Listen address (default ":8443")
--no-cors Disable CORS handling
--no-favicon Disable default favicon delivered when no favicon.ico is present in current directory
-q, --quiet Don't log requests to STDOUT
--version version for dirhttps
```

Expand All @@ -64,3 +66,10 @@ $ dirhttps
$ dirhttps -l :1234
$ dirhttps -l 127.0.0.2:8443
```


## License

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).

dirhttps is licensed under the MIT License.
71 changes: 56 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package cmd

import (
"fmt"
"github.com/maetthu/dirhttps/internal/lib/version"
"github.com/mitchellh/go-homedir"
"github.com/rs/cors"
"github.com/spf13/cobra"
"log"
"net/http"
"net/http/httputil"
"os"
"path/filepath"

"github.com/maetthu/dirhttps/internal/lib/static"
"github.com/maetthu/dirhttps/internal/lib/version"
"github.com/mitchellh/go-homedir"
"github.com/rs/cors"
"github.com/spf13/cobra"
)

const (
Expand All @@ -19,9 +21,9 @@ const (
)

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

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

certAvailable := true

if _, err := os.Stat(certFile); err != nil {
if _, err := os.Stat(certFile); err != nil {
certAvailable = false
}

if _, err := os.Stat(keyFile); err != nil {
if _, err := os.Stat(keyFile); err != nil {
certAvailable = false
}

Expand All @@ -53,7 +55,7 @@ var rootCmd = &cobra.Command{
"Store a certificate to \"%s\" and a key file to \"%s\" or provide the --cert and --key flags",
certFile,
keyFile,
)
)
}

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

handler := logger(http.FileServer(http.Dir(dir)))
handler := http.FileServer(http.Dir(dir))

// favicon
if disableFavicon, _ := cmd.Flags().GetBool("no-favicon"); !disableFavicon {
handler = favicon(handler)
}

// logger
if quiet, _ := cmd.Flags().GetBool("quiet"); !quiet {
handler = logger(handler)
}

// permit some CORS stuff
if disableCORS, _ := cmd.Flags().GetBool("no-cors"); !disableCORS {
handler = cors.New(cors.Options{
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {return true},
AllowOriginFunc: func(origin string) bool { return true },
}).Handler(handler)
}

Expand All @@ -103,7 +115,7 @@ func Execute() {
}
}

func init(){
func init() {
home, err := homedir.Dir()

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

rootCmd.Flags().Bool("cache", false, "Enable client side caching")
rootCmd.Flags().BoolP("dump", "d",false, "Dump client request headers to STDOUT")
rootCmd.Flags().BoolP("dump", "d", false, "Dump client request headers to STDOUT")
rootCmd.Flags().BoolP("quiet", "q", false, "Don't log requests to STDOUT")
rootCmd.Flags().Bool("no-cors", false, "Disable CORS handling")
rootCmd.Flags().Bool(
"no-favicon",
false,
"Disable default favicon delivered when no favicon.ico is present in current directory",
)
}

func favicon(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI != "/favicon.ico" {
handler.ServeHTTP(w, r)
return
}

i, err := os.Stat("favicon.ico")

if !os.IsNotExist(err) && !i.IsDir() {
handler.ServeHTTP(w, r)
return
}

w.Header().Add("Content-Type", "image/vnd.microsoft.icon")
_, err = w.Write(static.Favicon)

if err != nil {
log.Print(err)
}
})
}

func logger(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf( "%s \"%s %s %s\"\n", r.RemoteAddr, r.Method, r.URL, r.Proto)
log.Printf("%s \"%s %s %s\"\n", r.RemoteAddr, r.Method, r.URL, r.Proto)
handler.ServeHTTP(w, r)
})
}
Expand Down
23 changes: 23 additions & 0 deletions helper/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
)

func main() {
f, err := os.Open("favicon.ico")

if err != nil {
log.Fatal(err)
}

b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%#v", b)
}
Binary file added helper/favicon.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions internal/lib/static/favicon.go

Large diffs are not rendered by default.

0 comments on commit e10f21f

Please sign in to comment.