home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OTSimpleDownloadHTTPTest.c
-
- Contains: A test program for the simple HTTP download sample.
-
- Written by: Quinn "The Eskimo!"
-
- Copyright: Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/23/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
- #include <DriverServices.h>
- /////////////////////////////////////////////////////////////////////
- // The OT debugging macros in <OTDebug.h> require this variable to
- // be set.
- #include <TextUtils.h>
-
- #ifndef qDebug
- #define qDebug 1
- #endif
-
- /////////////////////////////////////////////////////////////////////
- // Pick up all the standard OT stuff.
-
- #include <OpenTransport.h>
-
- /////////////////////////////////////////////////////////////////////
- // Pick up all the OT TCP/IP stuff.
-
- #include <OpenTptInternet.h>
-
- /////////////////////////////////////////////////////////////////////
- // Pick up the OTDebugBreak and OTAssert macros.
-
- #include <OTDebug.h>
-
- /////////////////////////////////////////////////////////////////////
- // OTDebugStr is not defined in any OT header files, but it is
- // exported by the libraries, so we define the prototype here.
-
- extern pascal void OTDebugStr(const char* str);
-
- #include <OpenTptInternet.h>
- #include <PLStringFuncs.h>
- #include <Threads.h>
- #include <Files.h>
-
- #include <string.h>
- #include <stdio.h>
- #include <ctype.h>
-
- #include "OTSimpleDownloadHTTP.h"
-
- enum {
- kNotHTTPURLErr = -666,
- kWhatImplementationErr = -667
- };
-
- enum {
- kDownloadSimple,
- kDownloadFaster
- };
-
- static OSStatus DownloadURL(const char *urlString)
- {
- OSStatus err;
- size_t hostCharCount;
- char hostName[256];
- Str255 tickCountString;
- Str255 destFileName;
- FSSpec destFSSpec;
- short destFileRefNum;
- char httpGetCommand[256];
- OTTimeStamp startTime;
- UInt32 timeToDownload;
- long fileSize;
-
- err = noErr;
- // First check that the urlString begins with "http://"
- if ( strspn(urlString, "http://") != strlen("http://") ) {
- err = kNotHTTPURLErr;
- }
-
- // Now skip over the "http://" and extract the host name.
- if (err == noErr) {
- // Skip over the "http://".
- urlString += strlen("http://");
-
- // Count the characters before the next slash.
- hostCharCount = strcspn(urlString, "/");
-
- // Extract those characters from the URL into hostName
- // and then make sure it's null terminated.
- (void) strncpy(hostName, urlString, hostCharCount);
- hostName[hostCharCount] = 0;
- urlString += hostCharCount;
-
- // Add a ":80" to the host name if necessary.
- if ( strchr( hostName, ':' ) == nil ) {
- strcat( hostName, ":80" );
- }
-
- if (err == noErr) {
- NumToString(TickCount(), tickCountString);
- PStrCopy(destFileName, "\pSimpleDownload#");
- PStrCat(destFileName, tickCountString);
- (void) FSMakeFSSpec(0, 0, destFileName, &destFSSpec);
- (void) FSpCreate(&destFSSpec, 'R*ch', 'TEXT', 0);
- err = FSpOpenDF(&destFSSpec, fsRdWrPerm, &destFileRefNum);
- if (err != noErr) {
- destFileRefNum = 0;
- }
- }
-
- if (err == noErr) {
- // Now place the URL into the HTTP command that we send to DownloadHTTPSimple.
- if ( *urlString == 0 ) {
- urlString = "/";
- }
- (void) sprintf(httpGetCommand, "GET %s HTTP/1.0%c%c%c%c", urlString, 13, 10, 13, 10);
- printf("Calling DownloadHTTPXxxx(“%s”, “%s”, %04x).\n", hostName, urlString, destFileRefNum);
-
- OTGetTimeStamp(&startTime);
- err = DownloadHTTPSimple(hostName, httpGetCommand, destFileRefNum);
- timeToDownload = OTElapsedMicroseconds(&startTime);
- }
-
- if (err == noErr) {
- OTAssert( "DownloadURL: GetFPos failed", GetFPos(destFileRefNum, &fileSize) == noErr);
- printf("Bytes downloaded: %d.\n", fileSize);
- printf("Time to download: %dus.\n", timeToDownload);
- printf("Bytes per second: %f.\n", (float) fileSize / (((float) timeToDownload) / 1000000.0) );
- }
-
- // Clean up.
- if (destFileRefNum != 0) {
- (void) FSClose(destFileRefNum);
- }
- if (err != noErr) {
- //(void) FSpDelete(&destFSSpec);
- }
-
- }
-
- return (err);
- }
-
- static UInt32 gLastPrinted = 0;
-
- static pascal OSStatus ProgressThread(void *junkParam)
- {
- #pragma unused(junkParam)
- OSStatus junk;
-
- while (true) {
- if ( TickCount() > (gLastPrinted + 60) ) {
- printf(".");
- fflush(stdout);
- gLastPrinted = TickCount();
- }
- junk = YieldToAnyThread();
- OTAssert("ProgressThread: YieldToAnyThread failed", junk == noErr);
- }
-
- return (noErr);
- }
-
- static const char *defaultURLs[10] = {
- "http://www.apple.com",
- "http://www.apple.com/developer/",
- "http://developer.apple.com/dev/opentransport/",
- "http://developer.apple.com/technotes/index.html",
- "",
- "",
- "",
- "",
- "",
- ""
- };
-
- void main(void)
- {
- OSStatus err;
- OSStatus junk;
- char urlString[256];
- UInt32 i;
- ThreadID progressThread;
-
- printf("Hello Cruel World!\n");
- printf("\n");
- printf("OTDownloadHTTP!\n");
- printf("-- Downloads a URL using OT's thread support\n");
- printf("-- or using notifiers.\n");
- printf("\n");
-
- err = InitOpenTransport();
- if (err == noErr) {
-
- for (i = 0; i < 10; i++) {
- if ( *defaultURLs[i] != 0 ) {
- printf("%d> %s\n", i, defaultURLs[i]);
- }
- }
- printf("\n");
- printf("Enter a URL (or type a number from the above list):\n");
- gets(urlString);
- if ( strlen(urlString) == 1 && isdigit(urlString[0])) {
- strcpy( urlString, defaultURLs[ urlString[0] - '0' ] );
- }
- if ( urlString[0] == 0 ) {
- strcpy( urlString, defaultURLs[0] );
- }
- err = NewThread(kCooperativeThread,
- (ThreadEntryProcPtr) ProgressThread, nil,
- 0, kCreateIfNeeded,
- nil,
- &progressThread);
- if (err == noErr) {
- err = DownloadURL(urlString);
- junk = DisposeThread(progressThread, nil, true);
- OTAssert("main: DisposeThread failed", junk == noErr);
- }
-
- CloseOpenTransport();
- }
-
- if (err == noErr) {
- printf("Success.\n");
- } else {
- printf("Failed with error %d.\n", err);
- }
- printf("Done. Press command-Q to Quit.\n");
- }
-
-
-