/[osn-commons]/trunk/freehttpd/main.c
ViewVC logotype

Contents of /trunk/freehttpd/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (show annotations)
Sun Aug 11 17:35:46 2024 UTC (7 months, 2 weeks ago) by rakinar2
File MIME type: text/x-c
File size: 3476 byte(s)
feat(freehttpd): file serving and directory indexing

1 #include "freehttpd.h"
2 #include <errno.h>
3 #include <getopt.h>
4 #include <magic.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 static struct option const long_options[] = {
11 { "help", no_argument, NULL, 'h' },
12 { "version", no_argument, NULL, 'v' },
13 { "config", required_argument, NULL, 'c' },
14 { NULL, 0, NULL, 0 }
15 };
16
17 static const char *const short_options = "hvc:";
18
19 struct config
20 {
21 const char *progname;
22 const char *config_file;
23 };
24
25 static struct config config = { 0 };
26
27 static void
28 usage (void)
29 {
30 printf ("Usage: %s [OPTIONS]\n", config.progname);
31 printf ("Options:\n");
32 printf (" -h, --help Print this help message\n");
33 printf (" -v, --version Print version information\n");
34 printf (" -c, --config Specify configuration file\n");
35 }
36
37 static void
38 version (void)
39 {
40 printf ("freehttpd 0.0.0\n");
41 }
42
43 static bool
44 validate_settings (void)
45 {
46 return true;
47 }
48
49 static void
50 initialize (const char *progname)
51 {
52 config.progname = progname;
53 config.config_file = "freehttpd.conf";
54 }
55
56 int
57 main (int argc, char **argv)
58 {
59 initialize (argv[0]);
60
61 while (true)
62 {
63 int option_index = 0;
64 int c = getopt_long (argc, argv, short_options, long_options,
65 &option_index);
66
67 if (c == -1)
68 break;
69
70 switch (c)
71 {
72 case 'h':
73 usage ();
74 exit (EXIT_SUCCESS);
75
76 case 'v':
77 version ();
78 exit (EXIT_SUCCESS);
79
80 case 'c':
81 config.config_file = optarg;
82 break;
83
84 case '?':
85 default:
86 exit (EXIT_FAILURE);
87 }
88 }
89
90 if (!validate_settings ())
91 exit (EXIT_FAILURE);
92
93 magic_t magic = magic_open (MAGIC_MIME);
94 unsigned int port = 8080;
95 size_t uri_len = 1024;
96 ecode_t code = E_OK;
97
98 if (magic == NULL)
99 {
100 fprintf (stderr, "%s: failed to initialize magic\n",
101 config.progname);
102 exit (EXIT_FAILURE);
103 }
104
105 if (magic_load (magic, NULL) != 0)
106 {
107 fprintf (stderr, "%s: failed to load magic database\n",
108 config.progname);
109 magic_close (magic);
110 exit (EXIT_FAILURE);
111 }
112
113 freehttpd_t *freehttpd = freehttpd_init (magic);
114
115 code = freehttpd_setopt (freehttpd, FREEHTTPD_CONFIG_PORT, &port);
116
117 if (code != E_OK)
118 goto error;
119
120 code = freehttpd_setopt (freehttpd, FREEHTTPD_CONFIG_ADDR, NULL);
121
122 if (code != E_OK)
123 goto error;
124
125 code = freehttpd_setopt (freehttpd, FREEHTTPD_CONFIG_MAX_URI_LEN, &uri_len);
126
127 if (code != E_OK)
128 goto error;
129
130 char *docroot = getenv ("FREEHTTPD_DOCROOT");
131 code = freehttpd_setopt (freehttpd, FREEHTTPD_CONFIG_DOCROOT,
132 docroot ? docroot : "/var/www");
133
134 if (code != E_OK)
135 goto error;
136
137 code = freehttpd_start (freehttpd);
138
139 if (code != E_OK)
140 {
141 error:
142 fprintf (stderr, "%s: failed to start: %d: %s\n", config.progname,
143 code, strerror (errno));
144 freehttpd_free (freehttpd);
145 exit (EXIT_FAILURE);
146 }
147
148 freehttpd_free (freehttpd);
149 magic_close (magic);
150 return 0;
151 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26