github.com/Kolosok86/http@v0.1.2/fcgi/child.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fcgi
     6  
     7  // This file implements FastCGI from the perspective of a child process.
     8  
     9  import (
    10  	"context"
    11  	"errors"
    12  	"fmt"
    13  	"io"
    14  	"net"
    15  	"net/http/cgi"
    16  	"os"
    17  	"strings"
    18  	"time"
    19  
    20  	"github.com/Kolosok86/http"
    21  )
    22  
    23  // request holds the state for an in-progress request. As soon as it's complete,
    24  // it's converted to an http.Request.
    25  type request struct {
    26  	pw        *io.PipeWriter
    27  	reqId     uint16
    28  	params    map[string]string
    29  	buf       [1024]byte
    30  	rawParams []byte
    31  	keepConn  bool
    32  }
    33  
    34  // envVarsContextKey uniquely identifies a mapping of CGI
    35  // environment variables to their values in a request context
    36  type envVarsContextKey struct{}
    37  
    38  func newRequest(reqId uint16, flags uint8) *request {
    39  	r := &request{
    40  		reqId:    reqId,
    41  		params:   map[string]string{},
    42  		keepConn: flags&flagKeepConn != 0,
    43  	}
    44  	r.rawParams = r.buf[:0]
    45  	return r
    46  }
    47  
    48  // parseParams reads an encoded []byte into Params.
    49  func (r *request) parseParams() {
    50  	text := r.rawParams
    51  	r.rawParams = nil
    52  	for len(text) > 0 {
    53  		keyLen, n := readSize(text)
    54  		if n == 0 {
    55  			return
    56  		}
    57  		text = text[n:]
    58  		valLen, n := readSize(text)
    59  		if n == 0 {
    60  			return
    61  		}
    62  		text = text[n:]
    63  		if int(keyLen)+int(valLen) > len(text) {
    64  			return
    65  		}
    66  		key := readString(text, keyLen)
    67  		text = text[keyLen:]
    68  		val := readString(text, valLen)
    69  		text = text[valLen:]
    70  		r.params[key] = val
    71  	}
    72  }
    73  
    74  // response implements http.ResponseWriter.
    75  type response struct {
    76  	req            *request
    77  	header         http.Header
    78  	code           int
    79  	wroteHeader    bool
    80  	wroteCGIHeader bool
    81  	w              *bufWriter
    82  }
    83  
    84  func newResponse(c *child, req *request) *response {
    85  	return &response{
    86  		req:    req,
    87  		header: http.Header{},
    88  		w:      newWriter(c.conn, typeStdout, req.reqId),
    89  	}
    90  }
    91  
    92  func (r *response) Header() http.Header {
    93  	return r.header
    94  }
    95  
    96  func (r *response) Write(p []byte) (n int, err error) {
    97  	if !r.wroteHeader {
    98  		r.WriteHeader(http.StatusOK)
    99  	}
   100  	if !r.wroteCGIHeader {
   101  		r.writeCGIHeader(p)
   102  	}
   103  	return r.w.Write(p)
   104  }
   105  
   106  func (r *response) WriteHeader(code int) {
   107  	if r.wroteHeader {
   108  		return
   109  	}
   110  	r.wroteHeader = true
   111  	r.code = code
   112  	if code == http.StatusNotModified {
   113  		// Must not have body.
   114  		r.header.Del("Content-Type")
   115  		r.header.Del("Content-Length")
   116  		r.header.Del("Transfer-Encoding")
   117  	}
   118  	if r.header.Get("Date") == "" {
   119  		r.header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
   120  	}
   121  }
   122  
   123  // writeCGIHeader finalizes the header sent to the client and writes it to the output.
   124  // p is not written by writeHeader, but is the first chunk of the body
   125  // that will be written. It is sniffed for a Content-Type if none is
   126  // set explicitly.
   127  func (r *response) writeCGIHeader(p []byte) {
   128  	if r.wroteCGIHeader {
   129  		return
   130  	}
   131  	r.wroteCGIHeader = true
   132  	fmt.Fprintf(r.w, "Status: %d %s\r\n", r.code, http.StatusText(r.code))
   133  	if _, hasType := r.header["Content-Type"]; r.code != http.StatusNotModified && !hasType {
   134  		r.header.Set("Content-Type", http.DetectContentType(p))
   135  	}
   136  	r.header.Write(r.w)
   137  	r.w.WriteString("\r\n")
   138  	r.w.Flush()
   139  }
   140  
   141  func (r *response) Flush() {
   142  	if !r.wroteHeader {
   143  		r.WriteHeader(http.StatusOK)
   144  	}
   145  	r.w.Flush()
   146  }
   147  
   148  func (r *response) Close() error {
   149  	r.Flush()
   150  	return r.w.Close()
   151  }
   152  
   153  type child struct {
   154  	conn    *conn
   155  	handler http.Handler
   156  
   157  	requests map[uint16]*request // keyed by request ID
   158  }
   159  
   160  func newChild(rwc io.ReadWriteCloser, handler http.Handler) *child {
   161  	return &child{
   162  		conn:     newConn(rwc),
   163  		handler:  handler,
   164  		requests: make(map[uint16]*request),
   165  	}
   166  }
   167  
   168  func (c *child) serve() {
   169  	defer c.conn.Close()
   170  	defer c.cleanUp()
   171  	var rec record
   172  	for {
   173  		if err := rec.read(c.conn.rwc); err != nil {
   174  			return
   175  		}
   176  		if err := c.handleRecord(&rec); err != nil {
   177  			return
   178  		}
   179  	}
   180  }
   181  
   182  var errCloseConn = errors.New("fcgi: connection should be closed")
   183  
   184  var emptyBody = io.NopCloser(strings.NewReader(""))
   185  
   186  // ErrRequestAborted is returned by Read when a handler attempts to read the
   187  // body of a request that has been aborted by the web server.
   188  var ErrRequestAborted = errors.New("fcgi: request aborted by web server")
   189  
   190  // ErrConnClosed is returned by Read when a handler attempts to read the body of
   191  // a request after the connection to the web server has been closed.
   192  var ErrConnClosed = errors.New("fcgi: connection to web server closed")
   193  
   194  func (c *child) handleRecord(rec *record) error {
   195  	req, ok := c.requests[rec.h.Id]
   196  	if !ok && rec.h.Type != typeBeginRequest && rec.h.Type != typeGetValues {
   197  		// The spec says to ignore unknown request IDs.
   198  		return nil
   199  	}
   200  
   201  	switch rec.h.Type {
   202  	case typeBeginRequest:
   203  		if req != nil {
   204  			// The server is trying to begin a request with the same ID
   205  			// as an in-progress request. This is an error.
   206  			return errors.New("fcgi: received ID that is already in-flight")
   207  		}
   208  
   209  		var br beginRequest
   210  		if err := br.read(rec.content()); err != nil {
   211  			return err
   212  		}
   213  		if br.role != roleResponder {
   214  			c.conn.writeEndRequest(rec.h.Id, 0, statusUnknownRole)
   215  			return nil
   216  		}
   217  		req = newRequest(rec.h.Id, br.flags)
   218  		c.requests[rec.h.Id] = req
   219  		return nil
   220  	case typeParams:
   221  		// NOTE(eds): Technically a key-value pair can straddle the boundary
   222  		// between two packets. We buffer until we've received all parameters.
   223  		if len(rec.content()) > 0 {
   224  			req.rawParams = append(req.rawParams, rec.content()...)
   225  			return nil
   226  		}
   227  		req.parseParams()
   228  		return nil
   229  	case typeStdin:
   230  		content := rec.content()
   231  		if req.pw == nil {
   232  			var body io.ReadCloser
   233  			if len(content) > 0 {
   234  				// body could be an io.LimitReader, but it shouldn't matter
   235  				// as long as both sides are behaving.
   236  				body, req.pw = io.Pipe()
   237  			} else {
   238  				body = emptyBody
   239  			}
   240  			go c.serveRequest(req, body)
   241  		}
   242  		if len(content) > 0 {
   243  			// TODO(eds): This blocks until the handler reads from the pipe.
   244  			// If the handler takes a long time, it might be a problem.
   245  			req.pw.Write(content)
   246  		} else {
   247  			delete(c.requests, req.reqId)
   248  			if req.pw != nil {
   249  				req.pw.Close()
   250  			}
   251  		}
   252  		return nil
   253  	case typeGetValues:
   254  		values := map[string]string{"FCGI_MPXS_CONNS": "1"}
   255  		c.conn.writePairs(typeGetValuesResult, 0, values)
   256  		return nil
   257  	case typeData:
   258  		// If the filter role is implemented, read the data stream here.
   259  		return nil
   260  	case typeAbortRequest:
   261  		delete(c.requests, rec.h.Id)
   262  		c.conn.writeEndRequest(rec.h.Id, 0, statusRequestComplete)
   263  		if req.pw != nil {
   264  			req.pw.CloseWithError(ErrRequestAborted)
   265  		}
   266  		if !req.keepConn {
   267  			// connection will close upon return
   268  			return errCloseConn
   269  		}
   270  		return nil
   271  	default:
   272  		b := make([]byte, 8)
   273  		b[0] = byte(rec.h.Type)
   274  		c.conn.writeRecord(typeUnknownType, 0, b)
   275  		return nil
   276  	}
   277  }
   278  
   279  // filterOutUsedEnvVars returns a new map of env vars without the
   280  // variables in the given envVars map that are read for creating each http.Request
   281  func filterOutUsedEnvVars(envVars map[string]string) map[string]string {
   282  	withoutUsedEnvVars := make(map[string]string)
   283  	for k, v := range envVars {
   284  		if addFastCGIEnvToContext(k) {
   285  			withoutUsedEnvVars[k] = v
   286  		}
   287  	}
   288  	return withoutUsedEnvVars
   289  }
   290  
   291  func (c *child) serveRequest(req *request, body io.ReadCloser) {
   292  	r := newResponse(c, req)
   293  	httpReq, err := cgi.RequestFromMap(req.params)
   294  	if err != nil {
   295  		// there was an error reading the request
   296  		r.WriteHeader(http.StatusInternalServerError)
   297  		c.conn.writeRecord(typeStderr, req.reqId, []byte(err.Error()))
   298  	} else {
   299  		httpReq.Body = body
   300  		withoutUsedEnvVars := filterOutUsedEnvVars(req.params)
   301  		envVarCtx := context.WithValue(httpReq.Context(), envVarsContextKey{}, withoutUsedEnvVars)
   302  		httpReq = httpReq.WithContext(envVarCtx)
   303  		c.handler.ServeHTTP(r, httpReq)
   304  	}
   305  	// Make sure we serve something even if nothing was written to r
   306  	r.Write(nil)
   307  	r.Close()
   308  	c.conn.writeEndRequest(req.reqId, 0, statusRequestComplete)
   309  
   310  	// Consume the entire body, so the host isn't still writing to
   311  	// us when we close the socket below in the !keepConn case,
   312  	// otherwise we'd send a RST. (golang.org/issue/4183)
   313  	// TODO(bradfitz): also bound this copy in time. Or send
   314  	// some sort of abort request to the host, so the host
   315  	// can properly cut off the client sending all the data.
   316  	// For now just bound it a little and
   317  	io.CopyN(io.Discard, body, 100<<20)
   318  	body.Close()
   319  
   320  	if !req.keepConn {
   321  		c.conn.Close()
   322  	}
   323  }
   324  
   325  func (c *child) cleanUp() {
   326  	for _, req := range c.requests {
   327  		if req.pw != nil {
   328  			// race with call to Close in c.serveRequest doesn't matter because
   329  			// Pipe(Reader|Writer).Close are idempotent
   330  			req.pw.CloseWithError(ErrConnClosed)
   331  		}
   332  	}
   333  }
   334  
   335  // Serve accepts incoming FastCGI connections on the listener l, creating a new
   336  // goroutine for each. The goroutine reads requests and then calls handler
   337  // to reply to them.
   338  // If l is nil, Serve accepts connections from os.Stdin.
   339  // If handler is nil, http.DefaultServeMux is used.
   340  func Serve(l net.Listener, handler http.Handler) error {
   341  	if l == nil {
   342  		var err error
   343  		l, err = net.FileListener(os.Stdin)
   344  		if err != nil {
   345  			return err
   346  		}
   347  		defer l.Close()
   348  	}
   349  	if handler == nil {
   350  		handler = http.DefaultServeMux
   351  	}
   352  	for {
   353  		rw, err := l.Accept()
   354  		if err != nil {
   355  			return err
   356  		}
   357  		c := newChild(rw, handler)
   358  		go c.serve()
   359  	}
   360  }
   361  
   362  // ProcessEnv returns FastCGI environment variables associated with the request r
   363  // for which no effort was made to be included in the request itself - the data
   364  // is hidden in the request's context. As an example, if REMOTE_USER is set for a
   365  // request, it will not be found anywhere in r, but it will be included in
   366  // ProcessEnv's response (via r's context).
   367  func ProcessEnv(r *http.Request) map[string]string {
   368  	env, _ := r.Context().Value(envVarsContextKey{}).(map[string]string)
   369  	return env
   370  }
   371  
   372  // addFastCGIEnvToContext reports whether to include the FastCGI environment variable s
   373  // in the http.Request.Context, accessible via ProcessEnv.
   374  func addFastCGIEnvToContext(s string) bool {
   375  	// Exclude things supported by net/http natively:
   376  	switch s {
   377  	case "CONTENT_LENGTH", "CONTENT_TYPE", "HTTPS",
   378  		"PATH_INFO", "QUERY_STRING", "REMOTE_ADDR",
   379  		"REMOTE_HOST", "REMOTE_PORT", "REQUEST_METHOD",
   380  		"REQUEST_URI", "SCRIPT_NAME", "SERVER_PROTOCOL":
   381  		return false
   382  	}
   383  	if strings.HasPrefix(s, "HTTP_") {
   384  		return false
   385  	}
   386  	// Explicitly include FastCGI-specific things.
   387  	// This list is redundant with the default "return true" below.
   388  	// Consider this documentation of the sorts of things we expect
   389  	// to maybe see.
   390  	switch s {
   391  	case "REMOTE_USER":
   392  		return true
   393  	}
   394  	// Unknown, so include it to be safe.
   395  	return true
   396  }