From 5afc375042f6c93b31822eab8d65ab07fb5bc4c2 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 25 Feb 2025 22:14:27 +0100 Subject: [PATCH] Set user agent in getting HTTP status code Close #8 --- src/configloader/HttpGetStatusCode.go | 28 +++++++++++++++++++++++ src/configloader/ValidateInstanceLink.go | 5 ++-- src/configloader/ValidateInstitutionId.go | 5 ++-- src/configloader/configloader.go | 1 + 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/configloader/HttpGetStatusCode.go diff --git a/src/configloader/HttpGetStatusCode.go b/src/configloader/HttpGetStatusCode.go new file mode 100644 index 0000000..21f6ee9 --- /dev/null +++ b/src/configloader/HttpGetStatusCode.go @@ -0,0 +1,28 @@ +package configloader + +import ( + "net/http" +) + +// Returns the HTTP status code for a url while sending requests using +// a user-agent specific to the app. +func HttpGetStatusCode(url string) (int, error) { + + // create HTTP request + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return 0, err + } + + // set User-Agent header + req.Header.Set("User-Agent", "museum-digital-uploader") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return 0, err + } + defer resp.Body.Close() + + return resp.StatusCode, nil + +} diff --git a/src/configloader/ValidateInstanceLink.go b/src/configloader/ValidateInstanceLink.go index c8067ab..9a7d3e3 100644 --- a/src/configloader/ValidateInstanceLink.go +++ b/src/configloader/ValidateInstanceLink.go @@ -3,7 +3,6 @@ package configloader import ( "errors" "net/url" - "net/http" "strings" ) @@ -29,11 +28,11 @@ func ValidateInstanceLink(instanceUrl string) (string, error) { Host: parsed.Host, Path: "/musdb", } - resp, err := http.Get(musdbUrl.String()) + statusCode, err := HttpGetStatusCode(musdbUrl.String()) if err != nil { return "", err } - if resp.StatusCode > 399 { + if statusCode > 399 { return "", errors.New("The museum-digital subdomain does not contain a musdb path") } diff --git a/src/configloader/ValidateInstitutionId.go b/src/configloader/ValidateInstitutionId.go index 4fd0bf7..19c279d 100644 --- a/src/configloader/ValidateInstitutionId.go +++ b/src/configloader/ValidateInstitutionId.go @@ -2,7 +2,6 @@ package configloader import ( "errors" - "net/http" "strconv" ) @@ -16,12 +15,12 @@ func ValidateInstitutionId(institutionId int, instanceUrl string) (int, error) { return 0, errors.New("Institution ID cannot be negative") } - resp, err := http.Get(instanceUrl + "/institution/" + strconv.Itoa(institutionId)) + statusCode, err := HttpGetStatusCode(instanceUrl + "/institution/" + strconv.Itoa(institutionId)) if err != nil { return 0, err } - if resp.StatusCode != 200 { + if statusCode != 200 { return 0, errors.New("The institution page does not respond with HTTP 200, the institution does not seem to exist") } diff --git a/src/configloader/configloader.go b/src/configloader/configloader.go index 3e12417..385523d 100644 --- a/src/configloader/configloader.go +++ b/src/configloader/configloader.go @@ -9,6 +9,7 @@ import ( type MDWebDavUploaderConfig struct { InstanceLink string `json:"instance"` + Username string `json:"username"` Mail string `json:"mail"` WebDavAuthToken string `json:"token"` InstitutionId int `json:"institution_id"`