From 3b9dfe2c2219440af2073072e6b680668b968bb1 Mon Sep 17 00:00:00 2001
From: Rutger Broekhoff
Date: Wed, 19 Mar 2025 21:44:58 +0100
Subject: Make User-Agent configurable

---
 icalproxy.go | 37 ++++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)

(limited to 'icalproxy.go')

diff --git a/icalproxy.go b/icalproxy.go
index 77ced94..5f775ba 100644
--- a/icalproxy.go
+++ b/icalproxy.go
@@ -22,10 +22,11 @@ func main() {
 	}
 
 	handler := handler{
-		calURL: url.URL(cfg.CalendarURL.v),
-		ignore: cfg.Ignore,
-		tokens: cfg.UserTokens,
-		amsTz:  amsTz,
+		calURL:    url.URL(cfg.CalendarURL.v),
+		ignore:    cfg.Ignore,
+		tokens:    cfg.UserTokens,
+		amsTz:     amsTz,
+		userAgent: cfg.UserAgent,
 	}
 
 	mux := http.ServeMux{}
@@ -44,10 +45,11 @@ func main() {
 }
 
 type handler struct {
-	ignore ignoreRules
-	tokens map[string]userToken
-	calURL url.URL
-	amsTz  *time.Location
+	ignore    ignoreRules
+	tokens    map[string]userToken
+	calURL    url.URL
+	amsTz     *time.Location
+	userAgent string
 }
 
 func (h handler) makeTokenURL(token string) string {
@@ -63,13 +65,26 @@ func (h handler) handle(w http.ResponseWriter, r *http.Request) {
 	proxyUserID := r.URL.Query().Get("proxy_user_id")
 	proxyToken := r.URL.Query().Get("proxy_token")
 	if !userOK(h.tokens, proxyUserID, []byte(proxyToken)) {
-		http.Error(w, "Bad proxy_user_id or proxy_token", http.StatusUnauthorized)
+		http.Error(w, "Bad proxy_user_id or proxy_token",
+			http.StatusUnauthorized)
 		return
 	}
 
-	resp, err := http.Get(h.makeTokenURL(brightspaceToken))
+	req, err := http.NewRequestWithContext(r.Context(), http.MethodGet,
+		h.makeTokenURL(brightspaceToken), nil)
 	if err != nil {
-		http.Error(w, "Error sending request to Brightspace", http.StatusInternalServerError)
+		http.Error(w, "Error constructing HTTP request for Brightspace",
+			http.StatusInternalServerError)
+		return
+	}
+	if h.userAgent != "" {
+		req.Header.Add("User-Agent", h.userAgent)
+	}
+
+	resp, err := http.DefaultClient.Do(req)
+	if err != nil {
+		http.Error(w, "Error sending request to Brightspace",
+			http.StatusInternalServerError)
 		return
 	}
 
-- 
cgit v1.2.3