Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #1401 - bug in outgoing 'Host' HTTP Header of notifications #1403

Merged
merged 1 commit into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Fixed issues:
#1385 Fixed a bug in PATCH /entities/{EID}/attrs, about patching a relationship object to an array (this is not supported by the NGSI-LD spec, but Orion-LD supports it anyway - for now)
#1387 Part 1 of showChanges + previous values of attributes in notifications - for non-batch operations, non-distributed attributes, and non-delete operations
#1394 System Attributes in Notifications
#1401 Fixed a bug in the Host header of new notifications
1 change: 1 addition & 0 deletions src/lib/logMsg/traceLevels.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef enum TraceLevels
LmtDistOpResponseBuf, // Specific debugging of the incoming response of a distributed message
LmtDistOpResponseDetail, // Details on responses to distributed requests
LmtDistOpResponseHeaders, // HTTP headers of responses to distributed requests
LmtDistOpRequestHeaders, // HTTP headers of request of distributed requests
LmtDistOpList, // Linked list of DistOps

//
Expand Down
6 changes: 6 additions & 0 deletions src/lib/orionld/common/orionldRequestSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ bool orionldRequestSend
char contentLenHeader[128];

snprintf(contentTypeHeader, sizeof(contentTypeHeader), "Content-Type:%s", contentType);
LM_T(LmtDistOpRequestHeaders, ("Adding DistOp Request header '%s'", contentTypeHeader));
snprintf(contentLenHeader, sizeof(contentLenHeader), "Content-Length:%d", payloadLen);
LM_T(LmtDistOpRequestHeaders, ("Adding DistOp Request header '%s'", contentLenHeader));

headers = curl_slist_append(headers, contentTypeHeader);
curl_easy_setopt(cc.curl, CURLOPT_HTTPHEADER, headers);
Expand All @@ -287,12 +289,14 @@ bool orionldRequestSend
char linkHeaderString[512];

snprintf(linkHeaderString, sizeof(linkHeaderString), "Link: %s", linkHeader);
LM_T(LmtDistOpRequestHeaders, ("Adding DistOp Request header '%s'", linkHeader));
headers = curl_slist_append(headers, linkHeaderString);
curl_easy_setopt(cc.curl, CURLOPT_HTTPHEADER, headers);
}

if (acceptHeader != NULL)
{
LM_T(LmtDistOpRequestHeaders, ("Adding DistOp Request header '%s'", acceptHeader));
headers = curl_slist_append(headers, acceptHeader);
curl_easy_setopt(cc.curl, CURLOPT_HTTPHEADER, headers); // Should be enough with one call ...
}
Expand All @@ -304,6 +308,7 @@ bool orionldRequestSend
OrionldHttpHeader* headerP = &headerV[ix];

snprintf(headerString, sizeof(headerString), "%s:%s", headerName[headerP->type], headerP->value);
LM_T(LmtDistOpRequestHeaders, ("Adding DistOp Request header '%s'", headerString));
headers = curl_slist_append(headers, headerString);
++ix;
}
Expand All @@ -312,6 +317,7 @@ bool orionldRequestSend
if (orionldState.in.authorization != NULL)
{
snprintf(headerString, sizeof(headerString), "Authorization:%s", orionldState.in.authorization);
LM_T(LmtDistOpRequestHeaders, ("Adding DistOp Request header '%s'", headerString));
headers = curl_slist_append(headers, headerString);
}

Expand Down
3 changes: 0 additions & 3 deletions src/lib/orionld/common/orionldState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ bool orionldStartup = true;
char pgPortString[16];
char mongoServerVersion[32];
char userAgentHeaderNoLF[64]; // "User-Agent: orionld/" + ORIONLD_VERSION - initialized in orionldServiceInit()
char hostHeaderNoLF[128];
char hostHeader[128]; // Host: xxx
size_t hostHeaderLen;

//
// Variables for Mongo C Driver
Expand Down
3 changes: 0 additions & 3 deletions src/lib/orionld/common/orionldState.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,6 @@ extern int maxAge; // From orionld.cpp (CORS)
extern char userAgentHeader[64]; // From notificationSend.cpp - move to orionld.cpp?
extern size_t userAgentHeaderLen; // From notificationSend.cpp - move to orionld.cpp?
extern char userAgentHeaderNoLF[64]; // move to orionld.cpp?
extern char hostHeader[128]; // move to orionld.cpp?
extern char hostHeaderNoLF[128]; // move to orionld.cpp?
extern size_t hostHeaderLen; // move to orionld.cpp?
extern bool debugCurl; // From orionld.cpp
extern bool noCache; // From orionld.cpp
extern int cSubCounters; // Number of subscription counter updates before flush from sub-cache to DB
Expand Down
7 changes: 0 additions & 7 deletions src/lib/orionld/forwarding/distOpInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <stdio.h> // snprintf
#include <unistd.h> // gethostname

#include "orionld/common/orionldState.h" // hostHeaderNoLF
#include "orionld/forwarding/distOpInit.h" // Own interface


Expand All @@ -36,10 +35,4 @@
//
void distOpInit(void)
{
char hostName[100];

if (gethostname(hostName, sizeof(hostName) - 1) == -1)
snprintf(hostHeaderNoLF, sizeof(hostHeaderNoLF), "Host: unknown");
else
snprintf(hostHeaderNoLF, sizeof(hostHeaderNoLF), "Host: %s", hostName);
}
6 changes: 5 additions & 1 deletion src/lib/orionld/forwarding/distOpSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,12 @@ bool distOpSend(DistOp* distOpP, const char* dateHeader, const char* xForwardedF
// Date
headers = curl_slist_append(headers, dateHeader);

#if 0
// Host
headers = curl_slist_append(headers, hostHeaderNoLF);
char hostHeader[256];
snprintf(hostHeader, sizeof(hostHeader), "Host: %s", ip);
headers = curl_slist_append(headers, hostHeader);
#endif

// X-Forwarded-For
headers = curl_slist_append(headers, xForwardedForHeader);
Expand Down
27 changes: 21 additions & 6 deletions src/lib/orionld/notifications/httpsNotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ static size_t responseHeaderDebug(char* buffer, size_t size, size_t nitems, void



// -----------------------------------------------------------------------------
//
// notificationResponseBody -
//
static int notificationResponseBody(void* chunk, size_t size, size_t members, void* userP)
{
size_t chunkLen = members;
char* chunkP = (char*) chunk;

LM_T(LmtNotificationBody, ("Got a chunk of notification response (%d bytes): %s", chunkLen, chunkP));

return chunkLen;
}



// -----------------------------------------------------------------------------
//
// httpsNotify -
Expand Down Expand Up @@ -148,7 +164,6 @@ int httpsNotify(CachedSubscription* cSubP, struct iovec* ioVec, int ioVecLen, do
curl_easy_setopt(curlHandleP, CURLOPT_URL, url);
curl_easy_setopt(curlHandleP, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curlHandleP, CURLOPT_TIMEOUT_MS, 5000); // Timeout - hard-coded to 5 seconds for now ...
curl_easy_setopt(curlHandleP, CURLOPT_FAILONERROR, true); // Fail On Error - to detect 404 etc.
curl_easy_setopt(curlHandleP, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirections

// SSL options
Expand All @@ -165,6 +180,7 @@ int httpsNotify(CachedSubscription* cSubP, struct iovec* ioVec, int ioVecLen, do
char header[256];
strcpy(header, (char*) ioVec[ix].iov_base);
header[ioVec[ix].iov_len - 2] = 0;

LM_T(LmtNotificationHeaders, ("%s: Notification Request Header: '%s'", cSubP->subscriptionId, header));
headers = curl_slist_append(headers, header);
}
Expand All @@ -188,17 +204,16 @@ int httpsNotify(CachedSubscription* cSubP, struct iovec* ioVec, int ioVecLen, do
curl_easy_setopt(curlHandleP, CURLOPT_DEBUGFUNCTION, curlDebug);
}

// Debugging Incoming HTTP Headers?
// Debug Incoming HTTP Headers?
if (lmTraceIsSet(LmtNotificationHeaders) == true)
{
curl_easy_setopt(curlHandleP, CURLOPT_HEADERDATA, cSubP->subscriptionId);
curl_easy_setopt(curlHandleP, CURLOPT_HEADERFUNCTION, responseHeaderDebug); // Callback for received headers
}

//
// FIXME: Use curl_easy_setopt(CURLOPT_WRITEFUNCTION+CURLOPT_WRITEDATA) for debugging the response payload body
// See distOpSend.cpp and do the same here (reuse responseSave() and the struct HttpResponse
//
// Debug Incoming HTTP Body?
if (lmTraceIsSet(LmtNotificationBody) == true)
curl_easy_setopt(curlHandleP, CURLOPT_WRITEFUNCTION, notificationResponseBody); // Callback for reading the response body

// Add easy handler to the multi handler
curl_multi_add_handle(orionldState.multiP, curlHandleP);
Expand Down
5 changes: 5 additions & 0 deletions src/lib/orionld/notifications/notificationSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,11 @@ int notificationSend(OrionldAlterationMatch* mAltP, double timestamp, CURL** cur
if (headers > 50)
LM_X(1, ("Too many HTTP headers (>50) for a Notification - to support that many, the broker needs a SW update and to be recompiled"));

char hostHeader[256];
size_t hostHeaderLen;

hostHeaderLen = snprintf(hostHeader, sizeof(hostHeader), "Host: %s\r\n", mAltP->subP->ip);

int ioVecLen = headers + 3; // Request line + X headers + empty line + payload body
int headerIx = 7;
struct iovec ioVec[53] = {
Expand Down
2 changes: 0 additions & 2 deletions src/lib/orionld/notifications/orionldAlterationsTreat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,6 @@ void orionldAlterationsTreat(OrionldAlteration* altList)
LM_E(("Error starting HTTPS notifications: curl_multi_perform: error %d", cm));
curlError = true;
}
else
LM_T(LmtNotificationSend, ("Started HTTPS notifications"));
}
else
LM_T(LmtNotificationSend, ("No HTTPS notifications"));
Expand Down
5 changes: 1 addition & 4 deletions src/lib/orionld/rest/orionldServiceInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "logMsg/logMsg.h" // LM_*
#include "logMsg/traceLevels.h" // Lmt*

#include "orionld/common/orionldState.h" // orionldState, userAgentHeader, hostHeader
#include "orionld/common/orionldState.h" // orionldState, userAgentHeader
#include "orionld/context/orionldCoreContext.h" // orionldCoreContext, coreContextUrl
#include "orionld/context/orionldContextInit.h" // orionldContextInit
#include "orionld/rest/OrionLdRestService.h" // OrionLdRestService, ORION_LD_SERVICE_PREFIX_LEN
Expand Down Expand Up @@ -509,13 +509,10 @@ void orionldServiceInit(OrionLdRestServiceSimplifiedVector* restServiceVV, int v
//
// * userAgentHeader(Len): Notifications with HTTP(s)
// * userAgentHeaderNoLF: Forwarding
// * hostHeader(Len): Notifications with HTTP(s)
//
userAgentHeaderLen = snprintf(userAgentHeader, sizeof(userAgentHeader) -1, "User-Agent: orionld/%s\r\n", ORIONLD_VERSION); // Used in notifications as value of HTTP Header User-Agent
snprintf(userAgentHeaderNoLF, sizeof(userAgentHeaderNoLF) -1, "User-Agent: orionld/%s", ORIONLD_VERSION); // Used in forwarding as value of HTTP Header User-Agent

hostHeaderLen = snprintf(hostHeader, sizeof(hostHeader) - 1, "Host: %s\r\n", orionldHostName);

int svIx; // Service Vector Index
for (svIx = 0; svIx < vecItems; svIx++)
{
Expand Down
Loading
Loading