mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-28 05:14:34 +00:00
Reduce code size and make codebase more consistent (#2305)
This commit is contained in:
203 files changed
+4058
-3474
No files matched your search
@@ -18,26 +18,21 @@
|
||||
@end
|
||||
|
||||
@interface SUTestWebServerConnection : NSObject <NSStreamDelegate>
|
||||
|
||||
@property (nonatomic) NSString* workingDirectory;
|
||||
@property (nonatomic, weak) id<SUTestWebServerConnectionDelegate> delegate;
|
||||
@property (nonatomic) NSInputStream *inputStream;
|
||||
@property (nonatomic) NSOutputStream *outputStream;
|
||||
@property (nonatomic) NSData *dataToWrite;
|
||||
@property (nonatomic) NSInteger numBytesToWrite;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SUTestWebServerConnection
|
||||
{
|
||||
NSString* _workingDirectory;
|
||||
NSInputStream *_inputStream;
|
||||
NSOutputStream *_outputStream;
|
||||
NSData *_dataToWrite;
|
||||
NSInteger _numBytesToWrite;
|
||||
|
||||
__weak id<SUTestWebServerConnectionDelegate> _delegate;
|
||||
}
|
||||
|
||||
@synthesize workingDirectory = _workingDirectory;
|
||||
@synthesize delegate = _delegate;
|
||||
@synthesize inputStream = _inputStream;
|
||||
@synthesize outputStream = _outputStream;
|
||||
@synthesize dataToWrite = _dataToWrite;
|
||||
@synthesize numBytesToWrite = _numBytesToWrite;
|
||||
|
||||
- (instancetype)initWithNativeHandle:(CFSocketNativeHandle)handle workingDirectory:(NSString*)workingDirectory delegate:(id<SUTestWebServerConnectionDelegate>)delegate {
|
||||
- (instancetype)initWithNativeHandle:(CFSocketNativeHandle)handle workingDirectory:(NSString*)workingDirectory delegate:(id<SUTestWebServerConnectionDelegate>)delegate SPU_OBJC_DIRECT
|
||||
{
|
||||
self = [super init];
|
||||
assert(self != nil);
|
||||
|
||||
@@ -66,9 +61,10 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
NSInputStream *inputStream = self.inputStream;
|
||||
NSOutputStream *outputStream = self.outputStream;
|
||||
- (void)close SPU_OBJC_DIRECT
|
||||
{
|
||||
NSInputStream *inputStream = _inputStream;
|
||||
NSOutputStream *outputStream = _outputStream;
|
||||
if (inputStream == nil) {
|
||||
assert(outputStream == nil);
|
||||
return;
|
||||
@@ -77,19 +73,20 @@
|
||||
[outputStream close];
|
||||
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
self.inputStream = nil;
|
||||
self.outputStream = nil;
|
||||
[self.delegate connectionDidClose:self];
|
||||
_inputStream = nil;
|
||||
_outputStream = nil;
|
||||
[_delegate connectionDidClose:self];
|
||||
}
|
||||
|
||||
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
|
||||
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
|
||||
{
|
||||
if (eventCode == NSStreamEventEndEncountered) {
|
||||
[self close];
|
||||
return;
|
||||
}
|
||||
if (aStream == self.inputStream && eventCode == NSStreamEventHasBytesAvailable) {
|
||||
if (aStream == _inputStream && eventCode == NSStreamEventHasBytesAvailable) {
|
||||
uint8_t buffer[8096];
|
||||
const NSInteger numBytes = [self.inputStream read:buffer maxLength:sizeof(buffer)];
|
||||
const NSInteger numBytes = [_inputStream read:buffer maxLength:sizeof(buffer)];
|
||||
if (numBytes > 0) {
|
||||
NSString *request = [[NSString alloc] initWithBytes:buffer length:(NSUInteger)numBytes encoding:NSUTF8StringEncoding];
|
||||
NSArray *lines = [request componentsSeparatedByString:@"\r\n"];
|
||||
@@ -99,7 +96,7 @@
|
||||
if ([(NSString *)[parts objectAtIndex:0] isEqualToString:@"GET"]) {
|
||||
// Use NSURL to strip out query parameters
|
||||
NSString *path = [NSURL URLWithString:[parts objectAtIndex:1] relativeToURL:nil].path;
|
||||
NSString *filePath = [self.workingDirectory stringByAppendingString:path];
|
||||
NSString *filePath = [_workingDirectory stringByAppendingString:path];
|
||||
BOOL isDir = NO;
|
||||
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir] || isDir) {
|
||||
NSLog(@"%@ - 404", requestLine);
|
||||
@@ -113,17 +110,19 @@
|
||||
[self write404];
|
||||
}
|
||||
}
|
||||
} else if (aStream == self.outputStream && eventCode == NSStreamEventHasSpaceAvailable && self.dataToWrite != nil) {
|
||||
} else if (aStream == _outputStream && eventCode == NSStreamEventHasSpaceAvailable && _dataToWrite != nil) {
|
||||
[self checkIfCanWriteNow];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)write404 {
|
||||
- (void)write404 SPU_OBJC_DIRECT
|
||||
{
|
||||
NSString *body = @"<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1></body></html>";
|
||||
[self write:[body dataUsingEncoding:NSUTF8StringEncoding] status:NO];
|
||||
}
|
||||
|
||||
- (void)write:(NSData*)body status:(BOOL)status {
|
||||
- (void)write:(NSData*)body status:(BOOL)status SPU_OBJC_DIRECT
|
||||
{
|
||||
NSString *state = status ? @"200 OK" : @"404 Not Found";
|
||||
NSString *header = [NSString stringWithFormat:@"HTTP/1.0 %@\r\nContent-Length: %lu\r\n\r\n", state, body.length];
|
||||
NSMutableData *response = [[header dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
|
||||
@@ -131,37 +130,40 @@
|
||||
[self queueWrite:response];
|
||||
}
|
||||
|
||||
- (void)queueWrite:(NSData*)data {
|
||||
assert(self.dataToWrite == nil);
|
||||
- (void)queueWrite:(NSData*)data SPU_OBJC_DIRECT
|
||||
{
|
||||
assert(_dataToWrite == nil);
|
||||
assert(data != nil);
|
||||
assert(data.length > 0);
|
||||
self.dataToWrite = data;
|
||||
self.numBytesToWrite = (NSInteger)data.length;
|
||||
_dataToWrite = data;
|
||||
_numBytesToWrite = (NSInteger)data.length;
|
||||
[self checkIfCanWriteNow];
|
||||
}
|
||||
|
||||
- (void)checkIfCanWriteNow {
|
||||
assert(self.dataToWrite != nil);
|
||||
if (self.numBytesToWrite == 0) {
|
||||
- (void)checkIfCanWriteNow SPU_OBJC_DIRECT
|
||||
{
|
||||
assert(_dataToWrite != nil);
|
||||
if (_numBytesToWrite == 0) {
|
||||
// nothing more to write, we're done.
|
||||
self.dataToWrite = nil;
|
||||
self.numBytesToWrite = -1;
|
||||
} else if (self.outputStream.hasSpaceAvailable) {
|
||||
_dataToWrite = nil;
|
||||
_numBytesToWrite = -1;
|
||||
} else if (_outputStream.hasSpaceAvailable) {
|
||||
[self writeNow];
|
||||
}
|
||||
// otherwise wait for space available event
|
||||
}
|
||||
|
||||
- (void)writeNow {
|
||||
assert(self.outputStream != nil);
|
||||
assert(self.outputStream.hasSpaceAvailable);
|
||||
assert(self.dataToWrite != nil);
|
||||
NSData *dataToWrite = self.dataToWrite;
|
||||
const uint8_t *bytesOffset = (const uint8_t*)dataToWrite.bytes + ((NSInteger)dataToWrite.length - self.numBytesToWrite);
|
||||
const NSInteger bytesWritten = [self.outputStream write:bytesOffset maxLength:(NSUInteger)self.numBytesToWrite];
|
||||
- (void)writeNow SPU_OBJC_DIRECT
|
||||
{
|
||||
assert(_outputStream != nil);
|
||||
assert(_outputStream.hasSpaceAvailable);
|
||||
assert(_dataToWrite != nil);
|
||||
NSData *dataToWrite = _dataToWrite;
|
||||
const uint8_t *bytesOffset = (const uint8_t*)dataToWrite.bytes + ((NSInteger)dataToWrite.length - _numBytesToWrite);
|
||||
const NSInteger bytesWritten = [_outputStream write:bytesOffset maxLength:(NSUInteger)_numBytesToWrite];
|
||||
if (bytesWritten > 0) {
|
||||
self.numBytesToWrite = self.numBytesToWrite - bytesWritten;
|
||||
assert(self.numBytesToWrite >= 0);
|
||||
_numBytesToWrite = _numBytesToWrite - bytesWritten;
|
||||
assert(_numBytesToWrite >= 0);
|
||||
// wait for next space available event to write more
|
||||
} else {
|
||||
NSLog(@"Error: bytes written = %ld (%@)", bytesWritten, [NSString stringWithUTF8String:strerror(errno)]);
|
||||
@@ -170,7 +172,7 @@
|
||||
|
||||
@end
|
||||
|
||||
@interface SUTestWebServer () <SUTestWebServerConnectionDelegate> {
|
||||
SPU_OBJC_DIRECT_MEMBERS @interface SUTestWebServer () <SUTestWebServerConnectionDelegate> {
|
||||
CFSocketRef _socket;
|
||||
}
|
||||
|
||||
@@ -181,7 +183,8 @@
|
||||
|
||||
@end
|
||||
|
||||
static void connectCallback(CFSocketRef __unused s, CFSocketCallBackType type, CFDataRef __unused address, const void *data, void *info) {
|
||||
static void connectCallback(CFSocketRef __unused s, CFSocketCallBackType type, CFDataRef __unused address, const void *data, void *info)
|
||||
{
|
||||
if (type == kCFSocketAcceptCallBack) {
|
||||
assert(data != NULL);
|
||||
assert(info != NULL);
|
||||
@@ -196,7 +199,8 @@ static void connectCallback(CFSocketRef __unused s, CFSocketCallBackType type, C
|
||||
@synthesize connections = _connections;
|
||||
@synthesize workingDirectory = _workingDirectory;
|
||||
|
||||
- (instancetype)initWithPort:(int)port workingDirectory:(NSString*)workingDirectory {
|
||||
- (instancetype)initWithPort:(int)port workingDirectory:(NSString*)workingDirectory
|
||||
{
|
||||
self = [super init];
|
||||
assert(self != nil);
|
||||
|
||||
@@ -231,33 +235,33 @@ static void connectCallback(CFSocketRef __unused s, CFSocketCallBackType type, C
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)connectionDidClose:(SUTestWebServerConnection *)sender {
|
||||
assert(self.connections != nil);
|
||||
assert([self.connections containsObject:sender]);
|
||||
[self.connections removeObject:sender];
|
||||
- (void)connectionDidClose:(SUTestWebServerConnection *)sender
|
||||
{
|
||||
assert(_connections != nil);
|
||||
assert([_connections containsObject:sender]);
|
||||
[_connections removeObject:sender];
|
||||
}
|
||||
|
||||
- (void)accept:(CFSocketNativeHandle)address {
|
||||
SUTestWebServerConnection *conn = [[SUTestWebServerConnection alloc] initWithNativeHandle:address workingDirectory:self.workingDirectory delegate:self];
|
||||
- (void)accept:(CFSocketNativeHandle)address
|
||||
{
|
||||
SUTestWebServerConnection *conn = [[SUTestWebServerConnection alloc] initWithNativeHandle:address workingDirectory:_workingDirectory delegate:self];
|
||||
assert(conn != nil);
|
||||
if (conn) {
|
||||
assert(self.connections != nil);
|
||||
[self.connections addObject:conn];
|
||||
assert(_connections != nil);
|
||||
[_connections addObject:conn];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
for (SUTestWebServerConnection *conn in self.connections) {
|
||||
- (void)close
|
||||
{
|
||||
for (SUTestWebServerConnection *conn in _connections) {
|
||||
[conn close];
|
||||
}
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
|
||||
if (_socket) {
|
||||
CFSocketInvalidate(_socket);
|
||||
CFRelease(_socket);
|
||||
_socket = NULL;
|
||||
}
|
||||
#pragma clang diagnostic pop
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user