aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am1
-rw-r--r--configure.ac9
-rw-r--r--src/blogc-runserver/httpd.c49
-rw-r--r--src/common/error.c68
-rw-r--r--src/common/error.h8
-rw-r--r--src/common/file.c24
-rw-r--r--src/common/file.h1
7 files changed, 151 insertions, 9 deletions
diff --git a/Makefile.am b/Makefile.am
index f988199..98c36e4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -299,6 +299,7 @@ blogc_runserver_LDADD = \
libblogc_runserver.la \
libblogc_common.la \
libblogc_common_thread.la \
+ -lws2_32 \
$(NULL)
libblogc_runserver_la_SOURCES = \
diff --git a/configure.ac b/configure.ac
index 3443956..4626ef3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,7 +134,12 @@ RUNSERVER="disabled"
AC_ARG_ENABLE([runserver], AS_HELP_STRING([--enable-runserver],
[build blogc-runserver tool]))
AS_IF([test "x$enable_runserver" = "xyes"], [
- AC_CHECK_HEADERS([sys/socket.h netdb.h netinet/in.h arpa/inet.h winsock2.h])
+ AC_CHECK_HEADERS([sys/socket.h netdb.h netinet/in.h arpa/inet.h winsock2.h ws2tcpip.h])
+#, [], [], [[
+#ifdef HAVE_WINSOCK2_H
+#include <winsock2.h>
+#endif
+#]])
AC_CHECK_HEADERS([signal.h limits.h fcntl.h unistd.h sys/stat.h sys/types.h],, [
AC_MSG_ERROR([blogc-runserver tool requested but required headers not found])
])
@@ -225,7 +230,7 @@ AM_CONDITIONAL([USE_BGR_DEPS], [test "x$have_bgr_deps" = "xyes"])
BASH="$ac_cv_path_bash"
AC_SUBST(BASH)
-AC_CHECK_HEADERS([sys/stat.h sys/wait.h time.h])
+AC_CHECK_HEADERS([sys/stat.h sys/wait.h time.h windows.h])
LT_LIB_M
diff --git a/src/blogc-runserver/httpd.c b/src/blogc-runserver/httpd.c
index 57bcec4..9249b94 100644
--- a/src/blogc-runserver/httpd.c
+++ b/src/blogc-runserver/httpd.c
@@ -10,6 +10,11 @@
#include <config.h>
#endif /* HAVE_CONFIG_H */
+#if defined(WIN32) || defined(_WIN32)
+#define WINVER 0x0600
+#define _WIN32_WINNT 0x0600
+#endif /* WIN32 || _WIN32 */
+
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif /* HAVE_SYS_SOCKET_H */
@@ -34,8 +39,13 @@
#include <winsock2.h>
#endif /* HAVE_WINSOCK2_H */
+#ifdef HAVE_WS2TCPIP_H
+#include <ws2tcpip.h>
+#endif
+
#include <errno.h>
#include <stdio.h>
+#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@@ -49,6 +59,14 @@
#include "mime.h"
#include "httpd-utils.h"
+#if defined(WIN32) || defined(_WIN32)
+#include <stdint.h>
+typedef uint8_t u_int8_t;
+typedef uint16_t u_int16_t;
+typedef uint32_t u_int32_t;
+typedef unsigned short in_port_t;
+#endif /* WIN32 || _WIN32 */
+
#define LISTEN_BACKLOG 100
typedef struct {
@@ -65,6 +83,22 @@ typedef struct {
static void
+error_printf(const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+#if defined(WIN32) || defined(_WIN32)
+ bc_error_t *rv = bc_error_new_errno_vprintf(0, WSAGetLastError(), format, ap);
+#else
+ bc_error_t *rv = bc_error_new_errno_vprintf(0, errno, format, ap);
+#endif
+ va_end(ap);
+ fprintf(stderr, "%s\n", rv->msg);
+ bc_error_free(rv);
+}
+
+
+static void
error(int socket, int status_code, const char *error)
{
char *str = bc_strdup_printf(
@@ -122,7 +156,7 @@ handle_request(void *arg)
}
char *abs_path = bc_strdup_printf("%s/%s", docroot, path);
- char *real_path = realpath(abs_path, NULL);
+ char *real_path = bc_file_get_realpath(abs_path);
free(abs_path);
if (real_path == NULL) {
@@ -137,7 +171,7 @@ handle_request(void *arg)
goto point2;
}
- char *real_root = realpath(docroot, NULL);
+ char *real_root = bc_file_get_realpath(docroot);
if (real_root == NULL) {
status_code = 500;
error(client_socket, 500, "Internal Server Error");
@@ -251,7 +285,11 @@ br_httpd_get_ip(int af, const struct sockaddr *addr)
char host[INET6_ADDRSTRLEN];
if (af == AF_INET6) {
struct sockaddr_in6 *a = (struct sockaddr_in6*) addr;
+//#if defined(WIN32) || defined(_WIN32)
+// InetNtopA(af, &(a->sin6_addr), host, INET6_ADDRSTRLEN);
+//#else
inet_ntop(af, &(a->sin6_addr), host, INET6_ADDRSTRLEN);
+//#endif
}
else {
struct sockaddr_in *a = (struct sockaddr_in*) addr;
@@ -316,11 +354,12 @@ br_httpd_run(const char *host, const char *port, const char *docroot,
for (rp = result; rp != NULL; rp = rp->ai_next) {
final_host = br_httpd_get_ip(rp->ai_family, rp->ai_addr);
final_port = br_httpd_get_port(rp->ai_family, rp->ai_addr);
- server_socket = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+ server_socket = socket(rp->ai_family, rp->ai_socktype, IPPROTO_TCP);
+ printf("%d\n", rp->ai_protocol);
if (server_socket == -1) {
if (rp->ai_next == NULL) {
- fprintf(stderr, "Failed to open server socket (%s:%d): %s\n",
- final_host, final_port, strerror(errno));
+ error_printf("Failed to open server socket (%s:%d)", final_host,
+ final_port);
rv = 3;
goto cleanup0;
}
diff --git a/src/common/error.c b/src/common/error.c
index a3a08e7..ef6edff 100644
--- a/src/common/error.c
+++ b/src/common/error.c
@@ -6,9 +6,19 @@
* See the file LICENSE.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif /* HAVE_WINDOWS_H */
+
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <string.h>
#include "error.h"
#include "utils.h"
@@ -24,19 +34,73 @@ bc_error_new(bc_error_type_t type, const char *msg)
bc_error_t*
+bc_error_new_vprintf(bc_error_type_t type, const char *format, va_list ap)
+{
+ char *tmp = bc_strdup_vprintf(format, ap);
+ bc_error_t *rv = bc_error_new(type, tmp);
+ free(tmp);
+ return rv;
+}
+
+
+bc_error_t*
bc_error_new_printf(bc_error_type_t type, const char *format, ...)
{
va_list ap;
va_start(ap, format);
- char *tmp = bc_strdup_vprintf(format, ap);
+ bc_error_t *rv = bc_error_new_vprintf(type, format, ap);
va_end(ap);
- bc_error_t *rv = bc_error_new(type, tmp);
+ return rv;
+}
+
+
+bc_error_t*
+bc_error_new_errno_vprintf(bc_error_type_t type, int errno_, const char *format,
+ va_list ap)
+{
+ char *tmp = bc_strdup_vprintf(format, ap);
+#if defined(WIN32) || defined(_WIN32)
+ LPTSTR buf = "bola";
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, errno_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf,
+ 0, NULL);
+ bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, buf);
+ LocalFree(buf);
+#else
+ bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, strerror(errno_));
+#endif
free(tmp);
return rv;
}
bc_error_t*
+bc_error_new_errno_printf(bc_error_type_t type, int errno_, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ bc_error_t *rv = bc_error_new_errno_vprintf(type, errno_, format, ap);
+ va_end(ap);
+ return rv;
+}
+
+
+bc_error_t*
+bc_error_new_default_errno_printf(bc_error_type_t type, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+#if defined(WIN32) || defined(_WIN32)
+ bc_error_t *rv = bc_error_new_errno_vprintf(type, GetLastError(), format, ap);
+#else
+ bc_error_t *rv = bc_error_new_errno_vprintf(type, errno, format, ap);
+#endif
+ va_end(ap);
+ return rv;
+}
+
+
+bc_error_t*
bc_error_parser(bc_error_type_t type, const char *src, size_t src_len,
size_t current, const char *format, ...)
{
diff --git a/src/common/error.h b/src/common/error.h
index 2aac439..ccb4228 100644
--- a/src/common/error.h
+++ b/src/common/error.h
@@ -9,6 +9,7 @@
#ifndef _ERROR_H
#define _ERROR_H
+#include <stdarg.h>
#include <stddef.h>
// error handling is centralized here for the sake of simplicity :/
@@ -38,7 +39,14 @@ typedef struct {
} bc_error_t;
bc_error_t* bc_error_new(bc_error_type_t type, const char *msg);
+bc_error_t* bc_error_new_vprintf(bc_error_type_t type, const char *format, va_list ap);
bc_error_t* bc_error_new_printf(bc_error_type_t type, const char *format, ...);
+bc_error_t* bc_error_new_errno_vprintf(bc_error_type_t type, int errno_,
+ const char *format, va_list ap);
+bc_error_t* bc_error_new_errno_printf(bc_error_type_t type, int errno_,
+ const char *format, ...);
+bc_error_t* bc_error_new_default_errno_printf(bc_error_type_t type,
+ const char *format, ...);
bc_error_t* bc_error_parser(bc_error_type_t type, const char *src,
size_t src_len, size_t current, const char *format, ...);
void bc_error_print(bc_error_t *err, const char *prefix);
diff --git a/src/common/file.c b/src/common/file.c
index a2f7340..b44b202 100644
--- a/src/common/file.c
+++ b/src/common/file.c
@@ -7,9 +7,11 @@
*/
#include <errno.h>
+#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "file.h"
#include "error.h"
@@ -64,3 +66,25 @@ bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err)
return bc_string_free(str, false);
}
+
+
+char*
+bc_file_get_realpath(const char *path)
+{
+ if (path == NULL)
+ return NULL;
+
+#if defined(WIN32) || defined(_WIN32)
+ char *buf = bc_malloc(_MAX_PATH);
+ if (NULL == _fullpath(buf, path, _MAX_PATH)) {
+#else
+ char *buf = bc_malloc(PATH_MAX);
+ if (NULL == realpath(path, buf)) {
+#endif
+
+ free(buf);
+ return NULL;
+ }
+
+ return buf;
+}
diff --git a/src/common/file.h b/src/common/file.h
index 73793ea..73590c6 100644
--- a/src/common/file.h
+++ b/src/common/file.h
@@ -16,5 +16,6 @@
#define BC_FILE_CHUNK_SIZE 1024
char* bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err);
+char* bc_file_get_realpath(const char *path);
#endif /* _FILE_H */