mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Refactored fastcgi middleware to return errors
This commit is contained in:
parent
7578298b3f
commit
22707edcbf
1 changed files with 11 additions and 12 deletions
|
@ -6,7 +6,6 @@ package fastcgi
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -27,8 +26,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
rules = append(rules, rule)
|
rules = append(rules, rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
return func(next middleware.HandlerFunc) middleware.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
|
||||||
servedFcgi := false
|
servedFcgi := false
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
|
@ -38,15 +37,13 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
// Get absolute file paths
|
// Get absolute file paths
|
||||||
absPath, err := filepath.Abs(root + r.URL.Path)
|
absPath, err := filepath.Abs(root + r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO!
|
return http.StatusInternalServerError, err
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get absolute file paths
|
// Get absolute file path to website root
|
||||||
absRootPath, err := filepath.Abs(root)
|
absRootPath, err := filepath.Abs(root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO!
|
return http.StatusInternalServerError, err
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Separate remote IP and port
|
// Separate remote IP and port
|
||||||
|
@ -73,17 +70,17 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
|
|
||||||
fcgi, err := Dial("tcp", rule.address)
|
fcgi, err := Dial("tcp", rule.address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO!
|
return http.StatusBadGateway, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := fcgi.Get(env)
|
resp, err := fcgi.Get(env)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
// TODO!
|
return http.StatusBadGateway, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO!
|
return http.StatusBadGateway, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, vals := range resp.Header {
|
for key, vals := range resp.Header {
|
||||||
|
@ -100,8 +97,10 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !servedFcgi {
|
if !servedFcgi {
|
||||||
next(w, r)
|
return next(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0, nil
|
||||||
}
|
}
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue