Skip to content

Commit 13b8895

Browse files
committed
Change success and error pages lifetime sope to be per "session", and
remove the option error page string interpolation.
1 parent 041d4f8 commit 13b8895

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

apps/internal/local/server.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ type Result struct {
5353
// Server is an HTTP server.
5454
type Server struct {
5555
// Addr is the address the server is listening on.
56-
Addr string
57-
resultCh chan Result
58-
s *http.Server
59-
reqState string
56+
Addr string
57+
resultCh chan Result
58+
s *http.Server
59+
reqState string
60+
optionSuccessPage []byte
61+
optionErrorPage []byte
6062
}
6163

6264
// New creates a local HTTP server and starts it.
@@ -84,19 +86,13 @@ func New(reqState string, port int, successPage []byte, errorPage []byte) (*Serv
8486
return nil, err
8587
}
8688

87-
if len(successPage) > 0 {
88-
okPage = successPage
89-
}
90-
91-
if len(errorPage) > 0 {
92-
failPage = errorPage
93-
}
94-
9589
serv := &Server{
96-
Addr: fmt.Sprintf("http://localhost:%s", portStr),
97-
s: &http.Server{Addr: "localhost:0", ReadHeaderTimeout: time.Second},
98-
reqState: reqState,
99-
resultCh: make(chan Result, 1),
90+
Addr: fmt.Sprintf("http://localhost:%s", portStr),
91+
s: &http.Server{Addr: "localhost:0", ReadHeaderTimeout: time.Second},
92+
reqState: reqState,
93+
resultCh: make(chan Result, 1),
94+
optionSuccessPage: successPage,
95+
optionErrorPage: errorPage,
10096
}
10197
serv.s.Handler = http.HandlerFunc(serv.handler)
10298

@@ -153,7 +149,11 @@ func (s *Server) handler(w http.ResponseWriter, r *http.Request) {
153149
desc := html.EscapeString(q.Get("error_description"))
154150
// Note: It is a little weird we handle some errors by not going to the failPage. If they all should,
155151
// change this to s.error() and make s.error() write the failPage instead of an error code.
152+
if len(s.optionErrorPage) > 0 {
153+
_, _ = w.Write(s.optionErrorPage)
154+
} else {
156155
_, _ = w.Write([]byte(fmt.Sprintf(failPage, headerErr, desc)))
156+
}
157157
s.putResult(Result{Err: fmt.Errorf(desc)})
158158
return
159159
}
@@ -175,7 +175,11 @@ func (s *Server) handler(w http.ResponseWriter, r *http.Request) {
175175
return
176176
}
177177

178-
_, _ = w.Write(okPage)
178+
if len(s.optionSuccessPage) > 0 {
179+
_, _ = w.Write(s.optionSuccessPage)
180+
} else {
181+
_, _ = w.Write(okPage)
182+
}
179183
s.putResult(Result{Code: code})
180184
}
181185

0 commit comments

Comments
 (0)