Skip to content

Commit a0a7ccc

Browse files
committed
git-bundle-web-server: allow serving over HTTPS
Add '--cert' and '--key' options to 'git-bundle-web-server' to specify paths to a X.509 SSL certificate and private key (respectively) to be used to configure HTTPS. If both options are specified, the web server is hosted with 'ListenAndServeTLS()' using the given credentials; if both are not specified, the server is hosted (as it is now) with 'ListenAndServe()'. If only one of the two options is specified, an error (with usage) is thrown and the server is not hosted. Signed-off-by: Victoria Dye <[email protected]>
1 parent da780f2 commit a0a7ccc

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

cmd/git-bundle-web-server/main.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,25 @@ func serve(w http.ResponseWriter, r *http.Request) {
7474
w.Write(data)
7575
}
7676

77-
func startServer(server *http.Server, serverWaitGroup *sync.WaitGroup) {
77+
func startServer(server *http.Server,
78+
cert string, key string,
79+
serverWaitGroup *sync.WaitGroup,
80+
) {
7881
// Add to wait group
7982
serverWaitGroup.Add(1)
8083

8184
go func() {
8285
defer serverWaitGroup.Done()
8386

8487
// Return error unless it indicates graceful shutdown
85-
err := server.ListenAndServe()
86-
if err != http.ErrServerClosed {
88+
var err error
89+
if cert != "" {
90+
err = server.ListenAndServeTLS(cert, key)
91+
} else {
92+
err = server.ListenAndServe()
93+
}
94+
95+
if err != nil && err != http.ErrServerClosed {
8796
log.Fatal(err)
8897
}
8998
}()
@@ -92,15 +101,20 @@ func startServer(server *http.Server, serverWaitGroup *sync.WaitGroup) {
92101
}
93102

94103
func main() {
95-
parser := argparse.NewArgParser("git-bundle-web-server [--port <port>]")
104+
parser := argparse.NewArgParser("git-bundle-web-server [--port <port>] [--cert <filename> --key <filename>]")
96105
port := parser.String("port", "8080", "The port on which the server should be hosted")
106+
cert := parser.String("cert", "", "The path to the X.509 SSL certificate file to use in securely hosting the server")
107+
key := parser.String("key", "", "The path to the certificate's private key")
97108
parser.Parse(os.Args[1:])
98109

99110
// Additional option validation
100111
p, err := strconv.Atoi(*port)
101112
if err != nil || p < 0 || p > 65535 {
102113
parser.Usage("Invalid port '%s'.", *port)
103114
}
115+
if (*cert == "") != (*key == "") {
116+
parser.Usage("Both '--cert' and '--key' are needed to specify SSL configuration.")
117+
}
104118

105119
// Configure the server
106120
mux := http.NewServeMux()
@@ -112,7 +126,7 @@ func main() {
112126
serverWaitGroup := &sync.WaitGroup{}
113127

114128
// Start the server asynchronously
115-
startServer(server, serverWaitGroup)
129+
startServer(server, *cert, *key, serverWaitGroup)
116130

117131
// Intercept interrupt signals
118132
c := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)