Diff

Differences From Artifact [263f67c7ab]:

To Artifact [a1927fba4d]:


65
66
67
68
69
70
71


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90




91
92
93
94
95
96
97

98
99
100
101
102
103
104
105
106
107
108
109

110
111




112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129






130
131
132
133
134
135
136
137
	/* XXX:TODO: Unimplemented */
	return(0);
}

struct filed_fileinfo {
	int fd;
	size_t len;


};

static struct filed_fileinfo *filed_open_file(const char *path, struct filed_fileinfo *buffer) {
	/* XXX:TODO: Cache file descriptors */

	off_t len;
	int fd;

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		return(NULL);
	}

	len = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, SEEK_SET);

	buffer->fd = fd;
	buffer->len = len;





	return(buffer);
}

static char *filed_get_http_request(FILE *fp, char *buffer, size_t buffer_len) {
	/* XXX:TODO: Unimplemented */
	setlinebuf(fp);


	fp = fp;
	buffer = buffer;
	buffer_len = buffer_len;

	fflush(fp);

	return("./hello_world.txt");
}

static void filed_handle_client(int fd) {
	struct filed_fileinfo *fileinfo, fileinfo_b;
	char *path, path_b[1010];

	FILE *fp;





	fp = fdopen(fd, "w+b");
	if (fp == NULL) {
		close(fd);

		return;
	}

	path = filed_get_http_request(fp, path_b, sizeof(path_b));
	if (path == NULL) {
		fclose(fp);

		return;
	}

	fileinfo = filed_open_file(path, &fileinfo_b);
	if (fileinfo == NULL) {
		/* XXX: TODO: Return error page */
	} else {






		/* XXX: TODO: Send HTTP response header */
		sendfile(fd, fileinfo->fd, NULL, fileinfo->len);

		close(fileinfo->fd);
	}

	fclose(fp);








>
>



















>
>
>
>




<


>












>


>
>
>
>


















>
>
>
>
>
>
|







65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	/* XXX:TODO: Unimplemented */
	return(0);
}

struct filed_fileinfo {
	int fd;
	size_t len;
	char *lastmod;
	char *type;
};

static struct filed_fileinfo *filed_open_file(const char *path, struct filed_fileinfo *buffer) {
	/* XXX:TODO: Cache file descriptors */

	off_t len;
	int fd;

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		return(NULL);
	}

	len = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, SEEK_SET);

	buffer->fd = fd;
	buffer->len = len;

	/* XXX:TODO: Determine */
	buffer->type = "text/plain";
	buffer->lastmod = "Now";

	return(buffer);
}

static char *filed_get_http_request(FILE *fp, char *buffer, size_t buffer_len) {

	setlinebuf(fp);

	/* XXX:TODO: Unimplemented */
	fp = fp;
	buffer = buffer;
	buffer_len = buffer_len;

	fflush(fp);

	return("./hello_world.txt");
}

static void filed_handle_client(int fd) {
	struct filed_fileinfo *fileinfo, fileinfo_b;
	char *path, path_b[1010];
	char *date_current;
	FILE *fp;

	/* XXX:TODO: Determine */
	date_current = "Now";

	/* Open socket as ANSI I/O for ease of use */
	fp = fdopen(fd, "w+b");
	if (fp == NULL) {
		close(fd);

		return;
	}

	path = filed_get_http_request(fp, path_b, sizeof(path_b));
	if (path == NULL) {
		fclose(fp);

		return;
	}

	fileinfo = filed_open_file(path, &fileinfo_b);
	if (fileinfo == NULL) {
		/* XXX: TODO: Return error page */
	} else {
		fprintf(fp, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: filed\r\nLast-Modified: %s\r\nContent-Length: %llu\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
			date_current,
			fileinfo->lastmod,
			(unsigned long long) fileinfo->len,
			fileinfo->type
		);

		sendfile(fd, fileinfo->fd, NULL, fileinfo->len);

		close(fileinfo->fd);
	}

	fclose(fp);