Skip to content

Commit 6df1ee2

Browse files
committed
Improve rsa dfi http error handling and logging
1 parent 270c784 commit 6df1ee2

1 file changed

Lines changed: 73 additions & 46 deletions

File tree

bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ static const char *no_content_response_headers =
140140

141141
static const unsigned int DEFAULT_TIMEOUT = 0;
142142

143+
static void rsa_send_http_error(struct mg_connection *conn, int code, const char *msg);
144+
145+
#define RSA_LOG_REQUEST_ERROR(rsa, conn, fmt, ...) \
146+
do { \
147+
const struct mg_request_info *ri = mg_get_request_info(conn); \
148+
const char *client_ip = ri->remote_addr ? ri->remote_addr : "unknown"; \
149+
celix_logHelper_log((rsa)->loghelper, CELIX_LOG_LEVEL_ERROR, \
150+
"[%s %s from %s] " fmt, ri->request_method, ri->request_uri, client_ip, ##__VA_ARGS__); \
151+
} while(0)
152+
143153
static int remoteServiceAdmin_callback(struct mg_connection *conn);
144154
static celix_status_t remoteServiceAdmin_createEndpointDescription(remote_service_admin_t *admin, service_reference_pt reference, celix_properties_t *props, char *interface, endpoint_description_t **description);
145155
static celix_status_t remoteServiceAdmin_send(void *handle, endpoint_description_t *endpointDescription, char *request, celix_properties_t *metadata, char **reply);
@@ -150,6 +160,18 @@ static size_t remoteServiceAdmin_write(void *contents, size_t size, size_t nmemb
150160
static void remoteServiceAdmin_setupStopExportsThread(remote_service_admin_t* admin);
151161
static void remoteServiceAdmin_teardownStopExportsThread(remote_service_admin_t* admin);
152162

163+
static void rsa_send_http_error(struct mg_connection *conn, int code, const char *msg) {
164+
const char *reason = "Error";
165+
switch (code) {
166+
case 400: reason = "Bad Request"; break;
167+
case 404: reason = "Not Found"; break;
168+
case 413: reason = "Payload Too Large"; break;
169+
case 500: reason = "Internal Server Error"; break;
170+
case 503: reason = "Service Unavailable"; break;
171+
}
172+
mg_printf(conn, "HTTP/1.1 %d %s\r\nContent-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%s", code, reason, strlen(msg), msg);
173+
}
174+
153175
static void remoteServiceAdmin_curlshare_lock(CURL *handle, curl_lock_data data, curl_lock_access laccess, void *userptr)
154176
{
155177
(void)handle;
@@ -469,62 +491,67 @@ static int remoteServiceAdmin_callback(struct mg_connection *conn) {
469491

470492
if (strncmp(request_info->request_uri, "/service/", 9) == 0 && strcmp("POST", request_info->request_method) == 0) {
471493

472-
// uri = /services/myservice/call
494+
// uri = /service/{serviceId}/{rest}
473495
const char *uri = request_info->request_uri;
474-
// rest = myservice/call
496+
const char *rest = uri + 9;
475497

476-
const char *rest = uri+9;
477498
char *interfaceStart = strchr(rest, '/');
478-
int pos = interfaceStart - rest;
479-
char service[pos+1];
480-
strncpy(service, rest, pos);
481-
service[pos] = '\0';
482-
unsigned long serviceId = strtoul(service,NULL,10);
483-
484-
for (int i = 0; i < request_info->num_headers; i++) {
485-
struct mg_header header = request_info->http_headers[i];
486-
if (strncmp(header.name, "X-RSA-Metadata-", 15) == 0) {
487-
if (metadata == NULL) {
488-
metadata = celix_properties_create();
499+
int pos = interfaceStart ? (int)(interfaceStart - rest) : -1;
500+
if (interfaceStart == NULL || pos <= 0) {
501+
RSA_LOG_REQUEST_ERROR(rsa, conn, "Invalid service URI format: %s", uri);
502+
rsa_send_http_error(conn, 400, "Invalid service URI format");
503+
result = 1;
504+
} else {
505+
char service[pos + 1];
506+
strncpy(service, rest, pos);
507+
service[pos] = '\0';
508+
unsigned long serviceId = strtoul(service, NULL, 10);
509+
510+
for (int i = 0; i < request_info->num_headers; i++) {
511+
struct mg_header header = request_info->http_headers[i];
512+
if (strncmp(header.name, "X-RSA-Metadata-", 15) == 0) {
513+
if (metadata == NULL) {
514+
metadata = celix_properties_create();
515+
}
516+
celix_properties_set(metadata, header.name + 15, header.value);
517+
}
489518
}
490-
celix_properties_set(metadata, header.name + 15, header.value);
491-
}
492-
}
493519

494-
celixThreadRwlock_readLock(&rsa->exportedServicesLock);
495-
496-
//find endpoint
497-
hash_map_iterator_pt iter = hashMapIterator_create(rsa->exportedServices);
498-
while (hashMapIterator_hasNext(iter)) {
499-
hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
500-
celix_array_list_t *exports = hashMapEntry_getValue(entry);
501-
int expIt = 0;
502-
for (expIt = 0; expIt < celix_arrayList_size(exports); expIt++) {
503-
export_registration_t *check = celix_arrayList_get(exports, expIt);
504-
export_reference_t * ref = NULL;
505-
exportRegistration_getExportReference(check, &ref);
506-
endpoint_description_t * checkEndpoint = NULL;
507-
exportReference_getExportedEndpoint(ref, &checkEndpoint);
508-
if (serviceId == checkEndpoint->serviceId) {
509-
export = check;
510-
free(ref);
511-
break;
520+
celixThreadRwlock_readLock(&rsa->exportedServicesLock);
521+
522+
//find endpoint
523+
hash_map_iterator_pt iter = hashMapIterator_create(rsa->exportedServices);
524+
while (hashMapIterator_hasNext(iter)) {
525+
hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
526+
celix_array_list_t *exports = hashMapEntry_getValue(entry);
527+
int expIt = 0;
528+
for (expIt = 0; expIt < celix_arrayList_size(exports); expIt++) {
529+
export_registration_t *check = celix_arrayList_get(exports, expIt);
530+
export_reference_t * ref = NULL;
531+
exportRegistration_getExportReference(check, &ref);
532+
endpoint_description_t * checkEndpoint = NULL;
533+
exportReference_getExportedEndpoint(ref, &checkEndpoint);
534+
if (serviceId == checkEndpoint->serviceId) {
535+
export = check;
536+
free(ref);
537+
break;
538+
}
539+
free(ref);
540+
}
512541
}
513-
free(ref);
514-
}
515-
}
516-
hashMapIterator_destroy(iter);
542+
hashMapIterator_destroy(iter);
517543

518-
if (export != NULL) {
519-
exportRegistration_increaseUsage(export);
520-
} else {
521-
result = 0;
522-
RSA_LOG_WARNING(rsa, "No export registration found for service id %lu", serviceId);
544+
if (export != NULL) {
545+
exportRegistration_increaseUsage(export);
546+
} else {
547+
RSA_LOG_REQUEST_ERROR(rsa, conn, "No export registration found for service id %lu", serviceId);
548+
rsa_send_http_error(conn, 404, "Service not found");
549+
result = 1;
550+
}
551+
celixThreadRwlock_unlock(&rsa->exportedServicesLock);
523552
}
524-
celixThreadRwlock_unlock(&rsa->exportedServicesLock);
525553
}
526554

527-
528555
if (export != NULL) {
529556
uint64_t datalength = request_info->content_length;
530557
char* data = malloc(datalength + 1);

0 commit comments

Comments
 (0)