Bläddra i källkod

feature: 增加参数校验

chenqinghe 4 år sedan
förälder
incheckning
d6e423dcf3
2 ändrade filer med 36 tillägg och 6 borttagningar
  1. 10
    0
      cmd/webpprof/main.go
  2. 26
    6
      web/src/components/Upload.vue

+ 10
- 0
cmd/webpprof/main.go Visa fil

3
 import (
3
 import (
4
 	"crypto/md5"
4
 	"crypto/md5"
5
 	"encoding/hex"
5
 	"encoding/hex"
6
+	"errors"
6
 	"fmt"
7
 	"fmt"
7
 	"io/ioutil"
8
 	"io/ioutil"
8
 	"net/http"
9
 	"net/http"
37
 	filename := fmt.Sprintf("%s.pprof", hash)
38
 	filename := fmt.Sprintf("%s.pprof", hash)
38
 	f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
39
 	f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
39
 	if err != nil {
40
 	if err != nil {
41
+		if os.IsNotExist(err) {
42
+			c.AbortWithStatus(http.StatusNotFound)
43
+			return
44
+		}
40
 		c.AbortWithError(http.StatusInternalServerError, err)
45
 		c.AbortWithError(http.StatusInternalServerError, err)
41
 		return
46
 		return
42
 	}
47
 	}
94
 		return
99
 		return
95
 	}
100
 	}
96
 
101
 
102
+	if _, err := profile.ParseData(fdata); err != nil {
103
+		c.AbortWithError(http.StatusBadRequest, errors.New("unrecongnized profile format"))
104
+		return
105
+	}
106
+
97
 	fileDst := fmt.Sprintf("%s.pprof", rhash)
107
 	fileDst := fmt.Sprintf("%s.pprof", rhash)
98
 	file, err := os.Create(fileDst)
108
 	file, err := os.Create(fileDst)
99
 	if err != nil {
109
 	if err != nil {

+ 26
- 6
web/src/components/Upload.vue Visa fil

4
         class="upload-demo"
4
         class="upload-demo"
5
         drag
5
         drag
6
         action="/upload"
6
         action="/upload"
7
-        :on-success="uploadSuccess"
7
+        :on-progress="handleUploadProgress"
8
+        :on-success="handleUploadSuccess"
9
+        :on-error="handleUploadError"
8
     >
10
     >
9
       <i class="el-icon-upload"></i>
11
       <i class="el-icon-upload"></i>
10
       <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
12
       <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
11
-      <div class="el-upload__tip" slot="tip">上传文件不超过500kb</div>
13
+      <div class="el-upload__tip" slot="tip">拖动文件至此即可生成火焰图</div>
12
     </el-upload>
14
     </el-upload>
13
   </div>
15
   </div>
14
 </template>
16
 </template>
16
 <script>
18
 <script>
17
 export default {
19
 export default {
18
   name: "Upload",
20
   name: "Upload",
19
-  methods:{
20
-    uploadSuccess(response,file,fileList){
21
-      console.log(response,file,fileList)
22
-      window.location.href="http://localhost:8090/pprof/"+response.data+"/dot"
21
+  data() {
22
+    return {
23
+      src: 'http://localhost:8090/pprof/bd5001ffcea7949ff66b7368b1912aa7/dot',
24
+      loading: null
25
+    }
26
+  },
27
+  methods: {
28
+    handleUploadSuccess(response) {
29
+      this.loading.close()
30
+      window.location.href = "http://localhost:8090/pprof/" + response.data + "/dot"
31
+    },
32
+    handleUploadError() {
33
+      this.loading.close()
34
+      this.$message.error("文件上传失败或文件格式错误")
35
+    },
36
+    handleUploadProgress() {
37
+      this.loading = this.$loading({
38
+        lock: true,
39
+        text: 'Loading',
40
+        spinner: 'el-icon-loading',
41
+        background: 'rgba(0, 0, 0, 0.7)'
42
+      });
23
     }
43
     }
24
   }
44
   }
25
 }
45
 }