summaryrefslogtreecommitdiff
path: root/src/http.go
diff options
context:
space:
mode:
authorJeroen Wijenbergh <jeroenwijenbergh@protonmail.com>2022-03-21 14:58:58 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-20 20:29:52 +0200
commitfc56f8770923ec1997444a8318a18be0a8397520 (patch)
tree3c6522b9b6e44ca2ad6cd94b074da78eed2c1028 /src/http.go
parentd45f5df4dc5fa9ad8abdc47c940f6baf96fdbe45 (diff)
Wireguard: Add basic support
Diffstat (limited to 'src/http.go')
-rw-r--r--src/http.go98
1 files changed, 65 insertions, 33 deletions
diff --git a/src/http.go b/src/http.go
index 1374eed..5366c7e 100644
--- a/src/http.go
+++ b/src/http.go
@@ -2,6 +2,7 @@ package eduvpn
import (
"fmt"
+ "io"
"io/ioutil"
"net/http"
"net/url"
@@ -54,15 +55,16 @@ func (e *HTTPRequestCreateError) Error() string {
return fmt.Sprintf("failed to create HTTP request with url %s and error %v", e.URL, e.Err)
}
-type HTTPOptionalParams struct {
- Headers *http.Header
-}
+type URLParameters map[string]string
-func HTTPGet(url string) ([]byte, error) {
- return HTTPGetWithOptionalParams(url, nil)
+type HTTPOptionalParams struct {
+ Headers *http.Header
+ URLParameters *URLParameters
+ Body url.Values
}
-func HTTPConstructURL(baseURL string, parameters map[string]string) (string, error) {
+// Construct an URL including on parameters
+func HTTPConstructURL(baseURL string, parameters URLParameters) (string, error) {
url, err := url.Parse(baseURL)
if err != nil {
@@ -78,59 +80,89 @@ func HTTPConstructURL(baseURL string, parameters map[string]string) (string, err
return url.String(), nil
}
+// Convenience functions
+func HTTPGet(url string) ([]byte, error) {
+ return HTTPMethodWithOpts(http.MethodGet, url, nil)
+}
-func HTTPGetWithOptionalParams(url string, opts *HTTPOptionalParams) ([]byte, error) {
- client := &http.Client{}
- req, reqErr := http.NewRequest(http.MethodGet, url, nil)
- if reqErr != nil {
- return nil, &HTTPRequestCreateError{URL: url, Err: reqErr}
+func HTTPPost(url string, body url.Values) ([]byte, error) {
+ return HTTPMethodWithOpts(http.MethodGet, url, &HTTPOptionalParams{Body: body})
+}
+
+func HTTPGetWithOpts(url string, opts *HTTPOptionalParams) ([]byte, error) {
+ return HTTPMethodWithOpts(http.MethodGet, url, opts)
+}
+
+func HTTPPostWithOpts(url string, opts *HTTPOptionalParams) ([]byte, error) {
+ return HTTPMethodWithOpts(http.MethodPost, url, opts)
+}
+
+func httpOptionalURL(url string, opts *HTTPOptionalParams) (string, error) {
+ if opts != nil && opts.URLParameters != nil {
+ url, urlErr := HTTPConstructURL(url, *opts.URLParameters)
+
+ if urlErr != nil {
+ return url, &HTTPRequestCreateError{URL: url, Err: urlErr}
+ }
+ return url, nil
}
- if opts != nil && opts.Headers != nil {
+ return url, nil
+}
+
+func httpOptionalHeaders(req *http.Request, opts *HTTPOptionalParams) {
+ // Add headers
+ if opts != nil && opts.Headers != nil && req != nil {
for k, v := range *opts.Headers {
req.Header.Add(k, v[0])
}
}
- resp, respErr := client.Do(req)
- if respErr != nil {
- return nil, &HTTPResourceError{URL: url, Err: respErr}
- }
- defer resp.Body.Close()
+}
- body, readErr := ioutil.ReadAll(resp.Body)
- if readErr != nil {
- return nil, &HTTPReadError{URL: url, Err: readErr}
+func httpOptionalBodyReader(opts *HTTPOptionalParams) io.Reader {
+ if opts != nil && opts.Body != nil {
+ return strings.NewReader(opts.Body.Encode())
}
-
- return body, nil
+ return nil
}
-func HTTPPost(url string, body url.Values) ([]byte, error) {
- return HTTPPostWithOptionalParams(url, body, nil)
-}
+func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) ([]byte, error) {
-func HTTPPostWithOptionalParams(url string, data url.Values, opts *HTTPOptionalParams) ([]byte, error) {
+ // Make sure the url contains all the parameters
+ // This can return an error,
+ // it already has the right error so so we don't wrap it further
+ url, urlErr := httpOptionalURL(url, opts)
+ if urlErr != nil {
+ return nil, urlErr
+ }
+
+ // Create a client
client := &http.Client{}
- req, reqErr := http.NewRequest(http.MethodPost, url, strings.NewReader(data.Encode()))
+
+ // Create request object with the body reader generated from the optional arguments
+ req, reqErr := http.NewRequest(method, url, httpOptionalBodyReader(opts))
if reqErr != nil {
return nil, &HTTPRequestCreateError{URL: url, Err: reqErr}
}
- if opts != nil && opts.Headers != nil {
- for k, v := range *opts.Headers {
- req.Header.Add(k, v[0])
- }
- }
- resp, respErr := client.Do(req)
+ // Make sure the headers contain all the parameters
+ httpOptionalHeaders(req, opts)
+
+ // Do request
+ resp, respErr := client.Do(req)
if respErr != nil {
return nil, &HTTPResourceError{URL: url, Err: respErr}
}
+
+ // Request successful, make sure body is closed at the end
defer resp.Body.Close()
+ // Return a string
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return nil, &HTTPReadError{URL: url, Err: readErr}
}
+ // Return the body in bytes and signal that there was no error
return body, nil
}