diff options
author | scratko <m@scratko.xyz> | 2024-08-01 23:00:52 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-08-01 23:00:52 +0300 |
commit | 1f111dd2882ff2cb85fede51e8654118356600e5 (patch) | |
tree | 0556062f5e46f241c27a3cc18a46c38d6d2eb522 | |
parent | 5d4ea2faee069e94468e14232d09b774903285f1 (diff) | |
download | http-server-master.tar.gz http-server-master.tar.bz2 http-server-master.zip |
-rw-r--r-- | http_server.c | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/http_server.c b/http_server.c index e769c09..239c416 100644 --- a/http_server.c +++ b/http_server.c @@ -6,6 +6,7 @@ #include <string.h> #include <fcntl.h> #include <syslog.h> +#include <sys/resource.h> #ifndef INBUFSIZE @@ -215,12 +216,12 @@ static int command_processing(struct server *serv, struct session *cur_session, */ static int read_in_session(struct server *serv, struct session *cur_session) { - int read_res, i, end_idx; + int read_res, i, end_idx, bufp = cur_session->in_buf_used; char *line = NULL; end_idx = -1; - if(!cur_session->in_buf_used) - read_res = read(cur_session->fd, cur_session->in_buffer, INBUFSIZE); + read_res = read(cur_session->fd, cur_session->in_buffer + bufp, + INBUFSIZE - cur_session->in_buf_used); if(read_res <= 0) return 0; @@ -234,7 +235,7 @@ static int read_in_session(struct server *serv, struct session *cur_session) } } if(end_idx == -1) { - if(cur_session->in_buf_used == INBUFSIZE) + if(cur_session->in_buf_used >= INBUFSIZE) return 0; return 1; } @@ -242,7 +243,7 @@ static int read_in_session(struct server *serv, struct session *cur_session) line = malloc(end_idx+1); memcpy(line, cur_session->in_buffer, end_idx+1); } - cur_session->in_buf_used -= i+1; + cur_session->in_buf_used -= end_idx+1; memmove(cur_session->in_buffer, cur_session->in_buffer+end_idx+1, cur_session->in_buf_used); @@ -395,7 +396,8 @@ static int write_in_session(struct server *serv, struct session *cur_session) OUTBUFSIZE); /* end of file */ if(!read_result) { - close(cur_session->rs_fd); + if(cur_session->rs_fd != -1) + close(cur_session->rs_fd); cur_session->rs_fd = -1; cur_session->data_transfer_status = 0; cur_session->state = header_start; @@ -422,11 +424,12 @@ static int write_in_session(struct server *serv, struct session *cur_session) static void close_session(struct server *serv, int fd) { - close(fd); + if(fd != -1) + close(fd); serv->session_array[fd]->fd = -1; if(serv->session_array[fd]->path) free(serv->session_array[fd]->path); - if(serv->session_array[fd]->rs_fd) + if(serv->session_array[fd]->rs_fd != -1) close(serv->session_array[fd]->rs_fd); serv->session_array[fd] = NULL; } @@ -483,7 +486,7 @@ int main_loop(struct server *serv) read_result = read_in_session(serv, serv->session_array[i]); /* normal connection closure (by user or full of in_buffer) */ if(!read_result) - close_session(serv, i); + close_session(serv, serv->session_array[i]->fd); } if(serv->session_array[i] && FD_ISSET(i, &writefds)) { write_result = write_in_session(serv, serv->session_array[i]);; @@ -492,7 +495,7 @@ int main_loop(struct server *serv) * error 400 or 404 occurs */ if(!write_result) - close_session(serv, i); + close_session(serv, serv->session_array[i]->fd); } } } @@ -567,7 +570,10 @@ int main(int argc, char **argv) return 1; } demonization(); - openlog("scratko's http server", 0, LOG_USER); + struct rlimit core_limits; + core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY; + setrlimit(RLIMIT_CORE, &core_limits); + openlog("http server", 0, LOG_USER); syslog(LOG_INFO, "daemon started"); port = strtol(argv[1], NULL, 10); @@ -577,4 +583,5 @@ int main(int argc, char **argv) } return main_loop(&serv); syslog(LOG_INFO, "server terminated"); + closelog(); } |