Skip to content

Commit

Permalink
Merge pull request #1345 from FIWARE/issue/1343
Browse files Browse the repository at this point in the history
Fixed two issues: #1343 + #1344
  • Loading branch information
kzangeli authored Mar 23, 2023
2 parents ef3f5b0 + 8357923 commit 0d046de
Show file tree
Hide file tree
Showing 100 changed files with 1,382 additions and 165 deletions.
2 changes: 2 additions & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@
* Issue #1322 Default port for HTTPS is 443, for HTTP: 80
* Issue #1338 Removed the check for mongoc only, as all operations now support mongoc
* Issue #1340 Fixed a dangling pointer bug
* Issue #1343 Added the HTTP header (well, for mqtt notfications also) 'Host' for notifications
* Issue #1344 Accepting hyphen as a valid character for a tenant
4 changes: 2 additions & 2 deletions src/app/orionld/orionld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* CLI option '--insecure'.
*/
#include <stdio.h>
#include <unistd.h> // getppid, fork, setuid, sleep, etc.
#include <unistd.h> // getppid, fork, setuid, sleep, gethostname, etc.
#include <string.h>
#include <fcntl.h> // open
#include <sys/types.h>
Expand Down Expand Up @@ -1126,7 +1126,7 @@ int main(int argC, char* argV[])


//
// Get the hostname - needed for contexts created by the broker
// Get the hostname - needed for contexts created by the broker + notifications
//
gethostname(orionldHostName, sizeof(orionldHostName));
orionldHostNameLen = strlen(orionldHostName);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/orionld/common/orionldState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ bool orionldStartup = true;
char pgPortString[16];
char mongoServerVersion[32];
char userAgentHeaderNoLF[64]; // "User-Agent: orionld/" + ORIONLD_VERSION - initialized in orionldServiceInit()
char hostHeader[256]; // Host: xxx

char hostHeaderNoLF[128];
char hostHeader[128]; // Host: xxx
size_t hostHeaderLen;

//
// Variables for Mongo C Driver
Expand Down
10 changes: 6 additions & 4 deletions src/lib/orionld/common/orionldState.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,12 @@ extern bool experimental; // From orionld.cpp
extern bool mongocOnly; // From orionld.cpp
extern char allowedOrigin[64]; // From orionld.cpp (CORS)
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 (from orionldPostEntities.cpp)
extern char hostHeader[256]; // move to orionld.cpp (from orionldPostEntities.cpp)
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
8 changes: 4 additions & 4 deletions src/lib/orionld/forwarding/distOpInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <stdio.h> // snprintf
#include <unistd.h> // gethostname

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


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

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

// Host
headers = curl_slist_append(headers, hostHeader);
headers = curl_slist_append(headers, hostHeaderNoLF);

// X-Forwarded-For
headers = curl_slist_append(headers, xForwardedForHeader);
Expand Down
25 changes: 13 additions & 12 deletions src/lib/orionld/notifications/notificationSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static const char* normalizedHeaderNgsiV2 = (char*) "Ngsiv2-Attrsformat: norma
static const char* keyValuesHeaderNgsiV2 = (char*) "Ngsiv2-Attrsformat: keyValues\r\n";

char userAgentHeader[64]; // "User-Agent: orionld/" + ORIONLD_VERSION + \r\n" - initialized in orionldServiceInit()
size_t userAgentHeaderLen = 0; // Also set in orionldServiceInit()
size_t userAgentHeaderLen = 0; // Set in orionldServiceInit()



Expand Down Expand Up @@ -650,7 +650,7 @@ int notificationSend(OrionldAlterationMatch* mAltP, double timestamp, CURL** cur
strcpy(contentLenHeader, "Content-Length: 0"); // Can't modify inside static strings, so need a char-vec on the stack for contentLenHeader
snprintf(lenP, sizeLeftForLen, "%d\r\n", (int) contentLength); // Adding Content-Length inside contentLenHeader

int headers = 6; // the minimum number of request headers
int headers = 7; // the minimum number of request headers


//
Expand All @@ -672,14 +672,15 @@ int notificationSend(OrionldAlterationMatch* mAltP, double timestamp, CURL** cur
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"));

int ioVecLen = headers + 3; // Request line + X headers + empty line + payload body
int headerIx = 6;
int headerIx = 7;
struct iovec ioVec[53] = {
{ requestHeader, requestHeaderLen },
{ contentLenHeader, strlen(contentLenHeader) },
{ (void*) contentTypeHeaderJson, 32 }, // Index 2
{ (void*) userAgentHeader, userAgentHeaderLen },
{ (void*) hostHeader, hostHeaderLen },
{ (void*) acceptHeader, 26 },
{ (void*) normalizedHeader, 37 } // Index 5
{ (void*) normalizedHeader, 37 } // Index 6
};

//
Expand Down Expand Up @@ -715,23 +716,23 @@ int notificationSend(OrionldAlterationMatch* mAltP, double timestamp, CURL** cur
//
if (mAltP->subP->renderFormat == RF_CONCISE)
{
ioVec[5].iov_base = (void*) conciseHeader;
ioVec[5].iov_len = 34;
ioVec[6].iov_base = (void*) conciseHeader;
ioVec[6].iov_len = 34;
}
else if (mAltP->subP->renderFormat == RF_KEYVALUES)
{
ioVec[5].iov_base = (void*) simplifiedHeader;
ioVec[5].iov_len = 37;
ioVec[6].iov_base = (void*) simplifiedHeader;
ioVec[6].iov_len = 37;
}
else if ((mAltP->subP->renderFormat == RF_CROSS_APIS_NORMALIZED) || (mAltP->subP->renderFormat == RF_CROSS_APIS_NORMALIZED_COMPACT))
{
ioVec[5].iov_base = (void*) normalizedHeaderNgsiV2;
ioVec[5].iov_len = 32;
ioVec[6].iov_base = (void*) normalizedHeaderNgsiV2;
ioVec[6].iov_len = 32;
}
else if ((mAltP->subP->renderFormat == RF_CROSS_APIS_KEYVALUES) || (mAltP->subP->renderFormat == RF_CROSS_APIS_KEYVALUES_COMPACT))
{
ioVec[5].iov_base = (void*) keyValuesHeaderNgsiV2;
ioVec[5].iov_len = 31;
ioVec[6].iov_base = (void*) keyValuesHeaderNgsiV2;
ioVec[6].iov_len = 31;
}


Expand Down
4 changes: 2 additions & 2 deletions src/lib/orionld/rest/orionldMhdConnectionInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,9 +955,9 @@ static bool tenantCheck(char* tenantName)
return false;
}

if ((!isalnum(tenantName[len])) && (tenantName[len] != '_'))
if ((!isalnum(tenantName[len])) && (tenantName[len] != '_') && (tenantName[len] != '-'))
{
orionldError(OrionldBadRequestData, "Invalid Tenant", "bad character in tenant name - only underscore and alphanumeric characters are allowed", 400);
orionldError(OrionldBadRequestData, "Invalid Tenant", "invalid character in tenant name - only underscore, hyphen, and alphanumeric characters are allowed", 400);
return false;
}

Expand Down
6 changes: 4 additions & 2 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
#include "orionld/common/orionldState.h" // orionldState, userAgentHeader, hostHeader
#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 @@ -496,12 +496,14 @@ void orionldServiceInit(OrionLdRestServiceSimplifiedVector* restServiceVV, int v
//
// FIXME: This "block should move to orionld.cpp ?
//
// * userAgentHeader(Len): Notifications with HTTP
// * 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
12 changes: 7 additions & 5 deletions test/functionalTest/cases/0000_ngsild/ngsild_issue_1224.test
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ echo
echo "02. Create a subscription according to (2) of issue #1224"
echo "========================================================="
payload='{
"description": "A subscription to get air quality",
"id": "urn:S1",
"description": "A subscription to get air quality",
"type": "Subscription",
"entities": [
{
Expand Down Expand Up @@ -140,7 +141,7 @@ Location: /ngsi-ld/v1/entities/urn:ngsi-ld:AirQualityObserved:Guadalajara:ES1537
HTTP/1.1 201 Created
Content-Length: 0
Date: REGEX(.*)
Location: /ngsi-ld/v1/subscriptions/urn:ngsi-ld:subscription:REGEX(.*)
Location: /ngsi-ld/v1/subscriptions/urn:S1



Expand All @@ -164,9 +165,10 @@ Date: REGEX(.*)

06. Dump the accumulator, see one notification
==============================================
POST http://127.0.0.1:9997/notify?subscriptionId=urn:ngsi-ld:subscription:REGEX(.*)
Content-Length: 384
POST http://REGEX(.*)/notify?subscriptionId=urn:S1
Content-Length: 329
User-Agent: orionld/REGEX(.*)
Host: REGEX(.*)
Accept: application/json
Content-Type: application/json
Link: <https://smartdatamodels.org/context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"
Expand All @@ -189,7 +191,7 @@ Ngsild-Attribute-Format: Normalized
],
"id": "urn:ngsi-ld:Notification:REGEX(.*)",
"notifiedAt": "202REGEX(.*)Z",
"subscriptionId": "urn:ngsi-ld:subscription:REGEX(.*)",
"subscriptionId": "urn:S1",
"type": "Notification"
}
=======================================
Expand Down
3 changes: 2 additions & 1 deletion test/functionalTest/cases/0000_ngsild/ngsild_issue_1322.test
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ POST http://REGEX(.*)/yvonne?subscriptionId=urn:S1
Content-Length: 403
Authorization: Bearer {myToken}
User-Agent: orionld/REGEX(.*)
Host: REGEX(.*)
Accept: application/json
Ngsild-Tenant: captn
Content-Type: application/json
Expand Down Expand Up @@ -240,7 +241,7 @@ POST https://REGEX(.*)/yvonne
Content-Length: 403
Authorization: Bearer {myToken}
User-Agent: orionld/REGEX(.*)
Host: localhost:9977
Host: REGEX(.*)
Accept: application/json
Ngsild-Tenant: captn
Content-Type: application/json
Expand Down
Loading

0 comments on commit 0d046de

Please sign in to comment.