]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/urlPageGrab/cfSimpleGet.cpp
2 * cgGet.cpp - simplified interface to use CFNetwork to do one
3 * HTTP or HTTPS GET transaction.
6 #include "cfSimpleGet.h"
7 #include <CoreServices/CoreServices.h>
13 #define dprintf(args...) printf(args)
15 #define dprintf(args...)
18 CFDataRef
cfSimpleGet(
21 CFURLRef cfUrl
= NULL
;
22 CFMutableDataRef rtnData
= NULL
;
23 CFReadStreamRef rdStrm
= NULL
;
24 CFHTTPMessageRef myReq
= NULL
;
29 CFStreamStatus rsStat
;
31 cfUrl
= CFURLCreateWithBytes(kCFAllocatorDefault
,
32 (UInt8
*)url
, strlen(url
), kCFStringEncodingASCII
, NULL
);
34 printf("***Error creating URL for %s.\n", url
);
37 myReq
= CFHTTPMessageCreateRequest(kCFAllocatorDefault
,
38 CFSTR("GET"), cfUrl
, kCFHTTPVersion1_0
);
40 printf("***Error creating HTTP msg for %s.\n", url
);
43 /* no msg body, no headers */
45 rdStrm
= CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault
, myReq
);
47 printf("***Error creating CFReadStream for %s.\n", url
);
50 dprintf("...opening rd stream for %s\n", url
);
51 brtn
= CFReadStreamOpen(rdStrm
);
53 printf("***Error (1) opening CFReadStream for %s.\n", url
);
59 rsStat
= CFReadStreamGetStatus(rdStrm
);
61 case kCFStreamStatusError
:
62 printf("***Error (2) opening CFReadStream for %s.\n", url
);
64 case kCFStreamStatusAtEnd
:
65 printf("***Unexpected EOF while opening %s\n", url
);
67 case kCFStreamStatusClosed
:
68 printf("***Unexpected close while opening %s\n", url
);
70 case kCFStreamStatusNotOpen
:
71 case kCFStreamStatusOpening
:
74 case kCFStreamStatusOpen
:
75 case kCFStreamStatusReading
:
76 case kCFStreamStatusWriting
:
80 printf("***Unexpected status while opening %s\n", url
);
84 dprintf("...rd stream for %s open for business\n", url
);
87 rtnData
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
89 irtn
= CFReadStreamRead(rdStrm
, rbuf
, BUFSIZE
);
91 /* end of stream, normal exit */
93 dprintf("...EOF on rd stream for %s\n", url
);
96 /* FIXME - how to tell caller? */
97 printf("***Error reading %s\n", url
);
101 dprintf(" ...read %d bytes from %s\n", (int)irtn
, url
);
102 CFDataAppendBytes(rtnData
, rbuf
, irtn
);
116 /* error on zero bytes read */
117 if(CFDataGetLength(rtnData
) == 0) {
118 printf("***No data read from %s\n", url
);