Skip to content

Commit

Permalink
fix: improves uniqueness of proxy response file names.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Sep 9, 2022
1 parent 9aca25f commit 4f3602d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions proxy/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package proxy

import (
"fmt"
"gatehill.io/imposter/stringutil"
"github.com/google/uuid"
"mime"
"net/http"
"os"
"path"
"strings"
)

// generateRespFileName returns a unique filename for the given response
func generateRespFileName(
upstreamHost string,
dir string,
Expand All @@ -42,9 +45,6 @@ func generateRespFileName(
if baseFileName == "/" || baseFileName == "." {
baseFileName = "index"
}
if path.Ext(baseFileName) == "" {
baseFileName += getFileExtension(exchange.ResponseHeaders)
}
baseFileName = prefix + baseFileName

var parentDir, respFileName string
Expand All @@ -64,7 +64,25 @@ func generateRespFileName(
respFileName = req.Method + "-" + baseFileName
}

respFile = path.Join(parentDir, respFileName)
var suffix string
if path.Ext(baseFileName) == "" {
suffix = getFileExtension(exchange.ResponseHeaders)
} else {
suffix = ""
}
respFile = path.Join(parentDir, respFileName+suffix)

if _, err = os.Stat(respFile); err == nil {
// already exists - add url hash
suffix = "_" + stringutil.Sha1hashString(req.URL.String()) + suffix
respFile = path.Join(parentDir, respFileName+suffix)
}
if _, err = os.Stat(respFile); err == nil {
// already exists - add uuid
suffix = "_" + uuid.New().String() + suffix
respFile = path.Join(parentDir, respFileName+suffix)
}

return respFile, nil
}

Expand Down

0 comments on commit 4f3602d

Please sign in to comment.