1
1
package httpserver
2
2
3
3
import (
4
+ "crypto/tls"
4
5
"errors"
5
6
"net/http"
6
7
"os"
@@ -23,7 +24,9 @@ type Options struct {
23
24
BasicAuthReal string
24
25
Verbose bool
25
26
Sandbox bool
27
+ HTTP1Only bool
26
28
MaxFileSize int // 50Mb
29
+ MaxDumpBodySize int64
27
30
}
28
31
29
32
// HTTPServer instance
@@ -32,6 +35,9 @@ type HTTPServer struct {
32
35
layers http.Handler
33
36
}
34
37
38
+ // LayerHandler is the interface of all layer funcs
39
+ type Middleware func (http.Handler ) http.Handler
40
+
35
41
// New http server instance with options
36
42
func New (options * Options ) (* HTTPServer , error ) {
37
43
var h HTTPServer
@@ -50,18 +56,44 @@ func New(options *Options) (*HTTPServer, error) {
50
56
if options .Sandbox {
51
57
dir = SandboxFileSystem {fs : http .Dir (options .Folder ), RootFolder : options .Folder }
52
58
}
53
- h .layers = h .loglayer (http .FileServer (dir ))
59
+
60
+ httpHandler := http .FileServer (dir )
61
+ addHandler := func (newHandler Middleware ) {
62
+ httpHandler = newHandler (httpHandler )
63
+ }
64
+
65
+ // middleware
66
+ if options .EnableUpload {
67
+ addHandler (h .uploadlayer )
68
+ }
69
+
54
70
if options .BasicAuthUsername != "" || options .BasicAuthPassword != "" {
55
- h . layers = h . loglayer (h .basicauthlayer ( http . FileServer ( dir )) )
71
+ addHandler (h .basicauthlayer )
56
72
}
73
+
74
+ httpHandler = h .loglayer (httpHandler )
75
+
76
+ // add handler
77
+ h .layers = httpHandler
57
78
h .options = options
58
79
59
80
return & h , nil
60
81
}
61
82
83
+ func (t * HTTPServer ) makeHTTPServer (tlsConfig * tls.Config ) * http.Server {
84
+ httpServer := & http.Server {Addr : t .options .ListenAddress }
85
+ if t .options .HTTP1Only {
86
+ httpServer .TLSNextProto = make (map [string ]func (* http.Server , * tls.Conn , http.Handler ))
87
+ }
88
+ httpServer .TLSConfig = tlsConfig
89
+ httpServer .Handler = t .layers
90
+ return httpServer
91
+ }
92
+
62
93
// ListenAndServe requests over http
63
94
func (t * HTTPServer ) ListenAndServe () error {
64
- return http .ListenAndServe (t .options .ListenAddress , t .layers )
95
+ httpServer := t .makeHTTPServer (nil )
96
+ return httpServer .ListenAndServe ()
65
97
}
66
98
67
99
// ListenAndServeTLS requests over https
@@ -73,11 +105,7 @@ func (t *HTTPServer) ListenAndServeTLS() error {
73
105
if err != nil {
74
106
return err
75
107
}
76
- httpServer := & http.Server {
77
- Addr : t .options .ListenAddress ,
78
- TLSConfig : tlsConfig ,
79
- }
80
- httpServer .Handler = t .layers
108
+ httpServer := t .makeHTTPServer (tlsConfig )
81
109
return httpServer .ListenAndServeTLS ("" , "" )
82
110
}
83
111
return http .ListenAndServeTLS (t .options .ListenAddress , t .options .Certificate , t .options .CertificateKey , t .layers )
0 commit comments