Check-in [7dfe29c1bc]
Overview
Comment:Added basic support for HTTP responses
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7dfe29c1bc36cef488f5161e451d29df3777fec4
User & Date: rkeene on 2014-02-04 09:53:18
Other Links: manifest | tags
Context
2014-02-04
10:18
Added basic support for HTTP requests check-in: 4bbee8f607 user: rkeene tags: trunk
09:53
Added basic support for HTTP responses check-in: 7dfe29c1bc user: rkeene tags: trunk
07:43
Implemented basic stubs for sending files check-in: bdec14db58 user: rkeene tags: trunk
Changes

Modified filed.c from [263f67c7ab] to [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
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) {
	/* XXX:TODO: Unimplemented */
	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
		);
		/* XXX: TODO: Send HTTP response header */

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

		close(fileinfo->fd);
	}

	fclose(fp);