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

Last change on this file since 16 was 16, checked in by Nishi, on Sep 14, 2024 at 12:09:52 AM

can parse http now

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