summaryrefslogtreecommitdiff
path: root/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsHTTP.c
blob: 25821f6960386206b5c65b9e1b06802caa87ad4a (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 *	@file 	ejsHTTP.c
 *	@brief 	HTTP class for the EJ System Object Model
 */
/********************************** Copyright *********************************/
/*
 *	Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
 */
/********************************** Includes **********************************/

#include	"ejs.h"

#if UNUSED
/*********************************** Defines **********************************/

#define EJS_WEB_PROPERTY 	"-web"
#define EJS_HTTP_PROPERTY 	"-http"

#define EJS_HTTP_DISPOSED	550

/*
 *	Control structure for one HTTP request structure
 */
typedef struct HTTPControl {
	Ejs				*ejs;
	IWebResp		*webResp;
	AEECallback		*callback;
	MprBuf			*buf;
	EjsVar			*thisObj;
	char			*url;
	MprTime			requestStarted;
	uint			timeout;
} HTTPControl;

/****************************** Forward Declarations **************************/

static void cleanup(HTTPControl *hp);
static int	createWeb(Ejs *ejs, EjsVar *thisObj);
static void brewCallback(HTTPControl *hp);
static int	httpDestructor(Ejs *ejs, EjsVar *vp);
static void httpCallback(HTTPControl *hp, int responseCode);
static int 	setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv);

/******************************************************************************/
/*
 *	Constructor
 */

int ejsHTTPConstructor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc != 0 && argc != 2) {
		ejsError(ejs, EJS_ARG_ERROR, 
			"Bad usage: HTTP([obj = this, method = onComplete]);");
		return -1;
	}

	if (createWeb(ejs, thisObj) < 0) {
		return -1;
	}

	setCallback(ejs, thisObj, argc, argv);
	return 0;
}

/******************************************************************************/

static int createWeb(Ejs *ejs, EjsVar *thisObj)
{
	MprApp	*app;
	void	*web;

	app = mprGetApp(ejs);

	/*
	 *	Create one instance of IWeb for the entire application. Do it here
	 *	so only widgets that require HTTP incurr the overhead.
	 */
	web = mprGetKeyValue(ejs, "bpWeb");
	if (web == 0) {
		if (ISHELL_CreateInstance(app->shell, AEECLSID_WEB, &web) != SUCCESS) {
			ejsError(ejs, EJS_IO_ERROR, "Can't create IWEB");
			return -1;
		}
	}
	mprSetKeyValue(ejs, "bpWeb", web);
	return 0;
}

/******************************************************************************/
/************************************ Methods *********************************/
/******************************************************************************/
/*
 *	function setCallback(obj, methodString);
 */

static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc >= 1) {
		ejsSetProperty(ejs, thisObj, "obj", argv[0]);
	} else {
		ejsSetProperty(ejs, thisObj, "obj", thisObj);
	}

	if (argc >= 2) {
		ejsSetProperty(ejs, thisObj, "method", argv[1]);
	} else {
		ejsSetPropertyToString(ejs, thisObj, "method", "onComplete");
	}

	return 0;
}

/******************************************************************************/
/*
 *	function fetch();
 */

static int fetchProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
	HTTPControl		*hp;
	EjsProperty 	*pp;
	MprApp			*app;
	IWeb			*web;

	if (argc != 1 || !ejsVarIsString(argv[0])) {
		ejsError(ejs, EJS_ARG_ERROR, "Bad usage: fetch(url)");
		return -1;
	}

	app = mprGetApp(ejs);
	web = (IWeb*) mprGetKeyValue(ejs, "bpWeb");

	/*
	 *	Web options
 	 *
	 *	WEBOPT_USERAGENT (char*)		sets user agent
	 *	WEBOPT_HANDLERDATA (void*)
	 *	WEBOPT_CONNECTTIMEOUT (uint)	msec
	 *	WEBOPT_CONTENTLENGTH (long)
	 *	WEBOPT_IDLECONNTIMEOUT (int)
	 *	WEBOPT_ACTIVEXACTIONST (uint)	Number of active requests
	 *
	 *	WEBREQUEST_REDIRECT				redirect transparently
	 *
	 */

	hp = mprAllocType(ejs, HTTPControl);
	if (hp == 0) {
		ejsMemoryError(ejs);
		return -1;
	}

	hp->ejs = ejs;
	hp->buf = mprCreateBuf(hp, MPR_BUF_INCR, MPR_MAX_BUF);
	if (hp->buf == 0) {
		mprFree(hp);
		ejsMemoryError(ejs);
		return -1;
	}

	/*
	 *	We copy thisObj because we need to preserve both the var and the object.
	 *	We pass the var to brewCallback and so it must persist. The call to
	 *	ejsMakeObjPermanent will stop the GC from collecting the object.
	 */
	hp->thisObj = ejsDupVar(ejs, thisObj, EJS_SHALLOW_COPY);
	ejsSetVarName(ejs, hp->thisObj, "internalHttp");

	/*
	 *	Must keep a reference to the http object
	 */
	ejsMakeObjPermanent(hp->thisObj, 1);

	/*
	 *	Make a property so we can access the HTTPControl structure from other
	 *	methods.
	 */
	pp = ejsSetPropertyToPtr(ejs, thisObj, EJS_HTTP_PROPERTY, hp, 0);
	ejsMakePropertyEnumerable(pp, 0);
	ejsSetObjDestructor(ejs, hp->thisObj, httpDestructor);

	hp->url = mprStrdup(hp, argv[0]->string);

	hp->timeout = ejsGetPropertyAsInteger(ejs, thisObj, "timeout");
	mprGetTime(hp, &hp->requestStarted);

	hp->callback = mprAllocTypeZeroed(hp, AEECallback);
	CALLBACK_Init(hp->callback, brewCallback, hp);

	hp->webResp = 0;
	IWEB_GetResponse(web, 
		(web, &hp->webResp, hp->callback, hp->url,
		WEBOPT_HANDLERDATA, hp, 
		WEBOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)", 
		WEBOPT_CONNECTTIMEOUT, hp->timeout,
		WEBOPT_COPYOPTS, TRUE,
		WEBOPT_CONTENTLENGTH, 0,
		WEBOPT_END));

	ejsSetPropertyToString(ejs, thisObj, "status", "active");

	return 0;
}

/******************************************************************************/
/*
 *	Called whenver the http object is deleted. 
 */

static int httpDestructor(Ejs *ejs, EjsVar *thisObj)
{
	HTTPControl		*hp;

	/*
	 *	If the httpCallback has run, then this property will not exist
 	 */
	hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY);

	if (hp) {
		cleanup(hp);
	}

	return 0;
}

/******************************************************************************/
/*
 *	Stop the request immediately without calling the callback
 */

static int stopProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
	HTTPControl		*hp;

	hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY);

	if (hp) {
		cleanup(hp);
	}

	return 0;
}

/******************************************************************************/
/*
 *	Brew HTTP callback. Invoked for any return data.
 */

static void brewCallback(HTTPControl *hp)
{
	Ejs				*ejs;
	EjsVar			*thisObj;
	ISource			*source;
	WebRespInfo		*info;
	char 			data[MPR_BUF_INCR];
	int 			bytes;

	mprAssert(hp);
	mprAssert(hp->webResp);

	info = IWEBRESP_GetInfo(hp->webResp);

	if (info == 0) {
		mprAssert(info);
		/* should not happen */
		return;
	}

	ejs = hp->ejs;
	thisObj = hp->thisObj;

	if (! WEB_ERROR_SUCCEEDED(info->nCode)) {
		ejsSetPropertyToString(ejs, thisObj, "status", "error");
		httpCallback(hp, info->nCode);
		return;
	}

	if (hp->timeout) {
		if (mprGetTimeRemaining(hp, hp->requestStarted, hp->timeout) <= 0) {
			ejsSetPropertyToString(ejs, thisObj, "status", "timeout");
			httpCallback(hp, 504);
			return;
		}
	}

	/*
	 *	Normal success
	 */
	source = info->pisMessage;
	mprAssert(source);

	bytes = ISOURCE_Read(source, data, sizeof(data));

	switch (bytes) {
	case ISOURCE_WAIT:								// No data yet
		ISOURCE_Readable(source, hp->callback);
		break;

	case ISOURCE_ERROR:
		ejsSetPropertyToString(ejs, thisObj, "status", "error");
		httpCallback(hp, info->nCode);
		break;

	case ISOURCE_END:
		mprAddNullToBuf(hp->buf);
		ejsSetPropertyToString(ejs, thisObj, "status", "complete");
		httpCallback(hp, info->nCode);
		break;

	default:
		if (bytes > 0) {
			if (mprPutBlockToBuf(hp->buf, data, bytes) != bytes) {
				ejsSetPropertyToString(ejs, thisObj, "status", "partialData");
				httpCallback(hp, 500);
			}
		}
		ISOURCE_Readable(source, hp->callback);
		break;
	}
}

/******************************************************************************/
/*
 *	Invoke the HTTP completion method
 */

static void httpCallback(HTTPControl *hp, int responseCode)
{
	Ejs				*ejs;
	EjsVar			*thisObj, *callbackObj;
	MprArray		*args;
	char			*msg;
	const char		*callbackMethod;

	mprAssert(hp);
	mprAssert(hp->webResp);

	thisObj = hp->thisObj;
	ejs = hp->ejs;

	ejsSetPropertyToInteger(ejs, thisObj, "responseCode", responseCode);
	if (mprGetBufLength(hp->buf) > 0) {
		ejsSetPropertyToBinaryString(ejs, thisObj, "responseData", 
			mprGetBufStart(hp->buf), mprGetBufLength(hp->buf));
	}

	callbackObj = ejsGetPropertyAsVar(ejs, thisObj, "obj");
	callbackMethod = ejsGetPropertyAsString(ejs, thisObj, "method");

	if (callbackObj != 0 && callbackMethod != 0) {

		args = mprCreateItemArray(ejs, EJS_INC_ARGS, EJS_MAX_ARGS);
		mprAddItem(args, ejsDupVar(ejs, hp->thisObj, EJS_SHALLOW_COPY));

		if (ejsRunMethod(ejs, callbackObj, callbackMethod, args) < 0) {
			msg = ejsGetErrorMsg(ejs);
			mprError(ejs, MPR_LOC, "HTTP callback failed. Details: %s", msg);
		}
		ejsFreeMethodArgs(ejs, args);

	} else if (ejsRunMethod(ejs, thisObj, "onComplete", 0) < 0) {
		msg = ejsGetErrorMsg(ejs);
		mprError(ejs, MPR_LOC, "HTTP onComplete failed. Details: %s", msg);
	}

	cleanup(hp);
}

/******************************************************************************/
/*
 *	Cleanup
 */

static void cleanup(HTTPControl *hp)
{
	Ejs			*ejs;
	MprApp		*app;
	int			rc;

	mprAssert(hp);
	mprAssert(hp->webResp);

	ejs = hp->ejs;

	if (hp->webResp) {
		rc = IWEBRESP_Release(hp->webResp);
		// mprAssert(rc == 0);
		hp->webResp = 0;
	}

	if (hp->callback) {
		CALLBACK_Cancel(hp->callback);
		mprFree(hp->callback);
		hp->callback = 0;
	}		

	/*
	 *	Once the property is deleted, then if the destructor runs, it will
	 *	notice that the EJS_HTTP_PROPERTY is undefined.
	 */
	ejsDeleteProperty(ejs, hp->thisObj, EJS_HTTP_PROPERTY);

	/*
 	 *	Allow garbage collection to work on thisObj
	 */
	ejsMakeObjPermanent(hp->thisObj, 0);
	ejsFreeVar(ejs, hp->thisObj);

	mprFree(hp->buf);
	mprFree(hp->url);

	mprFree(hp);

	app = mprGetApp(ejs);


	ISHELL_SendEvent(app->shell, (AEECLSID) app->classId, EVT_USER, 0, 0);
}

/******************************************************************************/
/******************************** Initialization ******************************/
/******************************************************************************/

int ejsDefineHTTPClass(Ejs *ejs)
{
	EjsVar	*httpClass;

	httpClass =  
		ejsDefineClass(ejs, "HTTP", "Object", ejsHTTPConstructor);
	if (httpClass == 0) {
		return MPR_ERR_CANT_INITIALIZE;
	}

	/*
	 *	Define the methods
	 */
	ejsDefineCMethod(ejs, httpClass, "fetch", fetchProc, 0);
	ejsDefineCMethod(ejs, httpClass, "stop", stopProc, 0);
	ejsDefineCMethod(ejs, httpClass, "setCallback", setCallback, 0);

#if FUTURE
	ejsDefineCMethod(ejs, httpClass, "put", put, 0);
	ejsDefineCMethod(ejs, httpClass, "upload", upload, 0);
	ejsDefineCMethod(ejs, httpClass, "addUploadFile", addUploadFile, 0);
	ejsDefineCMethod(ejs, httpClass, "addPostData", addPostData, 0);
	ejsDefineCMethod(ejs, httpClass, "setUserPassword", setUserPassword, 0);
	ejsDefineCMethod(ejs, httpClass, "addCookie", addCookie, 0);
#endif

	/*
	 *	Define properties 
	 */
	ejsSetPropertyToString(ejs, httpClass, "status", "inactive");

	/*	This default should come from player.xml */

	ejsSetPropertyToInteger(ejs, httpClass, "timeout", 30 * 1000);
	ejsSetPropertyToInteger(ejs, httpClass, "responseCode", 0);

	return ejsObjHasErrors(httpClass) ? MPR_ERR_CANT_INITIALIZE: 0;
}

/******************************************************************************/

void ejsTermHTTPClass(Ejs *ejs)
{
	IWeb		*web;
	int			rc;

	web = (IWeb*) mprGetKeyValue(ejs, "bpWeb");
	if (web) {
		rc = IWEB_Release(web);
		mprAssert(rc == 0);
	}
}

#endif
/******************************************************************************/

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim:tw=78
 * vim600: sw=4 ts=4 fdm=marker
 * vim<600: sw=4 ts=4
 */