| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | #import "AWEzvXMLStream.h" |
| 35 | #import "AWEzvXMLNode.h" |
| 36 | #import "AWEzvStack.h" |
| 37 | |
| 38 | #import "AWEzvSupportRoutines.h" |
| 39 | |
| 40 | #define XMLCALL |
| 41 | #include <expat.h> |
| 42 | |
| 43 | |
| 44 | void xml_start_element (void *userData, |
| 45 | const XML_Char *name, |
| 46 | const XML_Char **atts); |
| 47 | void xml_end_element (void *userData, |
| 48 | const XML_Char *name); |
| 49 | void xml_char_data (void *userData, |
| 50 | const XML_Char *s, |
| 51 | int len); |
| 52 | |
| 53 | |
| 54 | @implementation AWEzvXMLStream |
| 55 | |
| 56 | - (id) initWithFileHandle:(NSFileHandle *)myConnection initiator:(int)myInitiator |
| 57 | { |
| 58 | if ((self = [super init])) { |
| 59 | connection = [myConnection retain]; |
| 60 | delegate = nil0; |
| 61 | nodeStack = [[AWEzvStack alloc] init]; |
| 62 | initiator = myInitiator; |
| 63 | negotiated = 0; |
| 64 | } |
| 65 | |
| 66 | return self; |
| 67 | } |
| 68 | |
| 69 | - (void)dealloc |
| 70 | { |
| 71 | if (connection != nil0) { |
| 72 | [connection closeFile]; |
| 73 | [connection release]; |
| 74 | } |
| 75 | |
| 76 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 77 | [nodeStack release]; |
| 78 | |
| 79 | [super dealloc]; |
| 80 | } |
| 81 | |
| 82 | - (NSFileHandle *)fileHandle { |
| 83 | return connection; |
| 84 | } |
| 85 | |
| 86 | - (void) readAndParse { |
| 87 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 88 | selector:@selector(dataReceived:) |
| 89 | name:NSFileHandleReadCompletionNotification |
| 90 | object:connection]; |
| 91 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 92 | selector:@selector(dataAvailable:) |
| 93 | name:NSFileHandleDataAvailableNotification |
| 94 | object:connection]; |
| 95 | |
| 96 | parser = XML_ParserCreate(NULL( ( void * ) 0 )); |
| 97 | XML_SetUserData(parser, self); |
| 98 | XML_SetElementHandler(parser, &xml_start_element, &xml_end_element); |
| 99 | XML_SetCharacterDataHandler(parser, &xml_char_data); |
| 100 | XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_NEVER); |
| 101 | |
| 102 | if (!negotiated && initiator) { |
| 103 | [self sendNegotiationInitiator:initiator]; |
| 104 | } |
| 105 | |
| 106 | [connection waitForDataInBackgroundAndNotify]; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | - (void) sendData:(NSData *)data { |
| 111 | [connection writeData:data]; |
| 112 | } |
| 113 | |
| 114 | - (void) sendString:(NSString *)string { |
| 115 | [connection writeData:[string dataUsingEncoding:NSUTF8StringEncoding]]; |
| 116 | } |
| 117 | |
| 118 | - (void) dataReceived:(NSNotification *)aNotification { |
| 119 | NSData *data = [[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem]; |
| 120 | int status; |
| 121 | |
| 122 | if ([data length] == 0) { |
| 123 | if (connection != nil0) |
| 124 | [[aNotification object] autorelease]; |
| 125 | connection = nil0; |
| 126 | [delegate XMLConnectionClosed]; |
| 127 | } |
| 128 | |
| 129 | status = XML_Parse(parser, [data bytes], [data length], [data length] == 0 ? 1 : 0); |
| 130 | |
| 131 | if (connection != nil0) |
| 132 | [[aNotification object] waitForDataInBackgroundAndNotify]; |
| 133 | } |
| 134 | |
| 135 | - (void)dataAvailable:(NSNotification *)aNotification { |
| 136 | [[aNotification object] readInBackgroundAndNotify]; |
| 137 | } |
| 138 | |
| 139 | - (void) setDelegate:(id)myDelegate { |
| 140 | delegate = myDelegate; |
| 141 | } |
| 142 | - (id) delegate { |
| 143 | return delegate; |
| 144 | } |
| 145 | |
| 146 | - (void) xmlStartElement:(const XML_Char *)name attributes:(const XML_Char **)attributes { |
| 147 | AWEzvXMLNode *node; |
| 148 | NSString *attribute, *value, *nodeName; |
| 149 | |
| 150 | nodeName = [NSString stringWithUTF8String:name]; |
| 151 | |
| 152 | node = [[[AWEzvXMLNode alloc] initWithType:AWEzvXMLElement name:nodeName] autorelease]; |
| 153 | |
| 154 | while (*attributes != NULL( ( void * ) 0 )) { |
| 155 | attribute = [NSString stringWithUTF8String:*attributes++]; |
| 156 | value = [NSString stringWithUTF8String:*attributes++]; |
| 157 | [node addAttribute:attribute withValue:value]; |
| 158 | } |
| 159 | |
| 160 | if ([nodeStack size] > 0 && [(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText) |
| 161 | [nodeStack pop]; |
| 162 | |
| 163 | if ([nodeStack size] > 0) { |
| 164 | [(AWEzvXMLNode *)[nodeStack top] addChild:node]; |
| 165 | } |
| 166 | |
| 167 | [nodeStack push:node]; |
| 168 | |
| 169 | if (([nodeName isEqualToString:@"stream:stream"]) && !negotiated) { |
| 170 | if (initiator) { |
| 171 | negotiated = 1; |
| 172 | } else { |
| 173 | [self sendNegotiationInitiator:0]; |
| 174 | } |
| Value stored to 'node' is never read |
| 175 | node = [nodeStack pop]; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | } |
| 180 | |
| 181 | - (void) xmlEndElement:(const XML_Char *)name { |
| 182 | NSString *nodeName = [NSString stringWithUTF8String:name]; |
| 183 | if (([nodeStack size] > 0) && ([(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText)) { |
| 184 | [nodeStack pop]; |
| 185 | } |
| 186 | else if ([nodeStack size] == 0 && [nodeName isEqualToString:@"stream:stream"]) { |
| 187 | |
| 188 | [self endConnection]; |
| 189 | return; |
| 190 | } |
| 191 | AWEzvXMLNode *node = [nodeStack top]; |
| 192 | |
| 193 | if (node != nil0 && [[node name] isEqualToString:nodeName]) { |
| 194 | [nodeStack pop]; |
| 195 | } else if ([[node name] isEqualToString:@"stream:stream"]) { |
| 196 | |
| 197 | [self endConnection]; |
| 198 | return; |
| 199 | } else { |
| 200 | AWEzvLog(@"Ending node that is not at top of stack"); |
| 201 | } |
| 202 | |
| 203 | if ([nodeStack size] == 0 && node != nil0) { |
| 204 | if (delegate != nil0) |
| 205 | [delegate XMLReceivedMessage:node]; |
| 206 | else |
| 207 | AWEzvLog(@"Received message but no delegate to send it to"); |
| 208 | } |
| 209 | |
| 210 | } |
| 211 | - (void) endConnection{ |
| 212 | [self sendString:@"</stream:stream>"]; |
| 213 | [connection closeFile]; |
| 214 | [connection release]; |
| 215 | connection = nil0; |
| 216 | [delegate XMLConnectionClosed]; |
| 217 | |
| 218 | } |
| 219 | |
| 220 | - (void) xmlCharData:(const XML_Char *)data length:(int)len { |
| 221 | AWEzvXMLNode *node; |
| 222 | NSString *newData; |
| 223 | |
| 224 | if ((len == 1) && (*data == '\n')) |
| 225 | return; |
| 226 | |
| 227 | newData = [[[NSString alloc] initWithData:[NSData dataWithBytes:data length:len] encoding:NSUTF8StringEncoding] autorelease]; |
| 228 | |
| 229 | if ([nodeStack size] > 0 && [(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText) { |
| 230 | node = [nodeStack top]; |
| 231 | if ([node name] != nil0) |
| 232 | [node setName:([[node name] stringByAppendingString:newData])]; |
| 233 | else |
| 234 | [node setName:newData]; |
| 235 | } else { |
| 236 | node = [[[AWEzvXMLNode alloc] initWithType:AWEzvXMLText name:newData] autorelease]; |
| 237 | if ([nodeStack top] != nil0) |
| 238 | [(AWEzvXMLNode *)[nodeStack top] addChild:node]; |
| 239 | [nodeStack push:node]; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | - (void) sendNegotiationInitiator:(int)myInitiator { |
| 244 | |
| 245 | [self sendString:@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"]; |
| 246 | |
| 247 | |
| 248 | NSDictionary *handshakeElements = [NSDictionary dictionaryWithObjectsAndKeys: |
| 249 | ([delegate uniqueID] ? [delegate uniqueID] : @"127.0.0.1"), @"to", |
| 250 | [[delegate manager] myInstanceName], @"from", |
| 251 | @"jabber:client", @"xmlns", |
| 252 | @"http://etherx.jabber.org/streams", @"xmlns:stream", |
| 253 | nil0]; |
| 254 | |
| 255 | |
| 256 | CFXMLElementInfo xmlElementInfo; |
| 257 | xmlElementInfo.attributes = (CFDictionaryRef)handshakeElements; |
| 258 | xmlElementInfo.attributeOrder = (CFArrayRef)[NSArray arrayWithObjects:@"to", @"from", @"xmlns", @"xmlns:stream", nil0]; |
| 259 | xmlElementInfo.isEmpty = YES( BOOL ) 1; |
| 260 | |
| 261 | |
| 262 | CFXMLNodeRef xmlNode = CFXMLNodeCreate(NULL( ( void * ) 0 ), kCFXMLNodeTypeElement, (CFStringRef)@"stream:stream", &xmlElementInfo, kCFXMLNodeCurrentVersion); |
| 263 | CFXMLTreeRef xmlTree = CFXMLTreeCreateWithNode(NULL( ( void * ) 0 ), xmlNode); |
| 264 | NSData *data = [(NSData *)CFXMLTreeCreateXMLData(NULL( ( void * ) 0 ), xmlTree) autorelease]; |
| 265 | CFRelease(xmlNode); |
| 266 | CFRelease(xmlTree); |
| 267 | |
| 268 | |
| 269 | NSMutableString *mutableString = [NSMutableString stringWithData:data encoding:NSUTF8StringEncoding]; |
| 270 | [mutableString deleteCharactersInRange:NSMakeRange([mutableString length] - 2, 1)]; |
| 271 | |
| 272 | |
| 273 | [self sendString:mutableString]; |
| 274 | |
| 275 | |
| 276 | if (!myInitiator) |
| 277 | negotiated = 1; |
| 278 | } |
| 279 | |
| 280 | @end |
| 281 | |
| 282 | |
| 283 | void xml_start_element (void *userData, |
| 284 | const XML_Char *name, |
| 285 | const XML_Char **atts) { |
| 286 | AWEzvXMLStream *self = userData; |
| 287 | [self xmlStartElement:name attributes:atts]; |
| 288 | } |
| 289 | |
| 290 | void xml_end_element (void *userData, |
| 291 | const XML_Char *name) { |
| 292 | AWEzvXMLStream *self = userData; |
| 293 | [self xmlEndElement:name]; |
| 294 | } |
| 295 | |
| 296 | void xml_char_data (void *userData, |
| 297 | const XML_Char *s, |
| 298 | int len) { |
| 299 | AWEzvXMLStream *self = userData; |
| 300 | [self xmlCharData:s length:len]; |
| 301 | } |