source: Main/trunk/Server/config.c@ 19

Last change on this file since 19 was 19, checked in by Nishi, on Sep 14, 2024 at 9:51:41 AM

add fallback option

  • Property svn:keywords set to Id
File size: 5.1 KB
RevLine 
[4]1/* $Id: config.c 19 2024-09-14 00:51:41Z nishi $ */
2
[16]3#define SOURCE
4
[4]5#include "tw_config.h"
[17]6#include "tw_module.h"
[4]7
8#include <stdio.h>
[7]9#include <stdint.h>
[4]10#include <stdlib.h>
11#include <string.h>
[12]12#include <unistd.h>
[4]13
14#include <cm_string.h>
15#include <cm_log.h>
16
[6]17struct tw_config config;
18
[12]19struct tw_config_entry* tw_vhost_match(const char* name, int port) {
20 int i;
21 for(i = 0; i < config.vhost_count; i++) {
[13]22 if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
[12]23 return &config.vhosts[i];
24 }
25 }
26 return &config.root;
27}
28
[7]29void tw_config_init(void) {
30 int i;
31 for(i = 0; i < MAX_PORTS + 1; i++) {
32 config.ports[i] = -1;
33 }
[12]34 for(i = 0; i < MAX_VHOSTS; i++) {
35 config.vhosts[i].sslkey = NULL;
36 config.vhosts[i].sslcert = NULL;
[19]37 config.vhosts[i].root = NULL;
[12]38 }
39 config.root.sslkey = NULL;
40 config.root.sslcert = NULL;
[19]41 config.root.root = NULL;
[12]42 config.vhost_count = 0;
[18]43 config.module_count = 0;
44 config.extension = NULL;
[17]45 config.server_root = cm_strdup(PREFIX);
[12]46 gethostname(config.hostname, 1024);
[7]47}
[6]48
49int tw_config_read(const char* path) {
[4]50 cm_log("Config", "Reading %s", path);
51 char cbuf[2];
52 cbuf[1] = 0;
[6]53 int ln = 0;
[4]54 FILE* f = fopen(path, "r");
[6]55 if(f != NULL) {
[4]56 char* line = malloc(1);
57 line[0] = 0;
[6]58 int stop = 0;
[12]59 struct tw_config_entry* current = &config.root;
[6]60 char* vhost = NULL;
61 while(stop == 0) {
[4]62 int c = fread(cbuf, 1, 1, f);
[6]63 if(cbuf[0] == '\n' || c <= 0) {
64 ln++;
[4]65 char* l = cm_trim(line);
[6]66 if(strlen(l) > 0 && l[0] != '#') {
[5]67 char** r = cm_split(l, " \t");
68 int i;
[6]69 if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
70 for(i = 1; r[i] != NULL; i++) {
71 if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
72 stop = 1;
73 break;
[5]74 }
75 }
[6]76 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
77 if(vhost != NULL) {
[12]78 cm_log("Config", "Already in virtual host section at line %d", ln);
[6]79 stop = 1;
80 } else {
81 if(r[1] == NULL) {
[12]82 cm_log("Config", "Missing virtual host at line %d", ln);
[6]83 stop = 1;
84 } else {
85 vhost = cm_strdup(r[1]);
[12]86 current = &config.vhosts[config.vhost_count++];
87 int i;
88 current->name = cm_strdup(vhost);
[13]89 current->port = -1;
[12]90 for(i = 0; vhost[i] != 0; i++) {
91 if(vhost[i] == ':') {
92 current->name[i] = 0;
93 current->port = atoi(current->name + i + 1);
94 break;
95 }
96 }
[6]97 }
98 }
99 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
100 if(vhost == NULL) {
[12]101 cm_log("Config", "Not in virtual host section at line %d", ln);
[6]102 stop = 1;
103 } else {
104 free(vhost);
105 vhost = NULL;
[12]106 current = &config.root;
[6]107 }
[7]108 } else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
109 for(i = 1; r[i] != NULL; i++) {
110 uint64_t port = atoi(r[i]);
111 cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
112 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
113 int j;
114 for(j = 0; config.ports[j] != -1; j++)
115 ;
116 config.ports[j] = port;
117 }
[12]118 } else if(cm_strcaseequ(r[0], "SSLKey")) {
119 if(r[1] == NULL) {
120 cm_log("Config", "Missing path at line %d", ln);
121 stop = 1;
122 } else {
123 if(current->sslkey != NULL) free(current->sslkey);
124 current->sslkey = cm_strdup(r[1]);
125 }
126 } else if(cm_strcaseequ(r[0], "SSLCertificate")) {
127 if(r[1] == NULL) {
128 cm_log("Config", "Missing path at line %d", ln);
129 stop = 1;
130 } else {
131 if(current->sslcert != NULL) free(current->sslcert);
132 current->sslcert = cm_strdup(r[1]);
133 }
[19]134 } else if(cm_strcaseequ(r[0], "DocumentRoot")) {
135 if(r[1] == NULL) {
136 cm_log("Config", "Missing path at line %d", ln);
137 stop = 1;
138 } else {
139 if(current->root != NULL) free(current->root);
140 current->root = cm_strdup(r[1]);
141 }
[17]142 } else if(cm_strcaseequ(r[0], "ServerRoot")) {
143 if(r[1] == NULL) {
144 cm_log("Config", "Missing path at line %d", ln);
145 stop = 1;
146 } else {
147 if(config.server_root != NULL) free(config.server_root);
148 config.server_root = cm_strdup(r[1]);
149 }
150 } else if(cm_strcaseequ(r[0], "LoadModule")) {
151 for(i = 1; r[i] != NULL; i++) {
152 void* mod = tw_module_load(r[i]);
153 if(mod != NULL) {
[18]154 config.modules[config.module_count++] = mod;
[17]155 if(tw_module_init(mod) != 0) {
156 stop = 1;
157 break;
158 }
159 } else {
160 stop = 1;
161 break;
162 }
163 }
[6]164 } else {
165 if(r[0] != NULL) {
166 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
167 }
168 stop = 1;
[5]169 }
170 for(i = 0; r[i] != NULL; i++) free(r[i]);
171 free(r);
[4]172 }
173 free(l);
174 free(line);
175 line = malloc(1);
176 line[0] = 0;
177 if(c <= 0) break;
[6]178 } else if(cbuf[0] != '\r') {
[4]179 char* tmp = line;
180 line = cm_strcat(tmp, cbuf);
181 free(tmp);
182 }
183 }
184 free(line);
185 fclose(f);
[6]186 return stop;
187 } else {
[5]188 cm_log("Config", "Could not open the file");
[4]189 return 1;
190 }
191}
Note: See TracBrowser for help on using the repository browser.