Skip to content

Commit 2da69dd

Browse files
Merge pull request #678 from joereuss12/project-name-to-user-agent-branch
Project name added to user-agent
2 parents b8b743f + 5455bc4 commit 2da69dd

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

client/handle_http.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func download_http(sourceUrl *url.URL, destination string, payload *payloadStruc
353353
// Start the workers
354354
for i := 1; i <= 5; i++ {
355355
wg.Add(1)
356-
go startDownloadWorker(sourceUrl.Path, destination, token, transfers, &wg, workChan, results)
356+
go startDownloadWorker(sourceUrl.Path, destination, token, transfers, payload, &wg, workChan, results)
357357
}
358358

359359
// For each file, send it to the worker
@@ -389,7 +389,7 @@ func download_http(sourceUrl *url.URL, destination string, payload *payloadStruc
389389

390390
}
391391

392-
func startDownloadWorker(source string, destination string, token string, transfers []TransferDetails, wg *sync.WaitGroup, workChan <-chan string, results chan<- TransferResults) {
392+
func startDownloadWorker(source string, destination string, token string, transfers []TransferDetails, payload *payloadStruct, wg *sync.WaitGroup, workChan <-chan string, results chan<- TransferResults) {
393393

394394
defer wg.Done()
395395
var success bool
@@ -407,7 +407,7 @@ func startDownloadWorker(source string, destination string, token string, transf
407407
for _, transfer := range transfers {
408408
transfer.Url.Path = file
409409
log.Debugln("Constructed URL:", transfer.Url.String())
410-
if downloaded, err = DownloadHTTP(transfer, finalDest, token); err != nil {
410+
if downloaded, err = DownloadHTTP(transfer, finalDest, token, payload); err != nil {
411411
log.Debugln("Failed to download:", err)
412412
var ope *net.OpError
413413
var cse *ConnectionSetupError
@@ -468,7 +468,7 @@ func parseTransferStatus(status string) (int, string) {
468468
}
469469

470470
// DownloadHTTP - Perform the actual download of the file
471-
func DownloadHTTP(transfer TransferDetails, dest string, token string) (int64, error) {
471+
func DownloadHTTP(transfer TransferDetails, dest string, token string, payload *payloadStruct) (int64, error) {
472472

473473
// Create the client, request, and context
474474
client := grab.NewClient()
@@ -513,6 +513,9 @@ func DownloadHTTP(transfer TransferDetails, dest string, token string) (int64, e
513513
// Set the headers
514514
req.HTTPRequest.Header.Set("X-Transfer-Status", "true")
515515
req.HTTPRequest.Header.Set("TE", "trailers")
516+
if payload != nil && payload.ProjectName != "" {
517+
req.HTTPRequest.Header.Set("User-Agent", payload.ProjectName)
518+
}
516519
req.WithContext(ctx)
517520

518521
// Test the transfer speed every 5 seconds
@@ -778,7 +781,7 @@ func (pr *ProgressReader) Size() int64 {
778781
}
779782

780783
// Recursively uploads a directory with all files and nested dirs, keeping file structure on server side
781-
func UploadDirectory(src string, dest *url.URL, token string, namespace namespaces.Namespace) (int64, error) {
784+
func UploadDirectory(src string, dest *url.URL, token string, namespace namespaces.Namespace, projectName string) (int64, error) {
782785
var files []string
783786
var amountDownloaded int64
784787
srcUrl := url.URL{Path: src}
@@ -798,7 +801,7 @@ func UploadDirectory(src string, dest *url.URL, token string, namespace namespac
798801
if err != nil {
799802
return 0, err
800803
}
801-
downloaded, err := UploadFile(file, &tempDest, token, namespace)
804+
downloaded, err := UploadFile(file, &tempDest, token, namespace, projectName)
802805
if err != nil {
803806
return 0, err
804807
}
@@ -813,7 +816,7 @@ func UploadDirectory(src string, dest *url.URL, token string, namespace namespac
813816
}
814817

815818
// UploadFile Uploads a file using HTTP
816-
func UploadFile(src string, origDest *url.URL, token string, namespace namespaces.Namespace) (int64, error) {
819+
func UploadFile(src string, origDest *url.URL, token string, namespace namespaces.Namespace, projectName string) (int64, error) {
817820

818821
log.Debugln("In UploadFile")
819822
log.Debugln("Dest", origDest.String())
@@ -888,6 +891,9 @@ func UploadFile(src string, origDest *url.URL, token string, namespace namespace
888891
}
889892
// Set the authorization header
890893
request.Header.Set("Authorization", "Bearer "+token)
894+
if projectName != "" {
895+
request.Header.Set("User-Agent", projectName)
896+
}
891897
var lastKnownWritten int64
892898
t := time.NewTicker(20 * time.Second)
893899
defer t.Stop()

client/handle_http_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestSlowTransfers(t *testing.T) {
193193
var err error
194194
// Do a quick timeout
195195
go func() {
196-
_, err = DownloadHTTP(transfers[0], filepath.Join(t.TempDir(), "test.txt"), "")
196+
_, err = DownloadHTTP(transfers[0], filepath.Join(t.TempDir(), "test.txt"), "", nil)
197197
finishedChannel <- true
198198
}()
199199

@@ -256,7 +256,7 @@ func TestStoppedTransfer(t *testing.T) {
256256
var err error
257257

258258
go func() {
259-
_, err = DownloadHTTP(transfers[0], filepath.Join(t.TempDir(), "test.txt"), "")
259+
_, err = DownloadHTTP(transfers[0], filepath.Join(t.TempDir(), "test.txt"), "", nil)
260260
finishedChannel <- true
261261
}()
262262

@@ -286,7 +286,7 @@ func TestConnectionError(t *testing.T) {
286286
addr := l.Addr().String()
287287
l.Close()
288288

289-
_, err = DownloadHTTP(TransferDetails{Url: url.URL{Host: addr, Scheme: "http"}, Proxy: false}, filepath.Join(t.TempDir(), "test.txt"), "")
289+
_, err = DownloadHTTP(TransferDetails{Url: url.URL{Host: addr, Scheme: "http"}, Proxy: false}, filepath.Join(t.TempDir(), "test.txt"), "", nil)
290290

291291
assert.IsType(t, &ConnectionSetupError{}, err)
292292

@@ -319,7 +319,7 @@ func TestTrailerError(t *testing.T) {
319319
assert.Equal(t, svr.URL, transfers[0].Url.String())
320320

321321
// Call DownloadHTTP and check if the error is returned correctly
322-
_, err := DownloadHTTP(transfers[0], filepath.Join(t.TempDir(), "test.txt"), "")
322+
_, err := DownloadHTTP(transfers[0], filepath.Join(t.TempDir(), "test.txt"), "", nil)
323323

324324
assert.NotNil(t, err)
325325
assert.EqualError(t, err, "transfer error: Unable to read test.txt; input/output error")

client/main.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ func getTokenName(destination *url.URL) (scheme, tokenName string) {
104104
}
105105

106106
// Do writeback to stash using SciTokens
107-
func doWriteBack(source string, destination *url.URL, namespace namespaces.Namespace, recursive bool) (int64, error) {
107+
func doWriteBack(source string, destination *url.URL, namespace namespaces.Namespace, recursive bool, projectName string) (int64, error) {
108108

109109
scitoken_contents, err := getToken(destination, namespace, true, "")
110110
if err != nil {
111111
return 0, err
112112
}
113113
if recursive {
114-
return UploadDirectory(source, destination, scitoken_contents, namespace)
114+
return UploadDirectory(source, destination, scitoken_contents, namespace, projectName)
115115
} else {
116-
return UploadFile(source, destination, scitoken_contents, namespace)
116+
return UploadFile(source, destination, scitoken_contents, namespace, projectName)
117117
}
118118
}
119119

@@ -543,7 +543,7 @@ func DoPut(localObject string, remoteDestination string, recursive bool) (bytesT
543543
log.Errorln(err)
544544
return 0, errors.New("Failed to get namespace information from source")
545545
}
546-
uploadedBytes, err := doWriteBack(localObjectUrl.Path, remoteDestUrl, ns, recursive)
546+
uploadedBytes, err := doWriteBack(localObjectUrl.Path, remoteDestUrl, ns, recursive, "")
547547
AddError(err)
548548
return uploadedBytes, err
549549

@@ -646,7 +646,7 @@ func DoGet(remoteObject string, localDestination string, recursive bool) (bytesT
646646
//Fill out the payload as much as possible
647647
payload.filename = remoteObjectUrl.Path
648648

649-
parse_job_ad(payload)
649+
parse_job_ad(&payload)
650650

651651
payload.start1 = time.Now().Unix()
652652

@@ -764,6 +764,9 @@ func DoStashCPSingle(sourceFile string, destination string, methods []string, re
764764
return 0, errors.New("Do not understand destination scheme")
765765
}
766766

767+
payload := payloadStruct{}
768+
parse_job_ad(&payload)
769+
767770
// Get the namespace of the remote filesystem
768771
// For write back, it will be the destination
769772
// For read it will be the source.
@@ -778,7 +781,7 @@ func DoStashCPSingle(sourceFile string, destination string, methods []string, re
778781
log.Errorln(err)
779782
return 0, errors.New("Failed to get namespace information from destination")
780783
}
781-
uploadedBytes, err := doWriteBack(source_url.Path, dest_url, ns, recursive)
784+
uploadedBytes, err := doWriteBack(source_url.Path, dest_url, ns, recursive, payload.ProjectName)
782785
AddError(err)
783786
return uploadedBytes, err
784787
}
@@ -814,16 +817,11 @@ func DoStashCPSingle(sourceFile string, destination string, methods []string, re
814817
destination = path.Join(destPath, sourceFilename)
815818
}
816819

817-
payload := payloadStruct{}
818820
payload.version = version
819821

820822
//Fill out the payload as much as possible
821823
payload.filename = source_url.Path
822824

823-
// ??
824-
825-
parse_job_ad(payload)
826-
827825
payload.start1 = time.Now().Unix()
828826

829827
// Go thru the download methods
@@ -918,7 +916,7 @@ func get_ips(name string) []string {
918916

919917
}
920918

921-
func parse_job_ad(payload payloadStruct) { // TODO: needs the payload
919+
func parse_job_ad(payload *payloadStruct) {
922920

923921
//Parse the .job.ad file for the Owner (username) and ProjectName of the callee.
924922

@@ -940,18 +938,34 @@ func parse_job_ad(payload payloadStruct) { // TODO: needs the payload
940938
}
941939

942940
// Get all matches from file
943-
classadRegex, e := regexp.Compile(`^\s*(Owner|ProjectName)\s=\s"(.*)"`)
941+
// Note: This appears to be invalid regex but is the only thing that appears to work. This way it successfully finds our matches
942+
classadRegex, e := regexp.Compile(`^*\s*(Owner|ProjectName)\s=\s"(.*)"`)
944943
if e != nil {
945944
log.Fatal(e)
946945
}
947946

948947
matches := classadRegex.FindAll(b, -1)
949-
950948
for _, match := range matches {
951-
if string(match[0]) == "Owner" {
952-
payload.Owner = string(match[1])
953-
} else if string(match) == "ProjectName" {
954-
payload.ProjectName = string(match[1])
949+
matchString := strings.TrimSpace(string(match))
950+
951+
if strings.HasPrefix(matchString, "Owner") {
952+
matchParts := strings.Split(strings.TrimSpace(matchString), "=")
953+
954+
if len(matchParts) == 2 { // just confirm we get 2 parts of the string
955+
matchValue := strings.TrimSpace(matchParts[1])
956+
matchValue = strings.Trim(matchValue, "\"") //trim any "" around the match if present
957+
payload.Owner = matchValue
958+
}
959+
}
960+
961+
if strings.HasPrefix(matchString, "ProjectName") {
962+
matchParts := strings.Split(strings.TrimSpace(matchString), "=")
963+
964+
if len(matchParts) == 2 { // just confirm we get 2 parts of the string
965+
matchValue := strings.TrimSpace(matchParts[1])
966+
matchValue = strings.Trim(matchValue, "\"") //trim any "" around the match if present
967+
payload.ProjectName = matchValue
968+
}
955969
}
956970
}
957971

client/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,5 @@ func TestParseNoJobAd(t *testing.T) {
331331
os.Setenv("_CONDOR_JOB_AD", path)
332332

333333
payload := payloadStruct{}
334-
parse_job_ad(payload)
334+
parse_job_ad(&payload)
335335
}

0 commit comments

Comments
 (0)