#include <stdlib.h>
#include <string.h>
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
#include <winsock2.h>
#else
#ifdef USE_POLL
char buffer[512];
char cbuf[2];
int phase = 0;
+ char* header;
+ int nl;
#ifdef USE_POLL
struct pollfd pollfds[1];
req->body = NULL;
req->version = NULL;
- char* header = malloc(1);
+ header = malloc(1);
header[0] = 0;
- int nl = 0;
+ nl = 0;
while(1) {
+ int i;
+ int len;
+ int n;
#ifndef USE_POLL
+ struct timeval tv;
FD_ZERO(&fds);
FD_SET(sock, &fds);
- struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
#endif
if(ssl == NULL || !SSL_has_pending(ssl)) {
#endif
#ifdef USE_POLL
- int n = poll(pollfds, 1, 5000);
+ n = poll(pollfds, 1, 5000);
#else
#ifdef __HAIKU__
- int n = select(32, &fds, NULL, NULL, &tv);
+ n = select(32, &fds, NULL, NULL, &tv);
#else
- int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
+ n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
#endif
#endif
if(n <= 0) {
#ifndef NO_SSL
}
#endif
- int len = tw_read(ssl, sock, buffer, 512);
+ 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) {
phase++;
}
} else {
+ char* tmp;
if(req->method == NULL) {
req->method = malloc(1);
req->method[0] = 0;
}
cbuf[0] = c;
- char* tmp = req->method;
+ tmp = req->method;
req->method = cm_strcat(tmp, cbuf);
free(tmp);
}
phase++;
}
} else {
+ char* tmp;
if(req->path == NULL) {
req->path = malloc(1);
req->path[0] = 0;
}
cbuf[0] = c;
- char* tmp = req->path;
+ tmp = req->path;
req->path = cm_strcat(tmp, cbuf);
free(tmp);
}
goto getout;
} else {
/* We have Method, Path, Version now */
-
+ int j;
+ char* p;
+ int incr;
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 = malloc(1);
p[0] = 0;
for(j = 0; req->path[j] != 0; j++) {
+ char* tmp;
if(req->path[j] == '/') {
cbuf[0] = '/';
for(; req->path[j] != 0 && req->path[j] == '/'; j++)
} else {
cbuf[0] = req->path[j];
}
- char* tmp = p;
+ tmp = p;
p = cm_strcat(tmp, cbuf);
free(tmp);
}
free(req->path);
req->path = p;
- int incr = 0;
+ 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];
+ char* pth;
cbuf[0] = oldc;
req->path[j] = 0;
- char* pth = req->path + incr;
+ pth = req->path + incr;
if(strcmp(pth, "..") == 0) {
int k;
phase++;
}
} else if(c != '\r') {
+ char* tmp;
if(req->version == NULL) {
req->version = malloc(1);
req->version[0] = 0;
}
cbuf[0] = c;
- char* tmp = req->version;
+ tmp = req->version;
req->version = cm_strcat(tmp, cbuf);
free(tmp);
}
phase++;
goto getout;
} else {
+ int j;
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] == ':') {
+ char* kv;
+ char* vv;
+ char** old;
+ int k;
header[j] = 0;
j++;
for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
;
- char* kv = header;
- char* vv = header + j;
+ kv = header;
+ vv = header + j;
- char** old = req->headers;
- int k;
+ old = req->headers;
for(k = 0; old[k] != NULL; k++)
;
req->headers = malloc(sizeof(*req->headers) * (k + 3));
header[0] = 0;
}
} else if(c != '\r') {
+ char* tmp;
nl = 0;
cbuf[0] = c;
- char* tmp = header;
+ tmp = header;
header = cm_strcat(tmp, cbuf);
free(tmp);
}
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;
+ {
+ char* result = malloc(1);
+ int i;
+ int j;
+ int incr;
+ char* p;
+ result[0] = 0;
+ 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;
+ 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] != '\\') {
+ char* tmp;
+ cbuf[0] = req->path[i];
+ 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;
+ free(req->path);
+ req->path = result;
+
+ 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];
+ char* pth;
+ cbuf[0] = oldc;
+ req->path[j] = 0;
+
+ 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);
}
- 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;
}
-
- incr = j + 1;
- if(oldc == 0) break;
}
+ free(req->path);
+ req->path = p;
+ return 0;
}
- free(req->path);
- req->path = p;
- return 0;
}
#include "tw_module.h"
#include "tw_version.h"
+#ifndef _MSC_VER
#include <unistd.h>
+#endif
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
-#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <time.h>
#include <cm_string.h>
#include <cm_log.h>
#include <cm_dir.h>
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
#ifndef NO_GETADDRINFO
#include <ws2tcpip.h>
#include <wspiapi.h>
#include <OS.h>
#endif
+#ifndef S_ISDIR
+#define S_ISDIR(x) ((x) & _S_IFDIR)
+#endif
+
extern struct tw_config config;
extern char tw_server[];
SOCKADDR addresses[MAX_PORTS];
int sockets[MAX_PORTS];
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
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
}
void close_socket(int sock) {
-#if defined(__MINGW32__)
+#if defined(__MINGW32__) || defined(_MSC_VER)
closesocket(sock);
#else
close(sock);
int tw_server_init(void) {
int i;
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
WSADATA wsa;
WSAStartup(MAKEWORD(2, 0), &wsa);
#endif
;
sockcount = i;
for(i = 0; config.ports[i] != -1; i++) {
+ int yes = 1;
+ int no = 0;
#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 defined(__MINGW32__) || defined(_MSC_VER)
if(sock == INVALID_SOCKET)
#else
if(sock < 0)
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)");
}
#endif
#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)");
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];
+ size_t incr;
if(mtime != 0 && cmtime != 0 && mtime <= cmtime) {
status = "304 Not Modified";
type = NULL;
f = NULL;
doc = NULL;
}
+#ifdef _MSC_VER
+ sprintf(construct, "%lu", (unsigned long)size);
+#else
sprintf(construct, "%llu", (unsigned long long)size);
+#endif
tw_write(ssl, sock, "HTTP/1.1 ", 9);
tw_write(ssl, sock, (char*)status, strlen(status));
tw_write(ssl, sock, "\r\n", 2);
tw_write(ssl, sock, "\r\n", 2);
}
}
- int i;
if(headers != NULL) {
+ int i;
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, "\r\n", 2);
if(doc == NULL && f == NULL) return;
- size_t incr = 0;
+ incr = 0;
while(1) {
if(f != NULL) {
char buffer[128];
char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) {
char address[1024];
+ char* st;
+ char* st2;
+ char* buffer;
+ char* str;
+ int i;
if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) {
sprintf(address, "<address>%s Server at %s</address>", tw_server, name, port);
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;
+ st = cm_strdup(tw_http_status(code));
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);
+ buffer = malloc(4096);
+ str = cm_strcat3(ERROR_HTML);
sprintf(buffer, str, st, st2);
free(str);
free(st);
void addstring(char** str, const char* add, ...) {
int i;
char cbuf[2];
- cbuf[1] = 0;
va_list va;
+ cbuf[1] = 0;
va_start(va, add);
for(i = 0; add[i] != 0; i++) {
cbuf[0] = add[i];
} else if(add[i] == 'd') {
int n = va_arg(va, int);
char* h = malloc(512);
- sprintf(h, "%d", n);
char* tmp = *str;
+ sprintf(h, "%d", n);
*str = cm_strcat(tmp, h);
free(tmp);
free(h);
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;
+ if(ext == NULL) return mime;
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;
char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
char* icon = "";
- if(mime == NULL) return "";
bool set = false;
int i;
+ if(mime == NULL) return "";
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;
SOCKADDR addr;
};
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
unsigned int WINAPI tw_server_pass(void* ptr) {
#elif defined(__HAIKU__)
int32_t tw_server_pass(void* ptr) {
#elif defined(_PSP) || defined(__PPU__)
int tw_server_pass(void* ptr) {
#endif
-#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__)
+#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER)
+#define FREE_PTR
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;
-
+ SSL* s = NULL;
#ifndef NO_SSL
SSL_CTX* ctx = NULL;
- SSL* s = NULL;
bool sslworks = false;
if(ssl) {
ctx = tw_create_ssl_ctx(port);
if(SSL_accept(s) <= 0) goto cleanup;
sslworks = true;
}
-#else
- void* s = NULL;
#endif
-
+ char* name = config.hostname;
char address[513];
- address[0] = 0;
+ int ret;
+ struct tw_http_request req;
+ struct tw_http_response res;
+ struct tw_tool tools;
#ifndef NO_GETADDRINFO
struct sockaddr* sa = (struct sockaddr*)&addr;
getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST);
#endif
+ address[0] = 0;
+#ifdef FREE_PTR
+ free(ptr);
+#endif
- 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);
+ 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;
+ char* tmp;
+ char* tmp2;
+ char* tmp3;
+ char* tmp4;
+ char* tmp5;
+ char* log;
+ char* vhost;
+ time_t cmtime;
+ bool rej;
+ char* host;
+ int port;
+ struct tw_config_entry* vhost_entry;
+ strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm);
+
for(i = 0; req.headers[i] != NULL; i += 2) {
if(cm_strcaseequ(req.headers[i], "User-Agent")) {
free(useragent);
}
}
- 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, "\"");
+ tmp = cm_strcat3(address, " - [", date);
+ tmp2 = cm_strcat3(tmp, "] \"", req.method);
+ tmp3 = cm_strcat3(tmp2, " ", req.path);
+ tmp4 = cm_strcat3(tmp3, " ", req.version);
+ tmp5 = cm_strcat3(tmp4, "\" \"", useragent);
+ log = cm_strcat(tmp5, "\"");
free(tmp);
free(tmp2);
free(tmp3);
cm_force_log(log);
free(log);
- char* vhost = cm_strdup(config.hostname);
- time_t cmtime = 0;
+ vhost = cm_strdup(config.hostname);
+ cmtime = 0;
if(req.headers != NULL) {
for(i = 0; req.headers[i] != NULL; i += 2) {
if(cm_strcaseequ(req.headers[i], "Host")) {
vhost = cm_strdup(req.headers[i + 1]);
} else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
struct tm tm;
+ time_t t;
+ struct tm* btm;
strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
-#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__)
- time_t t = 0;
- struct tm* btm = localtime(&t);
+#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER)
+ t = 0;
+ btm = localtime(&t);
cmtime = mktime(&tm);
cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
#else
}
}
}
- bool rej = false;
+ rej = false;
cm_log("Server", "Host is %s", vhost);
- int port = s == NULL ? 80 : 443;
- char* host = cm_strdup(vhost);
+ port = s == NULL ? 80 : 443;
+ host = cm_strdup(vhost);
for(i = 0; vhost[i] != 0; i++) {
if(vhost[i] == ':') {
host[i] = 0;
}
name = host;
cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
- struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
+ vhost_entry = tw_vhost_match(host, port);
#ifdef HAS_CHROOT
char* chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path;
if(chrootpath != NULL) {
}
}
if(!res._processed) {
+ char* path;
+ char* rpath;
+ struct stat st;
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);
+ path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
cm_log("Server", "Filesystem path is %s", path);
-#ifdef __MINGW32__
- char* rpath = cm_strdup(path);
+#if defined(__MINGW32__) || defined(_MSC_VER)
+ rpath = cm_strdup(path);
for(i = strlen(rpath) - 1; i >= 0; i--) {
if(rpath[i] == '/') {
int j;
}
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};
+ cm_log("Server", "Accessing directory without the slash at the end");
_tw_process_page(s, sock, tw_http_status(301), NULL, NULL, NULL, 0, headers, 0, 0);
free(headers[1]);
} else {
if(f != NULL) {
char* ext = NULL;
int j;
+ struct stat st;
+ char* mime;
for(j = strlen(p) - 1; j >= 0; j--) {
if(p[j] == '.') {
ext = cm_strdup(p + j);
break;
}
}
- struct stat st;
stat(p, &st);
- char* mime = tw_get_mime(ext, vhost_entry);
+ 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);
}
if(!found) {
char* str = malloc(1);
+ char** items;
+ int readme;
+ char** readmes;
+ int readme_count;
+ int hp;
str[0] = 0;
- char** items = cm_scandir(path);
+ 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, " <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;
+ readme = -1;
+ readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
+ 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* ext;
+ char* itm;
+ char* icon;
char* fpth = cm_strcat3(path, "/", items[i]);
struct stat s;
char size[512];
+ char* showmime;
+ char* mime;
size[0] = 0;
stat(fpth, &s);
if(phase == 0 && !S_ISDIR(s.st_mode)) {
continue;
}
}
- if(s.st_size < 1024ULL) {
+ if(s.st_size < NUM1024) {
sprintf(size, "%d", (int)s.st_size);
- } else if(s.st_size < 1024ULL * 1024) {
+ } else if(s.st_size < NUM1024 * 1024) {
sprintf(size, "%.1fK", (double)s.st_size / 1024);
- } else if(s.st_size < 1024ULL * 1024 * 1024) {
+ } else if(s.st_size < NUM1024 * 1024 * 1024) {
sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
- } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
+ } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024) {
sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
- } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
+ } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024 * 1024) {
sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
}
free(fpth);
- char* ext = NULL;
+ ext = NULL;
for(j = strlen(items[i]) - 1; j >= 0; j--) {
if(items[i][j] == '.') {
ext = cm_strdup(items[i] + j);
break;
}
}
- char* showmime = "";
- char* mime = tw_get_mime(ext, vhost_entry);
+ showmime = "";
+ mime = tw_get_mime(ext, vhost_entry);
if(strcmp(items[i], "../") == 0) {
mime = "misc/parent";
size[0] = 0;
} else {
showmime = mime;
}
- char* icon = tw_get_icon(mime, vhost_entry);
+ icon = tw_get_icon(mime, vhost_entry);
if(ext != NULL) free(ext);
- char* itm = cm_strdup(items[i]);
+ itm = cm_strdup(items[i]);
if(strlen(itm) >= 32) {
if(itm[strlen(itm) - 1] == '/') {
itm[31] = 0;
}
addstring(&str, " </table>\n");
if(readme != -1) {
- addstring(&str, "<hr>\n");
- char* fpth = cm_strcat3(path, "/", readmes[readme]);
struct stat s;
+ FILE* fr;
+ char* fpth;
+ addstring(&str, "<hr>\n");
+ fpth = cm_strcat3(path, "/", readmes[readme]);
stat(fpth, &s);
- FILE* fr = fopen(fpth, "r");
+ fr = fopen(fpth, "r");
if(fr != NULL) {
char* rmbuf = malloc(s.st_size + 1);
rmbuf[s.st_size] = 0;
free(fpth);
}
addstring(&str, " <hr>\n");
- int hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;
+ 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 {
}
} else {
char* ext = NULL;
+ char* mime;
+ FILE* f;
for(i = strlen(req.path) - 1; i >= 0; i--) {
if(req.path[i] == '.') {
ext = cm_strdup(req.path + i);
break;
}
}
- char* mime = tw_get_mime(ext, vhost_entry);
+ mime = tw_get_mime(ext, vhost_entry);
if(ext != NULL) free(ext);
- FILE* f = fopen(path, "rb");
+ f = fopen(path, "rb");
if(f == NULL) {
tw_http_error(s, sock, 403, name, port, vhost_entry);
} else {
SSL_free(s);
#endif
close_socket(sock);
-#ifdef __MINGW32__
- _endthreadex(0);
+#if defined(__MINGW32__) || defined(_MSC_VER)
+ _endthread(0);
#elif defined(__HAIKU__)
exit_thread(0);
#endif
extern SERVICE_STATUS_HANDLE status_handle;
#endif
-#if defined(__MINGW32__) || defined(__HAIKU__)
+#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER)
struct thread_entry {
#ifdef __HAIKU__
thread_id thread;
void tw_server_loop(void) {
int i;
-#if defined(__MINGW32__) || defined(__HAIKU__)
+#ifndef USE_POLL
+ fd_set fdset;
+ struct timeval tv;
+#endif
+#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER)
struct thread_entry threads[2048];
for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
threads[i].used = false;
pollfds[i].fd = sockets[i];
pollfds[i].events = POLLIN | POLLPRI;
}
-#else
- fd_set fdset;
- struct timeval tv;
#endif
while(running) {
+ int ret;
#ifdef USE_POLL
- int ret = poll(pollfds, sockcount, 1000);
+ ret = poll(pollfds, sockcount, 1000);
#else
FD_ZERO(&fdset);
for(i = 0; i < sockcount; i++) {
tv.tv_sec = 1;
tv.tv_usec = 0;
#ifdef __HAIKU__
- int ret = select(32, &fdset, NULL, NULL, &tv);
+ ret = select(32, &fdset, NULL, NULL, &tv);
#else
- int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
+ ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
#endif
#endif
if(ret == -1) {
-#ifndef __MINGW32__
+#if !defined(__MINGW32__) && !defined(_MSC_VER)
cm_log("Server", "Select failure: %s", strerror(errno));
#endif
break;
SOCKADDR claddr;
socklen_t clen = sizeof(claddr);
int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
- cm_log("Server", "New connection accepted");
-#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__)
+#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER)
+ int j;
struct pass_entry* e = malloc(sizeof(*e));
+ cm_log("Server", "New connection accepted");
e->sock = sock;
- e->ssl = config.ports[i] & (1ULL << 32);
+#ifdef _MSC_VER
+ e->ssl = config.ports[i] & (1UL << 31);
+#else
+ e->ssl = config.ports[i] & (1ULL << 31);
+#endif
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;
- }
- }
+#if defined(__MINGW32__) || defined(_MSC_VER)
+ _beginthread(tw_server_pass, 0, e);
#elif defined(_PSP) || defined(__PPU__)
tw_server_pass(e);
#elif defined(__HAIKU__)
- int j;
for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
if(threads[j].used) {
thread_info info;
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);
+ tw_server_pass(sock, config.ports[i] & (1ULL << 31), config.ports[i], claddr);
_exit(0);
} else {
close_socket(sock);