You've already forked speedtest-go
							
							Return 404 for non-existing files instead of 403
This commit is contained in:
		
							
								
								
									
										54
									
								
								web/fs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								web/fs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					package web
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Credit: https://stackoverflow.com/questions/49589685/good-way-to-disable-directory-listing-with-http-fileserver-in-go
 | 
				
			||||||
 | 
					type justFilesFilesystem struct {
 | 
				
			||||||
 | 
						fs http.FileSystem
 | 
				
			||||||
 | 
						// readDirBatchSize - configuration parameter for `Readdir` func
 | 
				
			||||||
 | 
						readDirBatchSize int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (fs justFilesFilesystem) Open(name string) (http.File, error) {
 | 
				
			||||||
 | 
						f, err := fs.fs.Open(name)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return neuteredStatFile{File: f, readDirBatchSize: fs.readDirBatchSize}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type neuteredStatFile struct {
 | 
				
			||||||
 | 
						http.File
 | 
				
			||||||
 | 
						readDirBatchSize int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (e neuteredStatFile) Stat() (os.FileInfo, error) {
 | 
				
			||||||
 | 
						s, err := e.File.Stat()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if s.IsDir() {
 | 
				
			||||||
 | 
						LOOP:
 | 
				
			||||||
 | 
							for {
 | 
				
			||||||
 | 
								fl, err := e.File.Readdir(e.readDirBatchSize)
 | 
				
			||||||
 | 
								switch err {
 | 
				
			||||||
 | 
								case io.EOF:
 | 
				
			||||||
 | 
									break LOOP
 | 
				
			||||||
 | 
								case nil:
 | 
				
			||||||
 | 
									for _, f := range fl {
 | 
				
			||||||
 | 
										if f.Name() == "index.html" {
 | 
				
			||||||
 | 
											return s, err
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return nil, os.ErrNotExist
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return s, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -91,13 +91,8 @@ func pages(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	conf := config.LoadedConfig()
 | 
						conf := config.LoadedConfig()
 | 
				
			||||||
 | 
						fs := justFilesFilesystem{fs: http.Dir(conf.AssetsPath), readDirBatchSize: 2}
 | 
				
			||||||
	uri := strings.Split(r.RequestURI, "?")[0]
 | 
						http.FileServer(fs).ServeHTTP(w, r)
 | 
				
			||||||
	if strings.HasSuffix(uri, ".html") || strings.HasSuffix(uri, ".js") {
 | 
					 | 
				
			||||||
		http.FileServer(http.Dir(conf.AssetsPath)).ServeHTTP(w, r)
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		w.WriteHeader(http.StatusForbidden)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func empty(w http.ResponseWriter, r *http.Request) {
 | 
					func empty(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user