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
RevLine 
[4]1/* $Id: config.c 18 2024-09-14 00:42:40Z 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;
37 }
38 config.root.sslkey = NULL;
39 config.root.sslcert = NULL;
40 config.vhost_count = 0;
[18]41 config.module_count = 0;
42 config.extension = NULL;
[17]43 config.server_root = cm_strdup(PREFIX);
[12]44 gethostname(config.hostname, 1024);
[7]45}
[6]46
47int tw_config_read(const char* path) {
[4]48 cm_log("Config", "Reading %s", path);
49 char cbuf[2];
50 cbuf[1] = 0;
[6]51 int ln = 0;
[4]52 FILE* f = fopen(path, "r");
[6]53 if(f != NULL) {
[4]54 char* line = malloc(1);
55 line[0] = 0;
[6]56 int stop = 0;
[12]57 struct tw_config_entry* current = &config.root;
[6]58 char* vhost = NULL;
59 while(stop == 0) {
[4]60 int c = fread(cbuf, 1, 1, f);
[6]61 if(cbuf[0] == '\n' || c <= 0) {
62 ln++;
[4]63 char* l = cm_trim(line);
[6]64 if(strlen(l) > 0 && l[0] != '#') {
[5]65 char** r = cm_split(l, " \t");
66 int i;
[6]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;
[5]72 }
73 }
[6]74 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
75 if(vhost != NULL) {
[12]76 cm_log("Config", "Already in virtual host section at line %d", ln);
[6]77 stop = 1;
78 } else {
79 if(r[1] == NULL) {
[12]80 cm_log("Config", "Missing virtual host at line %d", ln);
[6]81 stop = 1;
82 } else {
83 vhost = cm_strdup(r[1]);
[12]84 current = &config.vhosts[config.vhost_count++];
85 int i;
86 current->name = cm_strdup(vhost);
[13]87 current->port = -1;
[12]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 }
[6]95 }
96 }
97 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
98 if(vhost == NULL) {
[12]99 cm_log("Config", "Not in virtual host section at line %d", ln);
[6]100 stop = 1;
101 } else {
102 free(vhost);
103 vhost = NULL;
[12]104 current = &config.root;
[6]105 }
[7]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 }
[12]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 }
[17]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) {
[18]144 config.modules[config.module_count++] = mod;
[17]145 if(tw_module_init(mod) != 0) {
146 stop = 1;
147 break;
148 }
149 } else {
150 stop = 1;
151 break;
152 }
153 }
[6]154 } else {
155 if(r[0] != NULL) {
156 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
157 }
158 stop = 1;
[5]159 }
160 for(i = 0; r[i] != NULL; i++) free(r[i]);
161 free(r);
[4]162 }
163 free(l);
164 free(line);
165 line = malloc(1);
166 line[0] = 0;
167 if(c <= 0) break;
[6]168 } else if(cbuf[0] != '\r') {
[4]169 char* tmp = line;
170 line = cm_strcat(tmp, cbuf);
171 free(tmp);
172 }
173 }
174 free(line);
175 fclose(f);
[6]176 return stop;
177 } else {
[5]178 cm_log("Config", "Could not open the file");
[4]179 return 1;
180 }
181}
Note: See TracBrowser for help on using the repository browser.