Skip to content
Snippets Groups Projects
Commit 2b203e8b authored by ale's avatar ale
Browse files

support remote authentication credentials in URL

parent 5fb79e7c
No related branches found
No related tags found
No related merge requests found
......@@ -8,8 +8,10 @@ import (
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
......@@ -27,11 +29,32 @@ type SyncClient interface {
type remoteServer struct {
client *http.Client
remoteURL string
username string
password string
}
func extractAuth(uri string) (string, string, string) {
parsed, err := url.Parse(uri)
if err != nil {
return uri, "", ""
}
if strings.Contains("@", parsed.Host) {
hostParts := strings.SplitN(parsed.Host, "@", 2)
parsed.Host = hostParts[1]
authParts := strings.SplitN(hostParts[0], ":", 2)
if len(authParts) == 2 {
return parsed.String(), authParts[0], authParts[1]
}
}
return uri, "", ""
}
func NewRemoteServer(remoteURL string) *remoteServer {
remoteURL, username, password := extractAuth(remoteURL)
return &remoteServer{
remoteURL: remoteURL,
username: username,
password: password,
client: &http.Client{},
}
}
......@@ -48,6 +71,9 @@ func (r *remoteServer) DiffRequest(diffreq *diffRequest) (*diffResponse, error)
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if r.username != "" {
req.SetBasicAuth(r.username, r.password)
}
resp, err := r.client.Do(req)
if err != nil {
return nil, err
......@@ -120,7 +146,10 @@ func (r *remoteServer) SendBook(book *Book, files []*File) error {
if err != nil {
return err
}
req.Header.Add("Content-Type", w.FormDataContentType())
req.Header.Set("Content-Type", w.FormDataContentType())
if r.username != "" {
req.SetBasicAuth(r.username, r.password)
}
resp, err := r.client.Do(req)
if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment