added qse_ubi_t
separated task functions from httpd.c to httpd_task.c
This commit is contained in:
		| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: Awk.cpp 480 2011-05-25 14:00:19Z hyunghwan.chung $ | ||||
|  * $Id: Awk.cpp 510 2011-07-20 16:17:16Z hyunghwan.chung $ | ||||
|  *  | ||||
|     Copyright 2006-2011 Chung, Hyung-Hwan. | ||||
|     This file is part of QSE. | ||||
| @ -139,7 +139,7 @@ Awk::Console::~Console () | ||||
| { | ||||
| 	if (filename != QSE_NULL) | ||||
| 	{ | ||||
| 		qse_awk_free ((awk_t*)this, filename); | ||||
| 		qse_awk_freemem ((awk_t*)this, filename); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -230,7 +230,7 @@ Awk::Value::IntIndex::IntIndex (long_t x) | ||||
|  | ||||
| void* Awk::Value::operator new (size_t n, Run* run) throw () | ||||
| { | ||||
| 	void* ptr = qse_awk_rtx_alloc (run->rtx, QSE_SIZEOF(run) + n); | ||||
| 	void* ptr = qse_awk_rtx_allocmem (run->rtx, QSE_SIZEOF(run) + n); | ||||
| 	if (ptr == QSE_NULL) return QSE_NULL; | ||||
|  | ||||
| 	*(Run**)ptr = run; | ||||
| @ -239,7 +239,7 @@ void* Awk::Value::operator new (size_t n, Run* run) throw () | ||||
|  | ||||
| void* Awk::Value::operator new[] (size_t n, Run* run) throw ()  | ||||
| { | ||||
| 	void* ptr = qse_awk_rtx_alloc (run->rtx, QSE_SIZEOF(run) + n); | ||||
| 	void* ptr = qse_awk_rtx_allocmem (run->rtx, QSE_SIZEOF(run) + n); | ||||
| 	if (ptr == QSE_NULL) return QSE_NULL; | ||||
|  | ||||
| 	*(Run**)ptr = run; | ||||
| @ -249,25 +249,25 @@ void* Awk::Value::operator new[] (size_t n, Run* run) throw () | ||||
| #if !defined(__BORLANDC__) | ||||
| void Awk::Value::operator delete (void* ptr, Run* run)  | ||||
| { | ||||
| 	qse_awk_rtx_free (run->rtx, (char*)ptr-QSE_SIZEOF(run)); | ||||
| 	qse_awk_rtx_freemem (run->rtx, (char*)ptr-QSE_SIZEOF(run)); | ||||
| } | ||||
|  | ||||
| void Awk::Value::operator delete[] (void* ptr, Run* run)  | ||||
| { | ||||
| 	qse_awk_rtx_free (run->rtx, (char*)ptr-QSE_SIZEOF(run)); | ||||
| 	qse_awk_rtx_freemem (run->rtx, (char*)ptr-QSE_SIZEOF(run)); | ||||
| } | ||||
| #endif | ||||
|  | ||||
| void Awk::Value::operator delete (void* ptr)  | ||||
| { | ||||
| 	void* p = (char*)ptr-QSE_SIZEOF(Run*); | ||||
| 	qse_awk_rtx_free ((*(Run**)p)->rtx, p); | ||||
| 	qse_awk_rtx_freemem ((*(Run**)p)->rtx, p); | ||||
| } | ||||
|  | ||||
| void Awk::Value::operator delete[] (void* ptr)  | ||||
| { | ||||
| 	void* p = (char*)ptr-QSE_SIZEOF(Run*); | ||||
| 	qse_awk_rtx_free ((*(Run**)p)->rtx, p); | ||||
| 	qse_awk_rtx_freemem ((*(Run**)p)->rtx, p); | ||||
| } | ||||
|  | ||||
| Awk::Value::Value (): run (QSE_NULL), val (qse_awk_val_nil)  | ||||
| @ -303,7 +303,7 @@ Awk::Value::~Value () | ||||
| 	{ | ||||
| 		qse_awk_rtx_refdownval (run->rtx, val); | ||||
| 		if (cached.str.ptr != QSE_NULL) | ||||
| 			qse_awk_rtx_free (run->rtx, cached.str.ptr); | ||||
| 			qse_awk_rtx_freemem (run->rtx, cached.str.ptr); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -316,7 +316,7 @@ Awk::Value& Awk::Value::operator= (const Value& v) | ||||
| 		qse_awk_rtx_refdownval (run->rtx, val); | ||||
| 		if (cached.str.ptr != QSE_NULL) | ||||
| 		{ | ||||
| 			qse_awk_rtx_free (run->rtx, cached.str.ptr); | ||||
| 			qse_awk_rtx_freemem (run->rtx, cached.str.ptr); | ||||
| 			cached.str.ptr = QSE_NULL; | ||||
| 			cached.str.len = 0; | ||||
| 		} | ||||
| @ -339,7 +339,7 @@ void Awk::Value::clear () | ||||
|  | ||||
| 		if (cached.str.ptr != QSE_NULL) | ||||
| 		{ | ||||
| 			qse_awk_rtx_free (run->rtx, cached.str.ptr); | ||||
| 			qse_awk_rtx_freemem (run->rtx, cached.str.ptr); | ||||
| 			cached.str.ptr = QSE_NULL; | ||||
| 			cached.str.len = 0; | ||||
| 		} | ||||
| @ -485,7 +485,7 @@ int Awk::Value::setVal (Run* r, val_t* v) | ||||
| 		qse_awk_rtx_refdownval (this->run->rtx, val); | ||||
| 		if (cached.str.ptr != QSE_NULL) | ||||
| 		{ | ||||
| 			qse_awk_rtx_free (this->run->rtx, cached.str.ptr); | ||||
| 			qse_awk_rtx_freemem (this->run->rtx, cached.str.ptr); | ||||
| 			cached.str.ptr = QSE_NULL; | ||||
| 			cached.str.len = 0; | ||||
| 		} | ||||
| @ -1062,7 +1062,7 @@ static void free_function_map_value ( | ||||
| 	Awk::htb_t* map, void* dptr, Awk::size_t dlen) | ||||
| { | ||||
| 	Awk* awk = *(Awk**) QSE_XTN (map); | ||||
| 	qse_awk_free ((Awk::awk_t*)*awk, dptr); | ||||
| 	qse_awk_freemem ((Awk::awk_t*)*awk, dptr); | ||||
| } | ||||
|  | ||||
| int Awk::open ()  | ||||
| @ -1214,7 +1214,7 @@ int Awk::call ( | ||||
| 		if (nargs <= QSE_COUNTOF(buf)) ptr = buf; | ||||
| 		else | ||||
| 		{ | ||||
| 			ptr = (val_t**) qse_awk_alloc ( | ||||
| 			ptr = (val_t**) qse_awk_allocmem ( | ||||
| 				awk, QSE_SIZEOF(val_t*) * nargs); | ||||
| 			if (ptr == QSE_NULL) | ||||
| 			{ | ||||
| @ -1229,7 +1229,7 @@ int Awk::call ( | ||||
|  | ||||
| 	val_t* rv = qse_awk_rtx_call (runctx.rtx, name, ptr, nargs); | ||||
|  | ||||
| 	if (ptr != QSE_NULL && ptr != buf) qse_awk_free (awk, ptr); | ||||
| 	if (ptr != QSE_NULL && ptr != buf) qse_awk_freemem (awk, ptr); | ||||
|  | ||||
| 	if (rv == QSE_NULL)  | ||||
| 	{ | ||||
| @ -1409,7 +1409,7 @@ int Awk::xstrs_t::add (awk_t* awk, const char_t* arg, size_t len) | ||||
| 		size_t capa = this->capa; | ||||
|  | ||||
| 		capa += 64; | ||||
| 		ptr = (qse_xstr_t*) qse_awk_realloc ( | ||||
| 		ptr = (qse_xstr_t*) qse_awk_reallocmem ( | ||||
| 			awk, this->ptr, QSE_SIZEOF(qse_xstr_t)*(capa+1)); | ||||
| 		if (ptr == QSE_NULL) return -1; | ||||
|  | ||||
| @ -1433,9 +1433,9 @@ void Awk::xstrs_t::clear (awk_t* awk) | ||||
| 	if (this->ptr != QSE_NULL) | ||||
| 	{ | ||||
| 		while (this->len > 0) | ||||
| 			qse_awk_free (awk, this->ptr[--this->len].ptr); | ||||
| 			qse_awk_freemem (awk, this->ptr[--this->len].ptr); | ||||
|  | ||||
| 		qse_awk_free (awk, this->ptr); | ||||
| 		qse_awk_freemem (awk, this->ptr); | ||||
| 		this->ptr = QSE_NULL; | ||||
| 		this->capa = 0; | ||||
| 	} | ||||
| @ -1517,7 +1517,7 @@ int Awk::addFunction ( | ||||
| 	QSE_ASSERT (awk != QSE_NULL); | ||||
|  | ||||
| 	FunctionHandler* tmp = (FunctionHandler*)  | ||||
| 		qse_awk_alloc (awk, QSE_SIZEOF(handler)); | ||||
| 		qse_awk_allocmem (awk, QSE_SIZEOF(handler)); | ||||
| 	if (tmp == QSE_NULL) | ||||
| 	{ | ||||
| 		setError (QSE_AWK_ENOMEM); | ||||
| @ -1540,7 +1540,7 @@ int Awk::addFunction ( | ||||
| 		functionHandler); | ||||
| 	if (p == QSE_NULL)  | ||||
| 	{ | ||||
| 		qse_awk_free (awk, tmp); | ||||
| 		qse_awk_freemem (awk, tmp); | ||||
| 		retrieveError (); | ||||
| 		return -1; | ||||
| 	} | ||||
| @ -1550,7 +1550,7 @@ int Awk::addFunction ( | ||||
| 	if (pair == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_awk_delfnc (awk, name, nameLen); | ||||
| 		qse_awk_free (awk, tmp); | ||||
| 		qse_awk_freemem (awk, tmp); | ||||
|  | ||||
| 		setError (QSE_AWK_ENOMEM); | ||||
| 		return -1; | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: StdAwk.cpp 480 2011-05-25 14:00:19Z hyunghwan.chung $ | ||||
|  * $Id: StdAwk.cpp 510 2011-07-20 16:17:16Z hyunghwan.chung $ | ||||
|  * | ||||
|     Copyright 2006-2011 Chung, Hyung-Hwan. | ||||
|     This file is part of QSE. | ||||
| @ -114,7 +114,7 @@ int StdAwk::system (Run& run, Value& ret, const Value* args, size_t nargs, | ||||
| #elif defined(QSE_CHAR_IS_MCHAR) | ||||
| 	return ret.setInt ((long_t)::system(ptr)); | ||||
| #else | ||||
| 	char* mbs = (char*) qse_awk_alloc ((awk_t*)(Awk*)run, l*5+1); | ||||
| 	char* mbs = (char*) qse_awk_allocmem ((awk_t*)(Awk*)run, l*5+1); | ||||
| 	if (mbs == QSE_NULL) return -1; | ||||
|  | ||||
| 	/* at this point, the string is guaranteed to be  | ||||
| @ -126,14 +126,14 @@ int StdAwk::system (Run& run, Value& ret, const Value* args, size_t nargs, | ||||
| 	{ | ||||
| 		/* not the entire string is converted. | ||||
| 		 * mbs is not null-terminated properly. */ | ||||
| 		qse_awk_free ((awk_t*)(Awk*)run, mbs); | ||||
| 		qse_awk_freemem ((awk_t*)(Awk*)run, mbs); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	mbs[mbl] = '\0'; | ||||
| 	int n = ret.setInt ((long_t)::system(mbs)); | ||||
|  | ||||
| 	qse_awk_free ((awk_t*)(Awk*)run, mbs); | ||||
| 	qse_awk_freemem ((awk_t*)(Awk*)run, mbs); | ||||
| 	return n; | ||||
| #endif | ||||
| } | ||||
| @ -381,7 +381,7 @@ int StdAwk::open_console_in (Console& io) | ||||
| 		if (out.u.cpldup.len == 0) | ||||
| 		{ | ||||
| 			/* the name is empty */ | ||||
| 			qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 			qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 			runarg_index++; | ||||
| 			goto nextfile; | ||||
| 		} | ||||
| @ -393,7 +393,7 @@ int StdAwk::open_console_in (Console& io) | ||||
| 			arg.ptr = out.u.cpldup.ptr; | ||||
| 			arg.len = qse_strlen (arg.ptr); | ||||
| 			((Run*)io)->setError (QSE_AWK_EIONMNL, &arg); | ||||
| 			qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 			qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 			return -1; | ||||
| 		} | ||||
|  | ||||
| @ -414,7 +414,7 @@ int StdAwk::open_console_in (Console& io) | ||||
| 				arg.ptr = file; | ||||
| 				arg.len = qse_strlen (arg.ptr); | ||||
| 				((Run*)io)->setError (QSE_AWK_EOPEN, &arg); | ||||
| 				qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 				qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 				return -1; | ||||
| 			} | ||||
| 		} | ||||
| @ -423,11 +423,11 @@ int StdAwk::open_console_in (Console& io) | ||||
| 			rtx, file, qse_strlen(file)) == -1) | ||||
| 		{ | ||||
| 			if (sio != qse_sio_in) qse_sio_close (sio); | ||||
| 			qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 			qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 			return -1; | ||||
| 		} | ||||
|  | ||||
| 		qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 		qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 		io.setHandle (sio); | ||||
|  | ||||
| 		/* increment the counter of files successfully opened */ | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: misc.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ | ||||
|  * $Id: misc.c 510 2011-07-20 16:17:16Z hyunghwan.chung $ | ||||
|  * | ||||
|     Copyright 2006-2011 Chung, Hyung-Hwan. | ||||
|     This file is part of QSE. | ||||
| @ -20,29 +20,37 @@ | ||||
|  | ||||
| #include "awk.h" | ||||
|  | ||||
| void* qse_awk_alloc (qse_awk_t* awk, qse_size_t size) | ||||
| void* qse_awk_allocmem (qse_awk_t* awk, qse_size_t size) | ||||
| { | ||||
| 	return QSE_AWK_ALLOC (awk, size); | ||||
| 	void* ptr = QSE_AWK_ALLOC (awk, size); | ||||
| 	if (ptr == QSE_NULL)  qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL); | ||||
| 	return ptr; | ||||
| } | ||||
|  | ||||
| void* qse_awk_realloc (qse_awk_t* awk, void* ptr, qse_size_t size) | ||||
| void* qse_awk_reallocmem (qse_awk_t* awk, void* ptr, qse_size_t size) | ||||
| { | ||||
| 	return QSE_AWK_REALLOC (awk, ptr, size); | ||||
| 	void* nptr = QSE_AWK_REALLOC (awk, ptr, size); | ||||
| 	if (nptr == QSE_NULL) qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL); | ||||
| 	return nptr; | ||||
| } | ||||
|  | ||||
| void qse_awk_free (qse_awk_t* awk, void* ptr) | ||||
| void qse_awk_freemem (qse_awk_t* awk, void* ptr) | ||||
| { | ||||
| 	QSE_AWK_FREE (awk, ptr); | ||||
| } | ||||
|  | ||||
| qse_char_t* qse_awk_strdup (qse_awk_t* awk, const qse_char_t* s) | ||||
| { | ||||
| 	return QSE_AWK_STRDUP (awk, s); | ||||
| 	qse_char_t* ptr = QSE_AWK_STRDUP (awk, s); | ||||
| 	if (ptr == QSE_NULL) qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL); | ||||
| 	return ptr; | ||||
| } | ||||
|  | ||||
| qse_char_t* qse_awk_strxdup (qse_awk_t* awk, const qse_char_t* s, qse_size_t l) | ||||
| { | ||||
| 	return QSE_AWK_STRXDUP (awk, s, l); | ||||
| 	qse_char_t* ptr = QSE_AWK_STRXDUP (awk, s, l); | ||||
| 	if (ptr == QSE_NULL) qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL); | ||||
| 	return ptr; | ||||
| } | ||||
|  | ||||
| qse_long_t qse_awk_strxtolong ( | ||||
| @ -1085,18 +1093,18 @@ int qse_awk_matchrex ( | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
| void* qse_awk_rtx_alloc (qse_awk_rtx_t* rtx, qse_size_t size) | ||||
| void* qse_awk_rtx_allocmem (qse_awk_rtx_t* rtx, qse_size_t size) | ||||
| { | ||||
| 	return qse_awk_alloc (rtx->awk, size); | ||||
| 	return qse_awk_allocmem (rtx->awk, size); | ||||
| } | ||||
|  | ||||
| void* qse_awk_rtx_realloc (qse_awk_rtx_t* rtx, void* ptr, qse_size_t size) | ||||
| void* qse_awk_rtx_reallocmem (qse_awk_rtx_t* rtx, void* ptr, qse_size_t size) | ||||
| { | ||||
| 	return qse_awk_realloc (rtx->awk, ptr, size); | ||||
| 	return qse_awk_reallocmem (rtx->awk, ptr, size); | ||||
| } | ||||
|  | ||||
| void qse_awk_rtx_free (qse_awk_rtx_t* rtx, void* ptr) | ||||
| void qse_awk_rtx_freemem (qse_awk_rtx_t* rtx, void* ptr) | ||||
| { | ||||
| 	qse_awk_free (rtx->awk, ptr); | ||||
| 	qse_awk_freemem (rtx->awk, ptr); | ||||
| } | ||||
|  | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: std.c 481 2011-05-25 14:42:26Z hyunghwan.chung $ | ||||
|  * $Id: std.c 510 2011-07-20 16:17:16Z hyunghwan.chung $ | ||||
|  * | ||||
|     Copyright 2006-2011 Chung, Hyung-Hwan. | ||||
|     This file is part of QSE. | ||||
| @ -1013,7 +1013,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) | ||||
| 			if (out.u.cpldup.len == 0) | ||||
| 			{ | ||||
| 				/* the name is empty */ | ||||
| 				qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 				qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 				rxtn->c.in.index++; | ||||
| 				goto nextfile; | ||||
| 			} | ||||
| @ -1031,7 +1031,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) | ||||
| 				qse_awk_rtx_seterrnum ( | ||||
| 					rtx, QSE_AWK_EIONMNL, &errarg); | ||||
|  | ||||
| 				qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 				qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 				return -1; | ||||
| 			} | ||||
|  | ||||
| @ -1056,7 +1056,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) | ||||
| 					qse_awk_rtx_seterrnum ( | ||||
| 						rtx, QSE_AWK_EOPEN, &errarg); | ||||
|  | ||||
| 					qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 					qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 					return -1; | ||||
| 				} | ||||
| 			} | ||||
| @ -1065,11 +1065,11 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) | ||||
| 				rtx, file, qse_strlen(file)) <= -1) | ||||
| 			{ | ||||
| 				if (sio != qse_sio_in) qse_sio_close (sio); | ||||
| 				qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 				qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 				return -1; | ||||
| 			} | ||||
|  | ||||
| 			qse_awk_rtx_free (rtx, out.u.cpldup.ptr); | ||||
| 			qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); | ||||
| 			riod->handle = sio; | ||||
|  | ||||
| 			/* increment the counter of files successfully opened */ | ||||
| @ -1465,7 +1465,7 @@ static int fnc_system (qse_awk_rtx_t* run, const qse_cstr_t* fnm) | ||||
| 		char* mbs; | ||||
| 		qse_size_t mbl; | ||||
|  | ||||
| 		mbs = (char*) qse_awk_alloc (run->awk, len*5+1); | ||||
| 		mbs = (char*) qse_awk_allocmem (run->awk, len*5+1); | ||||
| 		if (mbs == QSE_NULL)  | ||||
| 		{ | ||||
| 			n = -1; | ||||
| @ -1489,7 +1489,7 @@ static int fnc_system (qse_awk_rtx_t* run, const qse_cstr_t* fnm) | ||||
| 		n = system (mbs); | ||||
|  | ||||
| 	skip_system_mbs: | ||||
| 		qse_awk_free (run->awk, mbs); | ||||
| 		qse_awk_freemem (run->awk, mbs); | ||||
| 	} | ||||
| #endif | ||||
|  | ||||
|  | ||||
| @ -704,7 +704,7 @@ void qse_xma_free (qse_xma_t* xma, void* b) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void qse_xma_dump (qse_xma_t* xma,  qse_xma_dumper_t dumper, void* target) | ||||
| void qse_xma_dump (qse_xma_t* xma, qse_xma_dumper_t dumper, void* ctx) | ||||
| { | ||||
| 	qse_xma_blk_t* tmp; | ||||
| 	qse_ulong_t fsum, asum;  | ||||
| @ -712,31 +712,31 @@ void qse_xma_dump (qse_xma_t* xma,  qse_xma_dumper_t dumper, void* target) | ||||
| 	qse_ulong_t isum; | ||||
| #endif | ||||
|  | ||||
| 	dumper (target, QSE_T("<XMA DUMP>\n")); | ||||
| 	dumper (ctx, QSE_T("<XMA DUMP>\n")); | ||||
|  | ||||
| #ifdef QSE_XMA_ENABLE_STAT | ||||
| 	dumper (target, QSE_T("== statistics ==\n")); | ||||
| 	dumper (ctx, QSE_T("== statistics ==\n")); | ||||
| #if QSE_SIZEOF_LONG >= QSE_SIZEOF_LONG_LONG | ||||
| 	dumper (target, QSE_T("total = %lu\n"), (unsigned long)xma->stat.total); | ||||
| 	dumper (target, QSE_T("alloc = %lu\n"), (unsigned long)xma->stat.alloc); | ||||
| 	dumper (target, QSE_T("avail = %lu\n"), (unsigned long)xma->stat.avail); | ||||
| 	dumper (ctx, QSE_T("total = %lu\n"), (unsigned long)xma->stat.total); | ||||
| 	dumper (ctx, QSE_T("alloc = %lu\n"), (unsigned long)xma->stat.alloc); | ||||
| 	dumper (ctx, QSE_T("avail = %lu\n"), (unsigned long)xma->stat.avail); | ||||
| #else | ||||
| 	dumper (target, QSE_T("total = %llu\n"), (unsigned long long)xma->stat.total); | ||||
| 	dumper (target, QSE_T("alloc = %llu\n"), (unsigned long long)xma->stat.alloc); | ||||
| 	dumper (target, QSE_T("avail = %llu\n"), (unsigned long long)xma->stat.avail); | ||||
| 	dumper (ctx, QSE_T("total = %llu\n"), (unsigned long long)xma->stat.total); | ||||
| 	dumper (ctx, QSE_T("alloc = %llu\n"), (unsigned long long)xma->stat.alloc); | ||||
| 	dumper (ctx, QSE_T("avail = %llu\n"), (unsigned long long)xma->stat.avail); | ||||
| #endif | ||||
| #endif | ||||
|  | ||||
| 	dumper (target, QSE_T("== blocks ==\n")); | ||||
| 	dumper (target, QSE_T(" size               avail address\n")); | ||||
| 	dumper (ctx, QSE_T("== blocks ==\n")); | ||||
| 	dumper (ctx, QSE_T(" size               avail address\n")); | ||||
| 	for (tmp = xma->head, fsum = 0, asum = 0; tmp; tmp = tmp->b.next) | ||||
| 	{ | ||||
| #if QSE_SIZEOF_LONG >= QSE_SIZEOF_LONG_LONG | ||||
| 		dumper (target, QSE_T(" %-18lu %-5d %p\n"),  | ||||
| 		dumper (ctx, QSE_T(" %-18lu %-5d %p\n"),  | ||||
| 			(unsigned long)tmp->size, tmp->avail, tmp | ||||
| 		); | ||||
| #else | ||||
| 		dumper (target, QSE_T(" %-18llu %-5d %p\n"),  | ||||
| 		dumper (ctx, QSE_T(" %-18llu %-5d %p\n"),  | ||||
| 			(unsigned long long)tmp->size, tmp->avail, tmp | ||||
| 		); | ||||
| #endif | ||||
| @ -748,22 +748,22 @@ void qse_xma_dump (qse_xma_t* xma,  qse_xma_dumper_t dumper, void* target) | ||||
| 	isum = (xma->stat.nfree + xma->stat.nused) * HDRSIZE; | ||||
| #endif | ||||
|  | ||||
| 	dumper (target, QSE_T("---------------------------------------\n")); | ||||
| 	dumper (ctx, QSE_T("---------------------------------------\n")); | ||||
| #if QSE_SIZEOF_LONG >= QSE_SIZEOF_LONG_LONG | ||||
| 	dumper (target, QSE_T("Allocated blocks: %18lu bytes\n"), (unsigned long)asum); | ||||
| 	dumper (target, QSE_T("Available blocks: %18lu bytes\n"), (unsigned long)fsum); | ||||
| 	dumper (ctx, QSE_T("Allocated blocks: %18lu bytes\n"), (unsigned long)asum); | ||||
| 	dumper (ctx, QSE_T("Available blocks: %18lu bytes\n"), (unsigned long)fsum); | ||||
| #else | ||||
| 	dumper (target, QSE_T("Allocated blocks: %18llu bytes\n"), (unsigned long long)asum); | ||||
| 	dumper (target, QSE_T("Available blocks: %18llu bytes\n"), (unsigned long long)fsum); | ||||
| 	dumper (ctx, QSE_T("Allocated blocks: %18llu bytes\n"), (unsigned long long)asum); | ||||
| 	dumper (ctx, QSE_T("Available blocks: %18llu bytes\n"), (unsigned long long)fsum); | ||||
| #endif | ||||
|  | ||||
| #ifdef QSE_XMA_ENABLE_STAT | ||||
| #if QSE_SIZEOF_LONG >= QSE_SIZEOF_LONG_LONG | ||||
| 	dumper (target, QSE_T("Internal use    : %18lu bytes\n"), (unsigned long)isum); | ||||
| 	dumper (target, QSE_T("Total           : %18lu bytes\n"), (unsigned long)(asum + fsum + isum)); | ||||
| 	dumper (ctx, QSE_T("Internal use    : %18lu bytes\n"), (unsigned long)isum); | ||||
| 	dumper (ctx, QSE_T("Total           : %18lu bytes\n"), (unsigned long)(asum + fsum + isum)); | ||||
| #else | ||||
| 	dumper (target, QSE_T("Internal use    : %18llu bytes\n"), (unsigned long long)isum); | ||||
| 	dumper (target, QSE_T("Total           : %18llu bytes\n"), (unsigned long long)(asum + fsum + isum)); | ||||
| 	dumper (ctx, QSE_T("Internal use    : %18llu bytes\n"), (unsigned long long)isum); | ||||
| 	dumper (ctx, QSE_T("Total           : %18llu bytes\n"), (unsigned long long)(asum + fsum + isum)); | ||||
| #endif | ||||
| #endif | ||||
|  | ||||
|  | ||||
| @ -10,7 +10,8 @@ libqsenet_la_SOURCES = \ | ||||
| 	http.c \ | ||||
| 	htre.c \ | ||||
| 	htrd.c \ | ||||
| 	httpd.c  | ||||
| 	httpd.c \ | ||||
| 	httpd_task.c | ||||
|  | ||||
| libqsenet_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn -L$(libdir) | ||||
| libqsenet_la_LIBADD = -lqsecmn | ||||
|  | ||||
| @ -71,7 +71,8 @@ am__base_list = \ | ||||
| am__installdirs = "$(DESTDIR)$(libdir)" | ||||
| LTLIBRARIES = $(lib_LTLIBRARIES) | ||||
| libqsenet_la_DEPENDENCIES = | ||||
| am_libqsenet_la_OBJECTS = http.lo htre.lo htrd.lo httpd.lo | ||||
| am_libqsenet_la_OBJECTS = http.lo htre.lo htrd.lo httpd.lo \ | ||||
| 	httpd_task.lo | ||||
| libqsenet_la_OBJECTS = $(am_libqsenet_la_OBJECTS) | ||||
| libqsenet_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \ | ||||
| 	$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ | ||||
| @ -242,7 +243,8 @@ libqsenet_la_SOURCES = \ | ||||
| 	http.c \ | ||||
| 	htre.c \ | ||||
| 	htrd.c \ | ||||
| 	httpd.c  | ||||
| 	httpd.c \ | ||||
| 	httpd_task.c | ||||
|  | ||||
| libqsenet_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn -L$(libdir) | ||||
| libqsenet_la_LIBADD = -lqsecmn | ||||
| @ -324,6 +326,7 @@ distclean-compile: | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htre.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/http.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/httpd.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/httpd_task.Plo@am__quote@ | ||||
|  | ||||
| .c.o: | ||||
| @am__fastdepCC_TRUE@	$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< | ||||
|  | ||||
| @ -21,11 +21,28 @@ | ||||
| #include "httpd.h" | ||||
| #include "../cmn/mem.h" | ||||
| #include <qse/cmn/chr.h> | ||||
| #include <qse/cmn/str.h> | ||||
|  | ||||
| #include <fcntl.h> | ||||
| #include <unistd.h> | ||||
| #include <errno.h> | ||||
| #include <pthread.h> | ||||
| #include <sys/time.h> | ||||
|  | ||||
| #if 0 | ||||
| #include <openssl.h> | ||||
| #endif | ||||
|  | ||||
| #include <qse/cmn/stdio.h> | ||||
|  | ||||
| typedef struct htrd_xtn_t htrd_xtn_t; | ||||
|  | ||||
| struct htrd_xtn_t | ||||
| { | ||||
| 	qse_size_t      client_index;  | ||||
| 	qse_httpd_t*    httpd; | ||||
| }; | ||||
|  | ||||
| QSE_IMPLEMENT_COMMON_FUNCTIONS (httpd) | ||||
|  | ||||
| #define DEFAULT_PORT        80 | ||||
| @ -114,46 +131,25 @@ void qse_httpd_setcbs (qse_httpd_t* httpd, qse_httpd_cbs_t* cbs) | ||||
| 	httpd->cbs = cbs; | ||||
| } | ||||
|  | ||||
| static QSE_INLINE void* httpd_alloc (qse_httpd_t* httpd, qse_size_t n) | ||||
| QSE_INLINE void* qse_httpd_allocmem (qse_httpd_t* httpd, qse_size_t size) | ||||
| { | ||||
| 	void* ptr = QSE_MMGR_ALLOC (httpd->mmgr, n); | ||||
| 	if (ptr == QSE_NULL) httpd->errnum = QSE_HTTPD_ENOMEM; | ||||
| 	void* ptr = QSE_MMGR_ALLOC (httpd->mmgr, size); | ||||
| 	if (ptr == QSE_NULL)  httpd->errnum = QSE_HTTPD_ENOMEM; | ||||
| 	return ptr; | ||||
| } | ||||
|  | ||||
| static void httpd_free (qse_httpd_t* httpd, void* ptr) | ||||
| QSE_INLINE void* qse_httpd_reallocmem (qse_httpd_t* httpd, void* ptr, qse_size_t size) | ||||
| { | ||||
| 	void* nptr = QSE_MMGR_REALLOC (httpd->mmgr, ptr, size); | ||||
| 	if (nptr == QSE_NULL)  httpd->errnum = QSE_HTTPD_ENOMEM; | ||||
| 	return nptr; | ||||
| } | ||||
|  | ||||
| QSE_INLINE void qse_httpd_freemem (qse_httpd_t* httpd, void* ptr) | ||||
| { | ||||
| 	QSE_MMGR_FREE (httpd->mmgr, ptr); | ||||
| } | ||||
|  | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
|  | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <fcntl.h> | ||||
| #include <unistd.h> | ||||
| #include <errno.h> | ||||
| #include <pthread.h> | ||||
| #include <sys/time.h> | ||||
|  | ||||
| #include <sys/stat.h> | ||||
| #include <stdarg.h> | ||||
| #include <stdio.h> | ||||
|  | ||||
| #include <qse/cmn/stdio.h> | ||||
|  | ||||
| #define MAX_SENDFILE_SIZE 4096 | ||||
| //#define MAX_SENDFILE_SIZE 64 | ||||
|  | ||||
| typedef struct htrd_xtn_t htrd_xtn_t; | ||||
|  | ||||
| struct htrd_xtn_t | ||||
| { | ||||
| 	qse_size_t      client_index;  | ||||
| 	qse_httpd_t*    httpd; | ||||
| }; | ||||
|  | ||||
| static int enqueue_task_unlocked ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client,  | ||||
| 	const qse_httpd_task_t* task, qse_size_t xtnsize) | ||||
| @ -167,12 +163,8 @@ static int enqueue_task_unlocked ( | ||||
| 		return -1; | ||||
| 	} | ||||
| */ | ||||
| 	node = QSE_MMGR_ALLOC (httpd->mmgr, QSE_SIZEOF(*node) + xtnsize); | ||||
| 	if (node == QSE_NULL) | ||||
| 	{ | ||||
| 		httpd->errnum = QSE_HTTPD_ENOMEM; | ||||
| 		return -1; | ||||
| 	} | ||||
| 	node = qse_httpd_allocmem (httpd, QSE_SIZEOF(*node) + xtnsize); | ||||
| 	if (node == QSE_NULL) return -1; | ||||
|  | ||||
| 	node->task = *task; | ||||
|  | ||||
| @ -183,7 +175,7 @@ static int enqueue_task_unlocked ( | ||||
| 		{ | ||||
| 			if (httpd->errnum == QSE_HTTPD_ENOERR)  | ||||
| 				httpd->errnum = QSE_HTTPD_ETASK; | ||||
| 			QSE_MMGR_FREE (httpd->mmgr, node); | ||||
| 			qse_httpd_freemem (httpd, node); | ||||
| 			return -1;	 | ||||
| 		} | ||||
| 	} | ||||
| @ -237,7 +229,7 @@ static int dequeue_task_unlocked ( | ||||
| 	client->task.queue.count--; | ||||
|  | ||||
| 	if (node->task.fini) node->task.fini (httpd, client, &node->task); | ||||
| 	QSE_MMGR_FREE (httpd->mmgr, node); | ||||
| 	qse_httpd_freemem (httpd, node); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
| @ -633,7 +625,7 @@ static void fini_client_array (qse_httpd_t* httpd) | ||||
| 		for (fd = 0; fd < array->capa; fd++) | ||||
| 			delete_from_client_array (httpd, fd); | ||||
|  | ||||
| 		free (array->data); | ||||
| 		qse_httpd_freemem (httpd, array->data); | ||||
| 		array->capa = 0; | ||||
| 		array->size = 0; | ||||
| 		array->data = QSE_NULL; | ||||
| @ -651,7 +643,7 @@ static qse_httpd_client_t* insert_into_client_array (qse_httpd_t* httpd, int fd, | ||||
| 		qse_httpd_client_t* tmp; | ||||
| 		qse_size_t capa = ((fd + ALIGN) / ALIGN) * ALIGN; | ||||
|  | ||||
| 		tmp = realloc (array->data, capa * QSE_SIZEOF(*tmp)); | ||||
| 		tmp = qse_httpd_reallocmem (httpd, array->data, capa * QSE_SIZEOF(*tmp)); | ||||
| 		if (tmp == QSE_NULL) return QSE_NULL; | ||||
|  | ||||
| 		QSE_MEMSET (&tmp[array->capa], 0, | ||||
| @ -1012,9 +1004,9 @@ oops: | ||||
|  | ||||
| static void free_listener (qse_httpd_t* httpd, listener_t* l) | ||||
| { | ||||
| 	if (l->host) QSE_MMGR_FREE (httpd->mmgr, l->host); | ||||
| 	if (l->host) qse_httpd_freemem (httpd, l->host); | ||||
| 	if (l->handle >= 0) close (l->handle); | ||||
| 	QSE_MMGR_FREE (httpd->mmgr, l); | ||||
| 	qse_httpd_freemem (httpd, l); | ||||
| } | ||||
|  | ||||
| static void free_listener_list (qse_httpd_t* httpd, listener_t* l) | ||||
| @ -1042,7 +1034,7 @@ static listener_t* parse_listener_string ( | ||||
| 		/* skip spaces */ | ||||
| 		while (QSE_ISSPACE(*p)) p++; | ||||
|  | ||||
| 		ltmp = httpd_alloc (httpd, QSE_SIZEOF(*ltmp)); | ||||
| 		ltmp = qse_httpd_allocmem (httpd, QSE_SIZEOF(*ltmp)); | ||||
| 		if (ltmp == QSE_NULL) goto oops; /* alloc set error number. so goto oops */ | ||||
|  | ||||
| 		QSE_MEMSET (ltmp, 0, QSE_SIZEOF(*ltmp)); | ||||
| @ -1112,7 +1104,7 @@ static listener_t* parse_listener_string ( | ||||
|  | ||||
| 		x = inet_pton (ltmp->family, host, <mp->addr); | ||||
| #ifdef QSE_CHAR_IS_WCHAR | ||||
| 		httpd_free (httpd, host); | ||||
| 		qse_httpd_freemem (httpd, host); | ||||
| #endif | ||||
| 		if (x != 1) | ||||
| 		{ | ||||
| @ -1249,246 +1241,3 @@ int qse_httpd_entask ( | ||||
| 	return ret; | ||||
| } | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| typedef struct task_sendtext_t task_sendtext_t; | ||||
| struct task_sendtext_t | ||||
| { | ||||
| 	const qse_mchar_t* ptr; | ||||
| 	qse_size_t         left; | ||||
| }; | ||||
|  | ||||
| static int httpd_init_sendtext ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	task_sendtext_t* xtn = qse_httpd_gettaskxtn (httpd, task); | ||||
|  | ||||
| 	QSE_MEMCPY (xtn, task->ctx, QSE_SIZEOF(*xtn)); | ||||
| 	QSE_MEMCPY (xtn + 1, xtn->ptr, xtn->left); | ||||
| 	xtn->ptr = (qse_mchar_t*)(xtn + 1); | ||||
|  | ||||
| 	task->ctx = xtn; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int httpd_main_sendtext ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	ssize_t n; | ||||
| 	size_t count; | ||||
| 	task_sendtext_t* ctx = (task_sendtext_t*)task->ctx; | ||||
|  | ||||
| 	count = MAX_SENDFILE_SIZE; | ||||
| 	if (count >= ctx->left) count = ctx->left; | ||||
|  | ||||
| 	n = send ( | ||||
| 		client->fd, | ||||
| 		ctx->ptr, | ||||
| 		count, | ||||
| 		0 | ||||
| 	); | ||||
|  | ||||
| 	if (n <= -1) return -1; | ||||
|  | ||||
| 	ctx->left -= n; | ||||
| 	if (ctx->left <= 0) return 0; | ||||
|  | ||||
| 	ctx->ptr += n; | ||||
| 	return 1; /* more work to do */ | ||||
| } | ||||
|  | ||||
| int qse_httpd_entasksendtext ( | ||||
|      qse_httpd_t* httpd, qse_httpd_client_t* client, const qse_mchar_t* text) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	task_sendtext_t data; | ||||
|  | ||||
| 	QSE_MEMSET (&data, 0, QSE_SIZEOF(data)); | ||||
| 	data.ptr = text; | ||||
| 	data.left = qse_mbslen(text); | ||||
|  | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.init = httpd_init_sendtext; | ||||
| 	task.main = httpd_main_sendtext; | ||||
| 	task.ctx = &data; | ||||
|  | ||||
| 	return qse_httpd_entask ( | ||||
| 		httpd, client, &task, QSE_SIZEOF(data) + data.left); | ||||
| } | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| /* TODO: send wide character string when QSE_CHAR_IS_WCHAR */ | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| typedef struct task_sendfmt_t task_sendfmt_t; | ||||
| struct task_sendfmt_t | ||||
| { | ||||
| 	qse_mchar_t*       org; | ||||
| 	const qse_mchar_t* ptr; | ||||
| 	qse_size_t         left; | ||||
| }; | ||||
|  | ||||
| static int httpd_init_sendfmt ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	task_sendfmt_t* xtn = qse_httpd_gettaskxtn (httpd, task); | ||||
|  | ||||
| 	QSE_MEMCPY (xtn, task->ctx, QSE_SIZEOF(*xtn)); | ||||
| 	task->ctx = xtn; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static void httpd_fini_sendfmt ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	task_sendfmt_t* ctx = (task_sendfmt_t*)task->ctx; | ||||
| 	httpd_free (httpd, ctx->org); | ||||
| } | ||||
|  | ||||
| static int httpd_main_sendfmt ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	ssize_t n; | ||||
| 	size_t count; | ||||
| 	task_sendfmt_t* ctx = (task_sendfmt_t*)task->ctx; | ||||
|  | ||||
| 	count = MAX_SENDFILE_SIZE; | ||||
| 	if (count >= ctx->left) count = ctx->left; | ||||
|  | ||||
| 	n = send ( | ||||
| 		client->fd, | ||||
| 		ctx->ptr, | ||||
| 		count, | ||||
| 		0 | ||||
| 	); | ||||
|  | ||||
| 	if (n <= -1) return -1; | ||||
|  | ||||
| 	ctx->left -= n; | ||||
| 	if (ctx->left <= 0) return 0; | ||||
|  | ||||
| 	ctx->ptr += n; | ||||
| 	return 1; /* more work to do */ | ||||
| } | ||||
|  | ||||
| int qse_httpd_entasksendfmt ( | ||||
|      qse_httpd_t* httpd, qse_httpd_client_t* client, const qse_mchar_t* fmt, ...) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	task_sendfmt_t data; | ||||
|  | ||||
| 	va_list ap; | ||||
| 	qse_mchar_t n[2]; | ||||
| 	qse_mchar_t* buf; | ||||
| 	int bytes_req, l; | ||||
|  | ||||
| 	va_start (ap, fmt); | ||||
| #if defined(_WIN32) && defined(_MSC_VER) | ||||
| 	bytes_req = _vsnprintf (n, 1, fmt, ap); | ||||
| #else | ||||
| 	bytes_req = vsnprintf (n, 1, fmt, ap); | ||||
| #endif | ||||
| 	va_end (ap); | ||||
|  | ||||
| 	if (bytes_req == -1)  | ||||
| 	{ | ||||
| 		qse_size_t capa = 256; | ||||
|  | ||||
| 		buf = (qse_mchar_t*) httpd_alloc (httpd, (capa + 1) * QSE_SIZEOF(*buf)); | ||||
| 		if (buf == QSE_NULL) return -1; | ||||
|  | ||||
| 		/* an old vsnprintf behaves differently from C99 standard. | ||||
| 		 * thus, it returns -1 when it can't write all the input given. */ | ||||
| 		for (;;)  | ||||
| 		{ | ||||
| 			va_start (ap, fmt); | ||||
| #if defined(_WIN32) && defined(_MSC_VER) | ||||
| 			l = _vsnprintf (buf, capa + 1, fmt, ap); | ||||
| #else | ||||
| 			l = vsnprintf (buf, capa + 1, fmt, ap); | ||||
| #endif | ||||
| 			va_end (ap); | ||||
|  | ||||
| 			if (l == -1) | ||||
| 			{ | ||||
| 				httpd_free (httpd, buf); | ||||
|  | ||||
| 				capa = capa * 2; | ||||
| 				buf = (qse_mchar_t*) httpd_alloc (httpd, (capa + 1) * QSE_SIZEOF(*buf)); | ||||
| 				if (buf == QSE_NULL) return  -1; | ||||
| 			} | ||||
| 			else break; | ||||
| 		} | ||||
| 	} | ||||
| 	else  | ||||
| 	{ | ||||
| 		/* vsnprintf returns the number of characters that would  | ||||
| 		 * have been written not including the terminating '\0'  | ||||
| 		 * if the _data buffer were large enough */ | ||||
| 		buf = (qse_mchar_t*) httpd_alloc (httpd, (bytes_req + 1) * QSE_SIZEOF(*buf)); | ||||
| 		if (buf == QSE_NULL) return -1; | ||||
|  | ||||
| 		va_start (ap, fmt); | ||||
| #if defined(_WIN32) && defined(_MSC_VER) | ||||
| 		l = _vsnprintf (buf, bytes_req + 1, fmt, ap); | ||||
| #else | ||||
| 		l = vsnprintf (buf, bytes_req + 1, fmt, ap); | ||||
| #endif | ||||
| 		va_end (ap); | ||||
|  | ||||
| 		if (l != bytes_req)  | ||||
| 		{ | ||||
| 			/* something got wrong ... */ | ||||
| 			httpd_free (httpd, buf); | ||||
|  | ||||
| 			httpd->errnum = QSE_HTTPD_EINTERN; | ||||
| 			return -1; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	QSE_MEMSET (&data, 0, QSE_SIZEOF(data)); | ||||
| 	data.org = buf; | ||||
| 	data.ptr = buf; | ||||
| 	data.left = l; | ||||
|  | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.init = httpd_init_sendfmt; | ||||
| 	task.fini = httpd_fini_sendfmt; | ||||
| 	task.main = httpd_main_sendfmt; | ||||
| 	task.ctx = &data; | ||||
|  | ||||
| 	return qse_httpd_entask ( | ||||
| 		httpd, client, &task, QSE_SIZEOF(data)); | ||||
| } | ||||
|  | ||||
| /* TODO: send wide character string when QSE_CHAR_IS_WCHAR */ | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| int qse_httpd_entasksendfile ( | ||||
|      qse_httpd_t* httpd, qse_httpd_client_t* client, int fd) | ||||
| { | ||||
| 	 /* TODO: */ | ||||
| 	return -1; | ||||
| } | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| static int task_main_disconnect ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	shutdown (client->fd, SHUT_RDWR); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_httpd_entaskdisconnect (qse_httpd_t* httpd, qse_httpd_client_t* client) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	 | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.main = task_main_disconnect; | ||||
|  | ||||
| 	return qse_httpd_entask (httpd, client, &task, 0); | ||||
| } | ||||
|  | ||||
							
								
								
									
										379
									
								
								qse/lib/net/httpd_task.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										379
									
								
								qse/lib/net/httpd_task.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,379 @@ | ||||
| #include "httpd.h" | ||||
| #include "../cmn/mem.h" | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/pio.h> | ||||
|  | ||||
| #include <fcntl.h> | ||||
| #include <unistd.h> | ||||
|  | ||||
| #ifdef HAVE_SYS_SENDFILE_H | ||||
| #include <sys/sendfile.h> | ||||
| #endif | ||||
|  | ||||
| #include <stdarg.h> | ||||
| #include <stdio.h> | ||||
|  | ||||
| #define MAX_SENDFILE_SIZE 4096 | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| static int task_main_disconnect ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	shutdown (client->fd, SHUT_RDWR); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_httpd_entaskdisconnect (qse_httpd_t* httpd, qse_httpd_client_t* client) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	 | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.main = task_main_disconnect; | ||||
|  | ||||
| 	return qse_httpd_entask (httpd, client, &task, 0); | ||||
| } | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| typedef struct task_sendtext_t task_sendtext_t; | ||||
| struct task_sendtext_t | ||||
| { | ||||
| 	const qse_mchar_t* ptr; | ||||
| 	qse_size_t         left; | ||||
| }; | ||||
|  | ||||
| static int httpd_init_sendtext ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	task_sendtext_t* xtn = qse_httpd_gettaskxtn (httpd, task); | ||||
|  | ||||
| 	QSE_MEMCPY (xtn, task->ctx, QSE_SIZEOF(*xtn)); | ||||
| 	QSE_MEMCPY (xtn + 1, xtn->ptr, xtn->left); | ||||
| 	xtn->ptr = (qse_mchar_t*)(xtn + 1); | ||||
|  | ||||
| 	task->ctx = xtn; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int httpd_main_sendtext ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	ssize_t n; | ||||
| 	size_t count; | ||||
| 	task_sendtext_t* ctx = (task_sendtext_t*)task->ctx; | ||||
|  | ||||
| 	count = MAX_SENDFILE_SIZE; | ||||
| 	if (count >= ctx->left) count = ctx->left; | ||||
|  | ||||
| 	n = send ( | ||||
| 		client->fd, | ||||
| 		ctx->ptr, | ||||
| 		count, | ||||
| 		0 | ||||
| 	); | ||||
|  | ||||
| 	if (n <= -1) return -1; | ||||
|  | ||||
| 	ctx->left -= n; | ||||
| 	if (ctx->left <= 0) return 0; | ||||
|  | ||||
| 	ctx->ptr += n; | ||||
| 	return 1; /* more work to do */ | ||||
| } | ||||
|  | ||||
| int qse_httpd_entasksendtext ( | ||||
|      qse_httpd_t* httpd, qse_httpd_client_t* client, const qse_mchar_t* text) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	task_sendtext_t data; | ||||
|  | ||||
| 	QSE_MEMSET (&data, 0, QSE_SIZEOF(data)); | ||||
| 	data.ptr = text; | ||||
| 	data.left = qse_mbslen(text); | ||||
|  | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.init = httpd_init_sendtext; | ||||
| 	task.main = httpd_main_sendtext; | ||||
| 	task.ctx = &data; | ||||
|  | ||||
| 	return qse_httpd_entask ( | ||||
| 		httpd, client, &task, QSE_SIZEOF(data) + data.left); | ||||
| } | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| /* TODO: send wide character string when QSE_CHAR_IS_WCHAR */ | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| typedef struct task_sendfmt_t task_sendfmt_t; | ||||
| struct task_sendfmt_t | ||||
| { | ||||
| 	qse_mchar_t*       org; | ||||
| 	const qse_mchar_t* ptr; | ||||
| 	qse_size_t         left; | ||||
| }; | ||||
|  | ||||
| static int httpd_init_sendfmt ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	task_sendfmt_t* xtn = qse_httpd_gettaskxtn (httpd, task); | ||||
|  | ||||
| 	QSE_MEMCPY (xtn, task->ctx, QSE_SIZEOF(*xtn)); | ||||
| 	task->ctx = xtn; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static void httpd_fini_sendfmt ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	task_sendfmt_t* ctx = (task_sendfmt_t*)task->ctx; | ||||
| 	qse_httpd_freemem (httpd, ctx->org); | ||||
| } | ||||
|  | ||||
| static int httpd_main_sendfmt ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	ssize_t n; | ||||
| 	size_t count; | ||||
| 	task_sendfmt_t* ctx = (task_sendfmt_t*)task->ctx; | ||||
|  | ||||
| 	count = MAX_SENDFILE_SIZE; | ||||
| 	if (count >= ctx->left) count = ctx->left; | ||||
|  | ||||
| 	n = send ( | ||||
| 		client->fd, | ||||
| 		ctx->ptr, | ||||
| 		count, | ||||
| 		0 | ||||
| 	); | ||||
|  | ||||
| 	if (n <= -1) return -1; | ||||
|  | ||||
| 	ctx->left -= n; | ||||
| 	if (ctx->left <= 0) return 0; | ||||
|  | ||||
| 	ctx->ptr += n; | ||||
| 	return 1; /* more work to do */ | ||||
| } | ||||
|  | ||||
| int qse_httpd_entasksendfmt ( | ||||
|      qse_httpd_t* httpd, qse_httpd_client_t* client, const qse_mchar_t* fmt, ...) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	task_sendfmt_t data; | ||||
|  | ||||
| 	va_list ap; | ||||
| 	qse_mchar_t n[2]; | ||||
| 	qse_mchar_t* buf; | ||||
| 	int bytes_req, l; | ||||
|  | ||||
| 	va_start (ap, fmt); | ||||
| #if defined(_WIN32) && defined(_MSC_VER) | ||||
| 	bytes_req = _vsnprintf (n, 1, fmt, ap); | ||||
| #else | ||||
| 	bytes_req = vsnprintf (n, 1, fmt, ap); | ||||
| #endif | ||||
| 	va_end (ap); | ||||
|  | ||||
| 	if (bytes_req == -1)  | ||||
| 	{ | ||||
| 		qse_size_t capa = 256; | ||||
|  | ||||
| 		buf = (qse_mchar_t*) qse_httpd_allocmem (httpd, (capa + 1) * QSE_SIZEOF(*buf)); | ||||
| 		if (buf == QSE_NULL) return -1; | ||||
|  | ||||
| 		/* an old vsnprintf behaves differently from C99 standard. | ||||
| 		 * thus, it returns -1 when it can't write all the input given. */ | ||||
| 		for (;;)  | ||||
| 		{ | ||||
| 			va_start (ap, fmt); | ||||
| #if defined(_WIN32) && defined(_MSC_VER) | ||||
| 			l = _vsnprintf (buf, capa + 1, fmt, ap); | ||||
| #else | ||||
| 			l = vsnprintf (buf, capa + 1, fmt, ap); | ||||
| #endif | ||||
| 			va_end (ap); | ||||
|  | ||||
| 			if (l == -1) | ||||
| 			{ | ||||
| 				qse_httpd_freemem (httpd, buf); | ||||
|  | ||||
| 				capa = capa * 2; | ||||
| 				buf = (qse_mchar_t*) qse_httpd_allocmem (httpd, (capa + 1) * QSE_SIZEOF(*buf)); | ||||
| 				if (buf == QSE_NULL) return  -1; | ||||
| 			} | ||||
| 			else break; | ||||
| 		} | ||||
| 	} | ||||
| 	else  | ||||
| 	{ | ||||
| 		/* vsnprintf returns the number of characters that would  | ||||
| 		 * have been written not including the terminating '\0'  | ||||
| 		 * if the _data buffer were large enough */ | ||||
| 		buf = (qse_mchar_t*) qse_httpd_allocmem (httpd, (bytes_req + 1) * QSE_SIZEOF(*buf)); | ||||
| 		if (buf == QSE_NULL) return -1; | ||||
|  | ||||
| 		va_start (ap, fmt); | ||||
| #if defined(_WIN32) && defined(_MSC_VER) | ||||
| 		l = _vsnprintf (buf, bytes_req + 1, fmt, ap); | ||||
| #else | ||||
| 		l = vsnprintf (buf, bytes_req + 1, fmt, ap); | ||||
| #endif | ||||
| 		va_end (ap); | ||||
|  | ||||
| 		if (l != bytes_req)  | ||||
| 		{ | ||||
| 			/* something got wrong ... */ | ||||
| 			qse_httpd_freemem (httpd, buf); | ||||
|  | ||||
| 			httpd->errnum = QSE_HTTPD_EINTERN; | ||||
| 			return -1; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	QSE_MEMSET (&data, 0, QSE_SIZEOF(data)); | ||||
| 	data.org = buf; | ||||
| 	data.ptr = buf; | ||||
| 	data.left = l; | ||||
|  | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.init = httpd_init_sendfmt; | ||||
| 	task.fini = httpd_fini_sendfmt; | ||||
| 	task.main = httpd_main_sendfmt; | ||||
| 	task.ctx = &data; | ||||
|  | ||||
| 	return qse_httpd_entask ( | ||||
| 		httpd, client, &task, QSE_SIZEOF(data)); | ||||
| } | ||||
|  | ||||
| /* TODO: send wide character string when QSE_CHAR_IS_WCHAR */ | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| typedef struct httpd_task_sendfile_t httpd_task_sendfile_t; | ||||
| struct httpd_task_sendfile_t | ||||
| { | ||||
| 	int fd; | ||||
| 	qse_foff_t left; | ||||
| 	qse_foff_t offset; | ||||
| }; | ||||
|  | ||||
| static int httpd_init_sendfile ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	httpd_task_sendfile_t* xtn = qse_httpd_gettaskxtn (httpd, task); | ||||
| 	QSE_MEMCPY (xtn, task->ctx, QSE_SIZEOF(*xtn)); | ||||
| 	task->ctx = xtn; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static void httpd_fini_sendfile ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	httpd_task_sendfile_t* ctx = (httpd_task_sendfile_t*)task->ctx; | ||||
| 	close (ctx->fd); | ||||
| } | ||||
|  | ||||
| static int httpd_main_sendfile ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	ssize_t n; | ||||
| 	size_t count; | ||||
| 	httpd_task_sendfile_t* ctx = (httpd_task_sendfile_t*)task->ctx; | ||||
|  | ||||
| 	count = MAX_SENDFILE_SIZE; | ||||
| 	if (count >= ctx->left) count = ctx->left; | ||||
|  | ||||
| 	n = sendfile ( | ||||
| 		client->fd, | ||||
| 		ctx->fd, | ||||
| 		&ctx->offset, | ||||
| 		count | ||||
| 	); | ||||
|  | ||||
| 	if (n <= -1) return -1; | ||||
|  | ||||
| 	ctx->left -= n; | ||||
| 	if (ctx->left <= 0) return 0; | ||||
|  | ||||
| 	return 1; /* more work to do */ | ||||
| } | ||||
|  | ||||
| int qse_httpd_entasksendfile ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client,  | ||||
| 	int fd, qse_foff_t offset, qse_foff_t size) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	httpd_task_sendfile_t data; | ||||
| 	 | ||||
| 	QSE_MEMSET (&data, 0, QSE_SIZEOF(data)); | ||||
| 	data.fd = fd; | ||||
| 	data.offset = offset; | ||||
| 	data.left = size; | ||||
|  | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.init = httpd_init_sendfile; | ||||
| 	task.main = httpd_main_sendfile; | ||||
| 	task.fini = httpd_fini_sendfile; | ||||
| 	task.ctx = &data; | ||||
|  | ||||
| 	return qse_httpd_entask (httpd, client, &task, QSE_SIZEOF(data)); | ||||
| } | ||||
|  | ||||
| /*------------------------------------------------------------------------*/ | ||||
|  | ||||
| #if 0 | ||||
| typedef struct httpd_task_cgi_t httpd_task_cgi_t; | ||||
| struct httpd_task_cgi_t | ||||
| { | ||||
| 	qse_pio_t* pio; | ||||
| }; | ||||
|  | ||||
| static int httpd_init_cgi ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	httpd_task_cgi_t* xtn = qse_httpd_gettaskxtn (httpd, task); | ||||
|  | ||||
| 	QSE_MEMSET (xtn, 0, QSE_SIZEOF(*xtn)); | ||||
| 	xtn->pio = qse_pio_open (httpd->mmgr, task->ctx); | ||||
| 	if (xtn->pio == QSE_NULL) | ||||
| 	{ | ||||
| 		httpd->errnum = QSE_HTTPD_ECGI; | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	task->ctx = xtn; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static void httpd_fini_cgi ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| 	httpd_task_cgi_t* xtn = task->ctx; | ||||
| 	qse_pio_close (xtn->pio); | ||||
| } | ||||
|  | ||||
| static void httpd_main_cgi ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
| { | ||||
| /* TODO */ | ||||
| 	return -1; | ||||
| } | ||||
|  | ||||
| int qse_httpd_entaskcgi ( | ||||
| 	qse_httpd_t* httpd, qse_httpd_client_t* client, const qse_char_t* path) | ||||
| { | ||||
| 	qse_httpd_task_t task; | ||||
| 	httpd_task_cgi_t data; | ||||
| 	 | ||||
| 	QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); | ||||
| 	task.init = httpd_init_cgi; | ||||
| 	task.main = httpd_main_cgi; | ||||
| 	task.fini = httpd_fini_cgi; | ||||
| 	task.ctx = path; | ||||
|  | ||||
| 	return qse_httpd_entask (httpd, client, &task, QSE_SIZEOF(data)); | ||||
| } | ||||
| #endif | ||||
		Reference in New Issue
	
	Block a user