Left: | ||
Right: |
OLD | NEW |
---|---|
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 |
11 | 11 |
12 package http | 12 package http |
r
2010/07/02 17:02:11
can we have all.bash build this?
| |
13 | 13 |
14 import ( | 14 import ( |
15 "bufio" | 15 "bufio" |
16 "crypto/rand" | |
17 "crypto/tls" | |
16 "fmt" | 18 "fmt" |
17 "io" | 19 "io" |
18 "log" | 20 "log" |
19 "net" | 21 "net" |
20 "os" | 22 "os" |
21 "path" | 23 "path" |
22 "strconv" | 24 "strconv" |
23 "strings" | 25 "strings" |
26 "time" | |
24 ) | 27 ) |
25 | 28 |
26 // Errors introduced by the HTTP server. | 29 // Errors introduced by the HTTP server. |
27 var ( | 30 var ( |
28 ErrWriteAfterFlush = os.NewError("Conn.Write called after Flush") | 31 ErrWriteAfterFlush = os.NewError("Conn.Write called after Flush") |
29 ErrBodyNotAllowed = os.NewError("http: response status code does not al low body") | 32 ErrBodyNotAllowed = os.NewError("http: response status code does not al low body") |
30 ErrHijacked = os.NewError("Conn has been hijacked") | 33 ErrHijacked = os.NewError("Conn has been hijacked") |
31 ) | 34 ) |
32 | 35 |
33 // Objects implementing the Handler interface can be | 36 // Objects implementing the Handler interface can be |
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
631 // } | 634 // } |
632 func ListenAndServe(addr string, handler Handler) os.Error { | 635 func ListenAndServe(addr string, handler Handler) os.Error { |
633 l, e := net.Listen("tcp", addr) | 636 l, e := net.Listen("tcp", addr) |
634 if e != nil { | 637 if e != nil { |
635 return e | 638 return e |
636 } | 639 } |
637 e = Serve(l, handler) | 640 e = Serve(l, handler) |
638 l.Close() | 641 l.Close() |
639 return e | 642 return e |
640 } | 643 } |
644 | |
645 // ListenAndServeTLS acts identically to ListenAndServe, expect that it | |
rsc1
2010/07/02 16:43:23
s/expect/except/
| |
646 // expects HTTPS connections. Additionally, files containing a certificate and | |
647 // matching private key for the server must be provided. | |
648 // | |
649 // A trivial example server is: | |
650 // | |
651 // import ( | |
652 // "http" | |
653 // "http/https" | |
rsc1
2010/07/02 16:43:23
d
| |
654 // "log" | |
655 // ) | |
656 // | |
657 // func handler(conn *http.Conn, req *http.Request) { | |
658 // conn.SetHeader("Content-Type", "text/plain") | |
659 // conn.Write([]byte("This is an example server.\n")) | |
660 // } | |
661 // | |
662 // func main() { | |
663 // http.HandleFunc("/", handler) | |
664 // 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 ) | |
rsc1
2010/07/02 16:43:23
s/https/http/
| |
666 // if err != nil { | |
667 // log.Crash(err) | |
rsc1
2010/07/02 16:43:23
log.Exit is probably better; the stack trace is no
| |
668 // } | |
669 // } | |
670 // | |
671 // 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 { | |
673 config := &tls.Config{ | |
674 Rand: rand.Reader, | |
675 Time: time.Seconds, | |
676 NextProtos: []string{"http/1.1"}, | |
677 } | |
678 | |
679 var err os.Error | |
680 config.Certificates = make([]tls.Certificate, 1) | |
681 config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) | |
682 if err != nil { | |
683 return err | |
684 } | |
685 | |
686 conn, err := net.Listen("tcp", addr) | |
687 if err != nil { | |
688 return err | |
689 } | |
690 | |
691 tlsListener := tls.NewListener(conn, config) | |
692 return Serve(tlsListener, handler) | |
693 } | |
OLD | NEW |