This repository was archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserv.c
More file actions
160 lines (145 loc) · 3.67 KB
/
Copy pathserv.c
File metadata and controls
160 lines (145 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2012-2013 Jon Mayo
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "httpd.h"
#include "daemonize.h"
#include "service.h"
#include "csv.h"
#include "logger.h"
#include "ext.h"
#include "mod_static_files.h"
#include "mod_counter.h"
struct service_config_info {
unsigned current_row;
char host_match[256];
char uri_match[256];
const struct module *module;
char arg[256];
};
static void on_row_end(void *user_ptr, unsigned row)
{
struct service_config_info *info = user_ptr;
if (row == 0)
return; /* ignore first row */
Debug("row=%d mod=%p arg=\"%s\"\n", row, info->module, info->arg);
service_register(info->host_match, info->uri_match, info->module,
info->arg);
}
static int on_data(void *user_ptr, unsigned row, unsigned col,
size_t len, const char *data)
{
struct service_config_info *info = user_ptr;
Debug("DATA [%d,%d] = '%s'\n", row, col, data);
assert(info != NULL);
if (row != info->current_row) {
info->arg[0] = 0;
info->module = NULL;
info->current_row = row;
}
if (row == 0)
return 0; /* ignore first row */
// TODO: start new row
Debug("COL %d\n", col);
switch (col) {
case 0:
// TODO: support "enabled" as 0/1 or no/yes
break;
case 1:
snprintf(info->host_match, sizeof(info->host_match), "%.*s",
(int)len, data);
break;
case 2:
snprintf(info->uri_match, sizeof(info->uri_match), "%.*s",
(int)len, data);
break;
case 3:
info->module = module_find(data);
Debug("module=%s (%p)\n", data, info->module);
break;
case 4:
snprintf(info->arg, sizeof(info->arg), "%.*s", (int)len, data);
break;
default:
return -1;
}
Debug("ROW %d\n", row);
Debug("\t[%u]='%.*s'\n", col, (int)len, data);
// TODO: implement this
return 0;
}
static int load_services(const char *filename)
{
FILE *f;
char buf[6]; // TODO: make this bigger
size_t len;
struct csv csv;
struct service_config_info info = { -1, "", "", NULL, "" };
#if __GLIBC_PREREQ(2, 7)
f = fopen(filename, "rbe");
#else
f = fopen(filename, "rb");
#endif
if (!f) {
perror(filename);
return -1;
}
csv_init(&csv, &info, on_data, on_row_end);
do {
len = fread(buf, 1, sizeof(buf), f);
if (!len && ferror(f)) {
perror(filename);
return -1;
}
if (csv_push(&csv, len, buf))
goto failure;
} while (!feof(f));
if (csv_eol(&csv))
goto failure;
fclose(f);
return 0;
failure:
fclose(f);
return -1;
}
static void module_register_all(void)
{
module_register("static_files", &mod_static_files);
module_register("counter", &mod_counter);
}
int main(int argc, char *argv[])
{
#ifdef USE_SYSLOG
char *prog_name;
prog_name = strrchr(argv[0], '/');
if (!prog_name)
prog_name = argv[0];
openlog(prog_name, LOG_PERROR | LOG_PID, LOG_DAEMON);
#endif
module_register_all();
load_services("serv.csv");
ext_config_load("mime.csv");
httpd_poolsize(100);
if (httpd_start(NULL, "8080")) {
Error("Unable to start -- Terminating\n");
return 1;
}
httpd_loop();
// daemonize();
Info("done -- Terminating\n");
return 0;
}