Diff

Differences From Artifact [50c442afde]:

To Artifact [263f67c7ab]:


            1  +#include <sys/sendfile.h>
     1      2   #include <sys/socket.h>
     2      3   #include <sys/types.h>
     3      4   #include <arpa/inet.h>
     4      5   #include <sys/mman.h>
            6  +#include <sys/stat.h>
     5      7   #include <pthread.h>
     6      8   #include <stdlib.h>
     7      9   #include <unistd.h>
           10  +#include <fcntl.h>
           11  +#include <stdio.h>
     8     12   
     9     13   /* Default values */
    10     14   #define MAX_FAILURE_COUNT 30
    11     15   #define PORT 8080
    12     16   #define THREAD_COUNT 10
    13     17   #define BIND_ADDR "::"
    14     18   
................................................................................
    58     62   }
    59     63   
    60     64   static int filed_logging_thread_init(void) {
    61     65   	/* XXX:TODO: Unimplemented */
    62     66   	return(0);
    63     67   }
    64     68   
    65         -static void filed_handle_client(int fd) {
           69  +struct filed_fileinfo {
           70  +	int fd;
           71  +	size_t len;
           72  +};
           73  +
           74  +static struct filed_fileinfo *filed_open_file(const char *path, struct filed_fileinfo *buffer) {
           75  +	/* XXX:TODO: Cache file descriptors */
           76  +
           77  +	off_t len;
           78  +	int fd;
           79  +
           80  +	fd = open(path, O_RDONLY);
           81  +	if (fd < 0) {
           82  +		return(NULL);
           83  +	}
           84  +
           85  +	len = lseek(fd, 0, SEEK_END);
           86  +	lseek(fd, 0, SEEK_SET);
           87  +
           88  +	buffer->fd = fd;
           89  +	buffer->len = len;
           90  +
           91  +	return(buffer);
           92  +}
           93  +
           94  +static char *filed_get_http_request(FILE *fp, char *buffer, size_t buffer_len) {
    66     95   	/* XXX:TODO: Unimplemented */
    67         -	fd = fd;
           96  +	setlinebuf(fp);
           97  +
           98  +	fp = fp;
           99  +	buffer = buffer;
          100  +	buffer_len = buffer_len;
          101  +
          102  +	fflush(fp);
          103  +
          104  +	return("./hello_world.txt");
          105  +}
          106  +
          107  +static void filed_handle_client(int fd) {
          108  +	struct filed_fileinfo *fileinfo, fileinfo_b;
          109  +	char *path, path_b[1010];
          110  +	FILE *fp;
          111  +
          112  +	fp = fdopen(fd, "w+b");
          113  +	if (fp == NULL) {
          114  +		close(fd);
          115  +
          116  +		return;
          117  +	}
          118  +
          119  +	path = filed_get_http_request(fp, path_b, sizeof(path_b));
          120  +	if (path == NULL) {
          121  +		fclose(fp);
          122  +
          123  +		return;
          124  +	}
          125  +
          126  +	fileinfo = filed_open_file(path, &fileinfo_b);
          127  +	if (fileinfo == NULL) {
          128  +		/* XXX: TODO: Return error page */
          129  +	} else {
          130  +		/* XXX: TODO: Send HTTP response header */
          131  +		sendfile(fd, fileinfo->fd, NULL, fileinfo->len);
          132  +
          133  +		close(fileinfo->fd);
          134  +	}
          135  +
          136  +	fclose(fp);
          137  +
    68    138   	return;
    69    139   }
    70    140   
    71    141   static void *filed_worker_thread(void *arg_v) {
    72    142   	struct filed_worker_thread_args *arg;
    73    143   	struct sockaddr_in6 addr;
    74    144   	socklen_t addrlen;
................................................................................
   101    171   		}
   102    172   
   103    173   		/* Reset failure count*/
   104    174   		failure_count = 0;
   105    175   
   106    176   		/* Handle socket */
   107    177   		filed_handle_client(fd);
   108         -
   109         -		/* Cleanup */
   110         -		close(fd);
   111    178   	}
   112    179   
   113    180   	/* XXX:TODO: Report error */
   114    181   	return(NULL);
   115    182   }
   116    183   
   117    184   static int filed_worker_threads_init(int fd, int thread_count) {
................................................................................
   142    209   	/* Ignore */
   143    210   	argc = argc;
   144    211   	argv = argv;
   145    212   
   146    213   	/* Create listening socket */
   147    214   	fd = filed_listen(bind_addr, port);
   148    215   	if (fd < 0) {
          216  +		perror("filed_listen");
          217  +
   149    218   		return(1);
   150    219   	}
   151    220   
   152    221   	/* Become a daemon */
   153    222   	/* XXX:TODO: Become a daemon */
   154    223   
   155    224   	/* Initialize */