mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-24 16:09:27 +00:00
Started on Feature #14. Server is now running. But cannot communicate.
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#import "MPPasswordCreatorViewController.h"
|
||||
#import "MPSettingsHelper.h"
|
||||
#import "MPUppercaseStringValueTransformer.h"
|
||||
#import "MPStringLengthValueTransformer.h"
|
||||
#import "MPServerDaemon.h"
|
||||
|
||||
@interface MPAppDelegate () {
|
||||
@@ -31,6 +32,7 @@
|
||||
+ (void)initialize {
|
||||
[MPSettingsHelper setupDefaults];
|
||||
[MPUppercaseStringValueTransformer registerTransformer];
|
||||
[MPStringLengthValueTransformer registerTransformer];
|
||||
}
|
||||
|
||||
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MPServerRequestHandler.h"
|
||||
|
||||
@interface MPAssociateRequestHandler : NSObject
|
||||
@interface MPAssociateRequestHandler : NSObject <MPServerRequestHandler>
|
||||
|
||||
@end
|
||||
|
||||
@@ -10,4 +10,12 @@
|
||||
|
||||
@implementation MPAssociateRequestHandler
|
||||
|
||||
+ (NSString *)identifyer {
|
||||
return @"associate";
|
||||
}
|
||||
|
||||
- (void)respondTo:(NSDictionary *)data {
|
||||
// todo;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -7,11 +7,80 @@
|
||||
//
|
||||
|
||||
#import "MPConnection.h"
|
||||
#import "HTTPMessage.h"
|
||||
|
||||
NSString *const MPRequestTypeAssociate = @"associate";
|
||||
NSString *const MPRequestTypeTestAssociate = @"test-associate";
|
||||
NSString *const MPRequestTypeGetLogins = @"get-logins";
|
||||
NSString *const MPRequestTypeGetLoginsCount = @"get-logins-count";
|
||||
NSString *const MPRequestTypeGetAllLogins = @"get-all-logins";
|
||||
NSString *const MPRequestTypeSetLogin = @"set-login";
|
||||
NSString *const MPRequestTypeGeneratePassword = @"generate-password";
|
||||
|
||||
NSString *const MPRequestTypeKey = @"RequestType";
|
||||
|
||||
@implementation MPConnection
|
||||
|
||||
+ (NSArray *)requestHander {
|
||||
|
||||
|
||||
}
|
||||
|
||||
- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path
|
||||
{
|
||||
// Override me to support methods such as POST.
|
||||
//
|
||||
// Things you may want to consider:
|
||||
// - Does the given path represent a resource that is designed to accept this method?
|
||||
// - If accepting an upload, is the size of the data being uploaded too big?
|
||||
// To do this you can check the requestContentLength variable.
|
||||
//
|
||||
// For more information, you can always access the HTTPMessage request variable.
|
||||
//
|
||||
// You should fall through with a call to [super supportsMethod:method atPath:path]
|
||||
//
|
||||
// See also: expectsRequestBodyFromMethod:atPath:
|
||||
|
||||
if([method isEqualToString:@"POST"]) {
|
||||
return YES;
|
||||
}
|
||||
return [super supportsMethod:method atPath:path];
|
||||
}
|
||||
|
||||
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path {
|
||||
return [super httpResponseForMethod:method URI:path];
|
||||
|
||||
NSError *error = nil;
|
||||
id obj = [NSJSONSerialization JSONObjectWithData:[request body] options:0 error:&error];
|
||||
if(error) {
|
||||
NSLog(@"Error while parsing request:%@", [error localizedDescription]);
|
||||
}
|
||||
if([obj isKindOfClass:[NSDictionary class]]) {
|
||||
NSDictionary *requestDict = obj;
|
||||
[self _parseRequest:requestDict];
|
||||
}
|
||||
else {
|
||||
NSLog(@"Wrong Request format. Unable to use JSON data");
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)processBodyData:(NSData *)postDataChunk {
|
||||
[request appendData:postDataChunk];
|
||||
}
|
||||
|
||||
|
||||
- (void)_parseRequest:(NSDictionary *)aRequest {
|
||||
NSString *requestType = aRequest[MPRequestTypeKey];
|
||||
if(!requestType) {
|
||||
NSLog(@"Malformed Request. Missing request type");
|
||||
}
|
||||
NSLog(@"%@", requestType);
|
||||
if([requestType isEqualToString:MPRequestTypeAssociate]) {
|
||||
return;
|
||||
}
|
||||
if([requestType isEqualToString:MPRequestTypeGeneratePassword]) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -9,13 +9,18 @@
|
||||
#import "MPServerDaemon.h"
|
||||
#import "MPSettingsHelper.h"
|
||||
#import "HTTPServer.h"
|
||||
#import "MPIconHelper.h"
|
||||
#import "MPConnection.h"
|
||||
#import "MPServerRequestHandler.h"
|
||||
|
||||
@interface MPServerDaemon () {
|
||||
@private
|
||||
HTTPServer *server;
|
||||
NSStatusItem *statusItem;
|
||||
}
|
||||
|
||||
@property (nonatomic, assign) BOOL isEnabled;
|
||||
@property (nonatomic, assign) BOOL showStatusItem;
|
||||
|
||||
@end
|
||||
|
||||
@@ -25,14 +30,17 @@
|
||||
self = [super init];
|
||||
if (self) {
|
||||
NSUserDefaultsController *defaultsController = [NSUserDefaultsController sharedUserDefaultsController];
|
||||
NSString *defaultsKeyPath = [NSString stringWithFormat:@"values.%@", kMPSettingsKeyEnableHttpServer];
|
||||
[self bind:@"isEnabled" toObject:defaultsController withKeyPath:defaultsKeyPath options:nil];
|
||||
NSString *enableServerKeyPath = [NSString stringWithFormat:@"values.%@", kMPSettingsKeyEnableHttpServer];
|
||||
NSString *showItemKeyPath = [NSString stringWithFormat:@"values.%@", kMPSettingsKeyShowMenuItem];
|
||||
[self bind:@"isEnabled" toObject:defaultsController withKeyPath:enableServerKeyPath options:nil];
|
||||
[self bind:@"showStatusItem" toObject:defaultsController withKeyPath:showItemKeyPath options:nil];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[statusItem release];
|
||||
[server release];
|
||||
[super dealloc];
|
||||
}
|
||||
@@ -47,23 +55,46 @@
|
||||
[self _setupServer];
|
||||
}
|
||||
NSError *error= nil;
|
||||
if(![server start:&error]) {
|
||||
if(![server start:&error]) {
|
||||
[NSApp presentError:error];
|
||||
}
|
||||
// setup menu item
|
||||
}
|
||||
else {
|
||||
/* Do not let the resource linger around */
|
||||
[server release];
|
||||
server = nil;
|
||||
}
|
||||
[self _updateStatusItem];
|
||||
}
|
||||
|
||||
|
||||
- (void)setShowStatusItem:(BOOL)showStatusItem {
|
||||
if(_showStatusItem != showStatusItem) {
|
||||
_showStatusItem = showStatusItem;
|
||||
[self _updateStatusItem];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_updateStatusItem {
|
||||
if(_isEnabled && _showStatusItem) {
|
||||
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
|
||||
[statusItem setImage:[MPIconHelper icon:MPIconServer ]];
|
||||
}
|
||||
else if(statusItem) {
|
||||
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
|
||||
statusItem = nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_setupServer {
|
||||
NSAssert(server == nil, @"Server should be nil");
|
||||
server = [[HTTPServer alloc] init];
|
||||
[server setConnectionClass:[MPConnection class]];
|
||||
[server setInterface:@"localhost"];
|
||||
NSInteger port = [[NSUserDefaults standardUserDefaults] integerForKey:kMPSettingsKeyHttpPort];
|
||||
[server setPort:port];
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -10,4 +10,8 @@
|
||||
|
||||
@protocol MPServerRequestHandler <NSObject>
|
||||
|
||||
@required
|
||||
+ (NSString *)identifyer;
|
||||
- (void)respondTo:(NSDictionary *)data;
|
||||
|
||||
@end
|
||||
|
||||
@@ -14,6 +14,7 @@ APPKIT_EXTERN NSString *const kMPSettingsKeyPasswordEncoding;
|
||||
APPKIT_EXTERN NSString *const kMPSettingsKeyOpenEmptyDatabaseOnLaunch;
|
||||
APPKIT_EXTERN NSString *const kMPSettingsKeyHttpPort;
|
||||
APPKIT_EXTERN NSString *const kMPSettingsKeyEnableHttpServer;
|
||||
APPKIT_EXTERN NSString *const kMPSettingsKeyShowMenuItem;
|
||||
|
||||
typedef NS_ENUM(NSUInteger, MPPasswordEncoding) {
|
||||
MPPasswordEncodingUTF8,
|
||||
|
||||
@@ -12,7 +12,9 @@ NSString *const kMPSettingsKeyPasteboardClearTimeout = @"ClipboardClearTimeout";
|
||||
NSString *const kMPSettingsKeyClearPasteboardOnQuit = @"ClearClipboardOnQuit";
|
||||
NSString *const kMPSettingsKeyOpenEmptyDatabaseOnLaunch = @"OpenEmptyDatabaseOnLaunch";
|
||||
NSString *const kMPSettingsKeyHttpPort =@"HttpPort";
|
||||
NSString *const kMPSettingsKeyEnableHttpServer = @"kMPSettingsKeyEnableHttpServer";
|
||||
NSString *const kMPSettingsKeyEnableHttpServer = @"EnableHttpServer";
|
||||
NSString *const kMPSettingsKeyShowMenuItem = @"ShowMenuItem";
|
||||
|
||||
|
||||
@implementation MPSettingsHelper
|
||||
|
||||
@@ -26,7 +28,8 @@ NSString *const kMPSettingsKeyEnableHttpServer = @"kMPSettingsKeyEnableHttpServe
|
||||
kMPSettingsKeyClearPasteboardOnQuit: @YES,
|
||||
kMPSettingsKeyOpenEmptyDatabaseOnLaunch: @YES,
|
||||
kMPSettingsKeyHttpPort: @19455,
|
||||
kMPSettingsKeyEnableHttpServer: @NO
|
||||
kMPSettingsKeyEnableHttpServer: @NO,
|
||||
kMPSettingsKeyShowMenuItem: @YES,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>497</string>
|
||||
<string>561</string>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
|
||||
Reference in New Issue
Block a user