mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-27 05:04:27 +00:00
Use properties to avoid having to disable warning for entire file
This commit is contained in:
1 parent
ff342f1e5b
commit
948eccb169
1 file changed
+65
-49
@@ -10,9 +10,6 @@
|
||||
#import <sys/socket.h>
|
||||
#import <netinet/in.h>
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
|
||||
|
||||
@class SUTestWebServerConnection;
|
||||
|
||||
@protocol SUTestWebServerConnectionDelegate <NSObject>
|
||||
@@ -22,16 +19,23 @@
|
||||
|
||||
@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;
|
||||
id<SUTestWebServerConnectionDelegate> _delegate;
|
||||
NSInputStream *_inputStream;
|
||||
NSOutputStream *_outputStream;
|
||||
NSData *_dataToWrite;
|
||||
NSInteger _numBytesToWrite;
|
||||
}
|
||||
@implementation SUTestWebServerConnection
|
||||
|
||||
@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 {
|
||||
self = [super init];
|
||||
@@ -63,17 +67,19 @@
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
if (!_inputStream) {
|
||||
assert(_outputStream == nil);
|
||||
NSInputStream *inputStream = self.inputStream;
|
||||
NSOutputStream *outputStream = self.outputStream;
|
||||
if (inputStream == nil) {
|
||||
assert(outputStream == nil);
|
||||
return;
|
||||
}
|
||||
[_inputStream close];
|
||||
[_outputStream close];
|
||||
[_inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[_outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
_inputStream = nil;
|
||||
_outputStream = nil;
|
||||
[_delegate connectionDidClose:self];
|
||||
[inputStream close];
|
||||
[outputStream close];
|
||||
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
self.inputStream = nil;
|
||||
self.outputStream = nil;
|
||||
[self.delegate connectionDidClose:self];
|
||||
}
|
||||
|
||||
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
|
||||
@@ -81,9 +87,9 @@
|
||||
[self close];
|
||||
return;
|
||||
}
|
||||
if (aStream == _inputStream && eventCode == NSStreamEventHasBytesAvailable) {
|
||||
if (aStream == self.inputStream && eventCode == NSStreamEventHasBytesAvailable) {
|
||||
uint8_t buffer[8096];
|
||||
const NSInteger numBytes = [_inputStream read:buffer maxLength:sizeof(buffer)];
|
||||
const NSInteger numBytes = [self.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"];
|
||||
@@ -93,7 +99,7 @@
|
||||
if ([parts[0] isEqualToString:@"GET"]) {
|
||||
// Use NSURL to strip out query parameters
|
||||
NSString *path = [NSURL URLWithString:parts[1] relativeToURL:nil].path;
|
||||
NSString *filePath = [_workingDirectory stringByAppendingString:path];
|
||||
NSString *filePath = [self.workingDirectory stringByAppendingString:path];
|
||||
BOOL isDir = NO;
|
||||
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir] || isDir) {
|
||||
NSLog(@"%@ - 404", requestLine);
|
||||
@@ -107,7 +113,7 @@
|
||||
[self write404];
|
||||
}
|
||||
}
|
||||
} else if (aStream == _outputStream && eventCode == NSStreamEventHasSpaceAvailable && _dataToWrite) {
|
||||
} else if (aStream == self.outputStream && eventCode == NSStreamEventHasSpaceAvailable && self.dataToWrite != nil) {
|
||||
[self checkIfCanWriteNow];
|
||||
}
|
||||
}
|
||||
@@ -126,35 +132,36 @@
|
||||
}
|
||||
|
||||
- (void)queueWrite:(NSData*)data {
|
||||
assert(_dataToWrite == nil);
|
||||
assert(self.dataToWrite == nil);
|
||||
assert(data != nil);
|
||||
assert(data.length > 0);
|
||||
_dataToWrite = data;
|
||||
_numBytesToWrite = (NSInteger)data.length;
|
||||
self.dataToWrite = data;
|
||||
self.numBytesToWrite = (NSInteger)data.length;
|
||||
[self checkIfCanWriteNow];
|
||||
}
|
||||
|
||||
- (void)checkIfCanWriteNow {
|
||||
assert(_dataToWrite != nil);
|
||||
if (_numBytesToWrite == 0) {
|
||||
assert(self.dataToWrite != nil);
|
||||
if (self.numBytesToWrite == 0) {
|
||||
// nothing more to write, we're done.
|
||||
_dataToWrite = nil;
|
||||
_numBytesToWrite = -1;
|
||||
} else if (_outputStream.hasSpaceAvailable) {
|
||||
self.dataToWrite = nil;
|
||||
self.numBytesToWrite = -1;
|
||||
} else if (self.outputStream.hasSpaceAvailable) {
|
||||
[self writeNow];
|
||||
}
|
||||
// otherwise wait for space available event
|
||||
}
|
||||
|
||||
- (void)writeNow {
|
||||
assert(_outputStream != nil);
|
||||
assert(_outputStream.hasSpaceAvailable);
|
||||
assert(_dataToWrite != nil);
|
||||
const uint8_t *bytesOffset = (const uint8_t*)_dataToWrite.bytes + ((NSInteger)_dataToWrite.length - _numBytesToWrite);
|
||||
const NSInteger bytesWritten = [_outputStream write:bytesOffset maxLength:(NSUInteger)_numBytesToWrite];
|
||||
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];
|
||||
if (bytesWritten > 0) {
|
||||
_numBytesToWrite -= bytesWritten;
|
||||
assert(_numBytesToWrite >= 0);
|
||||
self.numBytesToWrite = self.numBytesToWrite - bytesWritten;
|
||||
assert(self.numBytesToWrite >= 0);
|
||||
// wait for next space available event to write more
|
||||
} else {
|
||||
NSLog(@"Error: bytes written = %ld (%@)", bytesWritten, [NSString stringWithUTF8String:strerror(errno)]);
|
||||
@@ -164,24 +171,31 @@
|
||||
@end
|
||||
|
||||
@interface SUTestWebServer () <SUTestWebServerConnectionDelegate> {
|
||||
NSMutableArray *_connections;
|
||||
NSString *_workingDirectory;
|
||||
CFSocketRef _socket;
|
||||
}
|
||||
|
||||
@property (nonatomic) NSMutableArray *connections;
|
||||
@property (nonatomic) NSString *workingDirectory;
|
||||
|
||||
- (void)accept:(CFSocketNativeHandle)address;
|
||||
|
||||
@end
|
||||
|
||||
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);
|
||||
SUTestWebServer *server = (__bridge SUTestWebServer*)info;
|
||||
assert(server != nil);
|
||||
[server accept:*(const CFSocketNativeHandle*)data];
|
||||
}
|
||||
}
|
||||
|
||||
@implementation SUTestWebServer
|
||||
|
||||
@synthesize connections = _connections;
|
||||
@synthesize workingDirectory = _workingDirectory;
|
||||
|
||||
- (instancetype)initWithPort:(int)port workingDirectory:(NSString*)workingDirectory {
|
||||
self = [super init];
|
||||
assert(self != nil);
|
||||
@@ -218,30 +232,32 @@ static void connectCallback(CFSocketRef __unused s, CFSocketCallBackType type, C
|
||||
}
|
||||
|
||||
- (void)connectionDidClose:(SUTestWebServerConnection *)sender {
|
||||
assert(_connections != nil);
|
||||
assert([_connections containsObject:sender]);
|
||||
[_connections removeObject:sender];
|
||||
assert(self.connections != nil);
|
||||
assert([self.connections containsObject:sender]);
|
||||
[self.connections removeObject:sender];
|
||||
}
|
||||
|
||||
- (void)accept:(CFSocketNativeHandle)address {
|
||||
SUTestWebServerConnection *conn = [[SUTestWebServerConnection alloc] initWithNativeHandle:address workingDirectory:_workingDirectory delegate:self];
|
||||
SUTestWebServerConnection *conn = [[SUTestWebServerConnection alloc] initWithNativeHandle:address workingDirectory:self.workingDirectory delegate:self];
|
||||
assert(conn != nil);
|
||||
if (conn) {
|
||||
[_connections addObject:conn];
|
||||
assert(self.connections != nil);
|
||||
[self.connections addObject:conn];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
for (SUTestWebServerConnection *conn in _connections) {
|
||||
for (SUTestWebServerConnection *conn in self.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
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
Reference in new issue
Block a user