improved dns handling and added some code for url rewriting
This commit is contained in:
		| @ -1920,7 +1920,7 @@ static int httpd_main (int argc, qse_char_t* argv[]) | |||||||
| 	setup_signal_handlers (); | 	setup_signal_handlers (); | ||||||
|  |  | ||||||
| 	qse_httpd_getopt (httpd, QSE_HTTPD_TRAIT, &trait); | 	qse_httpd_getopt (httpd, QSE_HTTPD_TRAIT, &trait); | ||||||
| 	trait |= QSE_HTTPD_CGIERRTONUL | QSE_HTTPD_LOGACT | QSE_HTTPD_DNSNOAAAA; | 	trait |= QSE_HTTPD_CGIERRTONUL | QSE_HTTPD_LOGACT | QSE_HTTPD_DNSNOAAAA; /* TODO: make NOAAAA configurable */ | ||||||
| 	qse_httpd_setopt (httpd, QSE_HTTPD_TRAIT, &trait); | 	qse_httpd_setopt (httpd, QSE_HTTPD_TRAIT, &trait); | ||||||
|  |  | ||||||
| 	tmout.sec = 10; | 	tmout.sec = 10; | ||||||
| @ -1936,7 +1936,7 @@ static int httpd_main (int argc, qse_char_t* argv[]) | |||||||
| 	rcb.impede = impede_httpd; /* executed when qse_httpd_impede() is called */ | 	rcb.impede = impede_httpd; /* executed when qse_httpd_impede() is called */ | ||||||
| 	if (g_debug) rcb.logact = logact_httpd; /* i don't remember old logging handler */ | 	if (g_debug) rcb.logact = logact_httpd; /* i don't remember old logging handler */ | ||||||
| 	qse_httpd_setopt (httpd, QSE_HTTPD_RCB, &rcb); | 	qse_httpd_setopt (httpd, QSE_HTTPD_RCB, &rcb); | ||||||
| 	 |  | ||||||
| 	ret = qse_httpd_loopstd (httpd, QSE_NULL); | 	ret = qse_httpd_loopstd (httpd, QSE_NULL); | ||||||
|  |  | ||||||
| 	restore_signal_handlers (); | 	restore_signal_handlers (); | ||||||
|  | |||||||
| @ -34,6 +34,7 @@ typedef struct qse_httpd_mate_t   qse_httpd_mate_t; | |||||||
| typedef struct qse_httpd_server_t qse_httpd_server_t; | typedef struct qse_httpd_server_t qse_httpd_server_t; | ||||||
| typedef struct qse_httpd_client_t qse_httpd_client_t; | typedef struct qse_httpd_client_t qse_httpd_client_t; | ||||||
| typedef struct qse_httpd_dns_t    qse_httpd_dns_t; | typedef struct qse_httpd_dns_t    qse_httpd_dns_t; | ||||||
|  | typedef struct qse_httpd_urs_t    qse_httpd_urs_t; | ||||||
|  |  | ||||||
| enum qse_httpd_errnum_t | enum qse_httpd_errnum_t | ||||||
| { | { | ||||||
| @ -132,6 +133,13 @@ typedef void (*qse_httpd_resol_t) ( | |||||||
| 	void*              ctx | 	void*              ctx | ||||||
| ); | ); | ||||||
|  |  | ||||||
|  | typedef void (*qse_httpd_rewrite_t) ( | ||||||
|  | 	qse_httpd_t*       httpd, | ||||||
|  | 	const qse_mchar_t* url, | ||||||
|  | 	const qse_mchar_t* new_url, | ||||||
|  | 	void*              ctx | ||||||
|  | ); | ||||||
|  |  | ||||||
| typedef struct qse_httpd_scb_t qse_httpd_scb_t; | typedef struct qse_httpd_scb_t qse_httpd_scb_t; | ||||||
| struct qse_httpd_scb_t | struct qse_httpd_scb_t | ||||||
| { | { | ||||||
| @ -255,6 +263,14 @@ struct qse_httpd_scb_t | |||||||
| 		int (*recv) (qse_httpd_t* httpd, qse_httpd_dns_t* dns); | 		int (*recv) (qse_httpd_t* httpd, qse_httpd_dns_t* dns); | ||||||
| 		int (*send) (qse_httpd_t* httpd, qse_httpd_dns_t* dns, const qse_mchar_t* name, qse_httpd_resol_t resol, void* ctx); | 		int (*send) (qse_httpd_t* httpd, qse_httpd_dns_t* dns, const qse_mchar_t* name, qse_httpd_resol_t resol, void* ctx); | ||||||
| 	} dns; | 	} dns; | ||||||
|  |  | ||||||
|  | 	struct | ||||||
|  | 	{ | ||||||
|  | 		int (*open) (qse_httpd_t* httpd, qse_httpd_urs_t* urs); | ||||||
|  | 		void (*close) (qse_httpd_t* httpd, qse_httpd_urs_t* urs); | ||||||
|  | 		int (*recv) (qse_httpd_t* httpd, qse_httpd_urs_t* urs); | ||||||
|  | 		int (*send) (qse_httpd_t* httpd, qse_httpd_urs_t* urs, const qse_mchar_t* url, qse_httpd_rewrite_t rewrite, void* ctx); | ||||||
|  | 	} urs; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /* -------------------------------------------------------------------------- */ | /* -------------------------------------------------------------------------- */ | ||||||
| @ -387,7 +403,8 @@ enum qse_httpd_mate_type_t | |||||||
| { | { | ||||||
| 	QSE_HTTPD_SERVER, | 	QSE_HTTPD_SERVER, | ||||||
| 	QSE_HTTPD_CLIENT, | 	QSE_HTTPD_CLIENT, | ||||||
| 	QSE_HTTPD_DNS | 	QSE_HTTPD_DNS, | ||||||
|  | 	QSE_HTTPD_URS | ||||||
| }; | }; | ||||||
| typedef enum qse_httpd_mate_type_t  qse_httpd_mate_type_t; | typedef enum qse_httpd_mate_type_t  qse_httpd_mate_type_t; | ||||||
|  |  | ||||||
| @ -486,6 +503,17 @@ struct qse_httpd_dns_t | |||||||
| 	qse_ubi_t handle; | 	qse_ubi_t handle; | ||||||
| 	void* ctx; | 	void* ctx; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | struct qse_httpd_urs_t | ||||||
|  | { | ||||||
|  | 	/* PRIVATE == */ | ||||||
|  | 	QSE_HTTPD_MATE_HDR; | ||||||
|  |  | ||||||
|  | 	/* == PUBLIC == */ | ||||||
|  | 	qse_ubi_t handle; | ||||||
|  | 	void* ctx; | ||||||
|  | }; | ||||||
|  |  | ||||||
| /* -------------------------------------------------------------------------- */ | /* -------------------------------------------------------------------------- */ | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  | |||||||
| @ -142,6 +142,7 @@ enum qse_httpd_serverstd_opt_t | |||||||
| }; | }; | ||||||
| typedef enum qse_httpd_serverstd_opt_t qse_httpd_serverstd_opt_t; | typedef enum qse_httpd_serverstd_opt_t qse_httpd_serverstd_opt_t; | ||||||
|  |  | ||||||
|  | #define QSE_HTTPD_DNSSTD_DEFAULT_PORT         53 | ||||||
| #define QSE_HTTPD_DNSSTD_DEFAULT_TMOUT        3 | #define QSE_HTTPD_DNSSTD_DEFAULT_TMOUT        3 | ||||||
| #define QSE_HTTPD_DNSSTD_DEFAULT_RESENDS      2 | #define QSE_HTTPD_DNSSTD_DEFAULT_RESENDS      2 | ||||||
| #define QSE_HTTPD_DNSSTD_DEFAULT_CACHE_TTL    120 | #define QSE_HTTPD_DNSSTD_DEFAULT_CACHE_TTL    120 | ||||||
| @ -160,6 +161,18 @@ struct qse_httpd_dnsstd_t | |||||||
|  |  | ||||||
| typedef struct qse_httpd_dnsstd_t qse_httpd_dnsstd_t; | typedef struct qse_httpd_dnsstd_t qse_httpd_dnsstd_t; | ||||||
|  |  | ||||||
|  | #define QSE_HTTPD_URSSTD_DEFAULT_PORT    94 | ||||||
|  | #define QSE_HTTPD_URSSTD_DEFAULT_TMOUT   10 | ||||||
|  | #define QSE_HTTPD_URSSTD_DEFAULT_RESENDS 0 | ||||||
|  |  | ||||||
|  | struct qse_httpd_ursstd_t | ||||||
|  | { | ||||||
|  | 	qse_nwad_t nwad; | ||||||
|  | 	qse_ntime_t tmout; | ||||||
|  | 	qse_uint32_t resends; | ||||||
|  | }; | ||||||
|  | typedef struct qse_httpd_ursstd_t qse_httpd_ursstd_t; | ||||||
|  |  | ||||||
| #if defined(__cplusplus) | #if defined(__cplusplus) | ||||||
| extern "C" { | extern "C" { | ||||||
| #endif | #endif | ||||||
|  | |||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @ -382,7 +382,7 @@ static qse_htrd_recbs_t htrd_recbs = | |||||||
| }; | }; | ||||||
|  |  | ||||||
| /* ----------------------------------------------------------------------- */ | /* ----------------------------------------------------------------------- */ | ||||||
| static void tmr_idle_updated (qse_tmr_t* tmr, qse_size_t old_index, qse_size_t new_index, void* ctx); | static void tmr_idle_update (qse_tmr_t* tmr, qse_size_t old_index, qse_size_t new_index, void* ctx); | ||||||
| static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx); | static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx); | ||||||
|  |  | ||||||
| static void mark_bad_client (qse_httpd_client_t* client) | static void mark_bad_client (qse_httpd_client_t* client) | ||||||
| @ -420,7 +420,7 @@ static qse_httpd_client_t* new_client (qse_httpd_t* httpd, qse_httpd_client_t* t | |||||||
| 	qse_addtime (&idle_event.when, &httpd->opt.idle_limit, &idle_event.when); | 	qse_addtime (&idle_event.when, &httpd->opt.idle_limit, &idle_event.when); | ||||||
| 	idle_event.ctx = client; | 	idle_event.ctx = client; | ||||||
| 	idle_event.handler = tmr_idle_handle; | 	idle_event.handler = tmr_idle_handle; | ||||||
| 	idle_event.updater = tmr_idle_updated; | 	idle_event.updater = tmr_idle_update; | ||||||
|  |  | ||||||
| 	client->tmr_idle = qse_tmr_insert (httpd->tmr, &idle_event); | 	client->tmr_idle = qse_tmr_insert (httpd->tmr, &idle_event); | ||||||
| 	if (client->tmr_idle == QSE_TMR_INVALID) | 	if (client->tmr_idle == QSE_TMR_INVALID) | ||||||
| @ -618,7 +618,7 @@ printf ("MUX ADDHND CLIENT READ %d\n", client->handle.i); | |||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| static void tmr_idle_updated (qse_tmr_t* tmr, qse_size_t old_index, qse_size_t new_index, void* ctx) | static void tmr_idle_update (qse_tmr_t* tmr, qse_size_t old_index, qse_size_t new_index, void* ctx) | ||||||
| { | { | ||||||
| 	qse_httpd_client_t* client = (qse_httpd_client_t*)ctx; | 	qse_httpd_client_t* client = (qse_httpd_client_t*)ctx; | ||||||
| printf ("tmr_idle updated old_index %d new_index %d tmr_idle %d\n", (int)old_index, (int)new_index, (int)client->tmr_idle); | printf ("tmr_idle updated old_index %d new_index %d tmr_idle %d\n", (int)old_index, (int)new_index, (int)client->tmr_idle); | ||||||
| @ -650,7 +650,7 @@ printf ("client is idle purging....\n"); | |||||||
| 			qse_addtime (&idle_event.when, &client->server->httpd->opt.idle_limit, &idle_event.when); | 			qse_addtime (&idle_event.when, &client->server->httpd->opt.idle_limit, &idle_event.when); | ||||||
| 			idle_event.ctx = client; | 			idle_event.ctx = client; | ||||||
| 			idle_event.handler = tmr_idle_handle; | 			idle_event.handler = tmr_idle_handle; | ||||||
| 			idle_event.updater = tmr_idle_updated; | 			idle_event.updater = tmr_idle_update; | ||||||
|  |  | ||||||
| 			/* the timer must have been deleted when this callback is called. */ | 			/* the timer must have been deleted when this callback is called. */ | ||||||
| 			QSE_ASSERT (client->tmr_idle == QSE_TMR_INVALID);  | 			QSE_ASSERT (client->tmr_idle == QSE_TMR_INVALID);  | ||||||
| @ -833,6 +833,29 @@ static void deactivate_dns (qse_httpd_t* httpd) | |||||||
| 	httpd->opt.scb.mux.delhnd (httpd, httpd->mux, httpd->dns.handle); | 	httpd->opt.scb.mux.delhnd (httpd, httpd->mux, httpd->dns.handle); | ||||||
| 	httpd->opt.scb.dns.close (httpd, &httpd->dns); | 	httpd->opt.scb.dns.close (httpd, &httpd->dns); | ||||||
| } | } | ||||||
|  | /* ----------------------------------------------------------------------- */ | ||||||
|  |  | ||||||
|  | static int activate_urs (qse_httpd_t* httpd) | ||||||
|  | { | ||||||
|  | /* TODO: how to disable URS??? */ | ||||||
|  | 	if (httpd->opt.scb.urs.open (httpd, &httpd->urs) <= -1) return -1; | ||||||
|  |  | ||||||
|  | 	httpd->urs.type = QSE_HTTPD_URS; | ||||||
|  |  | ||||||
|  | 	if (httpd->opt.scb.mux.addhnd (httpd, httpd->mux, httpd->urs.handle, QSE_HTTPD_MUX_READ, &httpd->urs) <= -1)  | ||||||
|  | 	{ | ||||||
|  | 		httpd->opt.scb.urs.close (httpd, &httpd->urs); | ||||||
|  | 		return -1; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static void deactivate_urs (qse_httpd_t* httpd) | ||||||
|  | { | ||||||
|  | 	httpd->opt.scb.mux.delhnd (httpd, httpd->mux, httpd->urs.handle); | ||||||
|  | 	httpd->opt.scb.urs.close (httpd, &httpd->urs); | ||||||
|  | } | ||||||
|  |  | ||||||
| /* ----------------------------------------------------------------------- */ | /* ----------------------------------------------------------------------- */ | ||||||
|  |  | ||||||
| @ -1384,6 +1407,16 @@ static int perform_dns (qse_httpd_t* httpd, void* mux, qse_ubi_t handle, int mas | |||||||
| 	return httpd->opt.scb.dns.recv (httpd, dns); | 	return httpd->opt.scb.dns.recv (httpd, dns); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | static int perform_urs (qse_httpd_t* httpd, void* mux, qse_ubi_t handle, int mask, void* cbarg) | ||||||
|  | { | ||||||
|  | 	qse_httpd_urs_t* urs = (qse_httpd_urs_t*)cbarg; | ||||||
|  |  | ||||||
|  | 	QSE_ASSERT (mask & QSE_HTTPD_MUX_READ); | ||||||
|  | 	QSE_ASSERT (&httpd->urs == urs); | ||||||
|  |  | ||||||
|  | 	return httpd->opt.scb.urs.recv (httpd, urs); | ||||||
|  | } | ||||||
|  |  | ||||||
| static void purge_bad_clients (qse_httpd_t* httpd) | static void purge_bad_clients (qse_httpd_t* httpd) | ||||||
| { | { | ||||||
| 	qse_httpd_client_t* client; | 	qse_httpd_client_t* client; | ||||||
| @ -1450,6 +1483,9 @@ static int dispatch_mux ( | |||||||
|  |  | ||||||
| 		case QSE_HTTPD_DNS: | 		case QSE_HTTPD_DNS: | ||||||
| 			return perform_dns (httpd, mux, handle, mask, cbarg); | 			return perform_dns (httpd, mux, handle, mask, cbarg); | ||||||
|  |  | ||||||
|  | 		case QSE_HTTPD_URS: | ||||||
|  | 			return perform_urs (httpd, mux, handle, mask, cbarg); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	httpd->errnum = QSE_HTTPD_EINTERN; | 	httpd->errnum = QSE_HTTPD_EINTERN; | ||||||
| @ -1501,6 +1537,18 @@ int qse_httpd_loop (qse_httpd_t* httpd) | |||||||
| 	} | 	} | ||||||
| 	else httpd->dnsactive = 1; | 	else httpd->dnsactive = 1; | ||||||
|  |  | ||||||
|  | 	if (activate_urs (httpd) <= -1) | ||||||
|  | 	{ | ||||||
|  | 		if (httpd->opt.trait & QSE_HTTPD_LOGACT) | ||||||
|  | 		{ | ||||||
|  | 			qse_httpd_act_t msg; | ||||||
|  | 			msg.code = QSE_HTTPD_CATCH_MWARNMSG; | ||||||
|  | 			qse_mbscpy (msg.u.mwarnmsg, QSE_MT("cannot activate urs")); | ||||||
|  | 			httpd->opt.rcb.logact (httpd, &msg); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	else httpd->ursactive = 1; | ||||||
|  |  | ||||||
| 	if (activate_servers (httpd) <= -1)  | 	if (activate_servers (httpd) <= -1)  | ||||||
| 	{ | 	{ | ||||||
| 		if (httpd->dnsactive) deactivate_dns (httpd); | 		if (httpd->dnsactive) deactivate_dns (httpd); | ||||||
| @ -1544,6 +1592,7 @@ int qse_httpd_loop (qse_httpd_t* httpd) | |||||||
| 	purge_client_list (httpd); | 	purge_client_list (httpd); | ||||||
| 	deactivate_servers (httpd); | 	deactivate_servers (httpd); | ||||||
|  |  | ||||||
|  | 	if (httpd->ursactive) deactivate_urs (httpd); | ||||||
| 	if (httpd->dnsactive) deactivate_dns (httpd); | 	if (httpd->dnsactive) deactivate_dns (httpd); | ||||||
|  |  | ||||||
| 	httpd->opt.scb.mux.close (httpd, httpd->mux); | 	httpd->opt.scb.mux.close (httpd, httpd->mux); | ||||||
| @ -1684,7 +1733,7 @@ printf ("DNS_SEND.........................\n"); | |||||||
| #if 0 | #if 0 | ||||||
| int qse_httpd_rewriteurl (qse_httpd_t* httpd, const qse_mchar_t* url, qse_httpd_rewrite_t rewrite, void* ctx) | int qse_httpd_rewriteurl (qse_httpd_t* httpd, const qse_mchar_t* url, qse_httpd_rewrite_t rewrite, void* ctx) | ||||||
| { | { | ||||||
| 	return httpd->opt.scb.url.send (httpd, &httpd->dns, name, resol, ctx); | 	return httpd->opt.scb.urs.send (httpd, &httpd->dns, name, resol, ctx); | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | |||||||
| @ -27,7 +27,7 @@ | |||||||
|  |  | ||||||
| #define QSE_HTTPD_DEFAULT_PORT        80 | #define QSE_HTTPD_DEFAULT_PORT        80 | ||||||
| #define QSE_HTTPD_DEFAULT_SECURE_PORT 443 | #define QSE_HTTPD_DEFAULT_SECURE_PORT 443 | ||||||
| #define QSE_HTTPD_DEFAULT_DNS_PORT    53 |  | ||||||
| struct qse_httpd_t | struct qse_httpd_t | ||||||
| { | { | ||||||
| 	qse_mmgr_t* mmgr; | 	qse_mmgr_t* mmgr; | ||||||
| @ -47,6 +47,7 @@ struct qse_httpd_t | |||||||
| 	int stopreq: 1; | 	int stopreq: 1; | ||||||
| 	int impedereq: 1; | 	int impedereq: 1; | ||||||
| 	int dnsactive: 1; | 	int dnsactive: 1; | ||||||
|  | 	int ursactive: 1; | ||||||
|  |  | ||||||
| 	qse_mchar_t sname[128]; /* server name for the server header */ | 	qse_mchar_t sname[128]; /* server name for the server header */ | ||||||
| 	qse_mchar_t gtbuf[10][64]; /* GMT time buffers */ | 	qse_mchar_t gtbuf[10][64]; /* GMT time buffers */ | ||||||
| @ -81,6 +82,7 @@ struct qse_httpd_t | |||||||
|  |  | ||||||
| 	void* mux; | 	void* mux; | ||||||
| 	qse_httpd_dns_t dns; | 	qse_httpd_dns_t dns; | ||||||
|  | 	qse_httpd_urs_t urs; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /* qse_httpd_real_task_t is a private type to hide some private fields | /* qse_httpd_real_task_t is a private type to hide some private fields | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user