summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
Diffstat (limited to 'service')
-rw-r--r--service/http/parse.go15
-rw-r--r--service/http/uploads.go14
-rw-r--r--service/http/uploads_test.go73
3 files changed, 86 insertions, 16 deletions
diff --git a/service/http/parse.go b/service/http/parse.go
index a7516ca6..1f90930a 100644
--- a/service/http/parse.go
+++ b/service/http/parse.go
@@ -2,7 +2,6 @@ package http
import (
"net/http"
- "os"
)
// MaxLevel defines maximum tree depth for incoming request data and files.
@@ -81,20 +80,6 @@ func parseUploads(r *http.Request, cfg *UploadsConfig) *Uploads {
return u
}
-// exists if file exists.
-func exists(path string) bool {
- _, err := os.Stat(path)
- if err == nil {
- return true
- }
-
- if os.IsNotExist(err) {
- return false
- }
-
- return false
-}
-
// pushes new file upload into it's proper place.
func (d fileTree) push(k string, v []*FileUpload) {
keys := fetchIndexes(k)
diff --git a/service/http/uploads.go b/service/http/uploads.go
index 99484f4b..4d661682 100644
--- a/service/http/uploads.go
+++ b/service/http/uploads.go
@@ -128,3 +128,17 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error {
return err
}
+
+// exists if file exists.
+func exists(path string) bool {
+ _, err := os.Stat(path)
+ if err == nil {
+ return true
+ }
+
+ if os.IsNotExist(err) {
+ return false
+ }
+
+ return false
+}
diff --git a/service/http/uploads_test.go b/service/http/uploads_test.go
index 80599884..deb5fa57 100644
--- a/service/http/uploads_test.go
+++ b/service/http/uploads_test.go
@@ -78,6 +78,72 @@ func TestServer_Upload_File(t *testing.T) {
assert.Equal(t, `{"upload":`+fs+`}`, string(b))
}
+func TestServer_Upload_File_NoTmpDir(t *testing.T) {
+ st := &Handler{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: "-----",
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../php-src/tests/http/client.php upload pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ assert.NoError(t, st.rr.Start())
+ defer st.rr.Stop()
+
+ hs := &http.Server{Addr: ":8021", Handler: st,}
+ defer hs.Shutdown(context.Background())
+
+ go func() { hs.ListenAndServe() }()
+ time.Sleep(time.Millisecond * 10)
+
+ var mb bytes.Buffer
+ w := multipart.NewWriter(&mb)
+
+ f := mustOpen("uploads_test.go")
+ defer f.Close()
+ fw, err := w.CreateFormFile("upload", f.Name())
+ assert.NotNil(t, fw)
+ assert.NoError(t, err)
+ io.Copy(fw, f)
+
+ w.Close()
+
+ req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb)
+ assert.NoError(t, err)
+
+ req.Header.Set("Content-Type", w.FormDataContentType())
+
+ r, err := http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ b, err := ioutil.ReadAll(r.Body)
+ assert.NoError(t, err)
+
+ assert.NoError(t, err)
+ assert.Equal(t, 200, r.StatusCode)
+
+ fs := fileString("uploads_test.go", 5, "application/octet-stream")
+
+ assert.Equal(t, `{"upload":`+fs+`}`, string(b))
+}
+
+func Test_FileExists(t *testing.T) {
+ assert.True(t, exists("uploads_test.go"))
+ assert.False(t, exists("uploads_test."))
+}
+
func mustOpen(f string) *os.File {
r, err := os.Open(f)
if err != nil {
@@ -91,7 +157,7 @@ type fInfo struct {
Size int64 `json:"size"`
Mime string `json:"mime"`
Error int `json:"error"`
- MD5 string `json:"md5"`
+ MD5 string `json:"md5,omitempty"`
}
func fileString(f string, err int, mime string) string {
@@ -110,6 +176,11 @@ func fileString(f string, err int, mime string) string {
MD5: hex.EncodeToString(h.Sum(nil)),
}
+ if err != 0 {
+ v.MD5 = ""
+ v.Size = 0
+ }
+
r, _ := json.Marshal(v)
return string(r)