From f10f9a569b7f3cc6331d4ce616ea80b7b7a00f60 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 31 Oct 2014 08:24:49 +0000 Subject: [PATCH] touched up httpd debug logging --- qse/cmd/http/httpd.c | 27 +++--------- qse/include/qse/http/httpd.h | 10 +++-- qse/lib/http/httpd-proxy.c | 81 ++++++++++++++++++++++++------------ qse/lib/http/httpd-std-dns.h | 24 +++++++---- qse/lib/http/httpd-std-urs.h | 12 +++--- qse/lib/http/httpd-std.c | 63 ++++++++++------------------ qse/lib/http/httpd.c | 77 +++++++++++++++++++++------------- qse/lib/http/httpd.h | 20 +++++++++ 8 files changed, 175 insertions(+), 139 deletions(-) diff --git a/qse/cmd/http/httpd.c b/qse/cmd/http/httpd.c index bbcaa633..11ec9ea9 100644 --- a/qse/cmd/http/httpd.c +++ b/qse/cmd/http/httpd.c @@ -554,7 +554,6 @@ printf ("intercepted. to be handled locally ...\n"); } } -printf ("qpath ===> [%s]\n",qpath); if ((loccfg->proxy.allow_http && qse_mbszcasecmp (qpath, QSE_MT("http://"), (proto_len = 7)) == 0) || (loccfg->proxy.allow_https && qse_mbszcasecmp (qpath, QSE_MT("https://"), (proto_len = 8)) == 0)) { @@ -908,16 +907,18 @@ found: qse_size_t i; qse_httpd_serverstd_cgi_t* scgi; const qse_mchar_t* xpath_base; +#if 0 qse_mchar_t* qpath; qpath = qse_htre_getqpath(qinfo->req); +#endif scgi = (qse_httpd_serverstd_cgi_t*)result; qse_memset (scgi, 0, QSE_SIZEOF(*scgi)); -#if 1 -printf ("qinfo->xpath ####### [%s] %d [%s]\n", qinfo->xpath, qinfo->xpath_nx, qpath); - //if (qse_mbscmp (qinfo->xpath, QSE_MT("/tmp/version.cgi")) == 0) +#if 0 + printf ("qinfo->xpath [%s] %d [%s]\n", qinfo->xpath, qinfo->xpath_nx, qpath); + /*if (qse_mbscmp (qinfo->xpath, QSE_MT("/tmp/version.cgi")) == 0)*/ if (qse_mbscmp (qpath, QSE_MT("/local/version.cgi")) == 0) { scgi->cgi = 1; @@ -927,8 +928,6 @@ printf ("qinfo->xpath ####### [%s] %d [%s]\n", qinfo->xpath, qinfo->xpath_nx, qp return 0; } #endif - - if (!qinfo->xpath_nx) { xpath_base = qse_mbsbasename (qinfo->xpath); @@ -2489,22 +2488,6 @@ static void logact_httpd (qse_httpd_t* httpd, const qse_httpd_act_t* act) qse_printf (QSE_T("WARNING: %hs\n"), act->u.mwarnmsg); break; - case QSE_HTTPD_CATCH_MDBGMSG: - qse_printf (QSE_T("DEBUG: %hs\n"), act->u.mdbgmsg); - break; - - case QSE_HTTPD_ACCEPT_CLIENT: - qse_nwadtostr (&act->u.client->local_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); - qse_nwadtostr (&act->u.client->orgdst_addr, tmp2, QSE_COUNTOF(tmp2), QSE_NWADTOSTR_ALL); - qse_nwadtostr (&act->u.client->remote_addr, tmp3, QSE_COUNTOF(tmp3), QSE_NWADTOSTR_ALL); - qse_printf (QSE_T("accepted client %s(%s) from %s\n"), tmp, tmp2, tmp3); - break; - - case QSE_HTTPD_PURGE_CLIENT: - qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); - qse_printf (QSE_T("purged client - %s\n"), tmp); - break; - case QSE_HTTPD_READERR_CLIENT: qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); qse_printf (QSE_T("failed to read client - %s\n"), tmp); diff --git a/qse/include/qse/http/httpd.h b/qse/include/qse/http/httpd.h index 98135938..4a7e77b0 100644 --- a/qse/include/qse/http/httpd.h +++ b/qse/include/qse/http/httpd.h @@ -73,10 +73,17 @@ typedef enum qse_httpd_errnum_t qse_httpd_errnum_t; enum qse_httpd_opt_t { QSE_HTTPD_TRAIT, + + /** Default multiplexer timeout. */ QSE_HTTPD_TMOUT, + + /** Idle limit in #qse_ntime_t. + * The limit is enabled only if the value is greater than 0.0 */ QSE_HTTPD_IDLELIMIT, + QSE_HTTPD_MODPREFIX, QSE_HTTPD_MODPOSTFIX, + QSE_HTTPD_SCB, QSE_HTTPD_RCB }; @@ -489,9 +496,6 @@ enum qse_httpd_act_code_t { QSE_HTTPD_CATCH_MERRMSG, QSE_HTTPD_CATCH_MWARNMSG, - QSE_HTTPD_CATCH_MDBGMSG, - QSE_HTTPD_ACCEPT_CLIENT, - QSE_HTTPD_PURGE_CLIENT, QSE_HTTPD_READERR_CLIENT }; typedef enum qse_httpd_act_code_t qse_httpd_act_code_t; diff --git a/qse/lib/http/httpd-proxy.c b/qse/lib/http/httpd-proxy.c index fd0d562c..87cb618c 100644 --- a/qse/lib/http/httpd-proxy.c +++ b/qse/lib/http/httpd-proxy.c @@ -134,6 +134,7 @@ struct proxy_peer_htrd_xtn_t static void log_proxy_error (task_proxy_t* proxy, const qse_mchar_t* shortmsg) { +/* TODO: change this to HTTPD_DBGOUTXXXX */ qse_httpd_act_t msg; qse_size_t pos = 0; @@ -1350,7 +1351,16 @@ static void task_fini_proxy ( task_proxy_t* proxy = (task_proxy_t*)task->ctx; if (proxy->peer_status & PROXY_PEER_OPEN) + { + #if defined(QSE_HTTPD_DEBUG) + { + qse_mchar_t tmp[128]; + qse_nwadtombs (&proxy->peer.nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT2 ("Closing peer [%hs] - %zd\n", tmp, (qse_size_t)proxy->peer.handle); + } + #endif httpd->opt.scb.peer.close (httpd, &proxy->peer); + } if ((proxy->flags & (PROXY_UPGRADE_REQUESTED | PROXY_PROTOCOL_SWITCHED)) == PROXY_UPGRADE_REQUESTED) { @@ -1883,8 +1893,13 @@ static int task_main_proxy_1 ( else if (httpd->errnum == QSE_HTTPD_EACCES || httpd->errnum == QSE_HTTPD_ECONN) http_errnum = 403; - if (httpd->opt.trait & QSE_HTTPD_LOGACT) - log_proxy_error (proxy, "proxy peer connect error - "); + #if defined(QSE_HTTPD_DEBUG) + { + qse_mchar_t tmp[128]; + qse_nwadtombs (&proxy->peer.nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT1 ("Cannnot connect to peer [%hs]\n", tmp); + } + #endif goto oops; } @@ -1985,15 +2000,14 @@ static void on_peer_name_resolved (qse_httpd_t* httpd, const qse_mchar_t* name, } -{ -if (proxy->flags & PROXY_PEER_NAME_RESOLVED) -{ -qse_mchar_t xxxx[128]; -qse_nwadtombs (&proxy->peer.nwad, xxxx, 128, QSE_NWADTOMBS_ALL); -printf ("XXXXXXXXXXXXXXXXXXXXXXXXXX [%s] PEER NAME RESOLVED.....TO [%s]\n", name, xxxx); -} -} - +#if defined(QSE_HTTPD_DEBUG) + if (proxy->flags & PROXY_PEER_NAME_RESOLVED) + { + qse_mchar_t tmp[128]; + qse_nwadtombs (&proxy->peer.nwad, tmp, 128, QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT2 ("Peer name [%hs] resolved to [%hs]\n", name, tmp); + } +#endif } static void on_url_rewritten (qse_httpd_t* httpd, const qse_mchar_t* url, const qse_mchar_t* new_url, void* ctx) @@ -2007,7 +2021,7 @@ static void on_url_rewritten (qse_httpd_t* httpd, const qse_mchar_t* url, const proxy->flags &= ~(PROXY_REWRITE_URL | PROXY_URL_REWRITING); -printf ("XXXXXXXXXXXXXXXXXXXXXXXXXX URL REWRITTEN TO [%s].....\n", new_url); + HTTPD_DBGOUT2 ("URL rewritten from [%hs] to [%hs]\n", url, new_url); if (new_url[0] == QSE_MT('\0')) { /* no change. carry on */ @@ -2144,11 +2158,18 @@ printf ("XXXXXXXXXXXXXXXXXXXXXXXXXX URL REWRITTEN TO [%s].....\n", new_url); proxy->peer.nwad = nwad; proxy->flags |= PROXY_URL_REWRITTEN; proxy->flags &= ~PROXY_RESOLVE_PEER_NAME; /* skip dns */ -{ -qse_mchar_t xxxx[128]; -qse_nwadtombs (&proxy->peer.nwad, xxxx, 128, QSE_NWADTOMBS_ALL); -printf ("XXXXXXXXXXXXXXXXXXXXXXXXXX PEER NAME RESOLVED.....TO [%s] IN URLREWRITING NEW_URL[%s] %d %d\n", xxxx, new_url, (int)proxy->qpath_pos_in_reqfwdbuf, (int)proxy->qpath_len_in_reqfwdbuf); -} + + #if defined(QSE_HTTPD_DEBUG) + { + qse_mchar_t tmp[128]; + qse_nwadtombs (&proxy->peer.nwad, tmp, 128, QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT4 ("Peer name resolved to [%hs] in url rewriting. new_url [%hs] %d %d\n", + tmp, new_url, + (int)proxy->qpath_pos_in_reqfwdbuf, + (int)proxy->qpath_len_in_reqfwdbuf + ); + } + #endif /* the temporary string is not used. kill it */ qse_httpd_freemem (httpd, tmp); @@ -2177,8 +2198,6 @@ printf ("XXXXXXXXXXXXXXXXXXXXXXXXXX PEER NAME RESOLVED.....TO [%s] IN URLREWRITI { proxy->flags |= PROXY_INIT_FAILED; } - -printf ("XXXXXXXXXXXXXXXXXXXXXXXXXX URL REWRITTEN ....\n"); } static int task_main_proxy ( @@ -2309,12 +2328,6 @@ static int task_main_proxy ( proxy->res_consumed = 0; proxy->res_pending = 0; - -{ -char buf[100]; -qse_nwadtombs (&proxy->peer.nwad, buf, QSE_COUNTOF(buf), QSE_NWADTOMBS_ALL); -printf ("OPENDING PEER: [%s]\n", buf); -} httpd->errnum = QSE_HTTPD_ENOERR; n = httpd->opt.scb.peer.open (httpd, &proxy->peer); if (n <= -1) @@ -2324,11 +2337,25 @@ printf ("OPENDING PEER: [%s]\n", buf); else if (httpd->errnum == QSE_HTTPD_EACCES || httpd->errnum == QSE_HTTPD_ECONN) http_errnum = 403; - if (httpd->opt.trait & QSE_HTTPD_LOGACT) - log_proxy_error (proxy, "proxy peer initial connect error - "); + #if defined(QSE_HTTPD_DEBUG) + { + qse_mchar_t tmp[128]; + qse_nwadtombs (&proxy->peer.nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT1 ("Cannnot open peer [%hs]\n", tmp); + } + #endif + goto oops; } +#if defined(QSE_HTTPD_DEBUG) + { + qse_mchar_t tmp[128]; + qse_nwadtombs (&proxy->peer.nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT2 ("Opened peer [%hs] - %zd\n", tmp, (qse_size_t)proxy->peer.handle); + } +#endif + proxy->peer_status |= PROXY_PEER_OPEN; task->trigger.v[0].mask = QSE_HTTPD_TASK_TRIGGER_READ; task->trigger.v[0].handle = proxy->peer.handle; diff --git a/qse/lib/http/httpd-std-dns.h b/qse/lib/http/httpd-std-dns.h index fdecd3b6..7a666cff 100644 --- a/qse/lib/http/httpd-std-dns.h +++ b/qse/lib/http/httpd-std-dns.h @@ -354,15 +354,13 @@ static int dns_open (qse_httpd_t* httpd, qse_httpd_dns_t* dns) if (nwad.type != QSE_NWAD_NX && qse_getnwadport(&nwad) == 0) qse_setnwadport (&nwad, qse_hton16(QSE_HTTPD_DNSSTD_DEFAULT_PORT)); - if (httpd->opt.trait & QSE_HTTPD_LOGACT) +#if defined(QSE_HTTPD_DEBUG) { - qse_httpd_act_t msg; - qse_size_t pos; - msg.code = QSE_HTTPD_CATCH_MDBGMSG; - pos = qse_mbsxcpy (msg.u.mdbgmsg, QSE_COUNTOF(msg.u.mdbgmsg), "default nameserver set to "); - qse_nwadtombs (&nwad, &msg.u.mdbgmsg[pos], QSE_COUNTOF(msg.u.mdbgmsg) - pos, QSE_NWADTOMBS_ALL); - httpd->opt.rcb.logact (httpd, &msg); + qse_mchar_t tmp[128]; + qse_nwadtombs (&nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT1 ("Default DNS server set to [%hs]\n", tmp); } +#endif dns->handle[0] = open_client_socket (httpd, AF_INET, SOCK_DGRAM, 0); #if defined(AF_INET6) @@ -623,13 +621,21 @@ static int dns_recv (qse_httpd_t* httpd, qse_httpd_dns_t* dns, qse_httpd_hnd_t h qse_nwad_t* resolved_nwad = QSE_NULL; int cache_ttl = 0; -printf ("DNS_RECV....\n"); - httpd_xtn = qse_httpd_getxtn (httpd); fromlen = QSE_SIZEOF(fromaddr); len = recvfrom (handle, buf, QSE_SIZEOF(buf), 0, (struct sockaddr*)&fromaddr, &fromlen); +#if defined(QSE_HTTPD_DEBUG) + { + qse_nwad_t tmpnwad; + qse_mchar_t tmp[128]; + qse_skadtonwad (&fromaddr, &tmpnwad); + + qse_nwadtombs (&tmpnwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT2 ("Received DNS response of length %d from %s\n", (int)len, tmp); + } +#endif if (len < QSE_SIZEOF(*hdr)) goto done; /* packet too small */ hdr = (dns_hdr_t*)buf; diff --git a/qse/lib/http/httpd-std-urs.h b/qse/lib/http/httpd-std-urs.h index 4aec21cb..35880e76 100644 --- a/qse/lib/http/httpd-std-urs.h +++ b/qse/lib/http/httpd-std-urs.h @@ -113,15 +113,13 @@ static int urs_open (qse_httpd_t* httpd, qse_httpd_urs_t* urs) if (nwad.type != QSE_NWAD_NX && qse_getnwadport(&nwad) == 0) qse_setnwadport (&nwad, qse_hton16(QSE_HTTPD_URSSTD_DEFAULT_PORT)); - if (httpd->opt.trait & QSE_HTTPD_LOGACT) +#if defined(QSE_HTTPD_DEBUG) { - qse_httpd_act_t msg; - qse_size_t pos; - msg.code = QSE_HTTPD_CATCH_MDBGMSG; - pos = qse_mbsxcpy (msg.u.mdbgmsg, QSE_COUNTOF(msg.u.mdbgmsg), "default ursserver set to "); - qse_nwadtombs (&nwad, &msg.u.mdbgmsg[pos], QSE_COUNTOF(msg.u.mdbgmsg) - pos, QSE_NWADTOMBS_ALL); - httpd->opt.rcb.logact (httpd, &msg); + qse_mchar_t tmp[128]; + qse_nwadtombs (&nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOMBS_ALL); + HTTPD_DBGOUT1 ("Default URS server set to [%hs]\n", tmp); } +#endif #if defined(IPPROTO_SCTP) type = (proto == IPPROTO_SCTP)? SOCK_SEQPACKET: SOCK_DGRAM; diff --git a/qse/lib/http/httpd-std.c b/qse/lib/http/httpd-std.c index 0097c7de..8cfa4fb8 100644 --- a/qse/lib/http/httpd-std.c +++ b/qse/lib/http/httpd-std.c @@ -1052,7 +1052,7 @@ static int server_accept ( /* TODO: implement maximum number of client per server??? */ if (fd >= FD_SETSIZE) { -printf ("ERROR: too many client?\n"); + HTTPD_DEBUG ("ERROR: too many client - max %d, fd %d\n", (FD_SETSIZE, fd)); /*TODO: qse_httpd_seterrnum (httpd, QSE_HTTPD_EXXXXX);*/ goto oops; } @@ -1870,16 +1870,8 @@ static int file_ropen ( if (fio == QSE_NULL) return -1; *handle = FIO_TO_HANDLE(fio); - if (httpd->opt.trait & QSE_HTTPD_LOGACT) - { - qse_httpd_act_t msg; - qse_size_t pos; - msg.code = QSE_HTTPD_CATCH_MDBGMSG; - pos = qse_mbscpy (msg.u.mdbgmsg, QSE_MT("ropened file ")); - qse_mbsxcpy (&msg.u.mdbgmsg[pos], QSE_COUNTOF(msg.u.mdbgmsg) - pos, path); - httpd->opt.rcb.logact (httpd, &msg); - } + HTTPD_DBGOUT2 ("Opened file [%hs] for reading - %zd\n", path, (qse_size_t)*handle); return 0; } @@ -1893,21 +1885,15 @@ static int file_wopen ( if (fio == QSE_NULL) return -1; *handle = FIO_TO_HANDLE(fio); - if (httpd->opt.trait & QSE_HTTPD_LOGACT) - { - qse_httpd_act_t msg; - qse_size_t pos; - msg.code = QSE_HTTPD_CATCH_MDBGMSG; - pos = qse_mbscpy (msg.u.mdbgmsg, QSE_MT("wopened file ")); - qse_mbsxcpy (&msg.u.mdbgmsg[pos], QSE_COUNTOF(msg.u.mdbgmsg) - pos, path); - httpd->opt.rcb.logact (httpd, &msg); - } + HTTPD_DBGOUT2 ("Opened file [%hs] for writing - %zd\n", path, (qse_size_t)*handle); return 0; } static void file_close (qse_httpd_t* httpd, qse_httpd_hnd_t handle) { + HTTPD_DBGOUT1 ("Closed file %zd\n", (qse_size_t)handle); + qse_fio_fini (HANDLE_TO_FIO(handle)); qse_httpd_freemem (httpd, HANDLE_TO_FIO(handle)); } @@ -2073,17 +2059,9 @@ static int dir_open (qse_httpd_t* httpd, const qse_mchar_t* path, qse_httpd_hnd_ return -1; } - if (httpd->opt.trait & QSE_HTTPD_LOGACT) - { - qse_httpd_act_t msg; - qse_size_t pos; - msg.code = QSE_HTTPD_CATCH_MDBGMSG; - pos = qse_mbscpy (msg.u.mdbgmsg, QSE_MT("opened dir ")); - qse_mbsxcpy (&msg.u.mdbgmsg[pos], QSE_COUNTOF(msg.u.mdbgmsg) - pos, path); - httpd->opt.rcb.logact (httpd, &msg); - } - *handle = DIR_TO_HANDLE(d); + + HTTPD_DBGOUT2 ("Opened directory [%hs] - %zd\n", path, (qse_size_t)*handle); return 0; } @@ -2091,6 +2069,8 @@ static void dir_close (qse_httpd_t* httpd, qse_httpd_hnd_t handle) { dir_t* d; + HTTPD_DBGOUT1 ("Closing directory %zd\n", (qse_size_t)handle); + d = HANDLE_TO_DIR(handle); qse_dir_close (d->dp); @@ -2328,7 +2308,8 @@ static qse_htb_walk_t walk (qse_htb_t* htb, qse_htb_pair_t* pair, void* ctx) val = QSE_HTB_VPTR(pair); while (val) { -qse_printf (QSE_T("HEADER OK %d[%hs] %d[%hs]\n"), (int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair), (int)val->len, val->ptr); + + HTTPD_DEBUG ((QSE_T("HEADER OK %d[%hs] %d[%hs]\n"), (int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair), (int)val->len, val->ptr)); val = val->next; } return QSE_HTB_WALK_FORWARD; @@ -2350,18 +2331,16 @@ static int process_request ( * any more. once it's decoded in the peek mode, * the decoded query path is made available in the * non-peek mode as well */ - if (peek) qse_htre_perdecqpath(req); - - if (peek && (httpd->opt.trait & QSE_HTTPD_LOGACT)) + if (peek) { -/* TODO: improve logging */ - qse_httpd_act_t msg; - msg.code = QSE_HTTPD_CATCH_MDBGMSG; - qse_mbsxfmt (msg.u.mdbgmsg, QSE_COUNTOF(msg.u.mdbgmsg), - QSE_MT("%s %s"), qse_htre_getqmethodname(req), qse_htre_getqpath(req)); - httpd->opt.rcb.logact (httpd, &msg); + qse_htre_perdecqpath(req); + + /* TODO: proper request logging */ + + HTTPD_DBGOUT2 ("%s %s\n", qse_htre_getqmethodname(req), qse_htre_getqpath(req)); } + #if 0 qse_printf (QSE_T("================================\n")); qse_printf (QSE_T("[%lu] %hs REQUEST ==> [%hs] version[%d.%d %hs] method[%hs]\n"), @@ -2472,7 +2451,8 @@ if (qse_htre_getcontentlen(req) > 0) if (mth == QSE_HTTP_CONNECT) { -printf ("SWITCHING HTRD TO DUMMY.... %s\n", qse_htre_getqpath(req)); + HTTPD_DBGOUT1 ("Switching HTRD to DUMMY for [%hs]\n", qse_htre_getqpath(req)); + /* Switch the http read to a dummy mode so that the subsqeuent * input(request) is just treated as data to the request just * completed */ @@ -2480,7 +2460,8 @@ printf ("SWITCHING HTRD TO DUMMY.... %s\n", qse_htre_getqpath(req)); if (server_xtn->makersrc (httpd, client, req, &rsrc) <= -1) { -printf ("CANOT MAKE RESOURCE.... %s\n", qse_htre_getqpath(req)); + HTTPD_DBGOUT1 ("Cannot make resource for [%hs]\n", qse_htre_getqpath(req)); + /* failed to make a resource. just send the internal server error. * the makersrc handler can return a negative number to return * '500 Internal Server Error'. If it wants to return a specific diff --git a/qse/lib/http/httpd.c b/qse/lib/http/httpd.c index 9ee5d35b..ebeb8253 100644 --- a/qse/lib/http/httpd.c +++ b/qse/lib/http/httpd.c @@ -478,13 +478,18 @@ static qse_httpd_client_t* new_client (qse_httpd_t* httpd, qse_httpd_client_t* t goto oops; } - qse_gettime (&idle_event.when); - qse_addtime (&idle_event.when, &httpd->opt.idle_limit, &idle_event.when); - idle_event.ctx = client; - idle_event.handler = tmr_idle_handle; - idle_event.updater = tmr_idle_update; + if (httpd->opt.idle_limit.sec > 0 || + (httpd->opt.idle_limit.sec == 0 && httpd->opt.idle_limit.nsec > 0)) + { + /* idle limit is enabled when the limit is greater than 0.0 */ + qse_gettime (&idle_event.when); + qse_addtime (&idle_event.when, &httpd->opt.idle_limit, &idle_event.when); + idle_event.ctx = client; + idle_event.handler = tmr_idle_handle; + idle_event.updater = tmr_idle_update; - if (qse_httpd_inserttimerevent (httpd, &idle_event, &client->tmr_idle) <= -1) goto oops; + if (qse_httpd_inserttimerevent (httpd, &idle_event, &client->tmr_idle) <= -1) goto oops; + } qse_htrd_setoption (client->htrd, QSE_HTRD_REQUEST | QSE_HTRD_TRAILERS | QSE_HTRD_CANONQPATH); @@ -515,7 +520,7 @@ oops: if (client->tmr_idle != QSE_TMR_INVALID_INDEX) { qse_httpd_removetimerevent (httpd, client->tmr_idle); - /* cleint->tmr_idle = QSE_TMR_INVALID_INDEX; */ + client->tmr_idle = QSE_TMR_INVALID_INDEX; } if (client->htrd) qse_htrd_close (client->htrd); qse_httpd_freemem (httpd, client); @@ -533,8 +538,12 @@ static void free_client ( qse_htrd_close (client->htrd); -#if 0 -qse_printf (QSE_T("Debug: CLOSING SOCKET %d\n"), client->handle.i); +#if defined(QSE_HTTPD_DEBUG) + { + qse_mchar_t tmp[128]; + qse_nwadtombs (&client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); + HTTPD_DBGOUT2 ("Closing client [%hs] - %zd\n", tmp, (qse_size_t)client->handle); + } #endif if (client->status & QSE_HTTPD_CLIENT_HANDLE_RW_IN_MUX) @@ -567,13 +576,13 @@ static void purge_client (qse_httpd_t* httpd, qse_httpd_client_t* client) prev = client->prev; next = client->next; - if (httpd->opt.trait & QSE_HTTPD_LOGACT) +#if defined(QSE_HTTPD_DEBUG) { - qse_httpd_act_t msg; - msg.code = QSE_HTTPD_PURGE_CLIENT; - msg.u.client = client; - httpd->opt.rcb.logact (httpd, &msg); + qse_mchar_t tmp[128]; + qse_nwadtombs (&client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); + HTTPD_DBGOUT2 ("Purged client [%hs] - %zd\n", tmp, (qse_size_t)client->handle); } +#endif free_client (httpd, client); @@ -645,12 +654,11 @@ static int accept_client ( if (httpd->opt.scb.server.accept (httpd, server, &clibuf) <= -1) { -#if 1 -qse_mchar_t tmp[128]; -qse_nwadtombs (&server->dope.nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); -printf ("failed to accept from server [%s] [%d]\n", tmp, server->handle); -#endif - + #if QSE_HTTPD_DEBUG + qse_mchar_t tmp[128]; + qse_nwadtombs (&server->dope.nwad, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); + HTTPD_DBGOUT2 ("Failed to accept from server [%hs] [%d]\n", tmp, (int)server->handle); + #endif return -1; } @@ -697,19 +705,20 @@ printf ("failed to accept from server [%s] [%d]\n", tmp, server->handle); } httpd->client.list.count++; - if (httpd->opt.trait & QSE_HTTPD_LOGACT) + #if defined(QSE_HTTPD_DEBUG) { - qse_httpd_act_t msg; - msg.code = QSE_HTTPD_ACCEPT_CLIENT; - msg.u.client = client; - httpd->opt.rcb.logact (httpd, &msg); + qse_mchar_t tmp1[128], tmp2[128], tmp3[128]; + qse_nwadtombs (&client->local_addr, tmp1, QSE_COUNTOF(tmp1), QSE_NWADTOSTR_ALL); + qse_nwadtombs (&client->orgdst_addr, tmp2, QSE_COUNTOF(tmp2), QSE_NWADTOSTR_ALL); + qse_nwadtombs (&client->remote_addr, tmp3, QSE_COUNTOF(tmp3), QSE_NWADTOSTR_ALL); + HTTPD_DBGOUT3 ("Accepted client %hs(%hs) from %hs\n", tmp1, tmp2, tmp3); } + #endif } return 0; } - static void tmr_idle_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, void* ctx) { qse_httpd_client_t* client = (qse_httpd_client_t*)ctx; @@ -728,6 +737,7 @@ static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx) if (qse_cmptime(&diff, &client->server->httpd->opt.idle_limit) >= 0) { /* this client is idle */ + HTTPD_DBGOUT1 ("Purging idle client %d", (int)client->handle); purge_client (client->server->httpd, client); } else @@ -746,7 +756,10 @@ static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx) /* the timer must have been deleted when this callback is called. */ QSE_ASSERT (client->tmr_idle == QSE_TMR_INVALID_INDEX); if (qse_httpd_inserttimerevent (client->server->httpd, &idle_event, &client->tmr_idle) <= -1) + { + HTTPD_DBGOUT1 ("Cannot update idle timer for client %d. Marking it bad", (int)client->handle); mark_bad_client (client); + } } } } @@ -1695,6 +1708,8 @@ int qse_httpd_loop (qse_httpd_t* httpd) qse_mbscpy (msg.u.mwarnmsg, QSE_MT("cannot activate dns")); httpd->opt.rcb.logact (httpd, &msg); } + + HTTPD_DBGOUT0 ("Failed to activate DNS\n"); } else httpd->dnsactive = 1; @@ -1707,6 +1722,7 @@ int qse_httpd_loop (qse_httpd_t* httpd) qse_mbscpy (msg.u.mwarnmsg, QSE_MT("cannot activate urs")); httpd->opt.rcb.logact (httpd, &msg); } + HTTPD_DBGOUT0 ("Failed to activate URS\n"); } else httpd->ursactive = 1; @@ -1719,7 +1735,8 @@ int qse_httpd_loop (qse_httpd_t* httpd) } if (httpd->server.nactive <= 0) { -printf ("no active servers...\n"); + HTTPD_DBGOUT0 ("No servers are active. aborting\n"); + if (httpd->dnsactive) deactivate_dns (httpd); if (httpd->ursactive) deactivate_urs (httpd); httpd->opt.scb.mux.close (httpd, httpd->mux); @@ -1739,10 +1756,9 @@ printf ("no active servers...\n"); { if (qse_tmr_gettmout (httpd->tmr, QSE_NULL, &tmout) <= -1) tmout = httpd->opt.tmout; count = httpd->opt.scb.mux.poll (httpd, httpd->mux, &tmout); -printf ("polled ... ...%d client count = %d tmout = %d.%d\n", (int)count, (int)httpd->client.list.count, (int)tmout.sec, (int)tmout.nsec); + /*HTTPD_DBGOUT4 ("Multiplexer returned %d client count = %d tmout = %d.%d\n", (int)count, (int)httpd->client.list.count, (int)tmout.sec, (int)tmout.nsec);*/ if (count <= -1) { -printf ("mux errorr ... ...\n"); if (httpd->errnum != QSE_HTTPD_EINTR) { xret = -1; @@ -1893,13 +1909,13 @@ qse_mchar_t* qse_httpd_escapehtml (qse_httpd_t* httpd, const qse_mchar_t* str) int qse_httpd_resolvename (qse_httpd_t* httpd, const qse_mchar_t* name, qse_httpd_resolve_t resol, const qse_httpd_dns_server_t* dns_server, void* ctx) { -printf ("DNS_SEND.........................\n"); if (!httpd->dnsactive) { qse_httpd_seterrnum (httpd, QSE_HTTPD_ENODNS); return -1; } + HTTPD_DBGOUT1 ("Sending DNS request [%hs]\n", name); return httpd->opt.scb.dns.send (httpd, &httpd->dns, name, resol, dns_server, ctx); } @@ -1911,6 +1927,7 @@ int qse_httpd_rewriteurl (qse_httpd_t* httpd, const qse_mchar_t* url, qse_httpd_ return -1; } + HTTPD_DBGOUT1 ("Sending URS request [%hs]\n", url); return httpd->opt.scb.urs.send (httpd, &httpd->urs, url, rewrite, urs_server, ctx); } diff --git a/qse/lib/http/httpd.h b/qse/lib/http/httpd.h index 357a6707..4cafc340 100644 --- a/qse/lib/http/httpd.h +++ b/qse/lib/http/httpd.h @@ -24,10 +24,30 @@ /* private header file for httpd */ #include +#include #define QSE_HTTPD_DEFAULT_PORT 80 #define QSE_HTTPD_DEFAULT_SECURE_PORT 443 +#define QSE_HTTPD_DEBUG 1 +/*#undef QSE_HTTPD_DEBUG*/ + +#if defined(QSE_HTTPD_DEBUG) +# define HTTPD_DBGOUT0(fmt) qse_putmbsf("%hs:%d: " fmt, __FILE__, (int)__LINE__) +# define HTTPD_DBGOUT1(fmt,a1) qse_putmbsf("%hs:%d: " fmt, __FILE__, (int)__LINE__, (a1)) +# define HTTPD_DBGOUT2(fmt,a1,a2) qse_putmbsf("%hs:%d: " fmt, __FILE__, (int)__LINE__, (a1), (a2)) +# define HTTPD_DBGOUT3(fmt,a1,a2,a3) qse_putmbsf("%hs:%d: " fmt, __FILE__, (int)__LINE__, (a1), (a2), (a3)) +# define HTTPD_DBGOUT4(fmt,a1,a2,a3,a4) qse_putmbsf("%hs:%d: " fmt, __FILE__, (int)__LINE__, (a1), (a2), (a3), (a4)) +# define HTTPD_DBGOUT5(fmt,a1,a2,a3,a4,a5) qse_putmbsf("%hs:%d: " fmt, __FILE__, (int)__LINE__, (a1), (a2), (a3), (a4), (a5)) +#else +# define HTTPD_DBGOUT0(fmt) +# define HTTPD_DBGOUT1(fmt,a1) +# define HTTPD_DBGOUT2(fmt,a1,a2) +# define HTTPD_DBGOUT3(fmt,a1,a2,a3) +# define HTTPD_DBGOUT4(fmt,a1,a2,a3,a4) +# define HTTPD_DBGOUT5(fmt,a1,a2,a3,a4,a5) +#endif + struct qse_httpd_t { qse_mmgr_t* mmgr;