Skip to content

Commit 2b66b85

Browse files
committed
perf(serverHandler/responseData): provide status instead of detail errors
Providing response status directly, instead of giving out errors information and calculate response status according to them. BREAKING CHANGE: Root fields passing to template changed. before: ``` {{if .HasForbiddenError}}...{{end}} {{if .HasNotFoundError}}...{{end}} {{if .HasInternalError}}...{{end}} ``` after: ``` {{if eq .Status 403}}...{{end}} {{if eq .Status 404}}...{{end}} {{if eq .Status 500}}...{{end}} ```
1 parent bce0128 commit 2b66b85

File tree

7 files changed

+33
-59
lines changed

7 files changed

+33
-59
lines changed

src/serverHandler/content.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ func (h *handler) content(w http.ResponseWriter, r *http.Request, data *response
4242
header.Set("Content-Length", strconv.FormatInt(item.Size(), 10))
4343
header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
4444
header.Set("Last-Modified", item.ModTime().UTC().Format(http.TimeFormat))
45-
} else {
46-
// take effect only if (!HasForbiddenError && !HasNotFoundError)
47-
data.HasInternalError = true
45+
} else if data.Status == http.StatusOK {
46+
data.Status = http.StatusInternalServerError
4847
}
4948

50-
writeHeader(w, r, data)
49+
w.WriteHeader(data.Status)
5150
}

src/serverHandler/json.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ type jsonItem struct {
1515
}
1616

1717
type jsonResponseData struct {
18-
ForbiddenError bool `json:"forbiddenError"`
19-
NotFoundError bool `json:"notFoundError"`
20-
InternalError bool `json:"internalError"`
21-
2218
Item *jsonItem `json:"item"`
2319
SubItems []*jsonItem `json:"subItems"`
2420
}
@@ -48,11 +44,8 @@ func getJsonData(data *responseData) *jsonResponseData {
4844
}
4945

5046
return &jsonResponseData{
51-
ForbiddenError: data.HasForbiddenError,
52-
NotFoundError: data.HasNotFoundError,
53-
InternalError: data.HasInternalError,
54-
Item: item,
55-
SubItems: subItems,
47+
Item: item,
48+
SubItems: subItems,
5649
}
5750
}
5851

@@ -61,7 +54,7 @@ func (h *handler) json(w http.ResponseWriter, r *http.Request, data *responseDat
6154
header.Set("Content-Type", "application/json; charset=utf-8")
6255
header.Set("Cache-Control", "public, max-age=0")
6356

64-
writeHeader(w, r, data)
57+
w.WriteHeader(data.Status)
6558

6659
if needResponseBody(r.Method) {
6760
jsonData := getJsonData(data)

src/serverHandler/page.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func (h *handler) page(w http.ResponseWriter, r *http.Request, data *responseDat
77
header.Set("Content-Type", "text/html; charset=utf-8")
88
header.Set("Cache-Control", "public, max-age=0")
99

10-
writeHeader(w, r, data)
10+
w.WriteHeader(data.Status)
1111

1212
if needResponseBody(r.Method) {
1313
updateSubsItemHtml(data.SubItems)

src/serverHandler/responseData.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ type responseData struct {
4040
rawReqPath string
4141
handlerReqPath string
4242

43-
errors []error
44-
HasForbiddenError bool
45-
HasNotFoundError bool
46-
HasInternalError bool
43+
errors []error
44+
Status int
4745

4846
IsRoot bool
4947
Path string
@@ -259,6 +257,18 @@ func sortSubItems(subItems []*subItem) {
259257
},
260258
)
261259
}
260+
func getStatusByErr(err error) int {
261+
switch {
262+
case os.IsPermission(err):
263+
return http.StatusForbidden
264+
case os.IsNotExist(err):
265+
return http.StatusNotFound
266+
case err != nil:
267+
return http.StatusInternalServerError
268+
default:
269+
return http.StatusOK
270+
}
271+
}
262272

263273
func (h *handler) getResponseData(r *http.Request) (data *responseData) {
264274
requestUri := r.URL.Path
@@ -267,10 +277,7 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
267277
rawReqPath := util.CleanUrlPath(requestUri)
268278
reqPath := util.CleanUrlPath(rawReqPath[len(h.urlPrefix):]) // strip url prefix path
269279
errs := []error{}
270-
forbidden := false
271-
notFound := false
272-
internalError := false
273-
280+
status := http.StatusOK
274281
isRoot := rawReqPath == "/"
275282

276283
pathEntries := getPathEntries(rawReqPath, tailSlash)
@@ -289,28 +296,21 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
289296
file, item, _statErr := stat(reqFsPath, !h.emptyRoot)
290297
if _statErr != nil {
291298
errs = append(errs, _statErr)
292-
switch {
293-
case os.IsPermission(_statErr):
294-
forbidden = true
295-
case os.IsNotExist(_statErr):
296-
notFound = true
297-
default:
298-
internalError = true
299-
}
299+
status = getStatusByErr(_statErr)
300300
}
301301

302302
itemName := getItemName(item, r)
303303

304304
subInfos, _readdirErr := readdir(file, item, needResponseBody(r.Method))
305305
if _readdirErr != nil {
306306
errs = append(errs, _readdirErr)
307-
internalError = true
307+
status = http.StatusInternalServerError
308308
}
309309

310310
_mergeErrs := h.mergeAlias(rawReqPath, &subInfos)
311311
if len(_mergeErrs) > 0 {
312312
errs = append(errs, _mergeErrs...)
313-
internalError = true
313+
status = http.StatusInternalServerError
314314
}
315315

316316
subInfos = h.FilterItems(subInfos)
@@ -329,10 +329,8 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
329329
rawReqPath: rawReqPath,
330330
handlerReqPath: reqPath,
331331

332-
errors: errs,
333-
HasForbiddenError: forbidden,
334-
HasNotFoundError: notFound,
335-
HasInternalError: internalError,
332+
errors: errs,
333+
Status: status,
336334

337335
IsRoot: isRoot,
338336
Path: rawReqPath,

src/serverHandler/writeHeader.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/tpl/page.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
{{end}}{{end}}
5757
</ul>
5858

59-
{{if .HasForbiddenError}}
59+
{{if eq .Status 403}}
6060
<div class="error">403 resource is forbidden</div>
61-
{{else if .HasNotFoundError}}
61+
{{else if eq .Status 404}}
6262
<div class="error">404 resource not found</div>
63-
{{else if .HasInternalError}}
63+
{{else if eq .Status 500}}
6464
<div class="error">500 potential issue occurred</div>
6565
{{end}}
6666

src/tpl/page.html.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ const pageTplStr = `
6060
</li>
6161
{{end}}{{end}}
6262
</ul>
63-
{{if .HasForbiddenError}}
63+
{{if eq .Status 403}}
6464
<div class="error">403 resource is forbidden</div>
65-
{{else if .HasNotFoundError}}
65+
{{else if eq .Status 404}}
6666
<div class="error">404 resource not found</div>
67-
{{else if .HasInternalError}}
67+
{{else if eq .Status 500}}
6868
<div class="error">500 potential issue occurred</div>
6969
{{end}}
7070
<script type="text/javascript" src="{{.RootRelPath}}?assert=main.js"></script>

0 commit comments

Comments
 (0)