diff options
author | scratko <m@scratko.xyz> | 2025-07-26 03:39:46 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2025-07-26 03:50:32 +0300 |
commit | 9d5e8d5b2ab5ff406da3f956db1a8c1716af4cbd (patch) | |
tree | c1f876523e885fb8020805a0d1dd05c3403ce9b0 /http_server.c | |
parent | 1f111dd2882ff2cb85fede51e8654118356600e5 (diff) | |
download | http-server-9d5e8d5b2ab5ff406da3f956db1a8c1716af4cbd.tar.gz http-server-9d5e8d5b2ab5ff406da3f956db1a8c1716af4cbd.tar.bz2 http-server-9d5e8d5b2ab5ff406da3f956db1a8c1716af4cbd.zip |
Minor changes
Command processing uses end_idx instead of line length.
Additional check if the line ends only with \n, not \r\n.
Changed buffer offset when transferring data to the client (change_out_buffer).
Check select() for errors changed to -1.
Fixed index.html (title)
Diffstat (limited to 'http_server.c')
-rw-r--r-- | http_server.c | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/http_server.c b/http_server.c index 239c416..84f35a9 100644 --- a/http_server.c +++ b/http_server.c @@ -112,24 +112,24 @@ static int host_search_condition(const char *line) strncmp(line, "host:", strlen("host:"))); } -static int match_hosts(struct server *serv, const char *line, int length) +static int match_hosts(struct server *serv, const char *line, int end_idx) { int i, last_idx; - if(strlen("host:") > length) + if(strlen("host:") > end_idx) return 0; if(!host_search_condition(line)) return 0; - for(i = strlen("host:"); i < length; ++i) { + for(i = strlen("host:"); i <= end_idx; ++i) { if(line[i] == ' ') continue; break; } - if(i+strlen(serv->host) > length) + if(i+strlen(serv->host) > end_idx) return 0; if(strncmp(line+i, serv->host, strlen(serv->host))) return 0; - for(last_idx = i+strlen(serv->host); last_idx < length; ++last_idx) { + for(last_idx = i+strlen(serv->host); last_idx <= end_idx; ++last_idx) { if(line[last_idx] == ' ') continue; else @@ -140,20 +140,24 @@ static int match_hosts(struct server *serv, const char *line, int length) return 0; } -static int match_protocols(const char *line, int length) +static int match_protocols(const char *line, int end_idx) { int i; - /* skip extract path */ - for(i = 4; i < length && line[i] != ' '; ++i) + /* + * skip extract path + * find the space after the path + * and then ++i + */ + for(i = 4; i <= end_idx && line[i] != ' '; ++i) {} - if(i >= length) + if(i > end_idx) return 0; ++i; - if(i+strlen("HTTP/1.1") > length) + if(i+strlen("HTTP/1.1") > end_idx) return 0; if(strncmp(line+i, "HTTP/1.1", strlen("HTTP/1.1"))) return 0; - for(i += strlen("HTTP/1.1"); i < length; ++i) { + for(i += strlen("HTTP/1.1"); i <= end_idx; ++i) { if(line[i] == ' ') continue; if(line[i] == '\r' || line[i] == '\n') @@ -163,14 +167,18 @@ static int match_protocols(const char *line, int length) return 0; } -static char* extract_path(const char *line, int length) +static char* extract_path(const char *line, int end_idx) { - int i; + int i, length; char *path; - for(i = 4; i < length && line[i] != ' '; ++i) + /* + * find space after path to calculate + * number of characters in path + */ + for(i = 4; i <= end_idx && line[i] != ' '; ++i) {} - if(i >= length) + if(i > end_idx) return NULL; length = i-4+1 ; @@ -181,21 +189,21 @@ static char* extract_path(const char *line, int length) } static int command_processing(struct server *serv, struct session *cur_session, - const char *line, int length) + const char *line, int end_idx) { switch(cur_session->state) { case header_start: - if(length > strlen("GET") && !strncmp(line, "GET", strlen("GET"))) { - cur_session->path = extract_path(line, length); + if(end_idx > strlen("GET") && !strncmp(line, "GET", strlen("GET"))) { + cur_session->path = extract_path(line, end_idx); if(cur_session->path == NULL) return 0; - if(!match_protocols(line, length)) + if(!match_protocols(line, end_idx)) return 0; cur_session->state = waiting_host; return 1; } case waiting_host: - if(!match_hosts(serv, line, length)) + if(!match_hosts(serv, line, end_idx)) return 0; cur_session->state = empty_line; return 1; @@ -220,7 +228,7 @@ static int read_in_session(struct server *serv, struct session *cur_session) char *line = NULL; end_idx = -1; - read_res = read(cur_session->fd, cur_session->in_buffer + bufp, + read_res = read(cur_session->fd, cur_session->in_buffer + bufp, INBUFSIZE - cur_session->in_buf_used); if(read_res <= 0) return 0; @@ -249,10 +257,12 @@ static int read_in_session(struct server *serv, struct session *cur_session) /* skip the remaining data in the header */ if(cur_session->state == empty_line && - strncmp(cur_session->in_buffer, "\r\n", 2)) + (strncmp(cur_session->in_buffer, "\r\n", 2) || + strncmp(cur_session->in_buffer, "\n", 1))) continue; else if(cur_session->state == empty_line && - !strncmp(cur_session->in_buffer, "\r\n", 2)) + (!strncmp(cur_session->in_buffer, "\r\n", 2) || + !strncmp(cur_session->in_buffer, "\n", 1))) cur_session->in_buf_used = 0; if(!command_processing(serv, cur_session, line, end_idx)) { @@ -322,7 +332,7 @@ static int open_file(char *root, struct session *cur_session) static void change_out_buffer(char *out_buffer, int *out_buf_used, int writed_size) { - memmove(out_buffer, out_buffer+writed_size, (*out_buffer)-writed_size); + memmove(out_buffer, out_buffer+writed_size, (*out_buf_used)-writed_size); *out_buf_used -= writed_size; } @@ -390,7 +400,7 @@ static int write_in_session(struct server *serv, struct session *cur_session) } /* ---------- normal page write ---------- */ - /* if buffer doesn't contain any data*/ + /* if buffer doesn't contain any data */ if(!cur_session->out_buf_used) { read_result = read(cur_session->rs_fd, cur_session->out_buffer, OUTBUFSIZE); @@ -403,12 +413,12 @@ static int write_in_session(struct server *serv, struct session *cur_session) cur_session->state = header_start; return 0; } - cur_session->out_buf_used += read_result; + cur_session->out_buf_used = read_result; } write_result = write(cur_session->fd, cur_session->out_buffer, cur_session->out_buf_used); - /* writed remaining some of data */ + /* not all data has writed */ if(write_result < cur_session->out_buf_used) change_out_buffer(cur_session->out_buffer, &cur_session->out_buf_used, @@ -468,7 +478,7 @@ int main_loop(struct server *serv) } select_result = select(maxfd + 1, &readfds, &writefds, NULL, NULL); - if(!select_result) { + if(select_result == -1) { syslog(LOG_ERR, "select error"); return 3; } |