set socket to nonblock

Signed-off-by liudengfeng@kunteng.org
This commit is contained in:
liudf0716 2018-05-25 11:43:21 +08:00
parent ce22e49845
commit 98f2655376
5 changed files with 37 additions and 23 deletions

View File

@ -414,9 +414,11 @@ httpdReadRequest(httpd * server, request * r)
if (strcasecmp(cp, "POST") == 0)
r->request.method = HTTP_POST;
if (r->request.method == 0) {
#if !defined(_EPOLL_MODE_)
_httpd_net_write(r->clientSock, HTTP_METHOD_ERROR, strlen(HTTP_METHOD_ERROR));
_httpd_net_write(r->clientSock, cp, strlen(cp));
_httpd_writeErrorLog(server, r, LEVEL_ERROR, "Invalid method received");
#endif
return (-1);
}
cp = cp2 + 1;
@ -762,7 +764,8 @@ httpdOutputLengthDirect(request *r, const char *msg, int msg_len)
r->response.responseLength += msg_len;
if (r->response.headersSent == 0)
_httpd_sendHeaders(r, msg_len, 0);
_httpd_net_write(r->clientSock, msg, msg_len);
if (r->retcode >= 0)
r->retcode = _httpd_net_write(r->clientSock, msg, msg_len);
}
void
@ -817,7 +820,8 @@ httpdOutput(request * r, const char *msg)
r->response.responseLength += strlen(buf);
if (r->response.headersSent == 0)
httpdSendHeaders(r);
_httpd_net_write(r->clientSock, buf, strlen(buf));
if (r->retcode >= 0)
r->retcode = _httpd_net_write(r->clientSock, buf, strlen(buf));
}
void
@ -833,7 +837,8 @@ httpdPrintf(request * r, const char *fmt, ...)
vsnprintf(buf, HTTP_MAX_LEN, fmt, args);
va_end(args); /* Works with both stdargs.h and varargs.h */
r->response.responseLength += strlen(buf);
_httpd_net_write(r->clientSock, buf, strlen(buf));
if (r->retcode >= 0)
r->retcode = _httpd_net_write(r->clientSock, buf, strlen(buf));
}
void

View File

@ -146,7 +146,7 @@ extern "C" {
} httpd;
typedef struct {
int clientSock, readBufRemain;
int clientSock, readBufRemain, retcode;
httpReq request;
httpRes response;
httpVar *variables;

View File

@ -105,7 +105,10 @@ char *buf;
int len;
{
#if defined(_EPOLL_MODE_)
return send(sock, buf, len, MSG_NOSIGNAL);
int nret = send(sock, buf, len, MSG_NOSIGNAL);
if (nret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
return 0;
return nret == 0?-1:nret;
#else
// liudf modified 20160302
int nfds;
@ -161,7 +164,7 @@ _httpd_readChar(request * r, char *cp)
if (r->readBufRemain == 0) {
bzero(r->readBuf, HTTP_READ_BUF_LEN + 1);
r->readBufRemain = _httpd_net_read(r->clientSock, r->readBuf, HTTP_READ_BUF_LEN);
#if define(_EPOLL_MODE_)
#if defined(_EPOLL_MODE_)
if (0 == r->readBufRemain || (r->readBufRemain < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
return (0);
}
@ -511,7 +514,9 @@ _httpd_sendHeaders(request * r, int contentLength, int modTime)
totalLength += nret;
nret = snprintf(hdrBuf+totalLength, HTTP_READ_BUF_LEN-totalLength, "\r\n");
totalLength += nret;
_httpd_net_write(r->clientSock, hdrBuf, totalLength);
if (r->retcode >= 0) {
r->retcode = _httpd_net_write(r->clientSock, hdrBuf, totalLength);
}
}
httpDir *
@ -633,7 +638,9 @@ _httpd_catFile(request * r, const char *path)
len = read(fd, buf, HTTP_MAX_LEN);
while (len > 0) {
r->response.responseLength += len;
_httpd_net_write(r->clientSock, buf, len);
if (r->retcode >= 0) {
r->retcode = _httpd_net_write(r->clientSock, buf, len);
}
len = read(fd, buf, HTTP_MAX_LEN);
}
close(fd);
@ -694,7 +701,9 @@ void
_httpd_sendText(request * r, char *msg)
{
r->response.responseLength += strlen(msg);
_httpd_net_write(r->clientSock, msg, strlen(msg));
if (r->retcode >= 0) {
r->retcode = _httpd_net_write(r->clientSock, msg, strlen(msg));
}
}
int

View File

@ -725,7 +725,7 @@ epoll_loop(void)
for (index = 0; index < n; index++) {
request *r = NULL;
client_fd = events[index].data.fd;
if(client_fd == webserver->serverSock && (events[i].events & EPOLLIN)) {
if(client_fd == webserver->serverSock && (events[index].events & EPOLLIN)) {
struct sockaddr_in clientaddr;
size_t addrlen = sizeof(clientaddr);
for (;;) {
@ -756,19 +756,25 @@ epoll_loop(void)
}
r = (request *)events[index].data.ptr;
if (events[i].events & EPOLLIN) {
httpdReadRequest(webserver, r)
ev.events = EPOLLOUT;
if (epoll_ctl(epfd, EPOLL_CTL_MOD, r->clientSock, &ev) < 0) {
debug(LOG_WARNING, "epoll_ctl_mod error : %s ", strerror(errno));
if (events[index].events & EPOLLIN) {
if (httpdReadRequest(webserver, r) == -1) {
if (epoll_ctl(epfd, EPOLL_CTL_DEL, r->clientSock, &ev) < 0) {
debug(LOG_WARNING, "epoll_ctl_del[1] error : %s ", strerror(errno));
}
httpdEndRequest(r);
} else {
ev.events = EPOLLOUT;
if (epoll_ctl(epfd, EPOLL_CTL_MOD, r->clientSock, &ev) < 0) {
debug(LOG_WARNING, "epoll_ctl_mod error : %s ", strerror(errno));
}
}
} else if (events[i].events & EPOLLOUT) {
} else if (events[index].events & EPOLLOUT) {
httpdProcessRequest(webserver, r);
if (epoll_ctl(epfd, EPOLL_CTL_DEL, r->clientSock, &ev) < 0) {
debug(LOG_WARNING, "epoll_ctl_del[1] error : %s ", strerror(errno));
}
httpdEndRequest(r);
} else if (events[i].events & EPOLLERR) {
} else if (events[index].events & EPOLLERR) {
if (epoll_ctl(epfd, EPOLL_CTL_DEL, r->clientSock, &ev) < 0) {
debug(LOG_WARNING, "epoll_ctl_del[2] error : %s ", strerror(errno));
}

View File

@ -305,7 +305,6 @@ http_send_redirect(request * r, const char *url, const char *text)
safe_asprintf(&message, "<html><body>Please <a href='%s'>click here</a>.</body></html>", url);
httpdOutputDirect(r, message);
_httpd_closeSocket(r);
free(message);
}
@ -487,8 +486,6 @@ http_send_js_redirect(request *r, const char *redir_url)
#endif
httpdOutputLengthDirect(r, redirect_html, html_length);
_httpd_closeSocket(r);
free(redirect_html);
evbuffer_free(evb);
evbuffer_free(evb_redir_url);
@ -498,20 +495,17 @@ void
http_send_apple_redirect(request *r, const char *redir_url)
{
httpdPrintf(r, APPLE_REDIRECT_MSG, redir_url);
_httpd_closeSocket(r);
}
void
http_relay_wisper(request *r)
{
httpdOutputDirect(r, apple_wisper);
_httpd_closeSocket(r);
}
void send_http_page_direct(request *r, char *msg)
{
httpdOutputDirect(r, msg);
_httpd_closeSocket(r);
}
//<<< liudf added end