~hduran-8/+junk/caddy

« back to all changes in this revision

Viewing changes to debian/gocode/src/github.com/mholt/caddy/caddyhttp/fastcgi/fastcgi.go

  • Committer: Horacio Durán
  • Date: 2017-01-20 16:21:20 UTC
  • Revision ID: horacio.duran@canonical.com-20170120162120-l82mfqwmsclnk838
Upgrade to 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import (
7
7
        "errors"
8
8
        "io"
 
9
        "net"
9
10
        "net/http"
10
11
        "os"
11
12
        "path"
12
13
        "path/filepath"
13
14
        "strconv"
14
15
        "strings"
 
16
        "time"
15
17
 
16
18
        "github.com/mholt/caddy/caddyhttp/httpserver"
17
19
)
31
33
        ServerPort      string
32
34
}
33
35
 
 
36
// When a rewrite is performed, a header field of this name
 
37
// is added to the request
 
38
// It contains the original request URI before the rewrite.
 
39
const internalRewriteFieldName = "Caddy-Rewrite-Original-URI"
 
40
 
34
41
// ServeHTTP satisfies the httpserver.Handler interface.
35
42
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
36
43
        for _, rule := range h.Rules {
74
81
                        // Connect to FastCGI gateway
75
82
                        fcgiBackend, err := rule.dialer.Dial()
76
83
                        if err != nil {
 
84
                                if err, ok := err.(net.Error); ok && err.Timeout() {
 
85
                                        return http.StatusGatewayTimeout, err
 
86
                                }
77
87
                                return http.StatusBadGateway, err
78
88
                        }
 
89
                        defer fcgiBackend.Close()
 
90
                        fcgiBackend.SetReadTimeout(rule.ReadTimeout)
 
91
                        fcgiBackend.SetSendTimeout(rule.SendTimeout)
79
92
 
80
93
                        var resp *http.Response
81
94
                        contentLength, _ := strconv.Atoi(r.Header.Get("Content-Length"))
90
103
                                resp, err = fcgiBackend.Post(env, r.Method, r.Header.Get("Content-Type"), r.Body, contentLength)
91
104
                        }
92
105
 
93
 
                        if err != nil && err != io.EOF {
94
 
                                return http.StatusBadGateway, err
 
106
                        if err != nil {
 
107
                                if err, ok := err.(net.Error); ok && err.Timeout() {
 
108
                                        return http.StatusGatewayTimeout, err
 
109
                                } else if err != io.EOF {
 
110
                                        return http.StatusBadGateway, err
 
111
                                }
95
112
                        }
96
113
 
97
114
                        // Write response header
103
120
                                return http.StatusBadGateway, err
104
121
                        }
105
122
 
106
 
                        defer rule.dialer.Close(fcgiBackend)
107
 
 
108
123
                        // Log any stderr output from upstream
109
 
                        if fcgiBackend.stderr.Len() != 0 {
 
124
                        if stderr := fcgiBackend.StdErr(); stderr.Len() != 0 {
110
125
                                // Remove trailing newline, error logger already does this.
111
 
                                err = LogError(strings.TrimSuffix(fcgiBackend.stderr.String(), "\n"))
 
126
                                err = LogError(strings.TrimSuffix(stderr.String(), "\n"))
112
127
                        }
113
128
 
114
129
                        // Normally we would return the status code if it is an error status (>= 400),
201
216
        // or it might have been rewritten internally by the rewrite middleware (see issue #256).
202
217
        // If it was rewritten, there will be a header indicating the original URL,
203
218
        // which is needed to get the correct RequestURI value for PHP apps.
204
 
        const internalRewriteFieldName = "Caddy-Rewrite-Original-URI"
205
219
        reqURI := r.URL.RequestURI()
206
220
        if origURI := r.Header.Get(internalRewriteFieldName); origURI != "" {
207
221
                reqURI = origURI
208
 
                r.Header.Del(internalRewriteFieldName)
209
222
        }
210
223
 
211
224
        // Some variables are unused but cleared explicitly to prevent
258
271
                env[envVar[0]] = replacer.Replace(envVar[1])
259
272
        }
260
273
 
261
 
        // Add all HTTP headers to env variables
 
274
        // Add all HTTP headers (except Caddy-Rewrite-Original-URI ) to env variables
262
275
        for field, val := range r.Header {
 
276
                if strings.ToLower(field) == strings.ToLower(internalRewriteFieldName) {
 
277
                        continue
 
278
                }
263
279
                header := strings.ToUpper(field)
264
280
                header = headerNameReplacer.Replace(header)
265
281
                env["HTTP_"+header] = strings.Join(val, ", ")
266
282
        }
267
 
 
268
283
        return env, nil
269
284
}
270
285
 
296
311
        // Ignored paths
297
312
        IgnoredSubPaths []string
298
313
 
 
314
        // The duration used to set a deadline when reading from the FastCGI server.
 
315
        ReadTimeout time.Duration
 
316
 
 
317
        // The duration used to set a deadline when sending to the FastCGI server.
 
318
        SendTimeout time.Duration
 
319
 
299
320
        // FCGI dialer
300
321
        dialer dialer
301
322
}