Check-in [bdec14db58]
Overview
Comment:Implemented basic stubs for sending files
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: bdec14db5815cca918a7592379b56c6736b0743b
User & Date: rkeene on 2014-02-04 07:43:59
Other Links: manifest | tags
Context
2014-02-04
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
05:33
Basic socket server written, need to add HTTP support and file serving check-in: c34b109ceb user: rkeene tags: trunk
Changes

Modified filed.c from [50c442afde] to [263f67c7ab].


1
2
3
4

5
6
7


8
9
10
11
12
13
14

#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/mman.h>

#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>



/* Default values */
#define MAX_FAILURE_COUNT 30
#define PORT 8080
#define THREAD_COUNT 10
#define BIND_ADDR "::"

>




>



>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

/* Default values */
#define MAX_FAILURE_COUNT 30
#define PORT 8080
#define THREAD_COUNT 10
#define BIND_ADDR "::"

58
59
60
61
62
63
64




65





















66


67







































68
69
70
71
72
73
74
}

static int filed_logging_thread_init(void) {
	/* XXX:TODO: Unimplemented */
	return(0);
}





static void filed_handle_client(int fd) {





















	/* XXX:TODO: Unimplemented */


	fd = fd;







































	return;
}

static void *filed_worker_thread(void *arg_v) {
	struct filed_worker_thread_args *arg;
	struct sockaddr_in6 addr;
	socklen_t addrlen;







>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







62
63
64
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
}

static int filed_logging_thread_init(void) {
	/* 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);

	return;
}

static void *filed_worker_thread(void *arg_v) {
	struct filed_worker_thread_args *arg;
	struct sockaddr_in6 addr;
	socklen_t addrlen;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
		}

		/* Reset failure count*/
		failure_count = 0;

		/* Handle socket */
		filed_handle_client(fd);

		/* Cleanup */
		close(fd);
	}

	/* XXX:TODO: Report error */
	return(NULL);
}

static int filed_worker_threads_init(int fd, int thread_count) {







<
<
<







171
172
173
174
175
176
177



178
179
180
181
182
183
184
		}

		/* Reset failure count*/
		failure_count = 0;

		/* Handle socket */
		filed_handle_client(fd);



	}

	/* XXX:TODO: Report error */
	return(NULL);
}

static int filed_worker_threads_init(int fd, int thread_count) {
142
143
144
145
146
147
148


149
150
151
152
153
154
155
	/* Ignore */
	argc = argc;
	argv = argv;

	/* Create listening socket */
	fd = filed_listen(bind_addr, port);
	if (fd < 0) {


		return(1);
	}

	/* Become a daemon */
	/* XXX:TODO: Become a daemon */

	/* Initialize */







>
>







209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
	/* Ignore */
	argc = argc;
	argv = argv;

	/* Create listening socket */
	fd = filed_listen(bind_addr, port);
	if (fd < 0) {
		perror("filed_listen");

		return(1);
	}

	/* Become a daemon */
	/* XXX:TODO: Become a daemon */

	/* Initialize */