LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // HTTP server. See RFC 2616. | 5 // HTTP server. See RFC 2616. |
6 | 6 |
7 // TODO(rsc): | 7 // TODO(rsc): |
8 // logging | 8 // logging |
9 // cgi support | 9 // cgi support |
10 // post support | 10 // post support |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 func ListenAndServe(addr string, handler Handler) os.Error { | 635 func ListenAndServe(addr string, handler Handler) os.Error { |
636 l, e := net.Listen("tcp", addr) | 636 l, e := net.Listen("tcp", addr) |
637 if e != nil { | 637 if e != nil { |
638 return e | 638 return e |
639 } | 639 } |
640 e = Serve(l, handler) | 640 e = Serve(l, handler) |
641 l.Close() | 641 l.Close() |
642 return e | 642 return e |
643 } | 643 } |
644 | 644 |
645 // ListenAndServe acts identically to ListenAndServe, expect that it | 645 // ListenAndServeTLS acts identically to ListenAndServe, expect that it |
646 // expects HTTPS connections. Additionally, files containing a certificate and | 646 // except HTTPS connections. Additionally, files containing a certificate and |
647 // matching private key for the server must be provided. | 647 // matching private key for the server must be provided. |
648 // | 648 // |
649 // A trivial example server is: | 649 // A trivial example server is: |
650 // | 650 // |
651 // import ( | 651 // import ( |
652 // "http" | 652 // "http" |
653 // "http/https" | |
654 // "log" | 653 // "log" |
655 // ) | 654 // ) |
656 // | 655 // |
657 // func handler(conn *http.Conn, req *http.Request) { | 656 // func handler(conn *http.Conn, req *http.Request) { |
658 // conn.SetHeader("Content-Type", "text/plain") | 657 // conn.SetHeader("Content-Type", "text/plain") |
659 // conn.Write([]byte("This is an example server.\n")) | 658 // conn.Write([]byte("This is an example server.\n")) |
660 // } | 659 // } |
661 // | 660 // |
662 // func main() { | 661 // func main() { |
663 // http.HandleFunc("/", handler) | 662 // http.HandleFunc("/", handler) |
664 // log.Stdoutf("About to listen on 10443. Go to https://127.0.0.1:1
0443/") | 663 // log.Stdoutf("About to listen on 10443. Go to https://127.0.0.1:1
0443/") |
665 //» » err := https.ListenAndServe(":10443", "cert.pem", "key.pem", nil
) | 664 //» » err := http.ListenAndServe(":10443", "cert.pem", "key.pem", nil) |
666 // if err != nil { | 665 // if err != nil { |
667 //» » » log.Crash(err) | 666 //» » » log.Exit(err) |
668 // } | 667 // } |
669 // } | 668 // } |
670 // | 669 // |
671 // One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem. | 670 // One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem. |
672 func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Han
dler) os.Error { | 671 func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Han
dler) os.Error { |
673 config := &tls.Config{ | 672 config := &tls.Config{ |
674 Rand: rand.Reader, | 673 Rand: rand.Reader, |
675 Time: time.Seconds, | 674 Time: time.Seconds, |
676 NextProtos: []string{"http/1.1"}, | 675 NextProtos: []string{"http/1.1"}, |
677 } | 676 } |
678 | 677 |
679 var err os.Error | 678 var err os.Error |
680 config.Certificates = make([]tls.Certificate, 1) | 679 config.Certificates = make([]tls.Certificate, 1) |
681 config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) | 680 config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) |
682 if err != nil { | 681 if err != nil { |
683 return err | 682 return err |
684 } | 683 } |
685 | 684 |
686 conn, err := net.Listen("tcp", addr) | 685 conn, err := net.Listen("tcp", addr) |
687 if err != nil { | 686 if err != nil { |
688 return err | 687 return err |
689 } | 688 } |
690 | 689 |
691 tlsListener := tls.NewListener(conn, config) | 690 tlsListener := tls.NewListener(conn, config) |
692 return Serve(tlsListener, handler) | 691 return Serve(tlsListener, handler) |
693 } | 692 } |
LEFT | RIGHT |