void check_clients() { int i, connfd; char buf[MAXLINE]; rio_t rio; for (i = 0; (i <= p.maxi) && (p.nready > 0); i++) { connfd = p.clientfd[i]; rio = p.clientrio[i]; if ((connfd > 0) && FD_ISSET(connfd, &p.ready_set)){ p.nready--; if (Rio_readlineb(&rio, buf, MAXLINE) > 0) { get_msg(buf,buf); request_check(i, buf); while( rio.rio_cnt > 0 ){ Rio_readlineb(&rio,buf,MAXLINE); get_msg(buf,buf); request_check(i,buf); } } else{/* EOF detected. User Exit */ //debug char buf[MAX_MSG_LEN]; show_user(buf,MAX_MSG_LEN,user_table[connfd]); printf("!!Exit%s\n", buf); /* do quit command for user at connfd[i] */ quit_command(i); } //debug debug_list_all_user(); debug_list_all_channel(); printf("===================================\n"); } } }
static void http_io(IOCHAN i, int event) { struct http_channel *hc = iochan_getdata(i); while (event) { if (event == EVENT_INPUT) { int res, reqlen; struct http_buf *htbuf; htbuf = http_buf_create(hc->http_server); res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0); if (res == -1 && errno == EAGAIN) { http_buf_destroy(hc->http_server, htbuf); return; } if (res <= 0) { #if HAVE_SYS_TIME_H if (hc->http_server->record_file) { struct timeval tv; gettimeofday(&tv, 0); fprintf(hc->http_server->record_file, "%lld %lld %lld 0\n", (long long) tv.tv_sec, (long long) tv.tv_usec, (long long) iochan_getfd(i)); } #endif http_buf_destroy(hc->http_server, htbuf); fflush(hc->http_server->record_file); http_channel_destroy(i); return; } htbuf->buf[res] = '\0'; htbuf->len = res; http_buf_enqueue(&hc->iqueue, htbuf); while (1) { if (hc->state == Http_Busy) return; reqlen = request_check(hc->iqueue); if (reqlen <= 2) return; // we have a complete HTTP request nmem_reset(hc->nmem); #if HAVE_SYS_TIME_H if (hc->http_server->record_file) { struct timeval tv; int sz = 0; struct http_buf *hb; for (hb = hc->iqueue; hb; hb = hb->next) sz += hb->len; gettimeofday(&tv, 0); fprintf(hc->http_server->record_file, "%lld %lld %lld %d\n", (long long) tv.tv_sec, (long long) tv.tv_usec, (long long) iochan_getfd(i), sz); for (hb = hc->iqueue; hb; hb = hb->next) fwrite(hb->buf, 1, hb->len, hc->http_server->record_file); } #endif if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen))) { yaz_log(YLOG_WARN, "Failed to parse request"); http_error(hc, 400, "Bad Request"); return; } hc->response = 0; yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method, hc->request->path, *hc->request->search ? "?" : "", hc->request->search); if (hc->request->content_buf) yaz_log(YLOG_LOG, "%s", hc->request->content_buf); if (http_weshouldproxy(hc->request)) http_proxy(hc->request); else { // Execute our business logic! hc->state = Http_Busy; http_command(hc); } } } else if (event == EVENT_OUTPUT) { event = 0; if (hc->oqueue) { struct http_buf *wb = hc->oqueue; int res; res = send(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len, 0); if (res <= 0) { yaz_log(YLOG_WARN|YLOG_ERRNO, "write"); http_channel_destroy(i); return; } if (res == wb->len) { hc->oqueue = hc->oqueue->next; http_buf_destroy(hc->http_server, wb); } else { wb->len -= res; wb->offset += res; } if (!hc->oqueue) { if (!hc->keep_alive) { http_channel_destroy(i); return; } else { iochan_clearflag(i, EVENT_OUTPUT); if (hc->iqueue) event = EVENT_INPUT; } } } if (!hc->oqueue && hc->proxy && !hc->proxy->iochan) http_channel_destroy(i); // Server closed; we're done } else { yaz_log(YLOG_WARN, "Unexpected event on connection"); http_channel_destroy(i); event = 0; } } }