Skip to content

Commit

Permalink
iOS: remade local server on CocoaHTTPServer
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrys committed Mar 25, 2022
1 parent 367b2a0 commit e008c14
Show file tree
Hide file tree
Showing 74 changed files with 24,643 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -2227,4 +2227,6 @@ Rhodes contains code from the following other projects:

JXcore (https://github.com/jxcore/jxcore)
License: MIT-Style (https://github.com/jxcore/jxcore/blob/master/JXCORE_LICENSE)


CocoaHTTPServer (https://github.com/robbiehanson/CocoaHTTPServer)
License: BSD-style (https://github.com/robbiehanson/CocoaHTTPServer/blob/master/LICENSE.txt)
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Copyright (c) 2008-2010 Rhomobile, Inc.
Copyright (c) 2011-2016 Symbol Technologies LLC
Copyright (c) 2016-2020 Tau Technologies, Inc.
Copyright (c) 2016-2022 Tau Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 27 additions & 0 deletions platform/iphone/Classes/CocoaServer/CCocoaServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#import <Foundation/Foundation.h>
#import "HTTPServer.h"




@interface CCocoaServer : NSObject {

HTTPServer *httpServer;

}

+ (CCocoaServer*)sharedInstance;


-(id)init;
-(void)start;
-(void)stop;



-(void) createServer;

@end


107 changes: 107 additions & 0 deletions platform/iphone/Classes/CocoaServer/CCocoaServer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

#import "CCocoaServer.h"
#import "RhoHTTPConnection.h"


#import "DDLog.h"
#import "DDTTYLogger.h"


extern int rho_http_get_port();
extern const char* rho_http_direct_request( const char* method, const char* uri, const char* query, const void* headers, const char* body, int bodylen, int* responseLength );
extern void rho_http_free_response( const char* data );

extern void* rho_http_init_headers_list();
extern void rho_http_add_header( void* list, const char* name, const char* value );
extern void rho_http_free_headers_list( void* list );

static const int ddLogLevel = LOG_LEVEL_VERBOSE;


static CCocoaServer* ourSharedInstance = nil;

@implementation CCocoaServer

+ (CCocoaServer*)sharedInstance {
if (ourSharedInstance == nil) {
ourSharedInstance = [[CCocoaServer alloc] init];
}
return ourSharedInstance;
}

-(id)init {
[super init];
httpServer = nil;
return self;
}

-(void) createServer {
NSLog(@"$$$$$ HTTP SERVER CREATE()");

[DDLog addLogger:[DDTTYLogger sharedInstance]];

httpServer = [[HTTPServer alloc] init];
[httpServer setType:@"_http._tcp."];
[httpServer setPort:rho_http_get_port()]; // setup port
[httpServer setInterface:@"localhost"]; // accept only inner connections
[httpServer setConnectionClass:[RhoHTTPConnection class]]; // replace connection for replace responce functionlity
}

-(void)start{

if (httpServer == nil) {
NSLog(@"$$$$$ HTTP SERVER START() - NO SERVER !");
return;
}
NSLog(@"$$$$$ HTTP SERVER START()");

NSError *error;

if (![httpServer isRunning]) {
if([httpServer start:&error])
{
NSLog(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
}
else
{
NSLog(@"Error starting HTTP Server: %@", [error localizedDescription]);
}
}
}



-(void)stop {
if (httpServer == nil) {
NSLog(@"$$$$$ HTTP SERVER STOP() - NO SERVER !");

return;
}
NSLog(@"$$$$$ HTTP SERVER STOP()");

if ([httpServer isRunning]) {
[httpServer stop];
}
}


@end




void rho_cocoa_server_init() {
CCocoaServer* server = [CCocoaServer sharedInstance];
[server createServer];
[server start];
}

void rho_cocoa_server_stop() {
CCocoaServer* server = [CCocoaServer sharedInstance];
[server stop];
}

void rho_cocoa_server_start() {
CCocoaServer* server = [CCocoaServer sharedInstance];
[server start];
}
13 changes: 13 additions & 0 deletions platform/iphone/Classes/CocoaServer/RhoHTTPConnection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import <Foundation/Foundation.h>
#import "HTTPConnection.h"



@interface RhoHTTPConnection : HTTPConnection
{
}

- (void)replyToHTTPRequest;

@end

157 changes: 157 additions & 0 deletions platform/iphone/Classes/CocoaServer/RhoHTTPConnection.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#import "RhoHTTPConnection.h"
#import "GCDAsyncSocket.h"
#import "HTTPServer.h"
#import "HTTPMessage.h"
#import "HTTPResponse.h"
#import "HTTPAuthenticationRequest.h"
#import "DDNumber.h"
#import "DDRange.h"
#import "DDData.h"
#import "HTTPFileResponse.h"
#import "HTTPAsyncFileResponse.h"
#import "WebSocket.h"
#import "HTTPLogging.h"


extern const char* rho_http_direct_request( const char* method, const char* uri, const char* query, const void* headers, const char* body, int bodylen, int* responseLength );
extern void rho_http_free_response( const char* data );

extern void* rho_http_init_headers_list();
extern void rho_http_add_header( void* list, const char* name, const char* value );
extern void rho_http_free_headers_list( void* list );

#define HTTP_RESPONSE 90
#define HTTP_FINAL_RESPONSE 91
#define TIMEOUT_WRITE_BODY -1

static const int ddLogLevel = LOG_LEVEL_VERBOSE;

@implementation RhoHTTPConnection


- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path
{

if ([method isEqualToString:@"GET"])
return YES;

if ([method isEqualToString:@"POST"])
return YES;

if ([method isEqualToString:@"PUT"])
return YES;

if ([method isEqualToString:@"HEAD"])
return YES;

return NO;
}

/*
- (BOOL)expectsRequestBodyFromMethod:(NSString *)method atPath:(NSString *)path {
return YES;
}
*/

- (void)processBodyData:(NSData *)postDataChunk {
// only up to 256 Kb !!! - if larger we must accumulate data !
[request setBody:postDataChunk];
}

- (void)replyToHTTPRequest {

NSString *requestUri = [self requestURI];

NSURL* nsurl = [NSURL URLWithString:requestUri];
NSString *requestMethod = [request method];
NSDictionary* requestHeaders = [request allHeaderFields];
NSData* requestBody = [request body];





const char* uri = [[nsurl path] UTF8String];
const char* method = [requestMethod UTF8String];
const char* body = [requestBody bytes];
int bodylen = (int)requestBody.length;
const char* query = [[nsurl query] UTF8String];

void* cHeaders = rho_http_init_headers_list();

for (NSString* key in requestHeaders) {
NSString* value = [requestHeaders objectForKey:key];

rho_http_add_header(cHeaders, [key UTF8String], [value UTF8String]);

}

int len = 0;

//if ([requestUri isEqualToString:@"/system/js_api_entrypoint"]) {
// int o= 9;
// o = 0;
//}



const char* response = rho_http_direct_request(method, uri, query, cHeaders, body, bodylen, &len);

rho_http_free_headers_list(cHeaders);

//CRhoURLResponse* resp = nil;

if ( response != 0 ) {

/*
// parse http responce for prepare detailed responce instead of raw data
// not used currently - just send raw data
self.httpStatusCode = 0;
self.httpBody = nil;
self.httpHeaderName = nil;
self.httpHeaders = nil;
http_parser_settings settings;
settings.on_header_field = on_http_header;
settings.on_header_value = on_http_header_value;
settings.on_body = on_http_body;
settings.on_status = on_http_status;
settings.on_headers_complete = on_http_cb;
settings.on_message_begin = on_http_cb;
settings.on_message_complete = on_http_cb;
settings.on_url = on_http_data_cb;
http_parser *parser = malloc(sizeof(http_parser));
parser->data = self;
http_parser_init(parser, HTTP_RESPONSE);
http_parser_execute(parser, &settings, response, len);
NSString* strHttpVer = [NSString stringWithFormat:@"%d.%d",parser->http_major,parser->http_minor];
self.httpStatusCode = parser->status_code;
free(parser);
*/

NSData* responseData = [[NSData dataWithBytes:response length:len] copy];
rho_http_free_response(response);

char a[1];
a[0] = 0;
NSData* zz = [NSData dataWithBytes:a length:1];

[asyncSocket writeData:responseData withTimeout:TIMEOUT_WRITE_BODY tag:HTTP_FINAL_RESPONSE];

[self finishResponse];
}
else {
[self handleResourceNotFound];
}

// totally replace responce functionality
//[super replyToHTTPRequest];
}


@end
14 changes: 14 additions & 0 deletions platform/iphone/CocoaHTTPServer/Core/Categories/DDData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <Foundation/Foundation.h>

@interface NSData (DDData)

- (NSData *)md5Digest;

- (NSData *)sha1Digest;

- (NSString *)hexStringValue;

- (NSString *)base64Encoded;
- (NSData *)base64Decoded;

@end
Loading

0 comments on commit e008c14

Please sign in to comment.