File: socket.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (186 lines) | stat: -rw-r--r-- 5,310 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
 * This test tests basic client side socket
 */
#import "ObjectTesting.h"
#import <Foundation/Foundation.h>
#import <Foundation/NSStream.h>

#if	defined(GS_USE_GNUTLS)
#define	SSL_SUPPORTED	GS_USE_GNUTLS
#else
#define	SSL_SUPPORTED	1 /* Assume Apple supports it */
#endif

static NSOutputStream *defaultOutput = nil;
static NSInputStream *defaultInput = nil;
static int byteCount = 0;

static const uint8_t *rawstring = (const uint8_t*)"GET / HTTP/1.0\r\n\r\n";
static BOOL     done = NO;

@interface Listener : NSObject
@end

@implementation Listener

- (void)stream: (NSStream *)theStream handleEvent: (NSStreamEvent)streamEvent
{
  static uint8_t buffer[4096];
  static BOOL doneWrite = NO;
  int readSize;
NSLog(@"Got %ld on %p", (long int)streamEvent, theStream);
  switch (streamEvent) 
    {
    case NSStreamEventOpenCompleted: 
      {
	if (theStream == defaultOutput)
	  {
	    doneWrite = NO;
	  }
	break;
      }
    case NSStreamEventHasSpaceAvailable: 
      {
        NSAssert(theStream==defaultOutput, @"Wrong stream for reading");
        if (doneWrite == NO)
          {
            // there may be a problem so that write is not complete. 
            // However, since this is so short it is pretty much always ok.
	    NSLog(@"Written request");
	    doneWrite = YES;
            [defaultOutput write: rawstring
		       maxLength: strlen((char*)rawstring)];
	  }
/*
	else
	  {
            [defaultOutput close];
	    [defaultOutput removeFromRunLoop: [NSRunLoop currentRunLoop]
				     forMode: NSDefaultRunLoopMode];
	    NSLog(@"Closed %p", defaultOutput);
          }          
*/
        break;
      }
    case NSStreamEventHasBytesAvailable: 
      {
        NSAssert(theStream==defaultInput, @"Wrong stream for reading");
        readSize = [defaultInput read: buffer maxLength: 4096];
        if (readSize < 0)
          {
            // it is possible that readSize<0 but not an Error.
	    // For example would block
NSLog(@"%@", [defaultInput streamError]);
            NSAssert([defaultInput streamError]==nil, @"read error");
          }
        else if (readSize == 0)
	  {
            [defaultInput close];
	    [defaultInput removeFromRunLoop: [NSRunLoop currentRunLoop]
				    forMode: NSDefaultRunLoopMode];
	    NSLog(@"Closed %p", defaultInput);
	  }
        else
	  {
            byteCount += readSize;
	    NSLog(@"Read %d: %*.*s", readSize, readSize, readSize, buffer);
	  }
        break;
      }
    case NSStreamEventEndEncountered: 
      {
        [theStream close];
	[theStream removeFromRunLoop: [NSRunLoop currentRunLoop]
			     forMode: NSDefaultRunLoopMode];
        NSLog(@"Close %p", theStream);
        done = YES;
        break;
      }
    default: 
      {
        NSAssert1(1, @"Error! code is %ld",
          (long int)[[theStream streamError] code]);
        break;
      }  
    }
} 

int main()
{
  NSAutoreleasePool   *arp = [NSAutoreleasePool new];
  NSRunLoop *rl;
  NSHost *host;
  Listener *li;
  NSDate *d;

  rl = [NSRunLoop currentRunLoop];
  host = [NSHost hostWithName: @"www.google.com"];
  //host = [NSHost hostWithName: @"localhost"];

#if 1
  li = [[Listener new] autorelease];
  [NSStream getStreamsToHost: host port: 80
    inputStream: &defaultInput outputStream: &defaultOutput];

  [defaultInput setDelegate: li];
  [defaultOutput setDelegate: li];
  [defaultInput scheduleInRunLoop: rl forMode: NSDefaultRunLoopMode];
  [defaultOutput scheduleInRunLoop: rl forMode: NSDefaultRunLoopMode];
  [defaultInput open];
  [defaultOutput open];
  
  d = [NSDate dateWithTimeIntervalSinceNow: 30];
  while (done == NO && [d timeIntervalSinceNow] > 0.0)
    {
      [rl runMode: NSDefaultRunLoopMode beforeDate: d];
    }

  // I cannot verify the content at www.google.com,
  // so as long as it has something, that is PASSing
  testHopeful = YES;
  PASS(byteCount>0, "read www.google.com");
  testHopeful = NO;
  [defaultInput setDelegate: nil];
  [defaultOutput setDelegate: nil];
#endif

  START_SET("NSStream SSL")
    if (!SSL_SUPPORTED)
      SKIP("NSStream SSL functions not supported\nThe GNU TLS library was not provided when GNUstep-base was configured/built.")
    done = NO;
    byteCount = 0;
    defaultInput = nil;
    defaultOutput = nil;
    li = [[Listener new] autorelease];
    [NSStream getStreamsToHost: host port: 443
      inputStream: &defaultInput outputStream: &defaultOutput];

    [defaultInput setDelegate: li];
    [defaultOutput setDelegate: li];
    [defaultInput scheduleInRunLoop: rl forMode: NSDefaultRunLoopMode];
    [defaultOutput scheduleInRunLoop: rl forMode: NSDefaultRunLoopMode];
    [defaultInput setProperty: NSStreamSocketSecurityLevelNegotiatedSSL
		       forKey: NSStreamSocketSecurityLevelKey];
    [defaultOutput setProperty: NSStreamSocketSecurityLevelNegotiatedSSL
			forKey: NSStreamSocketSecurityLevelKey];
    [defaultInput open];
    [defaultOutput open];

    d = [NSDate dateWithTimeIntervalSinceNow: 30];
    while (done == NO && [d timeIntervalSinceNow] > 0.0)
      {
	[rl runMode: NSDefaultRunLoopMode beforeDate: d];
      }

    testHopeful = YES;
    PASS(byteCount>0, "read www.google.com https");
    testHopeful = NO;
    [defaultInput setDelegate: nil];
    [defaultOutput setDelegate: nil];
  END_SET("NSStream SSL")

  [arp release];
  return 0;
}

@end