From 0da66f85f0a376bd95674bcc49a0ee70d8d744b6 Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Mon, 26 Jun 2023 18:04:48 +0200 Subject: [PATCH] fix: Return proper values, when worker is in remote-content mode * When worker works in remote-content mode and address is set to some URL, then the Transmit function should return values according to result of HTTP request POST --- internal/work/dispatcher.go | 66 ++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/internal/work/dispatcher.go b/internal/work/dispatcher.go index 934efde8..cac0d4aa 100644 --- a/internal/work/dispatcher.go +++ b/internal/work/dispatcher.go @@ -274,35 +274,47 @@ func (d *Dispatcher) Transmit(sender dbus.Sender, addr string, messageID string, if err != nil { return TransmitResponseErr, nil, nil, NewDBusError("Transmit", fmt.Sprintf("cannot read HTTP response body: %v", err)) } - resp.Body.Close() - } - } - ch := make(chan yggdrasil.Response) - d.Outbound <- struct { - Data yggdrasil.Data - Resp chan yggdrasil.Response - }{ - Data: yggdrasil.Data{ - Type: yggdrasil.MessageTypeData, - MessageID: messageID, - ResponseTo: responseTo, - Version: 1, - Sent: time.Now(), - Directive: addr, - Metadata: metadata, - Content: data, - }, - Resp: ch, - } + err = resp.Body.Close() + if err != nil { + return TransmitResponseErr, nil, nil, NewDBusError("Transmit", fmt.Sprintf("cannot close HTTP response body: %v", err)) + } + responseCode = resp.StatusCode + responseMetadata = make(map[string]string) + for header := range resp.Header { + responseMetadata[header] = resp.Header.Get(header) + } + responseData = data + } else { + return TransmitResponseErr, nil, nil, NewDBusError("Transmit", fmt.Sprintf("URL: '%v' has no scheme", addr)) + } + } else { + ch := make(chan yggdrasil.Response) + d.Outbound <- struct { + Data yggdrasil.Data + Resp chan yggdrasil.Response + }{ + Data: yggdrasil.Data{ + Type: yggdrasil.MessageTypeData, + MessageID: messageID, + ResponseTo: responseTo, + Version: 1, + Sent: time.Now(), + Directive: addr, + Metadata: metadata, + Content: data, + }, + Resp: ch, + } - select { - case resp := <-ch: - responseCode = resp.Code - responseMetadata = resp.Metadata - responseData = resp.Data - case <-time.After(1 * time.Second): - return TransmitResponseErr, nil, nil, NewDBusError("com.redhat.Yggdrasil1.Dispatcher1.Transmit", "timeout reached waiting for response") + select { + case resp := <-ch: + responseCode = resp.Code + responseMetadata = resp.Metadata + responseData = resp.Data + case <-time.After(1 * time.Second): + return TransmitResponseErr, nil, nil, NewDBusError("com.redhat.Yggdrasil1.Dispatcher1.Transmit", "timeout reached waiting for response") + } } return }