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

Last change on this file since 12 was 12, checked in by Nishi, on Sep 13, 2024 at 10:36:03 PM

vhost works

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