~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/http/server.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
        "net"
21
21
        "os"
22
22
        "path"
23
 
        "runtime"
 
23
        "runtime/debug"
24
24
        "strconv"
25
25
        "strings"
26
26
        "sync"
152
152
        c.buf = bufio.NewReadWriter(br, bw)
153
153
 
154
154
        if tlsConn, ok := rwc.(*tls.Conn); ok {
 
155
                tlsConn.Handshake()
155
156
                c.tlsState = new(tls.ConnectionState)
156
157
                *c.tlsState = tlsConn.ConnectionState()
157
158
        }
254
255
                }
255
256
        }
256
257
 
257
 
        if w.header.Get("Date") == "" {
 
258
        if _, ok := w.header["Date"]; !ok {
258
259
                w.Header().Set("Date", time.UTC().Format(TimeFormat))
259
260
        }
260
261
 
314
315
                w.closeAfterReply = true
315
316
        }
316
317
 
 
318
        if w.header.Get("Connection") == "close" {
 
319
                w.closeAfterReply = true
 
320
        }
 
321
 
317
322
        // Cannot use Content-Length with non-identity Transfer-Encoding.
318
323
        if w.chunking {
319
324
                w.header.Del("Content-Length")
405
410
 
406
411
        // Is it a broken browser?
407
412
        var msg string
408
 
        switch agent := w.req.UserAgent; {
 
413
        switch agent := w.req.UserAgent(); {
409
414
        case strings.Contains(agent, "MSIE"):
410
415
                msg = "Internet Explorer"
411
416
        case strings.Contains(agent, "Chrome/"):
416
421
        msg += " would ignore this error page if this text weren't here.\n"
417
422
 
418
423
        // Is it text?  ("Content-Type" is always in the map)
419
 
        baseType := strings.Split(w.header.Get("Content-Type"), ";", 2)[0]
 
424
        baseType := strings.SplitN(w.header.Get("Content-Type"), ";", 2)[0]
420
425
        switch baseType {
421
426
        case "text/html":
422
427
                io.WriteString(w, "<!-- ")
490
495
                }
491
496
                c.rwc.Close()
492
497
 
493
 
                // TODO(rsc,bradfitz): this is boilerplate. move it to runtime.Stack()
494
498
                var buf bytes.Buffer
495
499
                fmt.Fprintf(&buf, "http: panic serving %v: %v\n", c.remoteAddr, err)
496
 
                for i := 1; i < 20; i++ {
497
 
                        pc, file, line, ok := runtime.Caller(i)
498
 
                        if !ok {
499
 
                                break
500
 
                        }
501
 
                        var name string
502
 
                        f := runtime.FuncForPC(pc)
503
 
                        if f != nil {
504
 
                                name = f.Name()
505
 
                        } else {
506
 
                                name = fmt.Sprintf("%#x", pc)
507
 
                        }
508
 
                        fmt.Fprintf(&buf, "  %s %s:%d\n", name, file, line)
509
 
                }
 
500
                buf.Write(debug.Stack())
510
501
                log.Print(buf.String())
511
502
        }()
512
503
 
584
575
// Handler object that calls f.
585
576
type HandlerFunc func(ResponseWriter, *Request)
586
577
 
587
 
// ServeHTTP calls f(w, req).
 
578
// ServeHTTP calls f(w, r).
588
579
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
589
580
        f(w, r)
590
581
}
605
596
// that replies to each request with a ``404 page not found'' reply.
606
597
func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
607
598
 
 
599
// StripPrefix returns a handler that serves HTTP requests
 
600
// by removing the given prefix from the request URL's Path
 
601
// and invoking the handler h. StripPrefix handles a
 
602
// request for a path that doesn't begin with prefix by
 
603
// replying with an HTTP 404 not found error.
 
604
func StripPrefix(prefix string, h Handler) Handler {
 
605
        return HandlerFunc(func(w ResponseWriter, r *Request) {
 
606
                if !strings.HasPrefix(r.URL.Path, prefix) {
 
607
                        NotFound(w, r)
 
608
                        return
 
609
                }
 
610
                r.URL.Path = r.URL.Path[len(prefix):]
 
611
                h.ServeHTTP(w, r)
 
612
        })
 
613
}
 
614
 
608
615
// Redirect replies to the request with a redirect to url,
609
616
// which may be a path relative to the request path.
610
617
func Redirect(w ResponseWriter, r *Request, url string, code int) {
922
929
 
923
930
// ListenAndServeTLS acts identically to ListenAndServe, except that it
924
931
// expects HTTPS connections. Additionally, files containing a certificate and
925
 
// matching private key for the server must be provided.
 
932
// matching private key for the server must be provided. If the certificate
 
933
// is signed by a certificate authority, the certFile should be the concatenation
 
934
// of the server's certificate followed by the CA's certificate.
926
935
//
927
936
// A trivial example server is:
928
937
//
947
956
//
948
957
// One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem.
949
958
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler) os.Error {
 
959
        server := &Server{Addr: addr, Handler: handler}
 
960
        return server.ListenAndServeTLS(certFile, keyFile)
 
961
}
 
962
 
 
963
// ListenAndServeTLS listens on the TCP network address srv.Addr and
 
964
// then calls Serve to handle requests on incoming TLS connections.
 
965
//
 
966
// Filenames containing a certificate and matching private key for
 
967
// the server must be provided. If the certificate is signed by a
 
968
// certificate authority, the certFile should be the concatenation
 
969
// of the server's certificate followed by the CA's certificate.
 
970
//
 
971
// If srv.Addr is blank, ":https" is used.
 
972
func (s *Server) ListenAndServeTLS(certFile, keyFile string) os.Error {
 
973
        addr := s.Addr
 
974
        if addr == "" {
 
975
                addr = ":https"
 
976
        }
950
977
        config := &tls.Config{
951
978
                Rand:       rand.Reader,
952
979
                Time:       time.Seconds,
966
993
        }
967
994
 
968
995
        tlsListener := tls.NewListener(conn, config)
969
 
        return Serve(tlsListener, handler)
 
996
        return s.Serve(tlsListener)
970
997
}
971
998
 
972
999
// TimeoutHandler returns a Handler that runs h with the given time limit.