1/*
2 * URLSimpleDownload test, X version.
3 */
4#include <stdlib.h>
5#include <stdio.h>
6#include <Carbon/Carbon.h>
7#include <time.h>
8#include <string.h>
9#include <ctype.h>
10
11#define DEFAULT_HOST		"gss.cdnow.com"
12#define DEFAULT_PATH		"/"
13#define DEFAULT_SSL			0		/* 0 --> http, 1 --> https */
14
15#define COMP_LENGTH		80
16#define URL_LENGTH		256
17
18
19/* shouldn't be needed if no kURLDisplayProgressFlag */
20static void Initialize()
21{
22	//InitGraf(&qd.thePort);
23	//InitWindows();
24	//InitCursor();
25	//InitMenus();
26	//InitFonts();
27}
28
29
30
31#if 0
32static time_t lastTime = (time_t)0;
33#define TIME_INTERVAL		3
34/*
35 * Just  keep UI alive in case we want to quit the app
36 */
37static pascal OSStatus eventCallback(
38     void* userContext,
39     EventRecord *event)
40{
41	time_t thisTime = time(0);
42
43	if((thisTime - lastTime) >= TIME_INTERVAL) {
44		printf("."); fflush(stdout);
45		lastTime = thisTime;
46	}
47	return noErr;
48}
49#endif
50
51/*
52 * Assuming *h contains ASCII text, dump it 'til the user cries uncle.
53 */
54#define BYTES_PER_SPURT		128
55
56static void
57dumpText(Handle h)
58{
59	Size	totalBytes;
60	Size	bytesWritten = 0;
61	Ptr		p;
62	Size	thisWrite;
63	Size	i;
64	char	resp;
65	char	c;
66	char	lastWasHex = 0;
67
68	HLock(h);
69	totalBytes = GetHandleSize(h);
70	if(totalBytes == 0) {
71		printf("*** Zero bytes obtained\n");
72		return;
73	}
74	p = *h;
75	while(bytesWritten < totalBytes) {
76		thisWrite = totalBytes - bytesWritten;
77		if(thisWrite > BYTES_PER_SPURT) {
78			thisWrite = BYTES_PER_SPURT;
79		}
80		for(i=0; i<thisWrite; i++) {
81			c = *p++;
82			if(isprint(c)) {
83				printf("%c", c);
84				lastWasHex = 0;
85			}
86			else {
87				if(!lastWasHex) {
88					printf("|");
89				}
90				printf("%02X|", (unsigned)c & 0xff);
91				lastWasHex = 1;
92			}
93		}
94		totalBytes += thisWrite;
95		if(totalBytes == bytesWritten) {
96			printf("\n");
97			break;
98		}
99		printf("\nMore (y/anything)? ");
100		fpurge(stdin);
101		resp = getchar();
102		if(resp != 'y') {
103			break;
104		}
105	}
106	HUnlock(h);
107	return;
108}
109
110int main()
111{
112	Handle		h;
113	char		hostName[COMP_LENGTH];
114	char		path[COMP_LENGTH];
115	char		url[URL_LENGTH];
116	char		scheme[10];			/* http, https */
117	char		isSsl = DEFAULT_SSL;
118	char		resp;
119	OSStatus	ortn;
120
121	Initialize();
122	strcpy(hostName, DEFAULT_HOST);
123	strcpy(path, DEFAULT_PATH);
124	if(isSsl) {
125		strcpy(scheme, "https");
126	}
127	else {
128		strcpy(scheme, "http");
129	}
130	while(1) {
131		printf("  h   Set Host      (current = %s)\n", hostName);
132		printf("  p   Set path      (current = %s)\n", path);
133		printf("  s   Set SSL true  (current = %d)\n", isSsl);
134		printf("  S   Set SSL false\n");
135		printf("  g   Get the URL\n");
136		printf("  q   quit\n");
137		printf("\nEnter command: ");
138		fpurge(stdin);
139		resp = getchar();
140		switch(resp) {
141			case 'h':
142				printf("Enter host name: ");
143				scanf("%s", hostName);
144				break;
145			case 'p':
146				printf("Enter path: ");
147				scanf("%s", path);
148				break;
149			case 's':
150				isSsl = 1;
151				strcpy(scheme, "https");
152				break;
153			case 'S':
154				isSsl = 0;
155				strcpy(scheme, "http");
156				break;
157			case 'g':
158				sprintf(url, "%s://%s%s", scheme, hostName, path);
159				printf("...url = %s\n", url);
160				h = NewHandle(0);				/* what the spec says */
161				ortn = URLSimpleDownload(url,
162					NULL,
163					h,
164					0,	//kURLDisplayProgressFlag,					// URLOpenFlags
165					NULL,		//eventCallback,
166					NULL);				// userContext
167				if(ortn) {
168					printf("URLSimpleDownload returned %d\n", (int)ortn);
169				}
170				else {
171					dumpText(h);
172				}
173				DisposeHandle(h);
174				break;
175			case 'q':
176				goto done;
177			default:
178				printf("Huh?\n");
179				continue;
180		}
181
182	}
183done:
184	return 0;
185}
186