Switches from GCDWebServer to CocoaHTTPServer so we can have HTTPS eventually
This commit is contained in:
@@ -7,18 +7,23 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <GCDWebServers/GCDWebServers.h>
|
||||
#import <CocoaHTTPServer/CocoaHTTPServer.h>
|
||||
|
||||
#import "MBIMJSONDataResponse.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef void (^MBIMBridgeOperationCompletionBlock)(NSObject * _Nullable response);
|
||||
|
||||
@interface MBIMBridgeOperation : NSOperation
|
||||
@property (class, nonatomic, readonly) NSString *endpointName;
|
||||
|
||||
@property (nonatomic, readonly) GCDWebServerRequest *request;
|
||||
@property (nonatomic, readonly) GCDWebServerCompletionBlock serverCompletionBlock;
|
||||
@property (nonatomic, strong) NSData *requestBodyData;
|
||||
@property (nonatomic, readonly) NSURL *requestURL;
|
||||
@property (nonatomic, readonly) MBIMBridgeOperationCompletionBlock serverCompletionBlock;
|
||||
|
||||
+ (nullable Class)operationClassForEndpointName:(NSString *)endpointName;
|
||||
- (instancetype)initWithRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completionBlock;
|
||||
- (instancetype)initWithRequestURL:(NSURL *)requestURL completion:(MBIMBridgeOperationCompletionBlock)completionBlock;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
@interface MBIMBridgeOperation (/*INTERNAL*/)
|
||||
@property (nonatomic, copy) GCDWebServerCompletionBlock serverCompletionBlock;
|
||||
@property (nonatomic, strong) GCDWebServerRequest *request;
|
||||
@property (nonatomic, strong) NSURL *requestURL;
|
||||
@property (nonatomic, copy) MBIMBridgeOperationCompletionBlock serverCompletionBlock;
|
||||
@end
|
||||
|
||||
@implementation MBIMBridgeOperation
|
||||
@@ -44,11 +44,11 @@
|
||||
return [[self _operationClassMapping] objectForKey:endpointName];
|
||||
}
|
||||
|
||||
- (instancetype)initWithRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completionBlock
|
||||
- (instancetype)initWithRequestURL:(NSURL *)requestURL completion:(MBIMBridgeOperationCompletionBlock)completionBlock
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.request = request;
|
||||
self.requestURL = requestURL;
|
||||
self.serverCompletionBlock = completionBlock;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "MBIMConversationListOperation.h"
|
||||
#import "MBIMHTTPUtilities.h"
|
||||
|
||||
#import <IMCore/IMCore.h>
|
||||
|
||||
@@ -28,7 +29,7 @@
|
||||
NSMutableDictionary *chatDict = [NSMutableDictionary dictionary];
|
||||
chatDict[@"guid"] = [chat guid];
|
||||
chatDict[@"displayName"] = [chat displayName];
|
||||
chatDict[@"date"] = GCDWebServerFormatRFC822([chat lastFinishedMessageDate]);
|
||||
chatDict[@"date"] = MBIMWebServerFormatRFC822([chat lastFinishedMessageDate]);
|
||||
|
||||
IMMessage *lastMessage = [chat lastMessage];
|
||||
if (lastMessage) {
|
||||
@@ -48,7 +49,7 @@
|
||||
[conversations addObject:chatDict];
|
||||
}
|
||||
|
||||
GCDWebServerResponse *response = [GCDWebServerDataResponse responseWithJSONObject:conversations];
|
||||
MBIMJSONDataResponse *response = [MBIMJSONDataResponse responseWithJSONObject:conversations];
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "MBIMMessagesListOperation.h"
|
||||
#import "MBIMHTTPUtilities.h"
|
||||
|
||||
#import <IMCore/IMCore.h>
|
||||
|
||||
@@ -21,9 +22,9 @@
|
||||
|
||||
- (void)main
|
||||
{
|
||||
GCDWebServerResponse *response = nil;
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:[self.request URL] resolvingAgainstBaseURL:NO];
|
||||
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:self.requestURL resolvingAgainstBaseURL:NO];
|
||||
|
||||
NSString *guid = nil;
|
||||
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
|
||||
@@ -35,14 +36,14 @@
|
||||
|
||||
if (!guid) {
|
||||
NSLog(@"No query item provided");
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
NSLog(@"Chat with guid: %@ not found", guid);
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -53,12 +54,12 @@
|
||||
for (IMMessageItem *imMessage in [[chat chatItems] messages]) {
|
||||
NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
|
||||
messageDict[@"text"] = [[imMessage body] string];
|
||||
messageDict[@"date"] = GCDWebServerFormatRFC822([imMessage time]);
|
||||
messageDict[@"date"] = MBIMWebServerFormatRFC822([imMessage time]);
|
||||
messageDict[@"sender"] = [imMessage sender];
|
||||
[messages addObject:messageDict];
|
||||
}
|
||||
|
||||
response = [GCDWebServerDataResponse responseWithJSONObject:messages];
|
||||
response = [MBIMJSONDataResponse responseWithJSONObject:messages];
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
|
||||
@@ -41,20 +41,25 @@
|
||||
|
||||
- (void)main
|
||||
{
|
||||
assert([self.request isKindOfClass:[GCDWebServerURLEncodedFormRequest class]]);
|
||||
NSObject<HTTPResponse> *response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
|
||||
GCDWebServerURLEncodedFormRequest *formRequest = (GCDWebServerURLEncodedFormRequest *)self.request;
|
||||
NSDictionary *args = [formRequest arguments];
|
||||
NSError *error = nil;
|
||||
NSDictionary *args = [NSJSONSerialization JSONObjectWithData:self.requestBodyData options:0 error:&error];
|
||||
if (error || args.count == 0) {
|
||||
self.serverCompletionBlock(response);
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *guid = [args objectForKey:@"guid"];
|
||||
NSString *messageBody = [args objectForKey:@"body"];
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
||||
if (!guid || !messageBody) {
|
||||
self.serverCompletionBlock(response);
|
||||
return;
|
||||
}
|
||||
|
||||
GCDWebServerResponse *response = nil;
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
||||
if (result) {
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:200];
|
||||
} else {
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
|
||||
}
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
|
||||
11
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.h
Normal file
11
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.h
Normal file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// MBIMHTTPUtilities.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NSString* MBIMWebServerFormatRFC822(NSDate *date);
|
||||
33
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.m
Normal file
33
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.m
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// MBIMHTTPUtilities.c
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#include "MBIMHTTPUtilities.h"
|
||||
|
||||
static NSDateFormatter* _dateFormatterRFC822 = nil;
|
||||
static dispatch_queue_t _dateFormatterQueue = NULL;
|
||||
|
||||
__attribute__((constructor))
|
||||
static void __InitializeDateFormatter()
|
||||
{
|
||||
_dateFormatterQueue = dispatch_queue_create("dateFormatter", DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
_dateFormatterRFC822 = [[NSDateFormatter alloc] init];
|
||||
_dateFormatterRFC822.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
|
||||
_dateFormatterRFC822.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
|
||||
_dateFormatterRFC822.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
|
||||
}
|
||||
|
||||
NSString* MBIMWebServerFormatRFC822(NSDate *date)
|
||||
{
|
||||
__block NSString *string = nil;
|
||||
dispatch_sync(_dateFormatterQueue, ^{
|
||||
string = [_dateFormatterRFC822 stringFromDate:date];
|
||||
});
|
||||
|
||||
return string;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// MBIMJSONDataResponse.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CocoaHTTPServer/CocoaHTTPServer.h>
|
||||
|
||||
@interface MBIMJSONDataResponse : HTTPDataResponse
|
||||
|
||||
+ (instancetype)responseWithJSONObject:(id)object;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// MBIMJSONDataResponse.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMJSONDataResponse.h"
|
||||
|
||||
@implementation MBIMJSONDataResponse
|
||||
|
||||
+ (instancetype)responseWithJSONObject:(id)object
|
||||
{
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];
|
||||
if (data == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
MBIMJSONDataResponse *response = [[self alloc] initWithData:data];
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
- (NSDictionary *)httpHeaders
|
||||
{
|
||||
return @{
|
||||
@"Content-Type" : @"application/json; charset=utf-8"
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user