added qse_httpd_mod_t and supporting functions
This commit is contained in:
		| @ -975,7 +975,12 @@ static int task_init_proxy ( | ||||
|  | ||||
| 	if (arg->rsrc->flags & QSE_HTTPD_RSRC_PROXY_ENABLE_URS) | ||||
| 	{ | ||||
| 		int x = httpd->opt.scb.urs.prerewrite (httpd, client, arg->req, arg->rsrc->host, &proxy->url_to_rewrite); | ||||
| 		int x; | ||||
|  | ||||
| 		if (arg->rsrc->urs_prerewrite_mod && arg->rsrc->urs_prerewrite_mod->urs_prerewrite) | ||||
| 			x = arg->rsrc->urs_prerewrite_mod->urs_prerewrite (arg->rsrc->urs_prerewrite_mod, client, arg->req, arg->rsrc->host, &proxy->url_to_rewrite); | ||||
| 		else | ||||
| 			x = httpd->opt.scb.urs.prerewrite (httpd, client, arg->req, arg->rsrc->host, &proxy->url_to_rewrite); | ||||
| 		if (x <= -1) goto oops; | ||||
|  | ||||
| printf (">>>>>>>>>>>>>>>>>>>>>>>> [%s] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n", proxy->url_to_rewrite); | ||||
|  | ||||
| @ -1,50 +1,17 @@ | ||||
| #if !defined(QSE_HTTPD_DEFAULT_MODPREFIX) | ||||
| #	if defined(_WIN32) | ||||
| #		define QSE_HTTPD_DEFAULT_MODPREFIX "qsehttpd-" | ||||
| #	elif defined(__OS2__) | ||||
| #		define QSE_HTTPD_DEFAULT_MODPREFIX "htd-" | ||||
| #	elif defined(__DOS__) | ||||
| #		define QSE_HTTPD_DEFAULT_MODPREFIX "htd-" | ||||
| #	else | ||||
| #		define QSE_HTTPD_DEFAULT_MODPREFIX "libqsehttpd-" | ||||
| #	endif | ||||
| #endif | ||||
|  | ||||
| #if !defined(QSE_HTTPD_DEFAULT_MODPOSTFIX) | ||||
| #	define QSE_HTTPD_DEFAULT_MODPOSTFIX "" | ||||
| #endif | ||||
|  | ||||
|  | ||||
| static int mod_open (qse_httpd_t* httpd, qse_httpd_mod_t* mod) | ||||
| { | ||||
| #if defined(USE_LTDL) | ||||
| 	void* h; | ||||
| 	qse_mchar_t* modpath; | ||||
| 	const qse_char_t* tmp[4]; | ||||
| 	int count; | ||||
|  | ||||
| 	count = 0; | ||||
| 	if (spec->prefix) tmp[count++] = spec->prefix; | ||||
| 	tmp[count++] = mod->name; | ||||
| 	if (spec->postfix) tmp[count++] = spec->postfix; | ||||
| 	tmp[count] = QSE_NULL; | ||||
|  | ||||
| 	#if defined(QSE_CHAR_IS_MCHAR) | ||||
| 	modpath = qse_mbsadup (tmp, QSE_NULL, httpd->mmgr); | ||||
| 	#else | ||||
| 	modpath = qse_wcsatombsdup (tmp, QSE_NULL, httpd->mmgr); | ||||
| 	#endif | ||||
| 	if (!modpath) | ||||
| 	h = lt_dlopenext (mod->fullname); | ||||
| 	if (h == QSE_NULL)  | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOMEM, QSE_NULL); | ||||
| 		qse_httpd_seterrnum (httpd, syserr_to_errnum(errno)); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	h = lt_dlopenext (modpath); | ||||
|  | ||||
| 	QSE_MMGR_FREE (httpd->mmgr, modpath); | ||||
|  | ||||
| 	if (h == QSE_NULL) return -1; | ||||
| 	mod->handle = h; | ||||
| 	return 0; | ||||
|  | ||||
| @ -52,69 +19,42 @@ static int mod_open (qse_httpd_t* httpd, qse_httpd_mod_t* mod) | ||||
| #elif defined(_WIN32) | ||||
|  | ||||
| 	HMODULE h; | ||||
| 	qse_char_t* modpath; | ||||
| 	const qse_char_t* tmp[4]; | ||||
| 	int count; | ||||
|  | ||||
| 	count = 0; | ||||
| 	if (spec->prefix) tmp[count++] = spec->prefix; | ||||
| 	tmp[count++] = spec->name; | ||||
| 	if (spec->postfix) tmp[count++] = spec->postfix; | ||||
| 	tmp[count] = QSE_NULL; | ||||
| 	QSE_ASSERT (QSE_SIZEOF(h) <= QSE_SIZEOF(void*)); | ||||
|  | ||||
| 	modpath = qse_stradup (tmp, QSE_NULL, httpd->mmgr); | ||||
| 	if (!modpath) | ||||
| 	h = LoadLibrary (mod->fullname); | ||||
| 	if (h == QSE_NULL)  | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOMEM, QSE_NULL); | ||||
| 		qse_httpd_seterrnum (httpd, syserr_to_errnum(GetLastError())); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	h = LoadLibrary (modpath); | ||||
|  | ||||
| 	QSE_MMGR_FREE (httpd->mmgr, modpath); | ||||
| 	 | ||||
| 	QSE_ASSERT (QSE_SIZEOF(h) <= QSE_SIZEOF(void*)); | ||||
|  | ||||
| 	if (h == QSE_NULL) return -1; | ||||
| 	mod->handle = h; | ||||
| 	return 0; | ||||
|  | ||||
| #elif defined(__OS2__) | ||||
|  | ||||
| 	HMODULE h; | ||||
| 	qse_mchar_t* modpath; | ||||
| 	const qse_char_t* tmp[4]; | ||||
| 	int count; | ||||
| 	char errbuf[CCHMAXPATH]; | ||||
| 	APIRET rc; | ||||
|  | ||||
| 	count = 0; | ||||
| 	if (spec->prefix) tmp[count++] = spec->prefix; | ||||
| 	tmp[count++] = spec->name; | ||||
| 	if (spec->postfix) tmp[count++] = spec->postfix; | ||||
| 	tmp[count] = QSE_NULL; | ||||
|  | ||||
| 	#if defined(QSE_CHAR_IS_MCHAR) | ||||
| 	modpath = qse_mbsadup (tmp, QSE_NULL, httpd->mmgr); | ||||
| 	#else | ||||
| 	modpath = qse_wcsatombsdup (tmp, QSE_NULL, httpd->mmgr); | ||||
| 	#endif | ||||
| 	if (!modpath) | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOMEM, QSE_NULL); | ||||
| 		return -1; | ||||
| 	} | ||||
| 	QSE_ASSERT (QSE_SIZEOF(h) <= QSE_SIZEOF(void*)); | ||||
|  | ||||
| 	/* DosLoadModule() seems to have severe limitation on  | ||||
| 	 * the file name it can load (max-8-letters.xxx) */ | ||||
| 	rc = DosLoadModule (errbuf, QSE_COUNTOF(errbuf) - 1, modpath, &h); | ||||
| 	if (rc != NO_ERROR) h = QSE_NULL; | ||||
| 	if (rc != NO_ERROR)  | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, syserr_to_errnum(rc)); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	QSE_MMGR_FREE (httpd->mmgr, modpath); | ||||
| 	if (h == QSE_NULL)  | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOENT); /* is this error code ok? */ | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	QSE_ASSERT (QSE_SIZEOF(h) <= QSE_SIZEOF(void*)); | ||||
|  | ||||
| 	if (h == QSE_NULL) return -1; | ||||
| 	mod->handle = h; | ||||
| 	return 0; | ||||
|  | ||||
| @ -125,34 +65,14 @@ static int mod_open (qse_httpd_t* httpd, qse_httpd_mod_t* mod) | ||||
| 	 * dos-extender only. the best is to enable QSE_ENABLE_STATIC_MODULE | ||||
| 	 * when building for DOS. */ | ||||
| 	void* h; | ||||
| 	qse_mchar_t* modpath; | ||||
| 	const qse_char_t* tmp[4]; | ||||
| 	int count; | ||||
|  | ||||
| 	count = 0; | ||||
| 	if (spec->prefix) tmp[count++] = spec->prefix; | ||||
| 	tmp[count++] = spec->name; | ||||
| 	if (spec->postfix) tmp[count++] = spec->postfix; | ||||
| 	tmp[count] = QSE_NULL; | ||||
|  | ||||
| 	#if defined(QSE_CHAR_IS_MCHAR) | ||||
| 	modpath = qse_mbsadup (tmp, QSE_NULL, httpd->mmgr); | ||||
| 	#else | ||||
| 	modpath = qse_wcsatombsdup (tmp, QSE_NULL, httpd->mmgr); | ||||
| 	#endif | ||||
| 	if (!modpath) | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOMEM, QSE_NULL); | ||||
| 		return 01; | ||||
| 	} | ||||
|  | ||||
| 	h = LoadModule (modpath); | ||||
| 	if (h == QSE_NULL)  | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, syserr_to_errnum(errno)); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	QSE_MMGR_FREE (httpd->mmgr, modpath); | ||||
| 	 | ||||
| 	QSE_ASSERT (QSE_SIZEOF(h) <= QSE_SIZEOF(void*)); | ||||
|  | ||||
| 	if (h == QSE_NULL) return -1; | ||||
| 	mod->handle = h; | ||||
| 	return 0; | ||||
|  | ||||
| @ -195,14 +115,26 @@ static void* mod_symbol (qse_httpd_t* httpd, qse_httpd_mod_t* handle, const qse_ | ||||
|  | ||||
| #if defined(USE_LTDL) | ||||
| 	s = lt_dlsym (mod->handle, mname); | ||||
| 	if (s == QSE_NULL) qse_httpd_seterrnum (httpd, syserr_to_errnum(errno)); | ||||
| #elif defined(_WIN32) | ||||
| 	s = GetProcAddress ((HMODULE)mod->handle, mname); | ||||
| 	if (s == QSE_NULL) qse_httpd_seterrnum (httpd, syserr_to_errnum(GetLastError())); | ||||
| #elif defined(__OS2__) | ||||
| 	if (DosQueryProcAddr ((HMODULE)mod->handle, 0, mname, (PFN*)&s) != NO_ERROR) s = QSE_NULL; | ||||
| 	{ | ||||
| 		APIRET rc; | ||||
| 		rc = DosQueryProcAddr ((HMODULE)mod->handle, 0, mname, (PFN*)&s); | ||||
| 		if (rc != NO_ERROR)  | ||||
| 		{ | ||||
| 			qse_httpd_seterrnum (httpd, syserr_to_errnum(rc)); | ||||
| 			s = QSE_NULL; | ||||
| 		} | ||||
| 	} | ||||
| #elif defined(__DOS__) | ||||
| 	s = GetProcAddress (mod->handle, mname); | ||||
| 	if (s == QSE_NULL) qse_httpd_seterrnum (httpd, syserr_to_errnum(errno)); | ||||
| #else | ||||
| 	s = QSE_NULL; | ||||
| 	qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOIMPL); | ||||
| #endif | ||||
|  | ||||
| #if defined(QSE_CHAR_IS_MCHAR) | ||||
|  | ||||
| @ -217,7 +217,6 @@ int qse_httpd_setopt (qse_httpd_t* httpd, qse_httpd_opt_t id, const void* value) | ||||
| 			httpd->opt.rcb = *(qse_httpd_rcb_t*)value; | ||||
| 			return 0; | ||||
|  | ||||
| 		 | ||||
| 	} | ||||
|  | ||||
| 	httpd->errnum = QSE_HTTPD_EINVAL; | ||||
| @ -1900,7 +1899,7 @@ static void unload_all_modules (qse_httpd_t* httpd) | ||||
| 		mod = httpd->modlist; | ||||
| 		httpd->modlist = mod->next; | ||||
|  | ||||
| /* call fini */ | ||||
| 		if (mod->unload) mod->unload (mod); | ||||
| 		httpd->opt.scb.mod.close (httpd, mod); | ||||
| 		qse_httpd_freemem (httpd, mod); | ||||
| 	} | ||||
| @ -1909,29 +1908,87 @@ static void unload_all_modules (qse_httpd_t* httpd) | ||||
| int qse_httpd_loadmod (qse_httpd_t* httpd, const qse_char_t* name) | ||||
| { | ||||
| 	qse_httpd_mod_t* mod; | ||||
| 	qse_size_t name_len; | ||||
| 	qse_size_t name_len, prefix_len, postfix_len, fullname_len; | ||||
| 	const qse_char_t* prefix, * postfix; | ||||
| 	qse_char_t* entry_point_name; | ||||
| 	qse_httpd_mod_load_t load; | ||||
|  | ||||
| /* TODO: no singly linked list speed up */ | ||||
|  | ||||
| 	name_len = qse_strlen(name); | ||||
|  | ||||
| 	mod = qse_httpd_allocmem (httpd, QSE_SIZEOF(*mod) + name_len + 1); | ||||
| 	if (httpd->opt.mod[0].len > 0) | ||||
| 	{ | ||||
| 		prefix = httpd->opt.mod[0].ptr; | ||||
| 		prefix_len = httpd->opt.mod[0].len; | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		prefix = QSE_T(QSE_HTTPD_DEFAULT_MODPREFIX); | ||||
| 		prefix_len = qse_strlen(prefix); | ||||
| 	} | ||||
|  | ||||
| 	if (httpd->opt.mod[1].len > 0) | ||||
| 	{ | ||||
| 		postfix = httpd->opt.mod[1].ptr; | ||||
| 		postfix_len = httpd->opt.mod[1].len; | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		postfix = QSE_T(QSE_HTTPD_DEFAULT_MODPOSTFIX); | ||||
| 		postfix_len = qse_strlen(postfix); | ||||
| 	} | ||||
|  | ||||
| 	/*  | ||||
| 	 * +15: length of _qse_httpd_mod_ | ||||
| 	 * +2: _\0 | ||||
| 	 */ | ||||
| 	fullname_len = prefix_len + name_len + postfix_len; | ||||
| 	mod = qse_httpd_allocmem (httpd, QSE_SIZEOF(*mod) + (name_len + 1 + fullname_len + 1 + 15 + name_len + 2) * QSE_SIZEOF(qse_char_t)); | ||||
| 	if (mod == QSE_NULL) return -1; | ||||
|  | ||||
| 	QSE_MEMSET (mod, 0, QSE_SIZEOF(*mod)); | ||||
| 	mod->name = mod + 1; | ||||
| 	qse_strcpy (mod->name, name); | ||||
|  | ||||
| 	mod->name = (qse_char_t*)(mod + 1); | ||||
| 	mod->fullname = mod->name + name_len + 1; | ||||
| 	entry_point_name = mod->fullname + fullname_len + 1; | ||||
| 	qse_strcpy (mod->name, name); | ||||
| 	qse_strjoin (mod->fullname, prefix, name, postfix, QSE_NULL); | ||||
| 	qse_strjoin (entry_point_name, QSE_T("_qse_httpd_mod_"), name, QSE_NULL); | ||||
|  | ||||
| printf ("%ls %ls %ls\n", mod->name, mod->fullname, entry_point_name); | ||||
| 	if (httpd->opt.scb.mod.open (httpd, mod) <= -1)  | ||||
| 	{ | ||||
| printf ("FAIL => %ls %ls %ls\n", mod->name, mod->fullname, entry_point_name); | ||||
| 		qse_httpd_freemem (httpd, mod); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| /* TODO: find init and execute it. if it fails, unload it. */ | ||||
| printf ("OK => %ls %ls %ls\n", mod->name, mod->fullname, entry_point_name); | ||||
| 	/* attempt qse_httpd_mod_xxx */ | ||||
| 	load = httpd->opt.scb.mod.symbol (httpd, mod, &entry_point_name[1]); | ||||
| 	if (!load) | ||||
| 	{ | ||||
| 		/* attempt _qse_awk_mod_xxx */ | ||||
| 		load = httpd->opt.scb.mod.symbol (httpd, mod, &entry_point_name[0]); | ||||
| 		if (!load) | ||||
| 		{ | ||||
| 			/* attempt qse_awk_mod_xxx_ */ | ||||
| 			entry_point_name[15 + name_len] = QSE_T('_'); | ||||
| 			entry_point_name[15 + name_len + 1] = QSE_T('\0'); | ||||
| 			load = httpd->opt.scb.mod.symbol (httpd, mod, &entry_point_name[1]); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if (load == QSE_NULL || load (mod) <= -1) | ||||
| 	{ | ||||
| 		httpd->opt.scb.mod.close (httpd, mod); | ||||
| 		qse_httpd_freemem (httpd, mod); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	mod->next = httpd->modlist; | ||||
| 	httpd->modlist = mod; | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user