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

Last change on this file since 18 was 18, checked in by Nishi, on Sep 14, 2024 at 9:42:40 AM

can return response now

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