1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <signal.h>
  4 #include <unistd.h>
  5 #include <errno.h>
  6 #include "config.h"
  7 #include "rh_string.h"
  8 #include "rh_event.h"
  9 #include "rhttpd.h"
 10 #include "server.h"
 11 #include "mimetype.h"
 12 #include "handler.h"
 13 #include "uri.h"
 14 #include "memcache.h"
 15 #include "log.h"
 16 
 17 static void signal_handler (int sig, short rh_events, void *arg);
 18 
 19 static int init_mimetypes (mimetype_base_t *mime, config_t *config);
 20 
 21 int main (int argc, char *argv[])
 22 {
 23         rh_event_base_t ev_base;
 24         rh_stat_cache_t *stat_cache;
 25         server_t        server;
 26         rh_event_t      ev_signal;
 27         config_t        *config;
 28         mimetype_base_t mimebase;
 29         handler_base_t  handlerbase;
 30         handler_base_t  handlerbase_misc;
 31         handler_t       *handler_error          = NULL;
 32         handler_t       *handler_accesslog      = NULL;
 33         memcache_t      memcache;
 34         
 35         close (STDIN_FILENO);
 36 
 37         /* this httpd knows only GMT.
 38          * its importend for last-modified compares */
 39         setenv ("TZ", "GMT", 1);
 40 
 41         printf ("This is rhttpd " RHTTPD_VERSION "\n");
 42         printf ("========= W A R N I N G =========\n"
 43                 "=                               =\n"
 44                 "= THIS IS EXPERIMENTAL SOFTWARE =\n"
 45                 "=                               =\n"
 46                 "= Dont use it in productive     =\n"
 47                 "= environment.                  =\n"
 48                 "=                               =\n"
 49                 "=================================\n");
 50 
 51         DEBUGLOG("Server is listening on 0.0.0.0:8080",0);
 52         
 53         signal (SIGPIPE, SIG_IGN);
 54         
 55         if (-1 == rh_event_base_init (&ev_base, RHTTPD_TIMEOUT))
 56                 return EXIT_FAILURE;
 57 
 58         memcache_init (&memcache, &ev_base.time);
 59         
 60         config = config_alloc ("../rhttpd.conf");
 61         if (NULL==config)
 62                 return -1;
 63         
 64         if (0 != config_reload (config)) {
 65                 printf ("cant load config\n");
 66                 return -1;
 67         }
 68 
 69         handler_base_init (&handlerbase, config);
 70         handler_base_init (&handlerbase_misc, config);
 71 
 72         /*
 73          * at the moment
 74          *
 75          * 
 76          *   ORDER IS IMPORTEND
 77          *   ^^^^^ ^^ ^^^^^^^^^
 78          *
 79          * 
 80          */
 81 #if 0
 82         handler_load (  &handlerbase,
 83                         "handler_alias",
 84                         "/home/development/rhttpd/src/handler_alias.so" );
 85 #endif
 86         
 87         handler_load (  &handlerbase,
 88                         "handler_vhost",
 89                         RHTTPD_MODULE_BASE "handler_vhost.so" );
 90         
 91         handler_load (  &handlerbase,
 92                         "handler_status",
 93                         RHTTPD_MODULE_BASE "handler_status.so" );
 94         
 95         handler_load (  &handlerbase,
 96                         "handler_stat",
 97                         RHTTPD_MODULE_BASE "handler_stat.so" );
 98         
 99         handler_load (  &handlerbase,
100                         "handler_dir",
101                         RHTTPD_MODULE_BASE "handler_dir.so" );
102 #if 0
103         handler_load (  &handlerbase,
104                         "handler_autoindex",
105                         "/home/development/rhttpd/src/handler_autoindex.so" );
106 #endif
107 #if 0
108         handler_load (  &handlerbase,
109                         "handler_range",
110                         "/home/development/rhttpd/src/handler_range.so" );
111 #endif
112         
113         handler_load (  &handlerbase,
114                         "handler_etag",
115                         RHTTPD_MODULE_BASE "handler_etag.so" );
116         
117         handler_load (  &handlerbase,
118                         "handler_file",
119                         RHTTPD_MODULE_BASE "handler_file.so" );
120 #if 1
121         handler_accesslog = handler_load (
122                         &handlerbase_misc,
123                         "handler_accesslog",
124                         RHTTPD_MODULE_BASE "handler_accesslog.so" );
125         
126         HANDLER_CALL_MAYBE(handler_accesslog, init_global);
127 #endif
128         handler_error = handler_load (&handlerbase_misc,
129                         "handler_error",
130                         RHTTPD_MODULE_BASE "handler_error.so" );
131 
132         HANDLER_CALL_MAYBE(handler_error    , init_global);
133         
134         if (handler_base_init_handlers (&handlerbase))
135                 return -1;
136         
137         if (handler_base_init_handlers (&handlerbase_misc))
138                 return -1;
139 
140         
141         /*
142          *
143          * mime type
144          *
145          */
146         if (init_mimetypes(&mimebase, config)) {
147                 return -1;
148         }
149         
150         /*
151          *
152          * stat cache
153          *
154          
155          */
156 
157         stat_cache = malloc(sizeof(*stat_cache));
158 
159         rh_event_signal_set (&ev_signal, &ev_base,
160                         SIGINT, signal_handler, &ev_base);
161 
162         if (-1 == rh_event_add (&ev_signal))
163                 return EXIT_FAILURE;
164 
165         if (-1 == rh_stat_cache_init (stat_cache, &ev_base, &mimebase))
166                 return EXIT_FAILURE;
167 
168         server_init (&server, stat_cache, config, &handlerbase, &memcache);
169         
170         if (-1 == server_bind (&server, &ev_base, "localhost", 8080))
171                 return EXIT_FAILURE;
172 
173         if (handler_error)
174                 server.handler_error = handler_dup (handler_error);
175         
176         if (handler_accesslog)
177                 server.handler_accesslog = handler_dup (handler_accesslog);
178 
179         
180         /*
181          *
182          * event loOoOOoOOop
183          *
184          *
185          */
186         rh_event_dispatch (&ev_base);
187 
188         
189         server_destroy (&server);
190         rh_stat_cache_destroy (stat_cache);
191         rh_event_base_destroy (&ev_base);
192 
193         free (stat_cache);
194         
195         mimetype_base_destroy (&mimebase);
196         
197         handler_base_destroy (&handlerbase);
198         handler_base_destroy (&handlerbase_misc);
199         
200         config_free(config);
201         
202         memcache_destroy (&memcache);
203         
204         close (STDERR_FILENO);
205         close (STDOUT_FILENO);
206         
207         return EXIT_SUCCESS;
208 }
209 
210 static void signal_handler (int sig, short rh_events, void *arg)
211 {
212         struct rh_event_base *base = arg;
213 
214 #if 1
215         DEBUGLOG ("sig(%3d) event(%hd) base(%p), going down...",
216                         sig, rh_events, arg);
217 #endif
218         rh_event_base_exit (base);
219 
220 }
221 
222 
223 static int init_mimetypes (mimetype_base_t *mimebase, config_t *config)
224 {
225         const config_entry_t    *config_entry;
226         const config_value_t    *config_value;
227         
228         mimetype_base_init (mimebase);
229 
230         config_entry = CONFIG_POP(config, TYPES_CONFIG);
231         if (config_entry) {
232                 CONFIG_ENTRY_FOREACH_VALUE(config_value, config_entry) {
233                         if (0 != mimetype_load (mimebase,
234                                                 MIMETYPE_CONTENT,
235                                                 config_value->value.data ) )
236                         {
237                                 printf ("%s(): cant load %.*s\n",
238                                                 __FUNCTION__,
239                                                 config_value->value.used,
240                                                 config_value->value.data );
241                                 return -1;
242                         }
243                 }
244                 
245                 config_push (config_entry);
246         }
247 
248         return 0;
249 }


syntax highlighted by Code2HTML, v. 0.9.1