Skip to content
Snippets Groups Projects
Commit 9ec5bd9e authored by rswindell's avatar rswindell
Browse files

Changed sockreadline() usage so it can mark socket as closed.

Added log output send_error().
send_error() doesn't attempt to send error .html file if it doesn't exist.
parent 2a6e4679
No related branches found
No related tags found
No related merge requests found
...@@ -469,6 +469,7 @@ static void close_request(http_session_t * session) ...@@ -469,6 +469,7 @@ static void close_request(http_session_t * session)
{ {
if(!session->req.keep_alive) { if(!session->req.keep_alive) {
close_socket(session->socket); close_socket(session->socket);
session->socket=INVALID_SOCKET;
session->finished=TRUE; session->finished=TRUE;
} }
} }
...@@ -595,12 +596,14 @@ static void send_error(char *message, http_session_t * session) ...@@ -595,12 +596,14 @@ static void send_error(char *message, http_session_t * session)
{ {
char error_code[4]; char error_code[4];
lprintf("%04d !ERROR %s",session->socket,message);
session->req.send_location=FALSE; session->req.send_location=FALSE;
SAFECOPY(error_code,message); SAFECOPY(error_code,message);
sprintf(session->req.request,"%s%s.html",error_dir,error_code); sprintf(session->req.request,"%s%s.html",error_dir,error_code);
if(session->http_ver > HTTP_0_9) if(session->http_ver > HTTP_0_9)
send_headers(session,message); send_headers(session,message);
sock_sendfile(session->socket,session->req.request); if(fexist(session->req.request))
sock_sendfile(session->socket,session->req.request);
close_request(session); close_request(session);
} }
...@@ -708,7 +711,7 @@ static BOOL read_mime_types(char* fname) ...@@ -708,7 +711,7 @@ static BOOL read_mime_types(char* fname)
return(mime_count>0); return(mime_count>0);
} }
static int sockreadline(SOCKET socket, time_t timeout, char *buf, size_t length) static int sockreadline(http_session_t * session, time_t timeout, char *buf, size_t length)
{ {
char ch; char ch;
DWORD i; DWORD i;
...@@ -717,21 +720,22 @@ static int sockreadline(SOCKET socket, time_t timeout, char *buf, size_t length) ...@@ -717,21 +720,22 @@ static int sockreadline(SOCKET socket, time_t timeout, char *buf, size_t length)
start=time(NULL); start=time(NULL);
for(i=0;TRUE;) { for(i=0;TRUE;) {
if(!socket_check(socket,&rd)) { if(!socket_check(session->socket,&rd)) {
close_socket(socket); close_socket(session->socket);
session->socket=INVALID_SOCKET;
return(-1); return(-1);
} }
if(!rd) { if(!rd) {
if(time(NULL)-start>timeout) { if(time(NULL)-start>timeout) {
close_socket(socket); close_socket(session->socket);
return(-1); /* time-out */ return(-1); /* time-out */
} }
mswait(1); mswait(1);
continue; /* no data */ continue; /* no data */
} }
if(recv(socket, &ch, 1, 0)!=1) if(recv(session->socket, &ch, 1, 0)!=1)
break; break;
if(ch=='\n') if(ch=='\n')
...@@ -750,7 +754,7 @@ static int sockreadline(SOCKET socket, time_t timeout, char *buf, size_t length) ...@@ -750,7 +754,7 @@ static int sockreadline(SOCKET socket, time_t timeout, char *buf, size_t length)
else else
buf[i]=0; buf[i]=0;
lprintf("%04d RX: %s",socket,buf); lprintf("%04d RX: %s",session->socket,buf);
return(0); return(0);
} }
...@@ -763,11 +767,11 @@ static BOOL parse_headers(http_session_t * session) ...@@ -763,11 +767,11 @@ static BOOL parse_headers(http_session_t * session)
int i; int i;
lprintf("%04d Parsing headers",session->socket); lprintf("%04d Parsing headers",session->socket);
while(!sockreadline(session->socket,TIMEOUT_THREAD_WAIT,req_line,sizeof(req_line))&&strlen(req_line)) { while(!sockreadline(session,TIMEOUT_THREAD_WAIT,req_line,sizeof(req_line))&&strlen(req_line)) {
/* Check this... SHOULD append lines starting with spaces or horizontal tabs. */ /* Check this... SHOULD append lines starting with spaces or horizontal tabs. */
while((recvfrom(session->socket,next_char,1,MSG_PEEK,NULL,0)>0) && (next_char[0]=='\t' || next_char[0]==' ')) { while((recvfrom(session->socket,next_char,1,MSG_PEEK,NULL,0)>0) && (next_char[0]=='\t' || next_char[0]==' ')) {
i=strlen(req_line); i=strlen(req_line);
sockreadline(session->socket,TIMEOUT_THREAD_WAIT,req_line+i,sizeof(req_line)-i); sockreadline(session,TIMEOUT_THREAD_WAIT,req_line+i,sizeof(req_line)-i);
} }
strtok(req_line,":"); strtok(req_line,":");
if((value=strtok(NULL,""))!=NULL) { if((value=strtok(NULL,""))!=NULL) {
...@@ -916,7 +920,7 @@ static BOOL get_req(http_session_t * session) ...@@ -916,7 +920,7 @@ static BOOL get_req(http_session_t * session)
char req_line[MAX_REQUEST_LINE]; char req_line[MAX_REQUEST_LINE];
char * p; char * p;
if(!sockreadline(session->socket,TIMEOUT_THREAD_WAIT,req_line,sizeof(req_line))) { if(!sockreadline(session,TIMEOUT_THREAD_WAIT,req_line,sizeof(req_line))) {
lprintf("%04d Got request line: %s",session->socket,req_line); lprintf("%04d Got request line: %s",session->socket,req_line);
p=get_method(req_line,session); p=get_method(req_line,session);
if(p!=NULL) { if(p!=NULL) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment