Skip to content

Commit

Permalink
vmpserverd: Rework CORS handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hmelder committed Mar 4, 2024
1 parent ada3558 commit f250599
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions Daemons/vmpserverd/src/VMPServerMain.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* SPDX-License-Identifier: MIT
*/

#include <MicroHTTPKit/HKHTTPConstants.h>
#import <MicroHTTPKit/MicroHTTPKit.h>
#import <glib.h>

Expand All @@ -25,6 +24,12 @@ @implementation VMPServerMain {
NSString *_startedAtDateISO8601;
}

#define DEFAULT_HEADERS \
(@{ \
@"Access-Control-Allow-Origin" : @"*", \
@"Content-Type" : @"application/json", \
})

+ (instancetype)serverWithConfiguration:(VMPConfigModel *)configuration error:(NSError **)error {
return [[VMPServerMain alloc] initWithConfiguration:configuration
forcePlatform:nil
Expand Down Expand Up @@ -88,12 +93,31 @@ - (instancetype)initWithConfiguration:(VMPConfigModel *)configuration

#pragma mark - HTTP handlers

// CORS Handler for all endpoints
- (HKHandlerBlock)_corsHandlerV1 {
return ^HKHTTPResponse *(HKHTTPRequest *request) {
HKHTTPResponse *response;
NSDictionary *headers = @{
@"Access-Control-Allow-Origin" : @"*",
@"Access-Control-Allow-Methods" : @"GET, OPTIONS",
@"Access-Control-Allow-Headers" : @"Authorization, Content-Type",
@"Access-Control-Max-Age" : @"3600",
};

response = [HKHTTPResponse responseWithStatus:200];
[response setHeaders:headers];

return response;
};
}

- (HKHandlerBlock)_statusHandlerV1 {
return ^HKHTTPResponse *(HKHTTPRequest *request) {
VMPProfileModel *profile;
HKHTTPJSONResponse *response;
profile = [_profileMgr currentProfile];

NSDictionary *response = @{
NSDictionary *data = @{
@"version" : _version,
@"platform" : [_profileMgr runtimePlatform],
@"profile" : @{
Expand All @@ -105,7 +129,9 @@ - (HKHandlerBlock)_statusHandlerV1 {
@"startedAt" : _startedAtDateISO8601,
};

return [HKHTTPJSONResponse responseWithJSONObject:response status:200 error:NULL];
response = [HKHTTPJSONResponse responseWithJSONObject:data status:200 error:NULL];
[response setHeaders:DEFAULT_HEADERS];
return response;
};
}

Expand Down Expand Up @@ -187,30 +213,12 @@ - (void)setupHTTPHandlers {
HKRoute *configRoute;
HKRoute *channelGraphRoute;
HKRoute *mountpointGraphRoute;
HKHandlerBlock CORSHandler;

router = [_httpServer router];
CORSHandler = [self _corsHandlerV1];

// FIXME: Implement authorization via middleware
[router setMiddleware:^HKHTTPResponse *(HKHTTPRequest *request) {
// Setup a very basic CORS handler
if ([request method] == HKHTTPMethodOptions) {
HKHTTPResponse *response;
NSDictionary *headers = @{
@"Access-Control-Allow-Origin" : @"*",
@"Access-Control-Allow-Methods" : @"GET, OPTIONS",
@"Access-Control-Allow-Headers" : @"Authorization, Content-Type",
@"Access-Control-Max-Age" : @"3600",
};

response = [HKHTTPResponse responseWithStatus:200];
[response setHeaders:headers];
[response setCheckIfEndpointExists:YES];

return response;
}

return nil;
}];

// GET /api/v1/status
statusRoute = [HKRoute routeWithPath:@"/api/v1/status"
Expand All @@ -229,10 +237,10 @@ - (void)setupHTTPHandlers {
method:HKHTTPMethodGET
handler:[self _mountpointGraphHandlerV1]];

[router registerRoute:statusRoute];
[router registerRoute:configRoute];
[router registerRoute:channelGraphRoute];
[router registerRoute:mountpointGraphRoute];
[router registerRoute:statusRoute withCORSHandler:CORSHandler];
[router registerRoute:configRoute withCORSHandler:CORSHandler];
[router registerRoute:channelGraphRoute withCORSHandler:CORSHandler];
[router registerRoute:mountpointGraphRoute withCORSHandler:CORSHandler];
}

#pragma mark - Server Lifecycle
Expand Down

0 comments on commit f250599

Please sign in to comment.