Index: /tags/1.03A/.clang-format
===================================================================
--- /tags/1.03A/.clang-format	(revision 125)
+++ /tags/1.03A/.clang-format	(revision 125)
@@ -0,0 +1,15 @@
+---
+# $Id$
+Language: Cpp
+UseTab: Always
+TabWidth: 8
+IndentWidth: 8
+PointerAlignment: Left
+ColumnLimit: 1024
+AllowShortIfStatementsOnASingleLine: Always
+AllowShortBlocksOnASingleLine: Never
+AllowShortLoopsOnASingleLine: true
+SpaceBeforeParens: Never
+AlignEscapedNewlines: DontAlign
+SortIncludes: false
+AllowShortEnumsOnASingleLine: false
Index: /tags/1.03A/.github/workflows/build.yml
===================================================================
--- /tags/1.03A/.github/workflows/build.yml	(revision 125)
+++ /tags/1.03A/.github/workflows/build.yml	(revision 125)
@@ -0,0 +1,99 @@
+# $Id$
+
+name: "Build Tewi HTTPd"
+
+on:
+  workflow_dispatch:
+  push:
+
+concurrency:
+  group: "build"
+  cancel-in-progress: true
+
+jobs:
+  build-win32:
+    name: "Build for Windows 32-bit"
+    runs-on: ubuntu-latest
+
+    permissions:
+      contents: write
+
+    steps:
+    - name: Checkout
+      uses: actions/checkout@v4
+    - name: Install packages
+      run: sudo apt-get install nsis mingw-w64
+    - name: Get OpenSSL
+      run: git clone https://github.com/clamwin/openssl
+    - name: Make it use the correct library
+      run: cp openssl/lib/mingw/x86/*.a openssl/lib/
+    - name: Build
+      run: ./installer.sh win32
+    - name: Rename
+      run: mv install-nossl.exe tewiinst32-nossl.exe && mv install-ssl.exe tewiinst32-ssl.exe
+    - name: Create a zipfile
+      run: zip -rv tewiinst32.zip tewiinst32-*.exe
+    - name: Upload artifact
+      uses: actions/upload-artifact@v4
+      with:
+        name: build-win32
+        path: tewiinst32.zip
+
+  build-win64:
+    name: "Build for Windows 64-bit"
+    runs-on: ubuntu-latest
+
+    permissions:
+      contents: write
+
+    steps:
+    - name: Checkout
+      uses: actions/checkout@v4
+    - name: Install packages
+      run: sudo apt-get install nsis mingw-w64
+    - name: Get OpenSSL
+      run: git clone https://github.com/clamwin/openssl
+    - name: Make it use the correct library
+      run: cp openssl/lib/mingw/x64/*.a openssl/lib/
+    - name: Build
+      run: ./installer.sh win64
+    - name: Rename
+      run: mv install-nossl.exe tewiinst64-nossl.exe && mv install-ssl.exe tewiinst64-ssl.exe
+    - name: Create a zipfile
+      run: zip -rv tewiinst64.zip tewiinst64-*.exe
+    - name: Upload artifact
+      uses: actions/upload-artifact@v4
+      with:
+        name: build-win64
+        path: tewiinst64.zip
+
+  release:
+    name: "Release"
+    runs-on: ubuntu-latest
+
+    permissions:
+      contents: write
+
+    needs: [build-win32, build-win64]
+
+    steps:
+    - name: Checkout
+      uses: actions/checkout@v4
+    - name: Download from build-win32
+      uses: actions/download-artifact@v4
+      with:
+        name: build-win32
+    - name: Download from build-win64
+      uses: actions/download-artifact@v4
+      with:
+        name: build-win64
+    - name: Extract
+      run: unzip tewiinst32.zip && unzip tewiinst64.zip
+    - name: Delete old release
+      run: gh release delete "$(make get-version)" --cleanup-tag -y || true
+      env:
+        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+    - name: Release
+      run: gh release create -t "v$(make get-version)" "$(make get-version)"  tewiinst32*.exe tewiinst64*.exe -n "Version \`$(make get-version)\` was build in this release"
+      env:
+        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Index: /tags/1.03A/Common/Makefile
===================================================================
--- /tags/1.03A/Common/Makefile	(revision 125)
+++ /tags/1.03A/Common/Makefile	(revision 125)
@@ -0,0 +1,19 @@
+# $Id$
+
+include $(PWD)/Platform/$(PLATFORM).mk
+
+.PHONY: all clean
+.SUFFIXES: .c .o
+
+OBJS = string.o log.o dir.o
+
+all: common.a
+
+common.a: $(OBJS)
+	$(AR) rcs $@ $(OBJS)
+
+.c.o:
+	$(CC) $(CFLAGS) -c -o $@ $<
+
+clean:
+	rm -f *.o *.a
Index: /tags/1.03A/Common/cm_dir.h
===================================================================
--- /tags/1.03A/Common/cm_dir.h	(revision 125)
+++ /tags/1.03A/Common/cm_dir.h	(revision 125)
@@ -0,0 +1,8 @@
+/* $Id$ */
+
+#ifndef __CM_DIR_H__
+#define __CM_DIR_H__
+
+char** cm_scandir(const char* path);
+
+#endif
Index: /tags/1.03A/Common/cm_log.h
===================================================================
--- /tags/1.03A/Common/cm_log.h	(revision 125)
+++ /tags/1.03A/Common/cm_log.h	(revision 125)
@@ -0,0 +1,9 @@
+/* $Id$ */
+
+#ifndef __CM_LOG_H__
+#define __CM_LOG_H__
+
+void cm_log(const char* name, const char* log, ...);
+void cm_force_log(const char* log);
+
+#endif
Index: /tags/1.03A/Common/cm_string.h
===================================================================
--- /tags/1.03A/Common/cm_string.h	(revision 125)
+++ /tags/1.03A/Common/cm_string.h	(revision 125)
@@ -0,0 +1,22 @@
+/* $Id$ */
+
+#ifndef __CM_STRING_H__
+#define __CM_STRING_H__
+
+#include <stdbool.h>
+
+int cm_hex(const char* str, int len);
+bool cm_nocase_endswith(const char* str, const char* end);
+bool cm_endswith(const char* str, const char* end);
+char* cm_html_escape(const char* str);
+char* cm_url_escape(const char* str);
+char* cm_strcat(const char* a, const char* b);
+char* cm_strcat3(const char* a, const char* b, const char* c);
+char* cm_strdup(const char* str);
+char* cm_trimstart(const char* str);
+char* cm_trimend(const char* str);
+char* cm_trim(const char* str);
+char** cm_split(const char* str, const char* by);
+bool cm_strcaseequ(const char* a, const char* b);
+
+#endif
Index: /tags/1.03A/Common/dir.c
===================================================================
--- /tags/1.03A/Common/dir.c	(revision 125)
+++ /tags/1.03A/Common/dir.c	(revision 125)
@@ -0,0 +1,61 @@
+/* $Id$ */
+
+#include "cm_dir.h"
+
+#include "cm_string.h"
+
+#include <sys/stat.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <string.h>
+
+int cm_sort(const void* _a, const void* _b) {
+	char* a = *(char**)_a;
+	char* b = *(char**)_b;
+	return strcmp(a, b);
+}
+
+char** cm_scandir(const char* path) {
+	DIR* dir = opendir(path);
+	if(dir != NULL) {
+		char** r = malloc(sizeof(*r));
+		r[0] = NULL;
+		struct dirent* d;
+		while((d = readdir(dir)) != NULL) {
+			if(strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
+				struct stat s;
+				char* p = cm_strcat3(path, "/", d->d_name);
+				stat(p, &s);
+				free(p);
+
+				char** old = r;
+				int i;
+				for(i = 0; old[i] != NULL; i++)
+					;
+				r = malloc(sizeof(*r) * (i + 2));
+				for(i = 0; old[i] != NULL; i++) r[i] = old[i];
+				r[i] = cm_strcat(d->d_name, S_ISDIR(s.st_mode) ? "/" : "");
+				r[i + 1] = NULL;
+				free(old);
+			}
+		}
+		int len;
+		for(len = 0; r[len] != NULL; len++)
+			;
+		qsort(r, len, sizeof(char*), cm_sort);
+
+		char** old = r;
+		int i;
+		for(i = 0; old[i] != NULL; i++)
+			;
+		r = malloc(sizeof(*r) * (i + 2));
+		for(i = 0; old[i] != NULL; i++) r[i + 1] = old[i];
+		r[0] = cm_strdup("../");
+		r[i + 1] = NULL;
+		free(old);
+
+		return r;
+	} else {
+		return NULL;
+	}
+}
Index: /tags/1.03A/Common/log.c
===================================================================
--- /tags/1.03A/Common/log.c	(revision 125)
+++ /tags/1.03A/Common/log.c	(revision 125)
@@ -0,0 +1,75 @@
+/* $Id$ */
+
+#include "cm_log.h"
+
+#include "cm_string.h"
+
+#include <time.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+FILE* logfile;
+
+bool cm_do_log = false;
+
+#define LOGNAME_LENGTH 12
+
+void cm_force_log(const char* log) {
+	time_t t = time(NULL);
+	struct tm* tm = localtime(&t);
+	char date[513];
+	strftime(date, 512, "%a %b %d %H:%M:%S %Z %Y", tm);
+	fprintf(logfile, "[%s] %s\n", date, log);
+	fflush(logfile);
+}
+
+void cm_log(const char* name, const char* log, ...) {
+	if(!cm_do_log) return;
+	va_list args;
+	va_start(args, log);
+	char namebuf[LOGNAME_LENGTH + 1];
+	memset(namebuf, '.', LOGNAME_LENGTH);
+	namebuf[LOGNAME_LENGTH] = 0;
+	int i;
+	for(i = 0; name[i] != 0 && i < LOGNAME_LENGTH; i++) {
+		namebuf[i] = name[i];
+	}
+
+	char* result = malloc(1);
+	result[0] = 0;
+
+	char cbuf[2];
+	cbuf[1] = 0;
+
+	for(i = 0; log[i] != 0; i++) {
+		if(log[i] == '%') {
+			i++;
+			if(log[i] == 's') {
+				char* tmp = result;
+				char* c = va_arg(args, char*);
+				result = cm_strcat(tmp, c == NULL ? "(null)" : c);
+				free(tmp);
+			} else if(log[i] == 'd') {
+				int a = va_arg(args, int);
+				char buf[128];
+				sprintf(buf, "%d", a);
+				char* tmp = result;
+				result = cm_strcat(tmp, buf);
+				free(tmp);
+			}
+		} else {
+			cbuf[0] = log[i];
+			char* tmp = result;
+			result = cm_strcat(tmp, cbuf);
+			free(tmp);
+		}
+	}
+
+	fprintf(logfile, "%s %s\n", namebuf, result);
+	va_end(args);
+
+	free(result);
+}
Index: /tags/1.03A/Common/string.c
===================================================================
--- /tags/1.03A/Common/string.c	(revision 125)
+++ /tags/1.03A/Common/string.c	(revision 125)
@@ -0,0 +1,204 @@
+/* $Id$ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <ctype.h>
+
+char* cm_strcat(const char* a, const char* b) {
+	if(a == NULL) a = "";
+	if(b == NULL) b = "";
+	char* str = malloc(strlen(a) + strlen(b) + 1);
+	memcpy(str, a, strlen(a));
+	memcpy(str + strlen(a), b, strlen(b));
+	str[strlen(a) + strlen(b)] = 0;
+	return str;
+}
+
+char* cm_strcat3(const char* a, const char* b, const char* c) {
+	char* tmp = cm_strcat(a, b);
+	char* str = cm_strcat(tmp, c);
+	free(tmp);
+	return str;
+}
+
+char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
+
+bool cm_endswith(const char* str, const char* end) {
+	if(strlen(str) < strlen(end)) return false;
+	int i;
+	for(i = strlen(str) - strlen(end); i < strlen(str); i++) {
+		if(str[i] != end[i - strlen(str) + strlen(end)]) return false;
+	}
+	return true;
+}
+
+bool cm_nocase_endswith(const char* str, const char* end) {
+	if(strlen(str) < strlen(end)) return false;
+	int i;
+	for(i = strlen(str) - strlen(end); i < strlen(str); i++) {
+		if(tolower(str[i]) != tolower(end[i - strlen(str) + strlen(end)])) return false;
+	}
+	return true;
+}
+
+char* cm_trimstart(const char* str) {
+	int i;
+	for(i = 0; str[i] != 0; i++) {
+		if(str[i] != ' ' && str[i] != '\t') {
+			return cm_strdup(str + i);
+		}
+	}
+	return cm_strdup("");
+}
+
+char* cm_trimend(const char* str) {
+	char* s = cm_strdup(str);
+	int i;
+	for(i = strlen(s) - 1; i >= 0; i--) {
+		if(s[i] != '\t' && s[i] != ' ') {
+			s[i + 1] = 0;
+			break;
+		}
+	}
+	return s;
+}
+
+char* cm_trim(const char* str) {
+	char* tmp = cm_trimstart(str);
+	char* s = cm_trimend(tmp);
+	free(tmp);
+	return s;
+}
+
+char** cm_split(const char* str, const char* by) {
+	int i;
+	char** r = malloc(sizeof(*r));
+	r[0] = NULL;
+	char* b = malloc(1);
+	b[0] = 0;
+	char cbuf[2];
+	cbuf[1] = 0;
+	bool dq = false;
+	bool sq = false;
+	for(i = 0;; i++) {
+		int j;
+		bool has = false;
+		for(j = 0; by[j] != 0; j++) {
+			if(by[j] == str[i]) {
+				has = true;
+				break;
+			}
+		}
+		if(!(dq || sq) && (has || str[i] == 0)) {
+			if(strlen(b) > 0) {
+				char** old = r;
+				int j;
+				for(j = 0; old[j] != NULL; j++)
+					;
+				r = malloc(sizeof(*r) * (j + 2));
+				for(j = 0; old[j] != NULL; j++) r[j] = old[j];
+				r[j] = b;
+				r[j + 1] = NULL;
+				free(old);
+			}
+			b = malloc(1);
+			b[0] = 0;
+			if(str[i] == 0) break;
+		} else {
+			if(str[i] == '"' && !sq) {
+				dq = !dq;
+			} else if(str[i] == '\'' && !dq) {
+				sq = !sq;
+			} else {
+				cbuf[0] = str[i];
+				char* tmp = b;
+				b = cm_strcat(tmp, cbuf);
+				free(tmp);
+			}
+		}
+	}
+	free(b);
+	return r;
+}
+
+bool cm_strcaseequ(const char* a, const char* b) {
+	if(a == NULL) return false;
+	if(b == NULL) return false;
+	if(strlen(a) != strlen(b)) return false;
+	int i;
+	for(i = 0; a[i] != 0; i++) {
+		if(tolower(a[i]) != tolower(b[i])) return false;
+	}
+	return true;
+}
+
+int cm_hex(const char* str, int len) {
+	int n = 0;
+	int i;
+	for(i = 0; i < len; i++) {
+		char c = str[i];
+		n *= 16;
+		if('0' <= c && c <= '9') {
+			n += c - '0';
+		} else if('a' <= c && c <= 'f') {
+			n += c - 'a' + 10;
+		} else if('A' <= c && c <= 'F') {
+			n += c - 'A' + 10;
+		}
+	}
+	return n;
+}
+
+char* cm_html_escape(const char* str) {
+	int i;
+	char* result = malloc(1);
+	result[0] = 0;
+	char cbuf[2];
+	cbuf[1] = 0;
+	for(i = 0; str[i] != 0; i++) {
+		cbuf[0] = str[i];
+		if(str[i] == '&') {
+			char* tmp = result;
+			result = cm_strcat(tmp, "&amp;");
+			free(tmp);
+		} else if(str[i] == '<') {
+			char* tmp = result;
+			result = cm_strcat(tmp, "&lt;");
+			free(tmp);
+		} else if(str[i] == '>') {
+			char* tmp = result;
+			result = cm_strcat(tmp, "&gt;");
+			free(tmp);
+		} else {
+			char* tmp = result;
+			result = cm_strcat(tmp, cbuf);
+			free(tmp);
+		}
+	}
+	return result;
+}
+
+char* cm_url_escape(const char* str) {
+	int i;
+	char* result = malloc(1);
+	result[0] = 0;
+	char cbuf[2];
+	cbuf[1] = 0;
+	for(i = 0; str[i] != 0; i++) {
+		cbuf[0] = str[i];
+		if('!' <= str[i] && str[i] <= '@' && str[i] != '.' && str[i] != '-' && str[i] != '/' && !('0' <= str[i] && str[i] <= '9')) {
+			char code[4];
+			sprintf(code, "%%%02X", str[i]);
+			char* tmp = result;
+			result = cm_strcat(tmp, code);
+			free(tmp);
+		} else {
+			char* tmp = result;
+			result = cm_strcat(tmp, cbuf);
+			free(tmp);
+		}
+	}
+	return result;
+}
Index: /tags/1.03A/Icons/README
===================================================================
--- /tags/1.03A/Icons/README	(revision 125)
+++ /tags/1.03A/Icons/README	(revision 125)
@@ -0,0 +1,1 @@
+Thanks to https://www.apache.org/icons/small/
Index: /tags/1.03A/LICENSE
===================================================================
--- /tags/1.03A/LICENSE	(revision 125)
+++ /tags/1.03A/LICENSE	(revision 125)
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>
Index: /tags/1.03A/Makefile
===================================================================
--- /tags/1.03A/Makefile	(revision 125)
+++ /tags/1.03A/Makefile	(revision 125)
@@ -0,0 +1,48 @@
+# $Id$
+
+PWD = `pwd`
+PLATFORM = generic
+PREFIX = /usr/local
+
+include Platform/$(PLATFORM).mk
+
+FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) PREFIX=$(PREFIX)
+
+.PHONY: all format clean ./Server ./Common ./Module ./Manpage get-version
+
+all: ./Server ./Module ./Manpage
+
+./Server/option: ./Server/option.c
+	cc -o $@ ./Server/option.c
+
+./Server:: ./Common ./Server/option
+	$(MAKE) -C $@ $(FLAGS) EXTOBJS="`./Server/option objs ../`" EXTLIBS="`./Server/option libs ../`" EXTCFLAGS="`./Server/option cflags ../`" EXTLDFLAGS="`./Server/option ldflags ../`"
+
+./Module:: ./Common
+	$(MAKE) -C $@ $(FLAGS)
+
+./Common::
+	$(MAKE) -C $@ $(FLAGS)
+
+./Manpage::
+	$(MAKE) -C $@ $(FLAGS)
+
+install: all
+	mkdir -p $(PREFIX)/bin $(PREFIX)/lib/tewi $(PREFIX)/share/man/man5 $(PREFIX)/etc
+	if [ ! -e $(PREFIX)/etc/tewi.conf ]; then cp example.conf $(PREFIX)/etc/tewi.conf ; fi
+	cp ./Server/tewi $(PREFIX)/bin/
+	cp ./Module/*.so $(PREFIX)/lib/tewi/
+	cp ./Manpage/tewi.conf.5 $(PREFIX)/share/man/man5/
+
+format:
+	clang-format --verbose -i `find ./Server ./Common ./Module "(" -name "*.c" -or -name "*.h" ")" -and -not -name "strptime.*"` config.h
+
+get-version:
+	@grep "define TW_VERSION" Server/tw_version.h | grep -Eo '"[^\]+' | sed -E 's/^"//g'
+
+clean:
+	$(MAKE) -C ./Server $(FLAGS) clean
+	$(MAKE) -C ./Module $(FLAGS) clean
+	$(MAKE) -C ./Common $(FLAGS) clean
+	$(MAKE) -C ./Manpage $(FLAGS) clean
+	rm -f ./Server/option
Index: /tags/1.03A/Manpage/Makefile
===================================================================
--- /tags/1.03A/Manpage/Makefile	(revision 125)
+++ /tags/1.03A/Manpage/Makefile	(revision 125)
@@ -0,0 +1,11 @@
+# $Id$
+
+.PHONY: all clean
+
+all: tewi.conf.5
+
+tewi.conf.5: tewi.conf.5.tmpl
+	sed "s%@PREFIX@%$(PREFIX)%g" tewi.conf.5.tmpl > $@ || rm -f $@
+
+clean:
+	rm -f tewi.conf.5
Index: /tags/1.03A/Manpage/tewi.conf.5.tmpl
===================================================================
--- /tags/1.03A/Manpage/tewi.conf.5.tmpl	(revision 125)
+++ /tags/1.03A/Manpage/tewi.conf.5.tmpl	(revision 125)
@@ -0,0 +1,92 @@
+\" $Id$
+.Dd 2024 Sep 16th
+.Dt TEWI 8
+.Os
+.Sh NAME
+.Nm tewi ,
+.Nm tewi.conf
+.Nd Tewi HTTPd
+.Sh DESCRIPTION
+.Nm
+is a simple public domain HTTP daemon written in C.
+.Sh CONFIGURATION
+.Ss "General settings"
+.Bl -tag -width 20 -compact
+.It Pa "Listen"
+Specifies the port(s) to be listened on.
+.It Pa "DocumentRoot"
+Specifies the document root.
+.It Pa "LoadModule"
+Loads the module.
+.It Pa "HidePort" "ShowPort"
+Shows/Hides the port in directory listing, and the default error page.
+.It Pa "MIMEType"
+Specifies the MIME type for files.
+\fBall\fR can be used instead of the extension if you want to make all extensions appear as same MIME.
+Later rules will override the MIME.
+
+Example:
+
+.Vb 1
+\&    MIMEType all application/octet-stream
+
+\&    MIMEType .html text/html
+.Ve
+
+.It Pa "BeginVirtualHost"
+Begins the virtual host section.
+.It Pa "EndVirtualHost"
+Ends the virtual host section.
+.El
+.Ss "TLS/SSL settings"
+.Bl -tag -width 20 -compact
+.It Pa "ListenSSL"
+Specifies the port(s) to be listened on, but with TLS/SSL.
+.It Pa "SSLKey"
+Specifies the TLS/SSL private key.
+This works in the virtual host section too, and can be used to switch key depending on the virtual host.
+.It Pa "SSLCertificate"
+Specifies the TLS/SSL certificate.
+This works in the virtual host section too, and can be used to switch certificate depending on the virtual host.
+.El
+.Ss "Access settings"
+.Bl -tag -width 20 -compact
+Default access is "Deny all".
+
+.It Pa "BeginDirectory"
+Begins the directory section.
+.It Pa "EndDirectory"
+Ends the directory section.
+.It Pa "Allow"
+Allows the connection. Only works with \fBall\fR for now.
+Will not work outside of the directory section.
+.It Pa "Deny"
+Denies the connection. Only works with \fBall\fR for now.
+Will not work outside of the directory section.
+.El
+.Ss "Directory settings"
+.Bl -tag -width 20 -compact
+.It Pa "DirectoryIndex"
+Specifies the directory index.
+.It Pa "Readme"
+Specifies the README file for directory listing.
+.It Pa "Icon"
+Sets the icon for the MIME type.
+\fBall\fR can be used instead of the MIME if you want to make all MIMEs appear as same icon.
+Later rules will override the icon.
+
+Example:
+
+.Vb 1
+\&    Icon all /icons/unknown.png
+
+\&    Icon text/* /icons/text.png
+.Ve
+.El
+.Sh FILES
+.Bl -tag -width @PREFIX@/etc/tewi.conf -compact
+.It Pa @PREFIX@/etc/tewi.conf
+Default path for the config
+.El
+.Sh AUTHORS
+.An Nishi Aq Mt nishi@nishi.boats
Index: /tags/1.03A/Module/Makefile
===================================================================
--- /tags/1.03A/Module/Makefile	(revision 125)
+++ /tags/1.03A/Module/Makefile	(revision 125)
@@ -0,0 +1,17 @@
+# $Id$
+
+include $(PWD)/Platform/$(PLATFORM).mk
+
+.PHONY: all clean
+.SUFFIXES: .c .o $(LIB)
+
+all: mod_cgi$(LIB)
+
+.o$(LIB):
+	$(CC) $(LDFLAGS) -shared -o $@ $< ../Common/common.a $(LIBS)
+
+.c.o:
+	$(CC) $(CFLAGS) -c -o $@ $<
+
+clean:
+	rm -f *.o *.so *.a *.dll
Index: /tags/1.03A/Module/mod_cgi.c
===================================================================
--- /tags/1.03A/Module/mod_cgi.c	(revision 125)
+++ /tags/1.03A/Module/mod_cgi.c	(revision 125)
@@ -0,0 +1,23 @@
+/* $Id$ */
+
+#include "../Server/tw_module.h"
+
+#include <cm_string.h>
+
+int mod_init(struct tw_config* config, struct tw_tool* tools) {
+	tools->log("CGI", "Initializing CGI module");
+	tools->add_version("CGI/1.1");
+	return 0;
+}
+
+int mod_config(struct tw_tool* tools, char** argv, int argc) {
+	if(cm_strcaseequ(argv[0], "AllowCGI")) {
+		return TW_CONFIG_PARSED;
+	}
+	return TW_CONFIG_NOTME;
+}
+
+int mod_request(struct tw_tool* tools, struct tw_http_request* req, struct tw_http_response* res) {
+	res->status = 403;
+	return TW_MODULE_STOP;
+}
Index: /tags/1.03A/Module/mod_example.c
===================================================================
--- /tags/1.03A/Module/mod_example.c	(revision 125)
+++ /tags/1.03A/Module/mod_example.c	(revision 125)
@@ -0,0 +1,14 @@
+/* $Id$ */
+/* This module will accept all directives, and always return 403 on the access. */
+
+#include "../Server/tw_module.h"
+
+int mod_init(struct tw_config* config, struct tw_tool* tools) {
+	tools->log("Example", "This is an example module");
+	tools->add_version("Example/0.0");
+	return 0;
+}
+
+int mod_config(struct tw_tool* tools, char** argv, int argc) { return TW_CONFIG_PARSED; }
+
+int mod_request(struct tw_tool* tools, struct tw_http_request* req, struct tw_http_response* res) { return TW_MODULE_ERROR(403); }
Index: /tags/1.03A/Platform/generic.mk
===================================================================
--- /tags/1.03A/Platform/generic.mk	(revision 125)
+++ /tags/1.03A/Platform/generic.mk	(revision 125)
@@ -0,0 +1,9 @@
+# $Id$
+
+CC = cc
+AR = ar
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC
+LDFLAGS =
+LIBS =
+EXEC =
+LIB = .so
Index: /tags/1.03A/Platform/haiku.mk
===================================================================
--- /tags/1.03A/Platform/haiku.mk	(revision 125)
+++ /tags/1.03A/Platform/haiku.mk	(revision 125)
@@ -0,0 +1,9 @@
+# $Id$
+
+CC = cc
+AR = ar
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC
+LDFLAGS =
+LIBS = -lnetwork
+EXEC =
+LIB = .so
Index: /tags/1.03A/Platform/linux.mk
===================================================================
--- /tags/1.03A/Platform/linux.mk	(revision 125)
+++ /tags/1.03A/Platform/linux.mk	(revision 125)
@@ -0,0 +1,9 @@
+# $Id$
+
+CC = cc
+AR = ar
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -D_DEFAULT_SOURCE -fPIC -D_XOPEN_SOURCE=600
+LDFLAGS =
+LIBS =
+EXEC =
+LIB = .so
Index: /tags/1.03A/Platform/netbsd.mk
===================================================================
--- /tags/1.03A/Platform/netbsd.mk	(revision 125)
+++ /tags/1.03A/Platform/netbsd.mk	(revision 125)
@@ -0,0 +1,9 @@
+# $Id$
+
+CC = cc
+AR = ar
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC
+LDFLAGS =
+LIBS =
+EXEC =
+LIB = .so
Index: /tags/1.03A/Platform/win32-service.mk
===================================================================
--- /tags/1.03A/Platform/win32-service.mk	(revision 125)
+++ /tags/1.03A/Platform/win32-service.mk	(revision 125)
@@ -0,0 +1,13 @@
+# $Id$
+
+PREFIX = C:/Tewi
+
+CC = i686-w64-mingw32-gcc
+AR = i686-w64-mingw32-ar
+WINDRES = i686-w64-mingw32-windres
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC -DSERVICE -mwindows
+LDFLAGS = -mwindows
+LIBS = -lws2_32
+EXEC = .exe
+LIB = .dll
+PREOBJS = tewi.res
Index: /tags/1.03A/Platform/win32.mk
===================================================================
--- /tags/1.03A/Platform/win32.mk	(revision 125)
+++ /tags/1.03A/Platform/win32.mk	(revision 125)
@@ -0,0 +1,13 @@
+# $Id$
+
+PREFIX = C:/Tewi
+
+CC = i686-w64-mingw32-gcc
+AR = i686-w64-mingw32-ar
+WINDRES = i686-w64-mingw32-windres
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC
+LDFLAGS =
+LIBS = -lws2_32
+EXEC = .exe
+LIB = .dll
+PREOBJS = tewi.res
Index: /tags/1.03A/Platform/win64-service.mk
===================================================================
--- /tags/1.03A/Platform/win64-service.mk	(revision 125)
+++ /tags/1.03A/Platform/win64-service.mk	(revision 125)
@@ -0,0 +1,13 @@
+# $Id$
+
+PREFIX = C:/Tewi
+
+CC = x86_64-w64-mingw32-gcc
+AR = x86_64-w64-mingw32-ar
+WINDRES = x86_64-w64-mingw32-windres
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC -mwindows
+LDFLAGS = -mwindows
+LIBS = -lws2_32
+EXEC = .exe
+LIB = .dll
+PREOBJS = tewi.res
Index: /tags/1.03A/Platform/win64.mk
===================================================================
--- /tags/1.03A/Platform/win64.mk	(revision 125)
+++ /tags/1.03A/Platform/win64.mk	(revision 125)
@@ -0,0 +1,13 @@
+# $Id$
+
+PREFIX = C:/Tewi
+
+CC = x86_64-w64-mingw32-gcc
+AR = x86_64-w64-mingw32-ar
+WINDRES = x86_64-w64-mingw32-windres
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC
+LDFLAGS =
+LIBS = -lws2_32
+EXEC = .exe
+LIB = .dll
+PREOBJS = tewi.res
Index: /tags/1.03A/README
===================================================================
--- /tags/1.03A/README	(revision 125)
+++ /tags/1.03A/README	(revision 125)
@@ -0,0 +1,13 @@
+Simple HTTP daemon
+
+Put under public domain, except Server/strptime.{c,h} and Icons/*.
+
+Original     : http://svn.nishi.boats/repo/tewi
+Git Mirror #1: http://git-mirror.nishi.boats/?p=tewi.git
+Git Mirror #2: https://git.chaotic.ninja/YakumoLabs/tewi
+Git Mirror #3: https://github.com/pnsk-lab/tewi
+
+Installers for Windows: http://f.nishi.boats/f/g/tewi
+                        or, GitHub releases
+Website               : http://nishi.boats/tewi
+Trac                  : https://trac.nishi.boats/tewi
Index: /tags/1.03A/Server/Makefile
===================================================================
--- /tags/1.03A/Server/Makefile	(revision 125)
+++ /tags/1.03A/Server/Makefile	(revision 125)
@@ -0,0 +1,22 @@
+# $Id$
+
+include $(PWD)/Platform/$(PLATFORM).mk
+
+.PHONY: all clean
+.SUFFIXES: .c .o
+
+OBJS = version.o main.o config.o server.o http.o module.o strptime.o $(EXTOBJS) $(PREOBJS)
+
+all: tewi$(EXEC)
+
+tewi$(EXEC): $(OBJS) ../Common/common.a
+	$(CC) $(LDFLAGS) $(EXTLDFLAGS) -o $@ $(OBJS) $(EXTLIBS) $(LIBS) ../Common/common.a
+
+.c.o:
+	$(CC) $(CFLAGS) $(EXTCFLAGS) -c -o $@ $<
+
+tewi.res: tewi.rc tewi.ico
+	$(WINDRES) tewi.rc -O coff -o $@
+
+clean:
+	rm -f *.o tewi *.exe *.res
Index: /tags/1.03A/Server/config.c
===================================================================
--- /tags/1.03A/Server/config.c	(revision 125)
+++ /tags/1.03A/Server/config.c	(revision 125)
@@ -0,0 +1,361 @@
+/* $Id$ */
+
+#define SOURCE
+
+#include "tw_config.h"
+#include "tw_module.h"
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef __MINGW32__
+#include <winsock2.h>
+#endif
+
+#include <cm_string.h>
+#include <cm_log.h>
+
+struct tw_config config;
+
+struct tw_config_entry* tw_vhost_match(const char* name, int port) {
+	int i;
+	for(i = 0; i < config.vhost_count; i++) {
+		if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
+			return &config.vhosts[i];
+		}
+	}
+	return &config.root;
+}
+
+bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost) {
+	int i;
+	bool found = false;
+	bool pathstart = false;
+	bool perm = false;
+again:
+	for(i = 0; i < vhost->dir_count; i++) {
+		struct tw_dir_entry* e = &vhost->dirs[i];
+		pathstart = false;
+		if(strlen(path) >= strlen(e->dir)) {
+			pathstart = true;
+			int j;
+			for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
+				if(path[j] != e->dir[j]) {
+					pathstart = false;
+					break;
+				}
+			}
+		}
+		char* noslash = cm_strdup(e->dir);
+		noslash[strlen(noslash) - 1] = 0;
+		if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
+			found = true;
+			if(strcmp(e->name, "all") == 0) {
+				perm = e->type == TW_DIR_ALLOW;
+			}
+		}
+		free(noslash);
+	}
+	if(!found && vhost != &config.root) {
+		vhost = &config.root;
+		goto again;
+	}
+	return perm;
+}
+
+void tw_config_init(void) {
+	int i;
+	for(i = 0; i < MAX_PORTS + 1; i++) {
+		config.ports[i] = -1;
+	}
+	for(i = 0; i < MAX_VHOSTS; i++) {
+		config.vhosts[i].sslkey = NULL;
+		config.vhosts[i].sslcert = NULL;
+		config.vhosts[i].root = NULL;
+	}
+	config.root.sslkey = NULL;
+	config.root.sslcert = NULL;
+	config.root.root = NULL;
+	config.root.mime_count = 0;
+	config.root.dir_count = 0;
+	config.root.icon_count = 0;
+	config.root.index_count = 0;
+	config.root.readme_count = 0;
+	config.root.hideport = 0;
+	config.vhost_count = 0;
+	config.module_count = 0;
+	config.extension = NULL;
+	config.server_root = cm_strdup(PREFIX);
+	gethostname(config.hostname, 1024);
+}
+
+int tw_config_read(const char* path) {
+	cm_log("Config", "Reading %s", path);
+	char cbuf[2];
+	cbuf[1] = 0;
+	int ln = 0;
+	FILE* f = fopen(path, "r");
+	if(f != NULL) {
+		char* line = malloc(1);
+		line[0] = 0;
+		int stop = 0;
+		struct tw_config_entry* current = &config.root;
+		char* vhost = NULL;
+		char* dir = NULL;
+		while(stop == 0) {
+			int c = fread(cbuf, 1, 1, f);
+			if(cbuf[0] == '\n' || c <= 0) {
+				ln++;
+				char* l = cm_trim(line);
+				if(strlen(l) > 0 && l[0] != '#') {
+					char** r = cm_split(l, " \t");
+					int i;
+					if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
+						for(i = 1; r[i] != NULL; i++) {
+							if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
+								stop = 1;
+								break;
+							}
+						}
+					} else if(cm_strcaseequ(r[0], "BeginDirectory")) {
+						if(dir != NULL) {
+							cm_log("Config", "Already in directory section at line %d", ln);
+							stop = 1;
+						} else {
+							if(r[1] == NULL) {
+								cm_log("Config", "Missing directory at line %d", ln);
+								stop = 1;
+							} else {
+								dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
+							}
+						}
+					} else if(cm_strcaseequ(r[0], "EndDirectory")) {
+						if(dir == NULL) {
+							cm_log("Config", "Not in directory section at line %d", ln);
+							stop = 1;
+						} else {
+							free(dir);
+							dir = NULL;
+						}
+					} else if(cm_strcaseequ(r[0], "Allow")) {
+						if(dir == NULL) {
+							cm_log("Config", "Not in directory section at line %d", ln);
+							stop = 1;
+						} else {
+							if(r[1] == NULL) {
+								cm_log("Config", "Missing argument at line %d", ln);
+								stop = 1;
+							} else {
+								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
+								e->name = cm_strdup(r[1]);
+								e->dir = cm_strdup(dir);
+								e->type = TW_DIR_ALLOW;
+							}
+						}
+					} else if(cm_strcaseequ(r[0], "Deny")) {
+						if(dir == NULL) {
+							cm_log("Config", "Not in directory section at line %d", ln);
+							stop = 1;
+						} else {
+							if(r[1] == NULL) {
+								cm_log("Config", "Missing argument at line %d", ln);
+								stop = 1;
+							} else {
+								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
+								e->name = cm_strdup(r[1]);
+								e->dir = cm_strdup(dir);
+								e->type = TW_DIR_DENY;
+							}
+						}
+					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
+						if(vhost != NULL) {
+							cm_log("Config", "Already in virtual host section at line %d", ln);
+							stop = 1;
+						} else {
+							if(r[1] == NULL) {
+								cm_log("Config", "Missing virtual host at line %d", ln);
+								stop = 1;
+							} else {
+								vhost = cm_strdup(r[1]);
+								current = &config.vhosts[config.vhost_count++];
+								current->dir_count = 0;
+								current->mime_count = 0;
+								current->icon_count = 0;
+								current->index_count = 0;
+								current->readme_count = 0;
+								current->hideport = -1;
+								int i;
+								current->name = cm_strdup(vhost);
+								current->port = -1;
+								for(i = 0; vhost[i] != 0; i++) {
+									if(vhost[i] == ':') {
+										current->name[i] = 0;
+										current->port = atoi(current->name + i + 1);
+										break;
+									}
+								}
+							}
+						}
+					} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
+						if(vhost == NULL) {
+							cm_log("Config", "Not in virtual host section at line %d", ln);
+							stop = 1;
+						} else {
+							free(vhost);
+							vhost = NULL;
+							current = &config.root;
+						}
+					} else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
+						for(i = 1; r[i] != NULL; i++) {
+							uint64_t port = atoi(r[i]);
+							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
+							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
+							int j;
+							for(j = 0; config.ports[j] != -1; j++)
+								;
+							config.ports[j] = port;
+						}
+					} else if(cm_strcaseequ(r[0], "HidePort")) {
+						current->hideport = 1;
+					} else if(cm_strcaseequ(r[0], "ShowPort")) {
+						current->hideport = 0;
+					} else if(cm_strcaseequ(r[0], "SSLKey")) {
+						if(r[1] == NULL) {
+							cm_log("Config", "Missing path at line %d", ln);
+							stop = 1;
+						} else {
+							if(current->sslkey != NULL) free(current->sslkey);
+							current->sslkey = cm_strdup(r[1]);
+						}
+					} else if(cm_strcaseequ(r[0], "SSLCertificate")) {
+						if(r[1] == NULL) {
+							cm_log("Config", "Missing path at line %d", ln);
+							stop = 1;
+						} else {
+							if(current->sslcert != NULL) free(current->sslcert);
+							current->sslcert = cm_strdup(r[1]);
+						}
+					} else if(cm_strcaseequ(r[0], "ServerRoot")) {
+						if(r[1] == NULL) {
+							cm_log("Config", "Missing path at line %d", ln);
+							stop = 1;
+						} else {
+							chdir(r[1]);
+						}
+					} else if(cm_strcaseequ(r[0], "DocumentRoot")) {
+						if(r[1] == NULL) {
+							cm_log("Config", "Missing path at line %d", ln);
+							stop = 1;
+						} else {
+							if(current->root != NULL) free(current->root);
+							current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
+						}
+					} else if(cm_strcaseequ(r[0], "ServerRoot")) {
+						if(r[1] == NULL) {
+							cm_log("Config", "Missing path at line %d", ln);
+							stop = 1;
+						} else {
+							if(config.server_root != NULL) free(config.server_root);
+							config.server_root = cm_strdup(r[1]);
+						}
+					} else if(cm_strcaseequ(r[0], "MIMEType")) {
+						if(r[1] == NULL) {
+							cm_log("Config", "Missing extension at line %d", ln);
+							stop = 1;
+						} else if(r[2] == NULL) {
+							cm_log("Config", "Missing MIME at line %d", ln);
+							stop = 1;
+						} else {
+							struct tw_mime_entry* e = &current->mimes[current->mime_count++];
+							e->ext = cm_strdup(r[1]);
+							e->mime = cm_strdup(r[2]);
+						}
+					} else if(cm_strcaseequ(r[0], "Icon")) {
+						if(r[1] == NULL) {
+							cm_log("Config", "Missing MIME at line %d", ln);
+							stop = 1;
+						} else if(r[2] == NULL) {
+							cm_log("Config", "Missing path at line %d", ln);
+							stop = 1;
+						} else {
+							struct tw_icon_entry* e = &current->icons[current->icon_count++];
+							e->mime = cm_strdup(r[1]);
+							e->icon = cm_strdup(r[2]);
+						}
+					} else if(cm_strcaseequ(r[0], "LoadModule")) {
+						for(i = 1; r[i] != NULL; i++) {
+							void* mod = tw_module_load(r[i]);
+							if(mod != NULL) {
+								config.modules[config.module_count++] = mod;
+								if(tw_module_init(mod) != 0) {
+									stop = 1;
+									break;
+								}
+							} else {
+								stop = 1;
+								break;
+							}
+						}
+					} else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
+						for(i = 1; r[i] != NULL; i++) {
+							current->indexes[current->index_count++] = cm_strdup(r[i]);
+						}
+					} else if(cm_strcaseequ(r[0], "Readme")) {
+						for(i = 1; r[i] != NULL; i++) {
+							current->readmes[current->readme_count++] = cm_strdup(r[i]);
+						}
+					} else {
+						stop = 1;
+						if(r[0] != NULL) {
+							int argc;
+							for(argc = 0; r[argc] != NULL; argc++)
+								;
+							stop = 0;
+							int i;
+							bool called = false;
+							struct tw_tool tools;
+							tw_init_tools(&tools);
+							for(i = 0; i < config.module_count; i++) {
+								tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
+								int resp;
+								if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
+									called = true;
+									break;
+								}
+								if(resp == TW_CONFIG_ERROR) {
+									stop = 1;
+									called = true;
+									break;
+								}
+							}
+							if(!called) {
+								cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
+								stop = 1;
+							}
+						}
+					}
+					for(i = 0; r[i] != NULL; i++) free(r[i]);
+					free(r);
+				}
+				free(l);
+				free(line);
+				line = malloc(1);
+				line[0] = 0;
+				if(c <= 0) break;
+			} else if(cbuf[0] != '\r') {
+				char* tmp = line;
+				line = cm_strcat(tmp, cbuf);
+				free(tmp);
+			}
+		}
+		free(line);
+		fclose(f);
+		return stop;
+	} else {
+		cm_log("Config", "Could not open the file");
+		return 1;
+	}
+}
Index: /tags/1.03A/Server/http.c
===================================================================
--- /tags/1.03A/Server/http.c	(revision 125)
+++ /tags/1.03A/Server/http.c	(revision 125)
@@ -0,0 +1,363 @@
+/* $Id$ */
+
+#define SOURCE
+
+#include "../config.h"
+
+#include "tw_http.h"
+
+#include "tw_server.h"
+
+#include <cm_log.h>
+#include <cm_string.h>
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef __MINGW32__
+#include <winsock2.h>
+#else
+#ifdef USE_POLL
+#include <poll.h>
+#else
+#include <sys/select.h>
+#endif
+#endif
+
+void tw_free_request(struct tw_http_request* req) {
+	if(req->method != NULL) free(req->method);
+	if(req->path != NULL) free(req->path);
+	if(req->query != NULL) free(req->query);
+	if(req->headers != NULL) {
+		int i;
+		for(i = 0; req->headers[i] != NULL; i++) free(req->headers[i]);
+		free(req->headers);
+	}
+	if(req->body != NULL) free(req->body);
+	if(req->version != NULL) free(req->version);
+
+	req->method = NULL;
+	req->path = NULL;
+	req->query = NULL;
+	req->headers = NULL;
+	req->body = NULL;
+	req->version = NULL;
+}
+
+int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) {
+	char buffer[512];
+	char cbuf[2];
+	int phase = 0;
+
+#ifdef USE_POLL
+	struct pollfd pollfds[1];
+	pollfds[0].fd = sock;
+	pollfds[0].events = POLLIN | POLLPRI;
+#else
+	fd_set fds;
+#endif
+
+	bool bad = false;
+
+	cbuf[1] = 0;
+
+	req->method = NULL;
+	req->path = NULL;
+	req->query = NULL;
+	req->headers = NULL;
+	req->body = NULL;
+	req->version = NULL;
+
+	char* header = malloc(1);
+	header[0] = 0;
+	int nl = 0;
+
+	while(1) {
+#ifndef USE_POLL
+		FD_ZERO(&fds);
+		FD_SET(sock, &fds);
+		struct timeval tv;
+		tv.tv_sec = 5;
+		tv.tv_usec = 0;
+#endif
+#ifndef NO_SSL
+		if(ssl == NULL || !SSL_has_pending(ssl)) {
+#endif
+#ifdef USE_POLL
+			int n = poll(pollfds, 1, 5000);
+#else
+#ifdef __HAIKU__
+		int n = select(32, &fds, NULL, NULL, &tv);
+#else
+		int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
+#endif
+#endif
+			if(n <= 0) {
+				cm_log("HTTP", "Timeout, disconncting");
+				free(header);
+				tw_free_request(req);
+				return -1;
+			}
+#ifndef NO_SSL
+		}
+#endif
+		int len = tw_read(ssl, sock, buffer, 512);
+		if(len <= 0) {
+			bad = true;
+			break;
+		}
+		int i;
+		for(i = 0; i < len; i++) {
+			char c = buffer[i];
+			if(phase == 0) {
+				if(c == ' ') {
+					if(req->method == NULL) {
+						tw_free_request(req);
+						bad = true;
+						goto getout;
+					} else {
+						phase++;
+					}
+				} else {
+					if(req->method == NULL) {
+						req->method = malloc(1);
+						req->method[0] = 0;
+					}
+					cbuf[0] = c;
+					char* tmp = req->method;
+					req->method = cm_strcat(tmp, cbuf);
+					free(tmp);
+				}
+			} else if(phase == 1) {
+				if(c == ' ') {
+					if(req->path == NULL) {
+						tw_free_request(req);
+						bad = true;
+						goto getout;
+					} else {
+						phase++;
+					}
+				} else {
+					if(req->path == NULL) {
+						req->path = malloc(1);
+						req->path[0] = 0;
+					}
+					cbuf[0] = c;
+					char* tmp = req->path;
+					req->path = cm_strcat(tmp, cbuf);
+					free(tmp);
+				}
+			} else if(phase == 2) {
+				if(c == '\n') {
+					if(req->version == NULL) {
+						tw_free_request(req);
+						bad = true;
+						goto getout;
+					} else {
+						/* We have Method, Path, Version now */
+
+						if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) {
+							cm_log("HTTP", "Bad HTTP Version");
+							bad = true;
+							goto getout;
+						}
+
+						int j;
+						char* p = malloc(1);
+						p[0] = 0;
+						for(j = 0; req->path[j] != 0; j++) {
+							if(req->path[j] == '/') {
+								cbuf[0] = '/';
+								for(; req->path[j] != 0 && req->path[j] == '/'; j++)
+									;
+								j--;
+							} else {
+								cbuf[0] = req->path[j];
+							}
+							char* tmp = p;
+							p = cm_strcat(tmp, cbuf);
+							free(tmp);
+						}
+						free(req->path);
+						req->path = p;
+
+						int incr = 0;
+						p = malloc(1);
+						p[0] = 0;
+						for(j = 0;; j++) {
+							if(req->path[j] == '/' || req->path[j] == 0) {
+								char oldc = req->path[j];
+								cbuf[0] = oldc;
+								req->path[j] = 0;
+
+								char* pth = req->path + incr;
+
+								if(strcmp(pth, "..") == 0) {
+									int k;
+									if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
+									for(k = strlen(p) - 1; k >= 0; k--) {
+										if(p[k] == '/') {
+											p[k + 1] = 0;
+											break;
+										}
+									}
+									if(strlen(p) == 0) {
+										free(p);
+										p = cm_strdup("/");
+									}
+								} else if(strcmp(pth, ".") == 0) {
+								} else {
+									char* tmp = p;
+									p = cm_strcat3(tmp, pth, cbuf);
+									free(tmp);
+								}
+
+								incr = j + 1;
+								if(oldc == 0) break;
+							}
+						}
+						free(req->path);
+						req->path = p;
+
+						cm_log("HTTP", "Request: %s %s %s", req->method, req->path, req->version);
+
+						phase++;
+					}
+				} else if(c != '\r') {
+					if(req->version == NULL) {
+						req->version = malloc(1);
+						req->version[0] = 0;
+					}
+					cbuf[0] = c;
+					char* tmp = req->version;
+					req->version = cm_strcat(tmp, cbuf);
+					free(tmp);
+				}
+			} else if(phase == 3) {
+				if(c == '\n') {
+					nl++;
+					if(nl == 2) {
+						phase++;
+						goto getout;
+					} else {
+						if(req->headers == NULL) {
+							req->headers = malloc(sizeof(*req->headers));
+							req->headers[0] = NULL;
+						}
+						int j;
+						for(j = 0; header[j] != 0; j++) {
+							if(header[j] == ':') {
+								header[j] = 0;
+								j++;
+								for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
+									;
+								char* kv = header;
+								char* vv = header + j;
+
+								char** old = req->headers;
+								int k;
+								for(k = 0; old[k] != NULL; k++)
+									;
+								req->headers = malloc(sizeof(*req->headers) * (k + 3));
+								for(k = 0; old[k] != NULL; k++) req->headers[k] = old[k];
+								req->headers[k] = cm_strdup(kv);
+								req->headers[k + 1] = cm_strdup(vv);
+								req->headers[k + 2] = NULL;
+								free(old);
+
+								cm_log("HTTP", "Header: %s: %s", kv, vv);
+
+								break;
+							}
+						}
+						free(header);
+						header = malloc(1);
+						header[0] = 0;
+					}
+				} else if(c != '\r') {
+					nl = 0;
+					cbuf[0] = c;
+					char* tmp = header;
+					header = cm_strcat(tmp, cbuf);
+					free(tmp);
+				}
+			}
+		}
+	}
+getout:
+	free(header);
+	if(bad) {
+		tw_free_request(req);
+		return 1;
+	}
+	char* result = malloc(1);
+	result[0] = 0;
+	int i;
+	for(i = 0; req->path[i] != 0; i++) {
+		if(req->path[i] == '?') {
+			req->path[i] = 0;
+			req->query = cm_strdup(req->path + i + 1);
+			break;
+		}
+	}
+	for(i = 0; req->path[i] != 0; i++) {
+		if(req->path[i] == '%') {
+			if(req->path[i + 1] == 0) continue;
+			cbuf[0] = cm_hex(req->path + i + 1, 2);
+			if(cbuf[0] != '\\') {
+				char* tmp = result;
+				result = cm_strcat(tmp, cbuf);
+				free(tmp);
+			}
+			i += 2;
+		} else if(req->path[i] != '\\') {
+			cbuf[0] = req->path[i];
+			char* tmp = result;
+			result = cm_strcat(tmp, cbuf);
+			free(tmp);
+		}
+	}
+	free(req->path);
+	req->path = result;
+
+	int incr = 0;
+	char* p = malloc(1);
+	p[0] = 0;
+	int j;
+	for(j = 0;; j++) {
+		if(req->path[j] == '/' || req->path[j] == 0) {
+			char oldc = req->path[j];
+			cbuf[0] = oldc;
+			req->path[j] = 0;
+
+			char* pth = req->path + incr;
+
+			if(strcmp(pth, "..") == 0) {
+				int k;
+				if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
+				for(k = strlen(p) - 1; k >= 0; k--) {
+					if(p[k] == '/') {
+						p[k + 1] = 0;
+						break;
+					}
+				}
+				if(strlen(p) == 0) {
+					free(p);
+					p = cm_strdup("/");
+				}
+			} else if(strcmp(pth, ".") == 0) {
+			} else {
+				char* tmp = p;
+				p = cm_strcat3(tmp, pth, cbuf);
+				free(tmp);
+			}
+
+			incr = j + 1;
+			if(oldc == 0) break;
+		}
+	}
+	free(req->path);
+	req->path = p;
+	return 0;
+}
Index: /tags/1.03A/Server/install.nsi
===================================================================
--- /tags/1.03A/Server/install.nsi	(revision 125)
+++ /tags/1.03A/Server/install.nsi	(revision 125)
@@ -0,0 +1,126 @@
+# $Id$
+
+VIFileVersion "1.0.0.0"
+VIProductVersion "1.0.0.0"
+VIAddVersionKey "FileVersion" "${VERSION}"
+VIAddVersionKey "LegalCopyright" "Public domain. Original by Nishi"
+VIAddVersionKey "FileDescription" "Tewi HTTPd Installer"
+
+LoadLanguageFile "${NSISDIR}\Contrib\Language files\Japanese.nlf"
+LoadLanguageFile "${NSISDIR}\Contrib\Language files\English.nlf"
+
+Name "Tewi HTTPd"
+OutFile "install.exe"
+InstallDir "C:\Tewi"
+Icon "tewi-install.ico"
+LicenseData ../LICENSE
+
+LangString EXEC_ONLY ${LANG_ENGLISH} "Install the executable only"
+LangString EXEC_ONLY ${LANG_JAPANESE} "実行機能のみをインストールする"
+LangString SERV_TOO ${LANG_ENGLISH} "Install the service too (NT-only)"
+LangString SERV_TOO ${LANG_JAPANESE} "サービスもインストールする(NTのみ)"
+LangString WAIT_STOP ${LANG_ENGLISH} "Waiting for 1 second so service can stop"
+LangString WAIT_STOP ${LANG_JAPANESE} "サービスが止まるのを待っています"
+
+!include "LogicLib.nsh"
+!include "Sections.nsh"
+
+Page license
+Page components
+Page instfiles
+UninstPage uninstConfirm
+UninstPage instfiles
+Section
+	CreateDirectory "$INSTDIR\etc"
+	CreateDirectory "$INSTDIR\www"
+	CreateDirectory "$INSTDIR\www\icons"
+	CreateDirectory "$INSTDIR\modules"
+	CreateDirectory "$INSTDIR\bin"
+	SetOutPath "$INSTDIR"
+	File /oname=LICENSE.txt "../LICENSE"
+	SetOutPath "$INSTDIR\modules"
+	File "../Module/*.dll"
+	SetOutPath "$INSTDIR\etc"
+	SetOverWrite off
+	File /oname=tewi.conf "../example-win.conf"
+	SetOutPath "$INSTDIR\www"
+	File /oname=index.html "../itworks.html"
+	SetOutPath "$INSTDIR\www\icons"
+	File "../Icons/*.png"
+	SetOverWrite on
+
+	CreateDirectory "$SMPROGRAMS\Tewi HTTPd"
+	CreateShortcut "$SMPROGRAMS\Tewi HTTPd\License.lnk" "$INSTDIR\LICENSE.txt" ""
+	CreateShortcut "$SMPROGRAMS\Tewi HTTPd\Start Tewi HTTPd.lnk" "$INSTDIR\bin\tewi.exe" ""
+	CreateShortcut "$SMPROGRAMS\Tewi HTTPd\Start Tewi HTTPd (verbose).lnk" "$INSTDIR\bin\tewi.exe" "-v"
+	CreateShortcut "$SMPROGRAMS\Tewi HTTPd\Uninstall Tewi HTTPd.lnk" "$INSTDIR\uninstall.exe" ""
+
+	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Tewi HTTPd" "DisplayName" "Tewi HTTPd"
+	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Tewi HTTPd" "UninstallString" '"$INSTDIR\uninstall.exe"'
+
+	WriteUninstaller "$INSTDIR\uninstall.exe"
+SectionEnd
+
+Section "$(EXEC_ONLY)" SEL_EXEC
+	SetOutPath "$INSTDIR\bin"
+	File "../tewi.exe"
+	WriteINIStr $INSTDIR\install.ini uninstall service false
+SectionEnd
+
+Section /o "$(SERV_TOO)" SEL_SERVICE
+	WriteINIStr $INSTDIR\install.ini uninstall service true
+	FileOpen $9 $INSTDIR\install.bat w
+	FileWrite $9 '"$SYSDIR\sc.exe" stop "TewiHTTPd"$\r$\n'
+	FileClose $9
+	nsExec::Exec '"$INSTDIR\install.bat"'
+	Pop $0
+	DetailPrint "$(WAIT_STOP)"
+	Sleep 1000
+	CreateDirectory "$INSTDIR\logs"
+	SetOutPath "$INSTDIR\bin"
+	File "../tewi.exe"
+	File /oname=tewisrv.exe "../tewi-service.exe"
+	FileOpen $9 $INSTDIR\install.bat w
+	FileWrite $9 '"$SYSDIR\sc.exe" delete "TewiHTTPd"$\r$\n'
+	FileWrite $9 '"$SYSDIR\sc.exe" create "TewiHTTPd" DisplayName= "Tewi HTTPd" binpath= "$INSTDIR\bin\tewisrv.exe" start= "auto"$\r$\n'
+	FileWrite $9 '"$SYSDIR\sc.exe" start "TewiHTTPd"$\r$\n'
+	FileClose $9
+	nsExec::Exec '"$INSTDIR\install.bat"'
+	Pop $0
+	Delete $INSTDIR\install.bat
+SectionEnd
+
+Function .onInit
+	StrCpy $1 ${SEL_EXEC}
+FunctionEnd
+
+Function .onSelChange
+	!insertmacro StartRadioButtons $1
+	!insertmacro RadioButton ${SEL_EXEC}
+	!insertmacro RadioButton ${SEL_SERVICE}
+	!insertmacro EndRadioButtons
+FunctionEnd
+
+Section "Uninstall"
+	ReadINIStr $8 $INSTDIR\install.ini uninstall service
+	${If} $8 == "true"
+		FileOpen $9 $INSTDIR\uninstall.bat w
+		FileWrite $9 '"$SYSDIR\sc.exe" stop "TewiHTTPd"$\r$\n'
+		FileClose $9
+		nsExec::Exec '"$INSTDIR\uninstall.bat"'
+		Pop $0
+		FileOpen $9 $INSTDIR\uninstall.bat w
+		DetailPrint "$(WAIT_STOP)"
+		Sleep 1000
+		FileWrite $9 '"$SYSDIR\sc.exe" delete "TewiHTTPd"$\r$\n'
+		FileClose $9
+		nsExec::Exec '"$INSTDIR\uninstall.bat"'
+		Pop $0
+		Delete $INSTDIR\uninstall.bat
+	${EndIf}
+
+	RMDir /r "$INSTDIR"
+	RMDir /r "$SMPROGRAMS\Tewi HTTPd"
+
+	DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Tewi HTTPd"
+SectionEnd
Index: /tags/1.03A/Server/main.c
===================================================================
--- /tags/1.03A/Server/main.c	(revision 125)
+++ /tags/1.03A/Server/main.c	(revision 125)
@@ -0,0 +1,167 @@
+/* $Id$ */
+
+#define SOURCE
+
+#include "../config.h"
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <signal.h>
+#include <stdlib.h>
+
+#ifndef NO_SSL
+#include <openssl/opensslv.h>
+#endif
+
+#include <cm_log.h>
+#include <cm_string.h>
+
+#include "tw_config.h"
+#include "tw_server.h"
+#include "tw_version.h"
+
+#ifdef __MINGW32__
+#include <windows.h>
+#endif
+
+extern bool cm_do_log;
+extern struct tw_config config;
+extern FILE* logfile;
+
+char tw_server[2048];
+
+int startup(int argc, char** argv);
+
+#ifdef SERVICE
+SERVICE_STATUS status;
+SERVICE_STATUS_HANDLE status_handle;
+
+void WINAPI servhandler(DWORD control) {
+	switch(control) {
+	case SERVICE_CONTROL_STOP:
+	case SERVICE_CONTROL_SHUTDOWN:
+		status.dwCurrentState = SERVICE_STOP_PENDING;
+		break;
+	}
+	SetServiceStatus(status_handle, &status);
+}
+
+void WINAPI servmain(DWORD argc, LPSTR* argv) {
+	logfile = fopen(PREFIX "/logs/tewi.log", "a");
+	if(logfile == NULL) logfile = stderr;
+	status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+	status.dwCurrentState = SERVICE_START_PENDING;
+	status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
+	status.dwWin32ExitCode = NO_ERROR;
+	status.dwServiceSpecificExitCode = 0;
+	status.dwCheckPoint = 0;
+	status.dwWaitHint = 0;
+	status_handle = RegisterServiceCtrlHandler("Tewi HTTPd", servhandler);
+	if(status_handle == NULL) return;
+	if(SetServiceStatus(status_handle, &status) == 0) return;
+	int st = startup(argc, argv);
+	if(st != -1) {
+		status.dwWin32ExitCode = NO_ERROR;
+		status.dwServiceSpecificExitCode = st;
+		status.dwCurrentState = SERVICE_STOPPED;
+		SetServiceStatus(status_handle, &status);
+		return;
+	}
+	status.dwCurrentState = SERVICE_RUNNING;
+	SetServiceStatus(status_handle, &status);
+	tw_server_loop();
+	status.dwCurrentState = SERVICE_STOPPED;
+	SetServiceStatus(status_handle, &status);
+}
+#endif
+
+int main(int argc, char** argv) {
+	logfile = stderr;
+#ifdef SERVICE
+	SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
+	StartServiceCtrlDispatcher(table);
+#else
+	int st = startup(argc, argv);
+	if(st != -1) return st;
+	tw_server_loop();
+#endif
+}
+
+int startup(int argc, char** argv) {
+	int i;
+	const char* confpath = PREFIX "/etc/tewi.conf";
+	if(argv != NULL) {
+		for(i = 1; i < argc; i++) {
+			if(argv[i][0] == '-') {
+				if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
+					if(!cm_do_log) {
+						cm_do_log = true;
+#ifndef NO_SSL
+						cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
+#else
+						cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
+#endif
+					} else {
+						cm_do_log = true;
+					}
+				} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
+					i++;
+					if(argv[i] == NULL) {
+						fprintf(stderr, "Missing argument\n");
+						return 1;
+					}
+					confpath = argv[i];
+				} else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
+					i++;
+					if(argv[i] == NULL) {
+						fprintf(stderr, "Missing argument\n");
+						return 1;
+					}
+					if(logfile != NULL && logfile != stderr) {
+						fclose(logfile);
+					}
+					logfile = fopen(argv[i], "a");
+					if(logfile == NULL) {
+						fprintf(stderr, "Failed to open logfile\n");
+						return 1;
+					}
+				} else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
+					printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
+					printf("Under public domain.\n");
+					printf("Original by 2024 Nishi\n");
+					printf("\n");
+					printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
+					printf("--config  | -C config      : Specify config\n");
+					printf("--logfile | -l logfile     : Specify logfile\n");
+					printf("--verbose | -v             : Verbose mode\n");
+					printf("--version | -V             : Version information\n");
+					return 0;
+				} else {
+					fprintf(stderr, "Unknown option: %s\n", argv[i]);
+					return 1;
+				}
+			}
+		}
+	}
+	tw_config_init();
+	if(tw_config_read(confpath) != 0) {
+		fprintf(stderr, "Could not read the config\n");
+		return 1;
+	}
+	if(tw_server_init() != 0) {
+		fprintf(stderr, "Could not initialize the server\n");
+		return 1;
+	}
+	sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
+	char* r = cm_strcat(tw_server, " running...");
+	cm_force_log(r);
+	free(r);
+#ifndef __MINGW32__
+	signal(SIGCHLD, SIG_IGN);
+	signal(SIGPIPE, SIG_IGN);
+#else
+	SetConsoleTitle(tw_server);
+#endif
+	return -1;
+}
Index: /tags/1.03A/Server/module.c
===================================================================
--- /tags/1.03A/Server/module.c	(revision 125)
+++ /tags/1.03A/Server/module.c	(revision 125)
@@ -0,0 +1,73 @@
+/* $Id$ */
+
+#define SOURCE
+
+#include "tw_module.h"
+
+#include "tw_config.h"
+
+#include <cm_string.h>
+#include <cm_log.h>
+
+#include <unistd.h>
+#include <stdlib.h>
+
+#ifdef __MINGW32__
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
+
+extern struct tw_config config;
+
+void* tw_module_load(const char* path) {
+	char* p = getcwd(NULL, 0);
+	chdir(config.server_root);
+	void* lib;
+#ifdef __MINGW32__
+	lib = LoadLibraryA(path);
+#else
+	lib = dlopen(path, RTLD_LAZY);
+#endif
+	if(lib == NULL) {
+		cm_log("Module", "Could not load %s", path);
+	}
+	chdir(p);
+	free(p);
+	return lib;
+}
+
+void* tw_module_symbol(void* mod, const char* sym) {
+#ifdef __MINGW32__
+	return GetProcAddress(mod, sym);
+#else
+	return dlsym(mod, sym);
+#endif
+}
+
+void tw_add_version(const char* string) {
+	if(config.extension == NULL) {
+		config.extension = cm_strcat(" ", string);
+	} else {
+		char* tmp = config.extension;
+		config.extension = cm_strcat3(tmp, " ", string);
+		free(tmp);
+	}
+}
+
+void tw_init_tools(struct tw_tool* tools) {
+	tools->log = cm_log;
+	tools->add_version = tw_add_version;
+}
+
+int tw_module_init(void* mod) {
+	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
+	if(mod_init == NULL) {
+		cm_log("Module", "Could not init a module");
+		return 1;
+	} else {
+		struct tw_tool tools;
+		tw_init_tools(&tools);
+		return mod_init(&config, &tools);
+	}
+}
Index: /tags/1.03A/Server/option.c
===================================================================
--- /tags/1.03A/Server/option.c	(revision 125)
+++ /tags/1.03A/Server/option.c	(revision 125)
@@ -0,0 +1,31 @@
+/* $Id$ */
+/* This file is not intended to be in the server. */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "../config.h"
+
+int main(int argc, char** argv) {
+	if(argc < 3) {
+		return 1;
+	}
+	if(strcmp(argv[1], "cflags") == 0) {
+#ifndef NO_SSL
+		printf("-I %s/openssl/include", argv[2]);
+#endif
+	} else if(strcmp(argv[1], "ldflags") == 0) {
+#ifndef NO_SSL
+		printf("-L %s/openssl/lib", argv[2]);
+#endif
+	} else if(strcmp(argv[1], "objs") == 0) {
+#ifndef NO_SSL
+		printf("ssl.o");
+#endif
+	} else if(strcmp(argv[1], "libs") == 0) {
+#ifndef NO_SSL
+		printf("-lssl -lcrypto");
+#endif
+	}
+	printf("\n");
+}
Index: /tags/1.03A/Server/server.c
===================================================================
--- /tags/1.03A/Server/server.c	(revision 125)
+++ /tags/1.03A/Server/server.c	(revision 125)
@@ -0,0 +1,941 @@
+/* $Id$ */
+
+#define SOURCE
+
+#include "../config.h"
+
+#include "tw_server.h"
+
+#ifndef NO_SSL
+#include "tw_ssl.h"
+#endif
+
+#include "tw_config.h"
+#include "tw_http.h"
+#include "tw_module.h"
+#include "tw_version.h"
+
+#include <unistd.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <time.h>
+
+#include <cm_string.h>
+#include <cm_log.h>
+#include <cm_dir.h>
+
+#ifdef __MINGW32__
+#include <ws2tcpip.h>
+#include <wspiapi.h>
+#include <winsock2.h>
+#include <process.h>
+#include <windows.h>
+
+#include "strptime.h"
+#else
+#ifdef USE_POLL
+#include <poll.h>
+#else
+#include <sys/select.h>
+#endif
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+#endif
+
+#ifdef __HAIKU__
+#include <OS.h>
+#endif
+
+extern struct tw_config config;
+extern char tw_server[];
+
+int sockcount = 0;
+
+SOCKADDR addresses[MAX_PORTS];
+int sockets[MAX_PORTS];
+
+#ifdef __MINGW32__
+const char* reserved_names[] = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
+#endif
+
+/* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
+int tw_wildcard_match(const char* wildcard, const char* target) {
+	const char *pw = wildcard, *pt = target;
+
+	while(1) {
+		if(*pt == 0) {
+			while(*pw == '*') pw++;
+			return *pw == 0;
+		} else if(*pw == 0) {
+			return 0;
+		} else if(*pw == '*') {
+			return *(pw + 1) == 0 || tw_wildcard_match(pw, pt + 1) || tw_wildcard_match(pw + 1, pt);
+		} else if(*pw == '?' || (*pw == *pt)) {
+			pw++;
+			pt++;
+			continue;
+		} else {
+			return 0;
+		}
+	}
+}
+
+void close_socket(int sock) {
+#if defined(__MINGW32__)
+	closesocket(sock);
+#else
+	close(sock);
+#endif
+}
+
+int tw_server_init(void) {
+	int i;
+#ifdef __MINGW32__
+	WSADATA wsa;
+	WSAStartup(MAKEWORD(2, 0), &wsa);
+#endif
+	for(i = 0; config.ports[i] != -1; i++)
+		;
+	sockcount = i;
+	for(i = 0; config.ports[i] != -1; i++) {
+#ifdef NO_IPV6
+		int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+#else
+		int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
+#endif
+#ifdef __MINGW32__
+		if(sock == INVALID_SOCKET)
+#else
+		if(sock < 0)
+#endif
+		{
+			cm_log("Server", "Socket creation failure");
+			return 1;
+		}
+		int yes = 1;
+		if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
+			close_socket(sock);
+			cm_log("Server", "setsockopt failure (reuseaddr)");
+			return 1;
+		}
+		if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
+			close_socket(sock);
+			cm_log("Server", "setsockopt failure (nodelay)");
+			return 1;
+		}
+#ifndef NO_IPV6
+		int no = 0;
+		if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
+			close_socket(sock);
+			cm_log("Server", "setsockopt failure (IPv6)");
+			return 1;
+		}
+#endif
+		memset(&addresses[i], 0, sizeof(addresses[i]));
+#ifdef NO_IPV6
+		addresses[i].sin_family = AF_INET;
+		addresses[i].sin_addr.s_addr = INADDR_ANY;
+		addresses[i].sin_port = htons(config.ports[i]);
+#else
+		addresses[i].sin6_family = AF_INET6;
+		addresses[i].sin6_addr = in6addr_any;
+		addresses[i].sin6_port = htons(config.ports[i]);
+#endif
+		if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
+			close_socket(sock);
+			cm_log("Server", "Bind failure");
+			return 1;
+		}
+		if(listen(sock, 128) < 0) {
+			close_socket(sock);
+			cm_log("Server", "Listen failure");
+			return 1;
+		}
+		sockets[i] = sock;
+	}
+	return 0;
+}
+
+size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
+#ifndef NO_SSL
+	if(ssl == NULL) {
+		return recv(s, data, len, 0);
+	} else {
+		return SSL_read(ssl, data, len);
+	}
+#else
+	return recv(s, data, len, 0);
+#endif
+}
+
+size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
+#ifndef NO_SSL
+	if(ssl == NULL) {
+		return send(s, data, len, 0);
+	} else {
+		return SSL_write(ssl, data, len);
+	}
+#else
+	return send(s, data, len, 0);
+#endif
+}
+
+#define ERROR_HTML \
+	"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
+	"<html>\n" \
+	"	<head>\n" \
+	"		<title>%s</title>\n" \
+	"	</head>\n" \
+	"	<body>\n" \
+	"		<h1>%s</h1>\n" \
+	"		<hr>\n" \
+	"		", \
+	    address, \
+	    "\n" \
+	    "	</body>\n" \
+	    "</html>\n"
+
+void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers, time_t mtime, time_t cmtime) {
+	char construct[512];
+	if(mtime != 0 && cmtime != 0 && mtime <= cmtime) {
+		status = "304 Not Modified";
+		type = NULL;
+		size = 0;
+		headers = NULL;
+		f = NULL;
+		doc = NULL;
+	}
+	sprintf(construct, "%llu", (unsigned long long)size);
+	tw_write(ssl, sock, "HTTP/1.1 ", 9);
+	tw_write(ssl, sock, (char*)status, strlen(status));
+	tw_write(ssl, sock, "\r\n", 2);
+	if(type != NULL) {
+		tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
+		tw_write(ssl, sock, (char*)type, strlen(type));
+		tw_write(ssl, sock, "\r\n", 2);
+	}
+	tw_write(ssl, sock, "Server: ", 6 + 2);
+	tw_write(ssl, sock, tw_server, strlen(tw_server));
+	tw_write(ssl, sock, "\r\n", 2);
+	if(size != 0) {
+		tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
+		tw_write(ssl, sock, construct, strlen(construct));
+		tw_write(ssl, sock, "\r\n", 2);
+		if(mtime != 0) {
+			struct tm* tm = gmtime(&mtime);
+			char date[513];
+			strftime(date, 512, "%a, %d %b %Y %H:%M:%S GMT", tm);
+			tw_write(ssl, sock, "Last-Modified: ", 5 + 8 + 2);
+			tw_write(ssl, sock, date, strlen(date));
+			tw_write(ssl, sock, "\r\n", 2);
+		}
+	}
+	int i;
+	if(headers != NULL) {
+		for(i = 0; headers[i] != NULL; i += 2) {
+			tw_write(ssl, sock, headers[i], strlen(headers[i]));
+			tw_write(ssl, sock, ": ", 2);
+			tw_write(ssl, sock, headers[i + 1], strlen(headers[i + 1]));
+			tw_write(ssl, sock, "\r\n", 2);
+		}
+	}
+	tw_write(ssl, sock, "\r\n", 2);
+	if(doc == NULL && f == NULL) return;
+	size_t incr = 0;
+	while(1) {
+		if(f != NULL) {
+			char buffer[128];
+			fread(buffer, size < 128 ? size : 128, 1, f);
+			tw_write(ssl, sock, buffer, size < 128 ? size : 128);
+		} else {
+			tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
+		}
+		incr += 128;
+		if(size <= 128) break;
+		size -= 128;
+	}
+}
+
+void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, time_t mtime, time_t cmtime) { _tw_process_page(ssl, sock, status, type, f, doc, size, NULL, mtime, cmtime); }
+
+const char* tw_http_status(int code) {
+	if(code == 200) {
+		return "200 OK";
+	} else if(code == 308) {
+		return "308 Permanent Redirect";
+	} else if(code == 400) {
+		return "400 Bad Request";
+	} else if(code == 401) {
+		return "401 Unauthorized";
+	} else if(code == 403) {
+		return "403 Forbidden";
+	} else if(code == 404) {
+		return "404 Not Found";
+	} else {
+		return "400 Bad Request";
+	}
+}
+
+char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) {
+	char address[1024];
+
+	if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) {
+		sprintf(address, "<address>%s Server at %s</address>", tw_server, name, port);
+	} else {
+		sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
+	}
+
+	char* st = cm_strdup(tw_http_status(code));
+	char* st2;
+	int i;
+	for(i = 0; st[i] != 0; i++) {
+		if(st[i] == ' ') {
+			st2 = cm_strdup(st + i + 1);
+			break;
+		}
+	}
+	char* buffer = malloc(4096);
+	char* str = cm_strcat3(ERROR_HTML);
+	sprintf(buffer, str, st, st2);
+	free(str);
+	free(st);
+	return buffer;
+}
+
+void tw_http_error(SSL* ssl, int sock, int error, char* name, int port, struct tw_config_entry* vhost) {
+	char* str = tw_http_default_error(error, name, port, vhost);
+	tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
+	free(str);
+}
+
+void addstring(char** str, const char* add, ...) {
+	int i;
+	char cbuf[2];
+	cbuf[1] = 0;
+	va_list va;
+	va_start(va, add);
+	for(i = 0; add[i] != 0; i++) {
+		cbuf[0] = add[i];
+		if(add[i] == '%') {
+			i++;
+			if(add[i] == 's') {
+				char* tmp = *str;
+				*str = cm_strcat(tmp, va_arg(va, const char*));
+				free(tmp);
+			} else if(add[i] == 'h') {
+				char* h = cm_html_escape(va_arg(va, const char*));
+				char* tmp = *str;
+				*str = cm_strcat(tmp, h);
+				free(tmp);
+				free(h);
+			} else if(add[i] == 'l') {
+				char* h = cm_url_escape(va_arg(va, const char*));
+				char* tmp = *str;
+				*str = cm_strcat(tmp, h);
+				free(tmp);
+				free(h);
+			} else if(add[i] == 'd') {
+				int n = va_arg(va, int);
+				char* h = malloc(512);
+				sprintf(h, "%d", n);
+				char* tmp = *str;
+				*str = cm_strcat(tmp, h);
+				free(tmp);
+				free(h);
+			} else if(add[i] == '%') {
+				char* tmp = *str;
+				*str = cm_strcat(tmp, "%");
+				free(tmp);
+			}
+		} else {
+			char* tmp = *str;
+			*str = cm_strcat(tmp, cbuf);
+			free(tmp);
+		}
+	}
+	va_end(va);
+}
+
+char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) {
+	char* mime = "application/octet-stream";
+	if(ext == NULL) return mime;
+	bool set = false;
+	int i;
+	for(i = 0; i < vhost_entry->mime_count; i++) {
+		if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
+			mime = vhost_entry->mimes[i].mime;
+			set = true;
+		}
+	}
+	if(!set) {
+		for(i = 0; i < config.root.mime_count; i++) {
+			if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
+				mime = config.root.mimes[i].mime;
+			}
+		}
+	}
+	return mime;
+}
+
+char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
+	char* icon = "";
+	if(mime == NULL) return "";
+	bool set = false;
+	int i;
+	for(i = 0; i < vhost_entry->icon_count; i++) {
+		if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
+			icon = vhost_entry->icons[i].icon;
+			set = true;
+		}
+	}
+	if(!set) {
+		for(i = 0; i < config.root.icon_count; i++) {
+			if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
+				icon = config.root.icons[i].icon;
+			}
+		}
+	}
+	return icon;
+}
+
+struct pass_entry {
+	int sock;
+	int port;
+	bool ssl;
+	SOCKADDR addr;
+};
+
+#ifdef __MINGW32__
+unsigned int WINAPI tw_server_pass(void* ptr) {
+#elif defined(__HAIKU__)
+int32_t tw_server_pass(void* ptr) {
+#endif
+#if defined(__HAIKU__) || defined(__MINGW32__)
+	int sock = ((struct pass_entry*)ptr)->sock;
+	bool ssl = ((struct pass_entry*)ptr)->ssl;
+	int port = ((struct pass_entry*)ptr)->port;
+	SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
+	free(ptr);
+#else
+	void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
+#endif
+	char* name = config.hostname;
+
+#ifndef NO_SSL
+	SSL_CTX* ctx = NULL;
+	SSL* s = NULL;
+	bool sslworks = false;
+	if(ssl) {
+		ctx = tw_create_ssl_ctx(port);
+		s = SSL_new(ctx);
+		SSL_set_fd(s, sock);
+		if(SSL_accept(s) <= 0) goto cleanup;
+		sslworks = true;
+	}
+#else
+		void* s = NULL;
+#endif
+
+	char address[513];
+	address[0] = 0;
+	struct sockaddr* sa = (struct sockaddr*)&addr;
+	getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST);
+
+	struct tw_http_request req;
+	struct tw_http_response res;
+	struct tw_tool tools;
+	res._processed = false;
+	tw_init_tools(&tools);
+	int ret = tw_http_parse(s, sock, &req);
+	if(ret == 0) {
+		char date[513];
+		time_t t = time(NULL);
+		struct tm* tm = localtime(&t);
+		strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm);
+
+		char* useragent = cm_strdup("");
+
+		int i;
+		for(i = 0; req.headers[i] != NULL; i += 2) {
+			if(cm_strcaseequ(req.headers[i], "User-Agent")) {
+				free(useragent);
+				useragent = cm_strdup(req.headers[i + 1]);
+			}
+		}
+
+		char* tmp = cm_strcat3(address, " - [", date);
+		char* tmp2 = cm_strcat3(tmp, "] \"", req.method);
+		char* tmp3 = cm_strcat3(tmp2, " ", req.path);
+		char* tmp4 = cm_strcat3(tmp3, " ", req.version);
+		char* tmp5 = cm_strcat3(tmp4, "\" \"", useragent);
+		char* log = cm_strcat(tmp5, "\"");
+		free(tmp);
+		free(tmp2);
+		free(tmp3);
+		free(tmp4);
+		free(tmp5);
+		free(useragent);
+		cm_force_log(log);
+		free(log);
+
+		char* vhost = cm_strdup(config.hostname);
+		time_t cmtime = 0;
+		if(req.headers != NULL) {
+			for(i = 0; req.headers[i] != NULL; i += 2) {
+				if(cm_strcaseequ(req.headers[i], "Host")) {
+					free(vhost);
+					vhost = cm_strdup(req.headers[i + 1]);
+				} else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
+					struct tm tm;
+					strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
+#ifdef __MINGW32__
+					time_t t = 0;
+					struct tm* btm = localtime(&t);
+					cmtime = mktime(&tm);
+					cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
+#else
+						cmtime = timegm(&tm);
+#endif
+				}
+			}
+		}
+		cm_log("Server", "Host is %s", vhost);
+		int port = s == NULL ? 80 : 443;
+		char* host = cm_strdup(vhost);
+		for(i = 0; vhost[i] != 0; i++) {
+			if(vhost[i] == ':') {
+				host[i] = 0;
+				port = atoi(host + i + 1);
+				break;
+			}
+		}
+		cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
+		struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
+		for(i = 0; i < config.module_count; i++) {
+			tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
+			if(mod_req != NULL) {
+				int ret = mod_req(&tools, &req, &res);
+				int co = ret & 0xff;
+				if(co == _TW_MODULE_PASS) continue;
+				if(co == _TW_MODULE_STOP) {
+					res._processed = true;
+					break;
+				}
+				if(co == _TW_MODULE_ERROR) {
+					tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port, vhost_entry);
+					break;
+				}
+			}
+		}
+		if(!res._processed) {
+			cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
+			char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
+			cm_log("Server", "Filesystem path is %s", path);
+			bool rej = false;
+#ifdef __MINGW32__
+			char* rpath = cm_strdup(path);
+			for(i = strlen(rpath) - 1; i >= 0; i--) {
+				if(rpath[i] == '/') {
+					int j;
+					for(j = i + 1; rpath[j] != 0; j++) {
+						if(rpath[j] == ':' || rpath[j] == '.') {
+							rpath[j] = 0;
+							break;
+						}
+					}
+					break;
+				}
+			}
+			for(i = 0; i < sizeof(reserved_names) / sizeof(reserved_names[0]); i++) {
+				char* n = cm_strcat("/", reserved_names[i]);
+				if(cm_nocase_endswith(rpath, n)) {
+					tw_http_error(s, sock, 403, name, port, vhost_entry);
+					free(n);
+					rej = true;
+					cm_log("Server", "XP Patch ; rejecting access to device");
+					break;
+				}
+				free(n);
+			}
+			free(rpath);
+#endif
+			struct stat st;
+			if(!rej && stat(path, &st) == 0) {
+				if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
+					tw_http_error(s, sock, 403, name, port, vhost_entry);
+				} else if(S_ISDIR(st.st_mode)) {
+					if(req.path[strlen(req.path) - 1] != '/') {
+						cm_log("Server", "Accessing directory without the slash at the end");
+						char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
+						_tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0);
+						free(headers[1]);
+					} else {
+						char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
+						int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
+						bool found = false;
+						for(i = 0; i < index_count; i++) {
+							char* p = cm_strcat3(path, "/", indexes[i]);
+							FILE* f = fopen(p, "rb");
+							if(f != NULL) {
+								char* ext = NULL;
+								int j;
+								for(j = strlen(p) - 1; j >= 0; j--) {
+									if(p[j] == '.') {
+										ext = cm_strdup(p + j);
+										break;
+									} else if(p[j] == '/') {
+										break;
+									}
+								}
+								struct stat st;
+								stat(p, &st);
+								char* mime = tw_get_mime(ext, vhost_entry);
+								tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
+								fclose(f);
+								if(ext != NULL) free(ext);
+								free(p);
+								found = true;
+								break;
+							}
+							free(p);
+						}
+						if(!found) {
+							char* str = malloc(1);
+							str[0] = 0;
+							char** items = cm_scandir(path);
+							addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
+							addstring(&str, "<html>\n");
+							addstring(&str, "	<head>\n");
+							addstring(&str, "		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
+							addstring(&str, "		<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
+							addstring(&str, "		<title>Index of %h</title>\n", req.path);
+							addstring(&str, "	</head>\n");
+							addstring(&str, "	<body>\n");
+							addstring(&str, "		<h1>Index of %h</h1>\n", req.path);
+							addstring(&str, "		<hr>\n");
+							addstring(&str, "		<table border=\"0\">\n");
+							addstring(&str, "			<tr>\n");
+							addstring(&str, "				<th></th>\n");
+							addstring(&str, "				<th>Filename</th>\n");
+							addstring(&str, "				<th>MIME</th>\n");
+							addstring(&str, "				<th>Size</th>\n");
+							addstring(&str, "			</tr>\n");
+							int readme = -1;
+							char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
+							int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
+							if(items != NULL) {
+								int phase = 0;
+							doit:
+								for(i = 0; items[i] != NULL; i++) {
+									int j;
+									char* fpth = cm_strcat3(path, "/", items[i]);
+									struct stat s;
+									char size[512];
+									size[0] = 0;
+									stat(fpth, &s);
+									if(phase == 0 && !S_ISDIR(s.st_mode)) {
+										free(fpth);
+										continue;
+									} else if(phase == 1 && S_ISDIR(s.st_mode)) {
+										free(fpth);
+										continue;
+									}
+									if(readme == -1) {
+										for(j = 0; j < readme_count; j++) {
+											if(strcmp(items[i], readmes[j]) == 0) {
+												readme = j;
+												break;
+											}
+										}
+										if(readme != -1) {
+											free(fpth);
+											continue;
+										}
+									}
+									if(s.st_size < 1024ULL) {
+										sprintf(size, "%d", (int)s.st_size);
+									} else if(s.st_size < 1024ULL * 1024) {
+										sprintf(size, "%.1fK", (double)s.st_size / 1024);
+									} else if(s.st_size < 1024ULL * 1024 * 1024) {
+										sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
+									} else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
+										sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
+									} else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
+										sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
+									}
+
+									free(fpth);
+
+									char* ext = NULL;
+									for(j = strlen(items[i]) - 1; j >= 0; j--) {
+										if(items[i][j] == '.') {
+											ext = cm_strdup(items[i] + j);
+											break;
+										} else if(items[i][j] == '/') {
+											break;
+										}
+									}
+									char* showmime = "";
+									char* mime = tw_get_mime(ext, vhost_entry);
+									if(strcmp(items[i], "../") == 0) {
+										mime = "misc/parent";
+										size[0] = 0;
+									} else if(items[i][strlen(items[i]) - 1] == '/') {
+										mime = "misc/dir";
+										size[0] = 0;
+									} else {
+										showmime = mime;
+									}
+									char* icon = tw_get_icon(mime, vhost_entry);
+									if(ext != NULL) free(ext);
+									char* itm = cm_strdup(items[i]);
+									if(strlen(itm) >= 32) {
+										if(itm[strlen(itm) - 1] == '/') {
+											itm[31] = 0;
+											itm[30] = '/';
+											itm[29] = '.';
+											itm[28] = '.';
+											itm[27] = '.';
+										} else {
+											itm[31] = 0;
+											itm[30] = '.';
+											itm[29] = '.';
+											itm[28] = '.';
+										}
+									}
+									addstring(&str, "<tr>\n");
+									addstring(&str, "	<td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
+									addstring(&str, "	<td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
+									addstring(&str, "	<td><code>  %h  </code></td>\n", showmime);
+									addstring(&str, "	<td><code>  %s  </code></td>\n", size);
+									addstring(&str, "</tr>\n");
+									free(itm);
+								}
+								phase++;
+								if(phase != 2) goto doit;
+								for(i = 0; items[i] != NULL; i++) free(items[i]);
+								free(items);
+							}
+							addstring(&str, "		</table>\n");
+							if(readme != -1) {
+								addstring(&str, "<hr>\n");
+								char* fpth = cm_strcat3(path, "/", readmes[readme]);
+								struct stat s;
+								stat(fpth, &s);
+								FILE* fr = fopen(fpth, "r");
+								if(fr != NULL) {
+									char* rmbuf = malloc(s.st_size + 1);
+									rmbuf[s.st_size] = 0;
+									fread(rmbuf, s.st_size, 1, fr);
+									addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
+									fclose(fr);
+									free(rmbuf);
+								}
+								free(fpth);
+							}
+							addstring(&str, "		<hr>\n");
+							int hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;
+							if(hp == 0) {
+								addstring(&str, "		<address>%s Server at %s Port %d</address>\n", tw_server, name, port);
+							} else {
+								addstring(&str, "		<address>%s Server at %s</address>\n", tw_server, name, port);
+							}
+							addstring(&str, "	</body>\n");
+							addstring(&str, "</html>\n");
+							tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
+							free(str);
+						}
+					}
+				} else {
+					char* ext = NULL;
+					for(i = strlen(req.path) - 1; i >= 0; i--) {
+						if(req.path[i] == '.') {
+							ext = cm_strdup(req.path + i);
+							break;
+						} else if(req.path[i] == '/') {
+							break;
+						}
+					}
+					char* mime = tw_get_mime(ext, vhost_entry);
+					if(ext != NULL) free(ext);
+					FILE* f = fopen(path, "rb");
+					tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
+					fclose(f);
+				}
+			} else {
+				tw_http_error(s, sock, 404, name, port, vhost_entry);
+			}
+			free(path);
+		}
+		free(vhost);
+		free(host);
+	} else if(ret == -1) {
+	} else {
+		tw_http_error(s, sock, 400, name, port, &config.root);
+	}
+	tw_free_request(&req);
+cleanup:
+#ifndef NO_SSL
+	if(sslworks) {
+		SSL_shutdown(s);
+	}
+	SSL_free(s);
+#endif
+	close_socket(sock);
+#ifdef __MINGW32__
+	_endthreadex(0);
+#elif defined(__HAIKU__)
+		exit_thread(0);
+#endif
+	;
+}
+
+#ifdef SERVICE
+extern SERVICE_STATUS status;
+extern SERVICE_STATUS_HANDLE status_handle;
+#endif
+
+#if defined(__MINGW32__) || defined(__HAIKU__)
+struct thread_entry {
+#ifdef __HAIKU__
+	thread_id thread;
+#else
+	HANDLE handle;
+#endif
+	bool used;
+};
+#endif
+
+void tw_server_loop(void) {
+	int i;
+#if defined(__MINGW32__) || defined(__HAIKU__)
+	struct thread_entry threads[2048];
+	for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
+		threads[i].used = false;
+	}
+#endif
+#ifdef USE_POLL
+	struct pollfd pollfds[sockcount];
+	for(i = 0; i < sockcount; i++) {
+		pollfds[i].fd = sockets[i];
+		pollfds[i].events = POLLIN | POLLPRI;
+	}
+#else
+		fd_set fdset;
+		struct timeval tv;
+#endif
+	while(1) {
+#ifdef USE_POLL
+		int ret = poll(pollfds, sockcount, 1000);
+#else
+			FD_ZERO(&fdset);
+			for(i = 0; i < sockcount; i++) {
+				FD_SET(sockets[i], &fdset);
+			}
+			tv.tv_sec = 1;
+			tv.tv_usec = 0;
+#ifdef __HAIKU__
+			int ret = select(32, &fdset, NULL, NULL, &tv);
+#else
+			int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
+#endif
+#endif
+		if(ret == -1) {
+#ifndef __MINGW32__
+			cm_log("Server", "Select failure: %s", strerror(errno));
+#endif
+			break;
+		} else if(ret == 0) {
+#ifdef SERVICE
+			if(status.dwCurrentState == SERVICE_STOP_PENDING) {
+				break;
+			}
+#endif
+		} else if(ret > 0) {
+			/* connection */
+			int i;
+			for(i = 0; i < sockcount; i++) {
+				bool cond;
+#ifdef USE_POLL
+				cond = pollfds[i].revents & POLLIN;
+#else
+					cond = FD_ISSET(sockets[i], &fdset);
+#endif
+				if(cond) {
+					SOCKADDR claddr;
+					int clen = sizeof(claddr);
+					int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
+					cm_log("Server", "New connection accepted");
+#if defined(__MINGW32__) || defined(__HAIKU__)
+					struct pass_entry* e = malloc(sizeof(*e));
+					e->sock = sock;
+					e->ssl = config.ports[i] & (1ULL << 32);
+					e->port = config.ports[i];
+					e->addr = claddr;
+#endif
+#ifdef __MINGW32__
+					int j;
+					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
+						if(threads[j].used) {
+							DWORD ex;
+							GetExitCodeThread(threads[j].handle, &ex);
+							if(ex != STILL_ACTIVE) {
+								CloseHandle(threads[j].handle);
+								threads[j].used = false;
+							}
+						}
+					}
+					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
+						if(!threads[j].used) {
+							threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
+							threads[j].used = true;
+							break;
+						}
+					}
+#elif defined(__HAIKU__)
+						int j;
+						for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
+							if(threads[j].used) {
+								thread_info info;
+								bool kill = false;
+								if(get_thread_info(threads[j].thread, &info) == B_OK) {
+								} else {
+									kill = true;
+								}
+								if(kill) {
+									threads[j].used = false;
+								}
+							}
+						}
+						for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
+							if(!threads[j].used) {
+								threads[j].thread = spawn_thread(tw_server_pass, "Tewi HTTPd", 60, e);
+								threads[j].used = true;
+								resume_thread(threads[j].thread);
+								break;
+							}
+						}
+#else
+					pid_t pid = fork();
+					if(pid == 0) {
+						int j;
+						for(j = 0; j < sockcount; j++) close_socket(sockets[j]);
+						tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
+						_exit(0);
+					} else {
+						close_socket(sock);
+					}
+#endif
+				}
+			}
+		}
+	}
+}
Index: /tags/1.03A/Server/ssl.c
===================================================================
--- /tags/1.03A/Server/ssl.c	(revision 125)
+++ /tags/1.03A/Server/ssl.c	(revision 125)
@@ -0,0 +1,41 @@
+/* $Id$ */
+
+#define SOURCE
+
+#include "tw_ssl.h"
+
+#include "tw_config.h"
+
+#include <stdio.h>
+
+#include <cm_log.h>
+
+extern struct tw_config config;
+
+int tw_ssl_cert_cb(SSL* ssl, void* arg) {
+	const char* s = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
+	if(s != NULL) {
+		cm_log("SSL", "Certificate request for %s", s);
+	} else {
+		s = config.hostname;
+		cm_log("SSL", "Could not get the servername, defaulting to the hostname: %s", s);
+	}
+	struct tw_config_entry* e = tw_vhost_match(s, (__UINTPTR_TYPE__)arg);
+	if(e != NULL && e->sslkey != NULL && e->sslcert != NULL) {
+		SSL_use_PrivateKey_file(ssl, e->sslkey, SSL_FILETYPE_PEM);
+		SSL_use_certificate_file(ssl, e->sslcert, SSL_FILETYPE_PEM);
+		return 1;
+	} else if(config.root.sslkey != NULL && config.root.sslcert != NULL) {
+		SSL_use_PrivateKey_file(ssl, config.root.sslkey, SSL_FILETYPE_PEM);
+		SSL_use_certificate_file(ssl, config.root.sslcert, SSL_FILETYPE_PEM);
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+SSL_CTX* tw_create_ssl_ctx(__UINTPTR_TYPE__ port) {
+	SSL_CTX* ctx = SSL_CTX_new(TLS_server_method());
+	SSL_CTX_set_cert_cb(ctx, tw_ssl_cert_cb, (void*)port);
+	return ctx;
+}
Index: /tags/1.03A/Server/strptime.c
===================================================================
--- /tags/1.03A/Server/strptime.c	(revision 125)
+++ /tags/1.03A/Server/strptime.c	(revision 125)
@@ -0,0 +1,838 @@
+/*	$NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $	*/
+/* http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/lib/libc/time/strptime.c?only_with_tag=HEAD
+ * NetBSD implementation strptime().
+ * Format description: https://netbsd.gw.com/cgi-bin/man-cgi?strptime+3+NetBSD-current
+*/
+/*-
+ * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code was contributed to The NetBSD Foundation by Klaus Klein.
+ * Heavily optimised by David Laight
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+//#include <sys/cdefs.h>
+//__RCSID("$NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $");
+
+#ifdef __MINGW32__
+
+#include <ctype.h>
+#include <string.h>
+#include <time.h>
+#include <stdint.h>
+
+static const unsigned char *conv_num(const unsigned char *, int *, unsigned int, unsigned int);
+static const unsigned char *find_string(const unsigned char *, int *, const char * const *, const char * const *, int);
+
+/*
+ * We do not implement alternate representations. However, we always
+ * check whether a given modifier is allowed for a certain conversion.
+ */
+#define ALT_E			0x01
+#define ALT_O			0x02
+#define LEGAL_ALT(x)	{ if (alt_format & ~(x)) return NULL; }
+
+#define TM_YEAR_BASE	1900
+
+#define TM_SUNDAY       0
+#define TM_MONDAY       1
+#define TM_TUESDAY      2
+#define TM_WEDNESDAY    3
+#define TM_THURSDAY     4
+#define TM_FRIDAY       5
+#define TM_SATURDAY     6
+
+#define S_YEAR			(1 << 0)
+#define S_MON			(1 << 1)
+#define S_YDAY			(1 << 2)
+#define S_MDAY			(1 << 3)
+#define S_WDAY			(1 << 4)
+#define S_HOUR			(1 << 5)
+
+#define HAVE_MDAY(s)	(s & S_MDAY)
+#define HAVE_MON(s)		(s & S_MON)
+#define HAVE_WDAY(s)	(s & S_WDAY)
+#define HAVE_YDAY(s)	(s & S_YDAY)
+#define HAVE_YEAR(s)	(s & S_YEAR)
+#define HAVE_HOUR(s)	(s & S_HOUR)
+
+#define SECSPERMIN      60
+#define MINSPERHOUR     60
+#define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
+#define HOURSPERDAY     24
+
+#define HERE_D_T_FMT    "%a %b %e %H:%M:%S %Y"
+#define HERE_D_FMT      "%y/%m/%d"
+#define HERE_T_FMT_AMPM "%I:%M:%S %p"
+#define HERE_T_FMT      "%H:%M:%S"
+
+#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
+
+/*
+** Since everything in isleap is modulo 400 (or a factor of 400), we know that
+**	isleap(y) == isleap(y % 400)
+** and so
+**	isleap(a + b) == isleap((a + b) % 400)
+** or
+**	isleap(a + b) == isleap(a % 400 + b % 400)
+** This is true even if % means modulo rather than Fortran remainder
+** (which is allowed by C89 but not by C99 or later).
+** We use this to avoid addition overflow problems.
+*/
+
+#define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
+
+#ifdef _MSC_VER
+#define tzname              _tzname
+#define strncasecmp         _strnicmp
+#endif
+
+#ifdef TM_ZONE
+static char* utc = "UTC";
+#endif
+/* RFC-822/RFC-2822 */
+static const char* const nast[] = {
+       "EST",    "CST",    "MST",    "PST",    "\0\0\0"
+};
+static const char* const nadt[] = {
+       "EDT",    "CDT",    "MDT",    "PDT",    "\0\0\0"
+};
+static const char* weekday_name[] =
+{
+    "Sunday", "Monday", "Tuesday", "Wednesday",
+    "Thursday", "Friday", "Saturday"
+};
+static const char* ab_weekday_name[] =
+{
+    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
+};
+static const char* month_name[] =
+{
+    "January", "February", "March", "April", "May", "June",
+    "July", "August", "September", "October", "November", "December"
+};
+static const char* ab_month_name[] =
+{
+    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+};
+static const char* am_pm[] = {"AM", "PM"};
+
+
+/*
+ * Table to determine the ordinal date for the start of a month.
+ * Ref: http://en.wikipedia.org/wiki/ISO_week_date
+ */
+static const int start_of_month[2][13] = {
+    /* non-leap year */
+    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
+    /* leap year */
+    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
+};
+
+/*
+ * Calculate the week day of the first day of a year. Valid for
+ * the Gregorian calendar, which began Sept 14, 1752 in the UK
+ * and its colonies. Ref:
+ * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
+ */
+
+static int
+first_wday_of(int yr)
+{
+    return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) /  4) +
+        (isleap(yr) ? 6 : 0) + 1) % 7;
+}
+
+#define delim(p)	((p) == '\0' || isspace((unsigned char)(p)))
+
+static int
+fromzone(const unsigned char **bp, struct tm *tm, int mandatory)
+{
+//    timezone_t tz;
+    char buf[512], *p;
+    const unsigned char *rp;
+
+    for (p = buf, rp = *bp; !delim(*rp) && p < &buf[sizeof(buf) - 1]; rp++)
+        *p++ = *rp;
+    *p = '\0';
+
+    if (mandatory)
+        *bp = rp;
+    if (!isalnum((unsigned char)*buf))
+        return 0;
+//    tz = tzalloc(buf);
+//    if (tz == NULL)
+//        return 0;
+
+    *bp = rp;
+    tm->tm_isdst = 0;	/* XXX */
+#ifdef TM_GMTOFF
+    tm->TM_GMTOFF = tzgetgmtoff(tz, tm->tm_isdst);
+#endif
+#ifdef TM_ZONE
+    // Can't use tzgetname() here because we are going to free()
+    tm->TM_ZONE = NULL; /* XXX */
+#endif
+//    tzfree(tz);
+    return 1;
+}
+
+char* strptime(const char *buf, const char *fmt, struct tm *tm)
+{
+    unsigned char c;
+    const unsigned char *bp, *ep, *zname;
+    int alt_format, i, split_year = 0, neg = 0, state = 0,
+        day_offset = -1, week_offset = 0, offs, mandatory;
+    const char *new_fmt;
+
+    bp = (const unsigned char *)buf;
+
+    while (bp != NULL && (c = *fmt++) != '\0') {
+        /* Clear `alternate' modifier prior to new conversion. */
+        alt_format = 0;
+        i = 0;
+
+        /* Eat up white-space. */
+        if (isspace(c)) {
+            while (isspace(*bp))
+                bp++;
+            continue;
+        }
+
+        if (c != '%')
+            goto literal;
+
+
+again:		switch (c = *fmt++) {
+        case '%':	/* "%%" is converted to "%". */
+literal:
+            if (c != *bp++)
+                return NULL;
+            LEGAL_ALT(0);
+            continue;
+
+        /*
+         * "Alternative" modifiers. Just set the appropriate flag
+         * and start over again.
+         */
+        case 'E':	/* "%E?" alternative conversion modifier. */
+            LEGAL_ALT(0);
+            alt_format |= ALT_E;
+            goto again;
+
+        case 'O':	/* "%O?" alternative conversion modifier. */
+            LEGAL_ALT(0);
+            alt_format |= ALT_O;
+            goto again;
+
+        /*
+         * "Complex" conversion rules, implemented through recursion.
+         */
+        case 'c':	/* Date and time, using the locale's format. */
+//            new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
+            new_fmt = HERE_D_T_FMT;
+            state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
+            goto recurse;
+
+        case 'F':	/* The date as "%Y-%m-%d". */
+            new_fmt = "%Y-%m-%d";
+            LEGAL_ALT(0);
+            state |= S_MON | S_MDAY | S_YEAR;
+            goto recurse;
+
+        case 'R':	/* The time as "%H:%M". */
+            new_fmt = "%H:%M";
+            LEGAL_ALT(0);
+            goto recurse;
+
+        case 'r':	/* The time in 12-hour clock representation. */
+//            new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
+            new_fmt = HERE_T_FMT_AMPM;
+            LEGAL_ALT(0);
+            goto recurse;
+
+        case 'X':	/* The time, using the locale's format. */
+            /* fall through */
+
+        case 'T':	/* The time as "%H:%M:%S". */
+            new_fmt = HERE_T_FMT;
+            LEGAL_ALT(0);
+
+recurse:
+            bp = (const unsigned char *)strptime((const char *)bp,
+                                new_fmt, tm);
+            LEGAL_ALT(ALT_E);
+            continue;
+
+        case 'x':	/* The date, using the locale's format. */
+            /* fall throug */
+
+        case 'D':	/* The date as "%y/%m/%d". */
+        {
+            new_fmt = HERE_D_FMT;
+            LEGAL_ALT(0);
+            state |= S_MON | S_MDAY | S_YEAR;
+            const int year = split_year ? tm->tm_year : 0;
+
+            bp = (const unsigned char *)strptime((const char *)bp,
+                                new_fmt, tm);
+            LEGAL_ALT(ALT_E);
+            tm->tm_year += year;
+            if (split_year && tm->tm_year % (2000 - TM_YEAR_BASE) <= 68)
+                tm->tm_year -= 2000 - TM_YEAR_BASE;
+            split_year = 1;
+            continue;
+        }
+        /*
+         * "Elementary" conversion rules.
+         */
+        case 'A':	/* The day of week, using the locale's form. */
+        case 'a':
+            bp = find_string(bp, &tm->tm_wday, weekday_name, ab_weekday_name, 7);
+            LEGAL_ALT(0);
+            state |= S_WDAY;
+            continue;
+
+        case 'B':	/* The month, using the locale's form. */
+        case 'b':
+        case 'h':
+            bp = find_string(bp, &tm->tm_mon, month_name, ab_month_name, 12);
+            LEGAL_ALT(0);
+            state |= S_MON;
+            continue;
+
+        case 'C':	/* The century number. */
+            i = 20;
+            bp = conv_num(bp, &i, 0, 99);
+
+            i = i * 100 - TM_YEAR_BASE;
+            if (split_year)
+                i += tm->tm_year % 100;
+            split_year = 1;
+            tm->tm_year = i;
+            LEGAL_ALT(ALT_E);
+            state |= S_YEAR;
+            continue;
+
+        case 'd':	/* The day of month. */
+        case 'e':
+            bp = conv_num(bp, &tm->tm_mday, 1, 31);
+            LEGAL_ALT(ALT_O);
+            state |= S_MDAY;
+            continue;
+
+        case 'k':	/* The hour (24-hour clock representation). */
+            LEGAL_ALT(0);
+            /* FALLTHROUGH */
+        case 'H':
+            bp = conv_num(bp, &tm->tm_hour, 0, 23);
+            LEGAL_ALT(ALT_O);
+            state |= S_HOUR;
+            continue;
+
+        case 'l':	/* The hour (12-hour clock representation). */
+            LEGAL_ALT(0);
+            /* FALLTHROUGH */
+        case 'I':
+            bp = conv_num(bp, &tm->tm_hour, 1, 12);
+            if (tm->tm_hour == 12)
+                tm->tm_hour = 0;
+            LEGAL_ALT(ALT_O);
+            state |= S_HOUR;
+            continue;
+
+        case 'j':	/* The day of year. */
+            i = 1;
+            bp = conv_num(bp, &i, 1, 366);
+            tm->tm_yday = i - 1;
+            LEGAL_ALT(0);
+            state |= S_YDAY;
+            continue;
+
+        case 'M':	/* The minute. */
+            bp = conv_num(bp, &tm->tm_min, 0, 59);
+            LEGAL_ALT(ALT_O);
+            continue;
+
+        case 'm':	/* The month. */
+            i = 1;
+            bp = conv_num(bp, &i, 1, 12);
+            tm->tm_mon = i - 1;
+            LEGAL_ALT(ALT_O);
+            state |= S_MON;
+            continue;
+
+        case 'p':	/* The locale's equivalent of AM/PM. */
+            bp = find_string(bp, &i, am_pm, NULL, 2);
+            if (HAVE_HOUR(state) && tm->tm_hour > 11)
+                return NULL;
+            tm->tm_hour += i * 12;
+            LEGAL_ALT(0);
+            continue;
+
+        case 'S':	/* The seconds. */
+            bp = conv_num(bp, &tm->tm_sec, 0, 61);
+            LEGAL_ALT(ALT_O);
+            continue;
+
+#ifndef TIME_MAX
+#define TIME_MAX	INT64_MAX
+#endif
+        case 's':	/* seconds since the epoch */
+            {
+                time_t sse = 0;
+                uint64_t rulim = TIME_MAX;
+
+                if (*bp < '0' || *bp > '9') {
+                    bp = NULL;
+                    continue;
+                }
+
+                do {
+                    sse *= 10;
+                    sse += *bp++ - '0';
+                    rulim /= 10;
+                } while ((sse * 10 <= TIME_MAX) &&
+                     rulim && *bp >= '0' && *bp <= '9');
+
+                if (sse < 0 || (uint64_t)sse > TIME_MAX) {
+                    bp = NULL;
+                    continue;
+                }
+#ifdef _WIN32
+                if (localtime_s(tm, &sse) == 0)
+#else
+                if (localtime_r(&sse, tm))
+#endif
+                    state |= S_YDAY | S_WDAY | S_MON | S_MDAY | S_YEAR;
+                else
+                    bp = NULL;
+            }
+            continue;
+
+        case 'U':	/* The week of year, beginning on sunday. */
+        case 'W':	/* The week of year, beginning on monday. */
+            /*
+             * This is bogus, as we can not assume any valid
+             * information present in the tm structure at this
+             * point to calculate a real value, so save the
+             * week for now in case it can be used later.
+             */
+            bp = conv_num(bp, &i, 0, 53);
+            LEGAL_ALT(ALT_O);
+            if (c == 'U')
+                day_offset = TM_SUNDAY;
+            else
+                day_offset = TM_MONDAY;
+            week_offset = i;
+            continue;
+
+        case 'w':	/* The day of week, beginning on sunday. */
+            bp = conv_num(bp, &tm->tm_wday, 0, 6);
+            LEGAL_ALT(ALT_O);
+            state |= S_WDAY;
+            continue;
+
+        case 'u':	/* The day of week, monday = 1. */
+            bp = conv_num(bp, &i, 1, 7);
+            tm->tm_wday = i % 7;
+            LEGAL_ALT(ALT_O);
+            state |= S_WDAY;
+            continue;
+
+        case 'g':	/* The year corresponding to the ISO week
+                 * number but without the century.
+                 */
+            bp = conv_num(bp, &i, 0, 99);
+            continue;
+
+        case 'G':	/* The year corresponding to the ISO week
+                 * number with century.
+                 */
+            do
+                bp++;
+            while (isdigit(*bp));
+            continue;
+
+        case 'V':	/* The ISO 8601:1988 week number as decimal */
+            bp = conv_num(bp, &i, 0, 53);
+            continue;
+
+        case 'Y':	/* The year. */
+            i = TM_YEAR_BASE;	/* just for data sanity... */
+            bp = conv_num(bp, &i, 0, 9999);
+            tm->tm_year = i - TM_YEAR_BASE;
+            LEGAL_ALT(ALT_E);
+            state |= S_YEAR;
+            continue;
+
+        case 'y':	/* The year within 100 years of the epoch. */
+            /* LEGAL_ALT(ALT_E | ALT_O); */
+            bp = conv_num(bp, &i, 0, 99);
+
+            if (split_year)
+                /* preserve century */
+                i += (tm->tm_year / 100) * 100;
+            else {
+                split_year = 1;
+                if (i <= 68)
+                    i = i + 2000 - TM_YEAR_BASE;
+            }
+            tm->tm_year = i;
+            state |= S_YEAR;
+            continue;
+
+        case 'Z':       // time zone name
+        case 'z':       //
+#ifdef _WIN32
+            _tzset();
+#else
+            tzset();
+#endif
+            mandatory = c == 'z';
+            /*
+             * We recognize all ISO 8601 formats:
+             * Z	= Zulu time/UTC
+             * [+-]hhmm
+             * [+-]hh:mm
+             * [+-]hh
+             * We recognize all RFC-822/RFC-2822 formats:
+             * UT|GMT
+             *          North American : UTC offsets
+             * E[DS]T = Eastern : -4 | -5
+             * C[DS]T = Central : -5 | -6
+             * M[DS]T = Mountain: -6 | -7
+             * P[DS]T = Pacific : -7 | -8
+             *          Nautical/Military
+             * [A-IL-M] = -1 ... -9 (J not used)
+             * [N-Y]  = +1 ... +12
+             * Note: J maybe used to denote non-nautical
+             *       local time
+             */
+            if (mandatory)
+                while (isspace(*bp))
+                    bp++;
+
+            zname = bp;
+            switch (*bp++) {
+            case 'G':
+                if (*bp++ != 'M')
+                    goto namedzone;
+                /*FALLTHROUGH*/
+            case 'U':
+                if (*bp++ != 'T')
+                    goto namedzone;
+                else if (!delim(*bp) && *bp++ != 'C')
+                    goto namedzone;
+                /*FALLTHROUGH*/
+            case 'Z':
+                if (!delim(*bp))
+                    goto namedzone;
+                tm->tm_isdst = 0;
+#ifdef TM_GMTOFF
+                tm->TM_GMTOFF = 0;
+#endif
+#ifdef TM_ZONE
+                tm->TM_ZONE = utc;
+#endif
+                continue;
+            case '+':
+                neg = 0;
+                break;
+            case '-':
+                neg = 1;
+                break;
+            default:
+namedzone:
+                bp = zname;
+
+                /* Nautical / Military style */
+                if (delim(bp[1]) &&
+                    ((*bp >= 'A' && *bp <= 'I') ||
+                     (*bp >= 'L' && *bp <= 'Y'))) {
+#ifdef TM_GMTOFF
+                    /* Argh! No 'J'! */
+                    if (*bp >= 'A' && *bp <= 'I')
+                        tm->TM_GMTOFF =
+                            (int)*bp - ('A' - 1);
+                    else if (*bp >= 'L' && *bp <= 'M')
+                        tm->TM_GMTOFF = (int)*bp - 'A';
+                    else if (*bp >= 'N' && *bp <= 'Y')
+                        tm->TM_GMTOFF = 'M' - (int)*bp;
+                    tm->TM_GMTOFF *= SECSPERHOUR;
+#endif
+#ifdef TM_ZONE
+                    tm->TM_ZONE = NULL; /* XXX */
+#endif
+                    bp++;
+                    continue;
+                }
+                /* 'J' is local time */
+                if (delim(bp[1]) && *bp == 'J') {
+#ifdef TM_GMTOFF
+                    tm->TM_GMTOFF = -timezone;
+#endif
+#ifdef TM_ZONE
+                    tm->TM_ZONE = NULL; /* XXX */
+#endif
+                    bp++;
+                    continue;
+                }
+
+                /*
+                 * From our 3 letter hard-coded table
+                 * XXX: Can be removed, handled by tzload()
+                 */
+                if (delim(bp[0]) || delim(bp[1]) ||
+                    delim(bp[2]) || !delim(bp[3]))
+                    goto loadzone;
+                ep = find_string(bp, &i, nast, NULL, 4);
+                if (ep != NULL) {
+#ifdef TM_GMTOFF
+                    tm->TM_GMTOFF = (-5 - i) * SECSPERHOUR;
+#endif
+#ifdef TM_ZONE
+                    tm->TM_ZONE = __UNCONST(nast[i]);
+#endif
+                    bp = ep;
+                    continue;
+                }
+                ep = find_string(bp, &i, nadt, NULL, 4);
+                if (ep != NULL) {
+                    tm->tm_isdst = 1;
+#ifdef TM_GMTOFF
+                    tm->TM_GMTOFF = (-4 - i) * SECSPERHOUR;
+#endif
+#ifdef TM_ZONE
+                    tm->TM_ZONE = __UNCONST(nadt[i]);
+#endif
+                    bp = ep;
+                    continue;
+                }
+                /*
+                 * Our current timezone
+                 */
+                ep = find_string(bp, &i,
+                             (const char * const *)NULL,
+                              NULL, 2);
+                if (ep != NULL) {
+                    tm->tm_isdst = i;
+#ifdef TM_GMTOFF
+                    tm->TM_GMTOFF = -timezone;
+#endif
+#ifdef TM_ZONE
+                    tm->TM_ZONE = tzname[i];
+#endif
+                    bp = ep;
+                    continue;
+                }
+loadzone:
+                /*
+                 * The hard way, load the zone!
+                 */
+                if (fromzone(&bp, tm, mandatory))
+                    continue;
+                goto out;
+            }
+            offs = 0;
+            for (i = 0; i < 4; ) {
+                if (isdigit(*bp)) {
+                    offs = offs * 10 + (*bp++ - '0');
+                    i++;
+                    continue;
+                }
+                if (i == 2 && *bp == ':') {
+                    bp++;
+                    continue;
+                }
+                break;
+            }
+            if (isdigit(*bp))
+                goto out;
+            switch (i) {
+            case 2:
+                offs *= SECSPERHOUR;
+                break;
+            case 4:
+                i = offs % 100;
+                offs /= 100;
+                if (i >= SECSPERMIN)
+                    goto out;
+                /* Convert minutes into decimal */
+                offs = offs * SECSPERHOUR + i * SECSPERMIN;
+                break;
+            default:
+out:
+                if (mandatory)
+                    return NULL;
+                bp = zname;
+                continue;
+            }
+            /* ISO 8601 & RFC 3339 limit to 23:59 max */
+            if (offs >= (HOURSPERDAY * SECSPERHOUR))
+                goto out;
+            if (neg)
+                offs = -offs;
+            tm->tm_isdst = 0;	/* XXX */
+#ifdef TM_GMTOFF
+            tm->TM_GMTOFF = offs;
+#endif
+#ifdef TM_ZONE
+            tm->TM_ZONE = NULL;	/* XXX */
+#endif
+            continue;
+
+        /*
+         * Miscellaneous conversions.
+         */
+        case 'n':	/* Any kind of white-space. */
+        case 't':
+            while (isspace(*bp))
+                bp++;
+            LEGAL_ALT(0);
+            continue;
+
+
+        default:	/* Unknown/unsupported conversion. */
+            return NULL;
+        }
+    }
+
+    if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
+        if (HAVE_MON(state) && HAVE_MDAY(state)) {
+            /* calculate day of year (ordinal date) */
+            tm->tm_yday =  start_of_month[isleap_sum(tm->tm_year,
+                TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
+            state |= S_YDAY;
+        } else if (day_offset != -1) {
+            /*
+             * Set the date to the first Sunday (or Monday)
+             * of the specified week of the year.
+             */
+            if (!HAVE_WDAY(state)) {
+                tm->tm_wday = day_offset;
+                state |= S_WDAY;
+            }
+            tm->tm_yday = (7 -
+                first_wday_of(tm->tm_year + TM_YEAR_BASE) +
+                day_offset) % 7 + (week_offset - 1) * 7 +
+                tm->tm_wday  - day_offset;
+            state |= S_YDAY;
+        }
+    }
+
+    if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
+        int isleap;
+
+        if (!HAVE_MON(state)) {
+            /* calculate month of day of year */
+            i = 0;
+            isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
+            while (tm->tm_yday >= start_of_month[isleap][i])
+                i++;
+            if (i > 12) {
+                i = 1;
+                tm->tm_yday -= start_of_month[isleap][12];
+                tm->tm_year++;
+            }
+            tm->tm_mon = i - 1;
+            state |= S_MON;
+        }
+
+        if (!HAVE_MDAY(state)) {
+            /* calculate day of month */
+            isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
+            tm->tm_mday = tm->tm_yday -
+                start_of_month[isleap][tm->tm_mon] + 1;
+            state |= S_MDAY;
+        }
+
+        if (!HAVE_WDAY(state)) {
+            /* calculate day of week */
+            i = 0;
+            week_offset = first_wday_of(tm->tm_year);
+            while (i++ <= tm->tm_yday) {
+                if (week_offset++ >= 6)
+                    week_offset = 0;
+            }
+            tm->tm_wday = week_offset;
+            state |= S_WDAY;
+        }
+    }
+
+    return (char*)bp;
+}
+
+
+static const unsigned char *
+conv_num(const unsigned char *buf, int *dest, unsigned int llim, unsigned int ulim)
+{
+    unsigned int result = 0;
+    unsigned char ch;
+
+    /* The limit also determines the number of valid digits. */
+    unsigned int rulim = ulim;
+
+    ch = *buf;
+    if (ch < '0' || ch > '9')
+        return NULL;
+
+    do {
+        result *= 10;
+        result += ch - '0';
+        rulim /= 10;
+        ch = *++buf;
+    } while ((result <= ulim) && rulim && ch >= '0' && ch <= '9');
+
+    if (result < llim || result > ulim)
+        return NULL;
+
+    *dest = result;
+    return buf;
+}
+
+static const unsigned char *
+find_string(const unsigned char *bp, int *tgt, const char * const *n1,
+        const char * const *n2, int c)
+{
+    int i;
+    size_t len;
+
+    /* check full name - then abbreviated ones */
+    for (; n1 != NULL; n1 = n2, n2 = NULL) {
+        for (i = 0; i < c; i++, n1++) {
+            len = strlen(*n1);
+            if (strncasecmp(*n1, (const char *)bp, len) == 0) {
+                *tgt = i;
+                return bp + len;
+            }
+        }
+    }
+
+    /* Nothing matched */
+    return NULL;
+}
+
+#endif
Index: /tags/1.03A/Server/strptime.h
===================================================================
--- /tags/1.03A/Server/strptime.h	(revision 125)
+++ /tags/1.03A/Server/strptime.h	(revision 125)
@@ -0,0 +1,10 @@
+#ifndef STRPTIME_H
+#define STRPTIME_H
+
+#include <time.h>
+
+#ifdef _WIN32
+char* strptime(const char *buf, const char *fmt, struct tm *tm);
+#endif
+
+#endif // STRPTIME_H
Index: /tags/1.03A/Server/tewi.rc
===================================================================
--- /tags/1.03A/Server/tewi.rc	(revision 125)
+++ /tags/1.03A/Server/tewi.rc	(revision 125)
@@ -0,0 +1,33 @@
+#include <winver.h>
+#include "tw_version.h"
+#include "../config.h"
+
+id ICON "./tewi.ico"
+VS_VERSION_INFO VERSIONINFO
+	FILEVERSION 1,0,0,0
+	PRODUCTVERSION 1,0,0,0
+BEGIN
+	BLOCK "StringFileInfo"
+	BEGIN
+		BLOCK "040904E4"
+		BEGIN
+			VALUE "CompanyName", "Nishi\0"
+#ifdef NO_SSL
+			VALUE "FileDescription", "Simple HTTP Daemon\0"
+#else
+			VALUE "FileDescription", "Simple HTTP Daemon with TLS/SSL\0"
+#endif
+			VALUE "LegalCopyright", "Public domain, original by Nishi\0"
+			VALUE "FileVersion", TW_VERSION
+			VALUE "ProductVersion", TW_VERSION
+			VALUE "ProductName", "Tewi HTTPd\0"
+			VALUE "License", "Unlicense\0"
+			VALUE "Info", "http://nishi.boats/tewi\0"
+		END
+	END
+	
+	BLOCK "VarFileInfo"
+	BEGIN
+		VALUE "Translation", 0x409, 65001
+	END
+END
Index: /tags/1.03A/Server/tw_config.h
===================================================================
--- /tags/1.03A/Server/tw_config.h	(revision 125)
+++ /tags/1.03A/Server/tw_config.h	(revision 125)
@@ -0,0 +1,93 @@
+/* $Id$ */
+
+#ifndef __TW_CONFIG_H__
+#define __TW_CONFIG_H__
+
+#include "tw_http.h"
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef __MINGW32__
+#include <winsock2.h>
+#define NO_IPV6
+#else
+#include <netinet/in.h>
+#ifdef __HAIKU__
+#define NO_IPV6
+#endif
+#endif
+
+#ifdef NO_IPV6
+#define SOCKADDR struct sockaddr_in
+#else
+#define SOCKADDR struct sockaddr_in6
+#endif
+
+#define MAX_PORTS 1024
+#define MAX_VHOSTS 1024
+#define MAX_MODULES 1024
+#define MAX_DIRS 1024
+#define MAX_MIME 1024
+#define MAX_ICON 1024
+#define MAX_INDEX 1024
+#define MAX_README 8
+
+enum TW_DIR_TYPE {
+	TW_DIR_ALLOW = 0,
+	TW_DIR_DENY
+};
+
+struct tw_dir_entry {
+	char* name;
+	char* dir;
+	int type;
+};
+
+struct tw_mime_entry {
+	char* ext;
+	char* mime;
+};
+
+struct tw_icon_entry {
+	char* mime;
+	char* icon;
+};
+
+struct tw_config_entry {
+	char* name;
+	int port;
+	char* sslkey;
+	char* sslcert;
+	char* root;
+	int hideport;
+	struct tw_dir_entry dirs[MAX_DIRS];
+	int dir_count;
+	struct tw_mime_entry mimes[MAX_DIRS];
+	int mime_count;
+	struct tw_icon_entry icons[MAX_DIRS];
+	int icon_count;
+	char* indexes[MAX_INDEX];
+	int index_count;
+	char* readmes[MAX_README];
+	int readme_count;
+};
+
+struct tw_config {
+	uint64_t ports[MAX_PORTS + 1]; /* If port & (1 << 32) is non-zero, it is SSL */
+	char hostname[1025];
+	struct tw_config_entry root;
+	struct tw_config_entry vhosts[MAX_VHOSTS];
+	void* modules[MAX_MODULES];
+	int module_count;
+	int vhost_count;
+	char* server_root;
+	char* extension;
+};
+
+void tw_config_init(void);
+int tw_config_read(const char* path);
+struct tw_config_entry* tw_vhost_match(const char* name, int port);
+bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost);
+
+#endif
Index: /tags/1.03A/Server/tw_http.h
===================================================================
--- /tags/1.03A/Server/tw_http.h	(revision 125)
+++ /tags/1.03A/Server/tw_http.h	(revision 125)
@@ -0,0 +1,37 @@
+/* $Id$ */
+
+#ifndef __TW_HTTP_H__
+#define __TW_HTTP_H__
+
+#include <stdbool.h>
+
+#include "../config.h"
+
+struct tw_http_request {
+	char* method;
+	char* path;
+	char* query;
+	char* version;
+	char** headers;
+	char* body;
+};
+
+struct tw_http_response {
+	char** headers;
+	int status;
+	bool _processed; /* Internal parameter */
+};
+
+#ifdef SOURCE
+#ifndef NO_SSL
+#include <openssl/ssl.h>
+#endif
+void tw_free_request(struct tw_http_request* req);
+#ifndef NO_SSL
+int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req);
+#else
+int tw_http_parse(void* ssl, int sock, struct tw_http_request* req);
+#endif
+#endif
+
+#endif
Index: /tags/1.03A/Server/tw_module.h
===================================================================
--- /tags/1.03A/Server/tw_module.h	(revision 125)
+++ /tags/1.03A/Server/tw_module.h	(revision 125)
@@ -0,0 +1,41 @@
+/* $Id$ */
+
+#ifndef __TW_MODULE_H__
+#define __TW_MODULE_H__
+
+#include "tw_config.h"
+#include "tw_http.h"
+
+struct tw_tool {
+	void (*log)(const char* name, const char* log, ...);
+	void (*add_version)(const char* string);
+};
+
+enum TW_MODULE_RETURN {
+	_TW_MODULE_PASS = 0, /* Pass to the next module. */
+	_TW_MODULE_STOP,     /* Do not pass to the next module. */
+	_TW_MODULE_ERROR,    /* Error, and do not pass to the next module. */
+
+	_TW_CONFIG_PARSED, /* Got parsed */
+	_TW_CONFIG_NOTME,  /* Did not parse */
+	_TW_CONFIG_ERROR   /* Error */
+};
+
+#define TW_MODULE_PASS _TW_MODULE_PASS
+#define TW_MODULE_STOP _TW_MODULE_STOP
+#define TW_MODULE_ERROR(x) (_TW_MODULE_ERROR | ((x) << 8))
+
+#define TW_CONFIG_PARSED _TW_CONFIG_PARSED
+#define TW_CONFIG_NOTME _TW_CONFIG_NOTME
+#define TW_CONFIG_ERROR _TW_CONFIG_ERROR
+
+typedef int (*tw_mod_init_t)(struct tw_config* config, struct tw_tool* tools);
+typedef int (*tw_mod_request_t)(struct tw_tool* tools, struct tw_http_request* req, struct tw_http_response* res);
+typedef int (*tw_mod_config_t)(struct tw_tool* tools, char** argv, int argc);
+
+void* tw_module_load(const char* path);
+void* tw_module_symbol(void* mod, const char* sym);
+void tw_init_tools(struct tw_tool* tools);
+int tw_module_init(void* mod);
+
+#endif
Index: /tags/1.03A/Server/tw_server.h
===================================================================
--- /tags/1.03A/Server/tw_server.h	(revision 125)
+++ /tags/1.03A/Server/tw_server.h	(revision 125)
@@ -0,0 +1,25 @@
+/* $Id$ */
+
+#ifndef __TW_SERVER_H__
+#define __TW_SERVER_H__
+
+#include "../config.h"
+
+#include <stddef.h>
+
+#ifndef NO_SSL
+#include <openssl/ssl.h>
+#endif
+
+int tw_server_init(void);
+void tw_server_loop(void);
+
+#ifndef NO_SSL
+size_t tw_read(SSL* ssl, int s, void* data, size_t len);
+size_t tw_write(SSL* ssl, int s, void* data, size_t len);
+#else
+size_t tw_read(void* ssl, int s, void* data, size_t len);
+size_t tw_write(void* ssl, int s, void* data, size_t len);
+#endif
+
+#endif
Index: /tags/1.03A/Server/tw_ssl.h
===================================================================
--- /tags/1.03A/Server/tw_ssl.h	(revision 125)
+++ /tags/1.03A/Server/tw_ssl.h	(revision 125)
@@ -0,0 +1,11 @@
+/* $Id$ */
+
+#ifndef __TW_SSL_H__
+#define __TW_SSL_H__
+
+#include <openssl/ssl.h>
+#include <stdint.h>
+
+SSL_CTX* tw_create_ssl_ctx(__UINTPTR_TYPE__ port);
+
+#endif
Index: /tags/1.03A/Server/tw_version.h
===================================================================
--- /tags/1.03A/Server/tw_version.h	(revision 125)
+++ /tags/1.03A/Server/tw_version.h	(revision 125)
@@ -0,0 +1,11 @@
+/* $Id$ */
+
+#ifndef __TW_VERSION_H__
+#define __TW_VERSION_H__
+
+#define TW_VERSION "1.03A\0"
+
+const char* tw_get_version(void);
+const char* tw_get_platform(void);
+
+#endif
Index: /tags/1.03A/Server/version.c
===================================================================
--- /tags/1.03A/Server/version.c	(revision 125)
+++ /tags/1.03A/Server/version.c	(revision 125)
@@ -0,0 +1,26 @@
+/* $Id$ */
+
+#define SOURCE
+
+#include "tw_version.h"
+
+const char* tw_version = TW_VERSION;
+
+const char* tw_platform =
+#if defined(PLATFORM)
+    PLATFORM
+#elif defined(__NetBSD__)
+    "NetBSD"
+#elif defined(__linux__)
+    "Linux"
+#elif defined(__MINGW32__)
+    "Windows"
+#elif defined(__HAIKU__)
+    "Haiku"
+#else
+    "Unix"
+#endif
+    ;
+
+const char* tw_get_version(void) { return tw_version; }
+const char* tw_get_platform(void) { return tw_platform; }
Index: /tags/1.03A/config.h.tmpl
===================================================================
--- /tags/1.03A/config.h.tmpl	(revision 125)
+++ /tags/1.03A/config.h.tmpl	(revision 125)
@@ -0,0 +1,24 @@
+/* $Id$ */
+
+#ifndef __CONFIG_H__
+#define __CONFIG_H__
+
+#undef NO_SSL
+#define USE_POLL
+
+/* DO NOT EDIT BELOW THIS LINE */
+
+#ifdef NO_SSL
+#define SSL void
+#endif
+
+#if defined(__MINGW32__) && defined(USE_POLL)
+#undef USE_POLL
+/* Force select(2) for Windows */
+#endif
+
+#endif
+
+/*
+vim: syntax=c
+*/
Index: /tags/1.03A/example-win.conf
===================================================================
--- /tags/1.03A/example-win.conf	(revision 125)
+++ /tags/1.03A/example-win.conf	(revision 125)
@@ -0,0 +1,31 @@
+# $Id$
+# This is an example config
+
+ServerRoot C:/Tewi
+
+Listen 80
+#ListenSSL 443
+
+#SSLKey key.pem
+#SSLCertificate cert.pem
+
+MIMEType all application/octet-stream
+MIMEType .html text/html
+MIMEType .txt text/plain
+MIMEType .png image/png
+
+Icon all /icons/unknown.png
+Icon text/* /icons/text.png
+Icon image/* /icons/image.png
+Icon misc/dir /icons/folder.png
+Icon misc/parent /icons/parent.png
+
+DirectoryIndex index.html
+
+Readme README
+
+DocumentRoot C:/Tewi/www
+
+BeginDirectory C:/Tewi/www
+	Allow all
+EndDirectory
Index: /tags/1.03A/example.conf
===================================================================
--- /tags/1.03A/example.conf	(revision 125)
+++ /tags/1.03A/example.conf	(revision 125)
@@ -0,0 +1,35 @@
+# $Id$
+# This is an example config
+
+#LoadModule /home/nishi/SVN/tewi/trunk/Module/mod_cgi.so
+
+Listen 80
+ListenSSL 443
+
+SSLKey key.pem
+SSLCertificate cert.pem
+
+MIMEType all application/octet-stream
+MIMEType .html text/html
+MIMEType .txt text/plain
+MIMEType .png image/png
+
+Icon all /icons/unknown.png
+Icon text/* /icons/text.png
+Icon image/* /icons/image.png
+Icon misc/dir /icons/folder.png
+Icon misc/parent /icons/parent.png
+
+DirectoryIndex index.html
+
+Readme README
+
+DocumentRoot /var/www
+
+BeginDirectory /
+	Allow all
+EndDirectory
+
+BeginDirectory /var/www/cgi-bin
+#	AllowCGI
+EndDirectory
Index: /tags/1.03A/installer.sh
===================================================================
--- /tags/1.03A/installer.sh	(revision 125)
+++ /tags/1.03A/installer.sh	(revision 125)
@@ -0,0 +1,41 @@
+#!/bin/sh
+# $Id$
+
+fail() {
+	rm -f tewi-service.exe
+	rm -f tewi.exe
+	mv config.h.bak config.h
+	exit 1
+}
+
+rm -f install-nossl.exe install-ssl.exe
+
+VERSION=`make get-version`
+
+sed "s/undef NO_SSL/define NO_SSL/g" config.h.tmpl > config.h
+
+make clean || fail
+make PLATFORM=$1 -j4 || fail
+cp Server/tewi.exe tewi.exe
+make clean || fail
+make PLATFORM=$1-service -j4 || fail
+cp Server/tewi.exe tewi-service.exe
+cd Server
+makensis -DVERSION=$VERSION install.nsi
+cp install.exe ../install-nossl.exe
+rm -f tewi.exe tewi-service.exe
+cd ..
+
+sed "s/define NO_SSL/undef NO_SSL/g" config.h.tmpl > config.h
+
+make clean || fail
+make PLATFORM=$1 -j4 || fail
+cp Server/tewi.exe tewi.exe
+make clean || fail
+make PLATFORM=$1-service -j4 || fail
+cp Server/tewi.exe tewi-service.exe
+cd Server
+makensis -DVERSION=$VERSION install.nsi
+cp install.exe ../install-ssl.exe
+rm -f tewi.exe tewi-service.exe
+cd ..
Index: /tags/1.03A/itworks.html
===================================================================
--- /tags/1.03A/itworks.html	(revision 125)
+++ /tags/1.03A/itworks.html	(revision 125)
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
+<html>
+	<head>
+		<title>It Works</title>
+	</head>
+	<body>
+		<h1>It Works!</h1>
+		<hr>
+		Congratulations - If you can see this page, it means Tewi HTTPd has been configured successfully!
+	</body>
+</html>
