Skip to content

Commit

Permalink
add no-serve-public-paths flag (#262)
Browse files Browse the repository at this point in the history
* allow config to ignore static paths inside /public, upgrade ci

add static files to try to resolve but ignore

add configuration for linux and macos

add field

broken impl.

clean up bad impl

if request path should be ignored by the static server, don't serve it

forgotten flag rename

slightly better description

upgrades

* Add logging of no-serve-public-paths and add -install support for it (#263)

* Add logging of no-serve-public-paths settings

* Add the NoServePublicPaths settings to the plist template

* Fix missing , causing test failures

Co-authored-by: Pez Cuckow <[email protected]>
  • Loading branch information
nonrational and Pezmc authored Oct 29, 2020
1 parent 458cc5c commit 87b05a8
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
go-version: [1.13.x, 1.14.x]
ruby-version: [2.5]
go-version: [1.14, 1.15]
ruby-version: [2.7]

name: ${{ matrix.os }} / go-${{ matrix.go-version }}
steps:
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ release:
test:
go test -v -race -coverprofile=coverage.out -covermode=atomic ./...

clean-test:
rm -rf ~/.gotest-macos-puma-dev

test-macos-filesystem-setup:
sudo mkdir -p /etc/resolver;
sudo chmod 0775 /etc/resolver;
sudo chown :staff /etc/resolver;

coverage: test
go tool cover -html=coverage.out -o coverage.html

Expand All @@ -50,4 +58,4 @@ test-macos-manual-setup-install: clean build
test -f "$$HOME/Library/Logs/puma-dev.log"
test 'Hi Puma!' == "$$(curl -s https://rack-hi-puma.puma)" && echo "PASS"

.PHONY: all release
.PHONY: release
7 changes: 7 additions & 0 deletions cmd/puma-dev/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
fPow = flag.Bool("pow", false, "Mimic pow's settings")
fLaunch = flag.Bool("launchd", false, "Use socket from launchd")

fNoServePublicPaths = flag.String("no-serve-public-paths", "", "Disable static file server for specific paths under /public")

fSetup = flag.Bool("setup", false, "Run system setup")
fStop = flag.Bool("stop", false, "Stop all puma-dev servers")

Expand Down Expand Up @@ -78,6 +80,7 @@ func main() {
LogfilePath: LogFilePath,
Timeout: (*fTimeout).String(),
TlsPort: *fInstallTLS,
NoServePublicPaths: *fNoServePublicPaths,
})

if err != nil {
Expand Down Expand Up @@ -176,6 +179,10 @@ func main() {
http.Pool = &pool
http.Debug = *fDebug
http.Events = &events
if len(*fNoServePublicPaths) > 0 {
http.IgnoredStaticPaths = strings.Split(*fNoServePublicPaths, ":")
fmt.Printf("* Ignoring files under: public{%s}\n", strings.Join(http.IgnoredStaticPaths, ", "))
}

http.Setup()

Expand Down
11 changes: 6 additions & 5 deletions cmd/puma-dev/main_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ func TestMainPumaDev_Darwin(t *testing.T) {
defer linkAllTestApps(t, appLinkDir)()

serveErr := configureAndBootPumaDevServer(t, map[string]string{
"d": "test:puma",
"dir": appLinkDir,
"dns-port": "65053",
"http-port": "65080",
"https-port": "65443",
"d": "test:puma",
"dir": appLinkDir,
"dns-port": "65053",
"http-port": "65080",
"https-port": "65443",
"no-serve-public-paths": "/packs:/config.json",
})

assert.NoError(t, serveErr)
Expand Down
21 changes: 13 additions & 8 deletions cmd/puma-dev/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
)

var (
fDebug = flag.Bool("debug", false, "enable debug output")
fDomains = flag.String("d", "test", "domains to handle, separate with :, defaults to test")
fHTTPPort = flag.Int("http-port", 9280, "port to listen on http for")
fTLSPort = flag.Int("https-port", 9283, "port to listen on https for")
fSysBind = flag.Bool("sysbind", false, "bind to ports 80 and 443")
fDir = flag.String("dir", "~/.puma-dev", "directory to watch for apps")
fTimeout = flag.Duration("timeout", 15*60*time.Second, "how long to let an app idle for")
fStop = flag.Bool("stop", false, "Stop all puma-dev servers")
fDebug = flag.Bool("debug", false, "enable debug output")
fDir = flag.String("dir", "~/.puma-dev", "directory to watch for apps")
fDomains = flag.String("d", "test", "domains to handle, separate with :, defaults to test")
fHTTPPort = flag.Int("http-port", 9280, "port to listen on http for")
fNoServePublicPaths = flag.String("no-serve-public-paths", "", "Disable static file server for specific paths under /public")
fStop = flag.Bool("stop", false, "Stop all puma-dev servers")
fSysBind = flag.Bool("sysbind", false, "bind to ports 80 and 443")
fTimeout = flag.Duration("timeout", 15*60*time.Second, "how long to let an app idle for")
fTLSPort = flag.Int("https-port", 9283, "port to listen on https for")
)

func main() {
Expand Down Expand Up @@ -99,6 +100,10 @@ func main() {
http.Pool = &pool
http.Debug = *fDebug
http.Events = &events
if len(*fNoServePublicPaths) > 0 {
http.IgnoredStaticPaths = strings.Split(*fNoServePublicPaths, ":")
fmt.Printf("* Ignoring files under: public{%s}\n", strings.Join(http.IgnoredStaticPaths, ", "))
}

http.Setup()

Expand Down
7 changes: 4 additions & 3 deletions cmd/puma-dev/main_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ func TestMainPumaDev_Linux(t *testing.T) {
defer linkAllTestApps(t, appLinkDir)()

configureAndBootPumaDevServer(t, map[string]string{
"dir": appLinkDir,
"http-port": "65080",
"https-port": "65443",
"dir": appLinkDir,
"http-port": "65080",
"https-port": "65443",
"no-serve-public-paths": "/packs:/config",
})

runPlatformAgnosticTestScenarios(t)
Expand Down
14 changes: 14 additions & 0 deletions cmd/puma-dev/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,18 @@ func runPlatformAgnosticTestScenarios(t *testing.T) {

assert.Equal(t, "rack wuz here", getURLWithHost(t, reqURL, statusHost))
})

t.Run("static-site ignore packs", func(t *testing.T) {
reqURL := fmt.Sprintf("http://localhost:%d/packs/site.js", *fHTTPPort)
statusHost := "static-site"

assert.Equal(t, "rack wuz here", getURLWithHost(t, reqURL, statusHost))
})

t.Run("static-site ignore config", func(t *testing.T) {
reqURL := fmt.Sprintf("http://localhost:%d/config.json", *fHTTPPort)
statusHost := "static-site"

assert.Equal(t, "rack wuz here", getURLWithHost(t, reqURL, statusHost))
})
}
36 changes: 30 additions & 6 deletions dev/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
)

type HTTPServer struct {
Address string
TLSAddress string
Pool *AppPool
Debug bool
Events *Events
Address string
TLSAddress string
Pool *AppPool
Debug bool
Events *Events
IgnoredStaticPaths []string

mux *pat.PatternServeMux
transport *httpu.Transport
Expand Down Expand Up @@ -147,7 +148,7 @@ func (h *HTTPServer) proxyReq(w http.ResponseWriter, req *http.Request) error {
return err
}

if app.Public && req.URL.Path != "/" {
if h.shouldServePublicPathForApp(app, req) {
safeURLPath := path.Clean(req.URL.Path)
path := filepath.Join(app.dir, "public", safeURLPath)

Expand Down Expand Up @@ -178,6 +179,29 @@ func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
}

func (h *HTTPServer) shouldServePublicPathForApp(a *App, req *http.Request) bool {
reqPath := path.Clean(req.URL.Path)

if !a.Public {
return false
}

if reqPath == "/" {
return false
}

for _, ignoredPath := range h.IgnoredStaticPaths {
if strings.HasPrefix(reqPath, ignoredPath) {
if h.Debug {
fmt.Fprintf(os.Stdout, "Not serving '%s' as it matches a path in no-serve-public-paths\n", reqPath)
}
return false
}
}

return true
}

func (h *HTTPServer) status(w http.ResponseWriter, req *http.Request) {
type appStatus struct {
Scheme string `json:"scheme"`
Expand Down
5 changes: 4 additions & 1 deletion dev/setup_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type InstallIntoSystemArgs struct {
LaunchAgentDirPath string
Domains string
Timeout string
NoServePublicPaths string
}

func InstallIntoSystem(config *InstallIntoSystemArgs) error {
Expand Down Expand Up @@ -138,6 +139,8 @@ func InstallIntoSystem(config *InstallIntoSystemArgs) error {
<string>%s</string>
<string>-timeout</string>
<string>%s</string>
<string>-no-serve-public-paths</string>
<string>%s</string>
</array>
<key>KeepAlive</key>
<true/>
Expand Down Expand Up @@ -181,7 +184,7 @@ func InstallIntoSystem(config *InstallIntoSystemArgs) error {

err = ioutil.WriteFile(
plist,
[]byte(fmt.Sprintf(userTemplate, binPath, dir, config.Domains, config.Timeout, config.ListenPort, config.TlsPort, logPath, logPath)),
[]byte(fmt.Sprintf(userTemplate, binPath, dir, config.Domains, config.Timeout, config.NoServePublicPaths, config.ListenPort, config.TlsPort, logPath, logPath)),
0644,
)

Expand Down
2 changes: 2 additions & 0 deletions dev/setup_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestInstallIntoSystem_FailsAsSuperuser(t *testing.T) {
TlsPort: 10443,
Domains: "test:localhost",
Timeout: "5s",
NoServePublicPaths: "",
ApplinkDirPath: "/tmp/gotest-dummy-applinkdir",
LaunchAgentDirPath: "/tmp/gotest-dummy-launchagent",
LogfilePath: "/tmp/gotest-dummy-logs/dummy.log",
Expand Down Expand Up @@ -80,6 +81,7 @@ func installIntoTestContext(t *testing.T) (string, string, func()) {
TlsPort: 10443,
Domains: "test:localhost",
Timeout: "5s",
NoServePublicPaths: "",
ApplinkDirPath: appLinkDir,
LaunchAgentDirPath: launchAgentDir,
LogfilePath: logFilePath,
Expand Down
3 changes: 3 additions & 0 deletions etc/static-hi-puma/public/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"path": "/public/config.json"
}
1 change: 1 addition & 0 deletions etc/static-hi-puma/public/packs/site.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* /public/packs/site.js */

0 comments on commit 87b05a8

Please sign in to comment.