Commits

Miki Tebeka committed ed79ea0 Merge

Merge mask

Comments (0)

Files changed (2)

 	"net/http"
 	"os"
 	"path/filepath"
+	"regexp"
 	"strings"
 	"sync"
 	"time"
 )
 
 const (
-	Version = "0.3.1"
+	Version = "0.4.0"
 )
 
 var ResourceMap map[string]Resource = nil
 var initMutex sync.Mutex
+var urlMask *regexp.Regexp
 
 func loadMap() (map[string]Resource, error) {
 	this := os.Args[0]
 
 type handler int
 
+func isMasked(path string) bool {
+	if urlMask == nil {
+		return false
+	}
+
+	return urlMask.MatchString(path)
+}
+
 func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
-	rsc := Get(req.URL.Path)
+	path := req.URL.Path
+	if isMasked(path) {
+		http.Error(w, fmt.Sprintf("Unauthorized - %s", path), http.StatusUnauthorized)
+		return
+	}
+
+	rsc := Get(path)
 	if rsc == nil {
 		http.NotFound(w, req)
 		return
 	}
 	return t, nil
 }
+
+// Mask masks URLs from being served (the HTTP server will return 401 Unauthorized)
+func Mask(mask *regexp.Regexp) {
+	urlMask = mask
+}
 	port = 9888
 )
 
+func TestMask(t *testing.T) {
+	resp := getResp(t, "/static/i.gif")
+	if resp.StatusCode != http.StatusUnauthorized {
+		t.Fatalf("got masked resource - %d\n", resp.StatusCode)
+	}
+}
+
 func TestText(t *testing.T) {
 	expected := map[string]string{
 		"Content-Size": "12",
 }
 
 func fixGOPATH(cwd string) {
-	path := os.Getenv("GOPATH")
-	if len(path) == 0 {
-		os.Setenv("GOPATH", fmt.Sprintf("%s/../..", cwd))
-	}
+	os.Setenv("GOPATH", fmt.Sprintf("%s/../..", cwd))
 }
 
 func init() {
 	os.Chdir(root)
 	defer os.Chdir(cwd)
 
-	cmd := exec.Command("go", "install")
+	cmd := exec.Command("go", "install", "nrsc")
+	if err := cmd.Run(); err != nil {
+		fmt.Printf("error installing: %s\n", err)
+		panic(err)
+	}
+
+	cmd = exec.Command("go", "build")
 	if err := cmd.Run(); err != nil {
 		fmt.Printf("error building: %s\n", err)
 		panic(err)
 	}
 }
 
-func checkPath(t *testing.T, path string, expected map[string]string) {
+func getResp(t *testing.T, path string) *http.Response {
 	server := startServer(t)
 	if server == nil {
-		return
+		return nil
 	}
 	defer server.Process.Kill()
 
 	resp, err := get(path)
 	if err != nil {
 		t.Fatalf("%s\n", err)
+		return nil
 	}
 
+	return resp
+}
+
+func checkPath(t *testing.T, path string, expected map[string]string) {
+	resp := getResp(t, path)
 	if resp.StatusCode != http.StatusOK {
 		t.Fatalf("bad reply - %s", resp.Status)
 	}
 	"fmt"
 	"net/http"
 	"os"
+	"regexp"
 
 	"nrsc"
 )
 
 func main() {
 	nrsc.Handle("/static/")
+	nrsc.Mask(regexp.MustCompile(".gif$"))
 	http.HandleFunc("/", indexHandler)
 	if err := http.ListenAndServe(":%d", nil); err != nil {
 		fmt.Fprintf(os.Stderr, "error: %%s\n", err)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.