aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/fileserver_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/fileserver_example.c')
-rw-r--r--src/daemon/fileserver_example.c113
1 files changed, 52 insertions, 61 deletions
diff --git a/src/daemon/fileserver_example.c b/src/daemon/fileserver_example.c
index 989ab7d5..6f39f491 100644
--- a/src/daemon/fileserver_example.c
+++ b/src/daemon/fileserver_example.c
@@ -37,78 +37,69 @@
37 37
38#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 38#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>"
39 39
40static int file_reader(void * cls, 40static int
41 size_t pos, 41file_reader (void *cls, size_t pos, char *buf, int max)
42 char * buf, 42{
43 int max) { 43 FILE *file = cls;
44 FILE * file = cls;
45 44
46 fseek(file, pos, SEEK_SET); 45 fseek (file, pos, SEEK_SET);
47 return fread(buf, 46 return fread (buf, 1, max, file);
48 1,
49 max,
50 file);
51} 47}
52 48
53static int ahc_echo(void * cls, 49static int
54 struct MHD_Connection * connection, 50ahc_echo (void *cls,
55 const char * url, 51 struct MHD_Connection *connection,
56 const char * method, 52 const char *url,
57 const char * upload_data, 53 const char *method,
58 const char * version, 54 const char *upload_data,
59 unsigned int * upload_data_size) { 55 const char *version, unsigned int *upload_data_size)
60 struct MHD_Response * response; 56{
57 struct MHD_Response *response;
61 int ret; 58 int ret;
62 FILE * file; 59 FILE *file;
63 struct stat buf; 60 struct stat buf;
64 61
65 if (0 != strcmp(method, "GET")) 62 if (0 != strcmp (method, "GET"))
66 return MHD_NO; /* unexpected method */ 63 return MHD_NO; /* unexpected method */
67 file = fopen(&url[1], "r"); 64 file = fopen (&url[1], "r");
68 if (file == NULL) { 65 if (file == NULL)
69 response = MHD_create_response_from_data(strlen(PAGE), 66 {
70 (void*) PAGE, 67 response = MHD_create_response_from_data (strlen (PAGE),
71 MHD_NO, 68 (void *) PAGE,
72 MHD_NO); 69 MHD_NO, MHD_NO);
73 ret = MHD_queue_response(connection, 70 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
74 MHD_HTTP_NOT_FOUND, 71 MHD_destroy_response (response);
75 response); 72 }
76 MHD_destroy_response(response); 73 else
77 } else { 74 {
78 stat(&url[1], 75 stat (&url[1], &buf);
79 &buf); 76 response = MHD_create_response_from_callback (buf.st_size,
80 response = MHD_create_response_from_callback(buf.st_size, 77 &file_reader,
81 &file_reader, 78 file,
82 file, 79 (MHD_ContentReaderFreeCallback)
83 (MHD_ContentReaderFreeCallback) &fclose); 80 & fclose);
84 ret = MHD_queue_response(connection, 81 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
85 MHD_HTTP_OK, 82 MHD_destroy_response (response);
86 response); 83 }
87 MHD_destroy_response(response);
88 }
89 return ret; 84 return ret;
90} 85}
91 86
92int main(int argc, 87int
93 char * const * argv) { 88main (int argc, char *const *argv)
94 struct MHD_Daemon * d; 89{
90 struct MHD_Daemon *d;
95 91
96 if (argc != 3) { 92 if (argc != 3)
97 printf("%s PORT SECONDS-TO-RUN\n", 93 {
98 argv[0]); 94 printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
99 return 1; 95 return 1;
100 } 96 }
101 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, 97 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
102 atoi(argv[1]), 98 atoi (argv[1]),
103 NULL, 99 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
104 NULL,
105 &ahc_echo,
106 PAGE,
107 MHD_OPTION_END);
108 if (d == NULL) 100 if (d == NULL)
109 return 1; 101 return 1;
110 sleep(atoi(argv[2])); 102 sleep (atoi (argv[2]));
111 MHD_stop_daemon(d); 103 MHD_stop_daemon (d);
112 return 0; 104 return 0;
113} 105}
114