#include "worker.hfa" #include #include #include #include #include #include #include "options.hfa" #include "protocol.hfa" #include "filecache.hfa" //============================================================================================= // Worker Thread //============================================================================================= void ?{}( Worker & this ) { ((thread&)this){ "Server Worker Thread", *options.clopts.instance }; this.pipe[0] = -1; this.pipe[1] = -1; this.done = false; } extern "C" { extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); } void main( Worker & this ) { park(); /* paranoid */ assert( this.pipe[0] != -1 ); /* paranoid */ assert( this.pipe[1] != -1 ); CONNECTION: for() { if( options.log ) sout | "=== Accepting connection ==="; int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, &this.cancel, 0p ); // int fd = accept4( this.[sockfd, addr, addrlen, flags] ); if(fd < 0) { if( errno == ECONNABORTED ) break; if( this.done && (errno == EINVAL || errno == EBADF) ) break; abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); } if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ==="; REQUEST: for() { bool closed; HttpCode code; const char * file; size_t name_size; // Read the http request size_t len = options.socket.buflen; char buffer[len]; if( options.log ) sout | "=== Reading request ==="; [code, closed, file, name_size] = http_read(fd, buffer, len, &this.cancel); // if we are done, break out of the loop if( closed ) break REQUEST; // If this wasn't a request retrun 400 if( code != OK200 ) { sout | "=== Invalid Request :" | code_val(code) | "==="; answer_error(fd, code); continue REQUEST; } if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) { if( options.log ) sout | "=== Request for /plaintext ==="; char text[] = "Hello, World!\n"; // Send the header int ret = answer_plain(fd, text, sizeof(text)); if( ret == -ECONNRESET ) break REQUEST; if( options.log ) sout | "=== Answer sent ==="; continue REQUEST; } if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) { if( options.log ) sout | "=== Request for /ping ==="; // Send the header int ret = answer_empty(fd); if( ret == -ECONNRESET ) break REQUEST; if( options.log ) sout | "=== Answer sent ==="; continue REQUEST; } if( options.log ) { sout | "=== Request for file " | nonl; write(sout, file, name_size); sout | " ==="; } if( !options.file_cache.path ) { if( options.log ) { sout | "=== File Not Found (" | nonl; write(sout, file, name_size); sout | ") ==="; } answer_error(fd, E405); continue REQUEST; } // Get the fd from the file cache int ans_fd; size_t count; [ans_fd, count] = get_file( file, name_size ); // If we can't find the file, return 404 if( ans_fd < 0 ) { if( options.log ) { sout | "=== File Not Found (" | nonl; write(sout, file, name_size); sout | ") ==="; } answer_error(fd, E404); continue REQUEST; } // Send the header int ret = answer_header(fd, count); if( ret == -ECONNRESET ) break REQUEST; // Send the desired file ret = sendfile( this.pipe, fd, ans_fd, count); if( ret == -ECONNRESET ) break REQUEST; if( options.log ) sout | "=== Answer sent ==="; } if( options.log ) sout | "=== Connection closed ==="; close(fd); continue CONNECTION; } }