From 6ac53d4c783340ae9139c7f4dcfe9bfddace5892 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 25 Sep 2016 02:57:21 +0200 Subject: runserver: reimplemented http server without libevent yeah, this patch implements a "complete" http server for static files. It is not the best code possible, and would be easily DDoS'able if used in production, as it spawns a thread for each request, without limiting. I'm sickish and this is the best code I can deliver now. At least it works! ;) --- .gitignore | 3 + Makefile.am | 72 ++++- README.md | 2 +- configure.ac | 6 +- m4/ax_pthread.m4 | 485 ++++++++++++++++++++++++++++++ src/blogc-runserver/httpd-utils.c | 100 ++++++ src/blogc-runserver/httpd-utils.h | 19 ++ src/blogc-runserver/httpd.c | 254 ++++++++++++++++ src/blogc-runserver/httpd.h | 14 + src/blogc-runserver/main.c | 316 +------------------ src/blogc-runserver/mime.c | 168 +++++++++++ src/blogc-runserver/mime.h | 15 + src/blogc/loader.c | 4 +- src/common/file.c | 6 +- src/common/file.h | 3 +- tests/blogc-runserver/check_httpd_utils.c | 163 ++++++++++ tests/blogc-runserver/check_mime.c | 92 ++++++ tests/blogc/check_loader.c | 3 +- 18 files changed, 1399 insertions(+), 326 deletions(-) create mode 100644 m4/ax_pthread.m4 create mode 100644 src/blogc-runserver/httpd-utils.c create mode 100644 src/blogc-runserver/httpd-utils.h create mode 100644 src/blogc-runserver/httpd.c create mode 100644 src/blogc-runserver/httpd.h create mode 100644 src/blogc-runserver/mime.c create mode 100644 src/blogc-runserver/mime.h create mode 100644 tests/blogc-runserver/check_httpd_utils.c create mode 100644 tests/blogc-runserver/check_mime.c diff --git a/.gitignore b/.gitignore index 60fd880..a222ebe 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ Makefile.in # installed .m4 files /m4/*.m4 +!/m4/ax_pthread.m4 !/m4/pkg.m4 # man pages @@ -56,6 +57,8 @@ blogc*.html /tests/blogc/check_renderer /tests/blogc/check_source_parser /tests/blogc/check_template_parser +/tests/blogc-runserver/check_httpd_utils +/tests/blogc-runserver/check_mime /tests/common/check_config_parser /tests/common/check_error /tests/common/check_utf8 diff --git a/Makefile.am b/Makefile.am index 7ecb21a..12cfad8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,6 +39,9 @@ noinst_HEADERS = \ src/blogc/renderer.h \ src/blogc/source-parser.h \ src/blogc/template-parser.h \ + src/blogc-runserver/httpd.h \ + src/blogc-runserver/httpd-utils.h \ + src/blogc-runserver/mime.h \ src/common/config-parser.h \ src/common/error.h \ src/common/file.h \ @@ -68,6 +71,10 @@ if BUILD_RUNSERVER bin_PROGRAMS += \ blogc-runserver \ $(NULL) + +noinst_LTLIBRARIES += \ + libblogc_runserver.la \ + $(NULL) endif check_PROGRAMS = \ @@ -142,13 +149,29 @@ blogc_runserver_SOURCES = \ blogc_runserver_CFLAGS = \ $(AM_CFLAGS) \ - $(LIBEVENT_CFLAGS) \ + $(PTHREAD_CFLAGS) \ $(NULL) blogc_runserver_LDADD = \ - $(LIBEVENT_LIBS) \ + $(PTHREAD_LIBS) \ + libblogc_runserver.la \ libblogc_common.la \ $(NULL) + +libblogc_runserver_la_SOURCES = \ + src/blogc-runserver/httpd.c \ + src/blogc-runserver/httpd-utils.c \ + src/blogc-runserver/mime.c \ + $(NULL) + +libblogc_runserver_la_CFLAGS = \ + $(AM_CFLAGS) \ + $(PTHREAD_CFLAGS) \ + $(NULL) + +libblogc_runserver_la_LIBADD = \ + $(PTHREAD_LIBS) \ + $(NULL) endif @@ -456,6 +479,51 @@ tests_common_check_utils_LDADD = \ libblogc_common.la \ $(NULL) +if BUILD_RUNSERVER +check_PROGRAMS += \ + tests/blogc-runserver/check_httpd_utils \ + tests/blogc-runserver/check_mime \ + $(NULL) + +tests_blogc_runserver_check_httpd_utils_SOURCES = \ + tests/blogc-runserver/check_httpd_utils.c \ + $(NULL) + +tests_blogc_runserver_check_httpd_utils_CFLAGS = \ + $(CMOCKA_CFLAGS) \ + $(NULL) + +tests_blogc_runserver_check_httpd_utils_LDFLAGS = \ + -no-install \ + -Wl,--wrap=read \ + $(NULL) + +tests_blogc_runserver_check_httpd_utils_LDADD = \ + $(CMOCKA_LIBS) \ + libblogc_runserver.la \ + libblogc_common.la \ + $(NULL) + +tests_blogc_runserver_check_mime_SOURCES = \ + tests/blogc-runserver/check_mime.c \ + $(NULL) + +tests_blogc_runserver_check_mime_CFLAGS = \ + $(CMOCKA_CFLAGS) \ + $(NULL) + +tests_blogc_runserver_check_mime_LDFLAGS = \ + -no-install \ + -Wl,--wrap=access \ + $(NULL) + +tests_blogc_runserver_check_mime_LDADD = \ + $(CMOCKA_LIBS) \ + libblogc_runserver.la \ + libblogc_common.la \ + $(NULL) +endif + endif TESTS = \ diff --git a/README.md b/README.md index 609468b..eeccb4d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Inside the source directory, run the following commands: $ make # make install -The `./configure` options listed above will enable building of helper tools. To learn more about these tools, please read the man pages. Be aware that [`blogc-runserver(1)`](https://blogc.rgm.io/man/blogc-runserver.1.html) depends on [libevent](http://libevent.org/), but this tool can be replaced by any HTTP server you want to use. +The `./configure` options listed above will enable building of helper tools. To learn more about these tools, please read the man pages. To create your first blog, please clone our example repository and adapt it to your needs: diff --git a/configure.ac b/configure.ac index ce27e54..69a337b 100644 --- a/configure.ac +++ b/configure.ac @@ -136,10 +136,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([signal.h stdarg.h fcntl.h unistd.h sys/stat.h sys/types.h],, [ + AC_CHECK_HEADERS([signal.h limits.h fcntl.h unistd.h sys/stat.h sys/types.h sys/socket.h netinet/in.h arpa/inet.h],, [ AC_MSG_ERROR([blogc-runserver tool requested but required headers not found]) ]) - PKG_CHECK_MODULES([LIBEVENT], [libevent >= 2.0]) + AX_PTHREAD([], [ + AC_MSG_ERROR([blogc-runserver tool requested but pthread is not supported]) + ]) RUNSERVER="enabled" have_runserver=yes ]) diff --git a/m4/ax_pthread.m4 b/m4/ax_pthread.m4 new file mode 100644 index 0000000..4c4051e --- /dev/null +++ b/m4/ax_pthread.m4 @@ -0,0 +1,485 @@ +# =========================================================================== +# http://www.gnu.org/software/autoconf-archive/ax_pthread.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) +# +# DESCRIPTION +# +# This macro figures out how to build C programs using POSIX threads. It +# sets the PTHREAD_LIBS output variable to the threads library and linker +# flags, and the PTHREAD_CFLAGS output variable to any special C compiler +# flags that are needed. (The user can also force certain compiler +# flags/libs to be tested by setting these environment variables.) +# +# Also sets PTHREAD_CC to any special C compiler that is needed for +# multi-threaded programs (defaults to the value of CC otherwise). (This +# is necessary on AIX to use the special cc_r compiler alias.) +# +# NOTE: You are assumed to not only compile your program with these flags, +# but also to link with them as well. For example, you might link with +# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS +# +# If you are only building threaded programs, you may wish to use these +# variables in your default LIBS, CFLAGS, and CC: +# +# LIBS="$PTHREAD_LIBS $LIBS" +# CFLAGS="$CFLAGS $PTHREAD_CFLAGS" +# CC="$PTHREAD_CC" +# +# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant +# has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to +# that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX). +# +# Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the +# PTHREAD_PRIO_INHERIT symbol is defined when compiling with +# PTHREAD_CFLAGS. +# +# ACTION-IF-FOUND is a list of shell commands to run if a threads library +# is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it +# is not found. If ACTION-IF-FOUND is not specified, the default action +# will define HAVE_PTHREAD. +# +# Please let the authors know if this macro fails on any platform, or if +# you have any other suggestions or comments. This macro was based on work +# by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help +# from M. Frigo), as well as ac_pthread and hb_pthread macros posted by +# Alejandro Forero Cuervo to the autoconf macro repository. We are also +# grateful for the helpful feedback of numerous users. +# +# Updated for Autoconf 2.68 by Daniel Richard G. +# +# LICENSE +# +# Copyright (c) 2008 Steven G. Johnson +# Copyright (c) 2011 Daniel Richard G. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation, either version 3 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 23 + +AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) +AC_DEFUN([AX_PTHREAD], [ +AC_REQUIRE([AC_CANONICAL_HOST]) +AC_REQUIRE([AC_PROG_CC]) +AC_REQUIRE([AC_PROG_SED]) +AC_LANG_PUSH([C]) +ax_pthread_ok=no + +# We used to check for pthread.h first, but this fails if pthread.h +# requires special compiler flags (e.g. on Tru64 or Sequent). +# It gets checked for in the link test anyway. + +# First of all, check if the user has set any of the PTHREAD_LIBS, +# etcetera environment variables, and if threads linking works using +# them: +if test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then + ax_pthread_save_CC="$CC" + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"]) + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS]) + AC_LINK_IFELSE([AC_LANG_CALL([], [pthread_join])], [ax_pthread_ok=yes]) + AC_MSG_RESULT([$ax_pthread_ok]) + if test "x$ax_pthread_ok" = "xno"; then + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" + fi + CC="$ax_pthread_save_CC" + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" +fi + +# We must check for the threads library under a number of different +# names; the ordering is very important because some systems +# (e.g. DEC) have both -lpthread and -lpthreads, where one of the +# libraries is broken (non-POSIX). + +# Create a list of thread flags to try. Items starting with a "-" are +# C compiler flags, and other items are library names, except for "none" +# which indicates that we try without any flags at all, and "pthread-config" +# which is a program returning the flags for the Pth emulation library. + +ax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" + +# The ordering *is* (sometimes) important. Some notes on the +# individual items follow: + +# pthreads: AIX (must check this before -lpthread) +# none: in case threads are in libc; should be tried before -Kthread and +# other compiler flags to prevent continual compiler warnings +# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) +# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads), Tru64 +# (Note: HP C rejects this with "bad form for `-t' option") +# -pthreads: Solaris/gcc (Note: HP C also rejects) +# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it +# doesn't hurt to check since this sometimes defines pthreads and +# -D_REENTRANT too), HP C (must be checked before -lpthread, which +# is present but should not be used directly; and before -mthreads, +# because the compiler interprets this as "-mt" + "-hreads") +# -mthreads: Mingw32/gcc, Lynx/gcc +# pthread: Linux, etcetera +# --thread-safe: KAI C++ +# pthread-config: use pthread-config program (for GNU Pth library) + +case $host_os in + + freebsd*) + + # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) + # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) + + ax_pthread_flags="-kthread lthread $ax_pthread_flags" + ;; + + hpux*) + + # From the cc(1) man page: "[-mt] Sets various -D flags to enable + # multi-threading and also sets -lpthread." + + ax_pthread_flags="-mt -pthread pthread $ax_pthread_flags" + ;; + + openedition*) + + # IBM z/OS requires a feature-test macro to be defined in order to + # enable POSIX threads at all, so give the user a hint if this is + # not set. (We don't define these ourselves, as they can affect + # other portions of the system API in unpredictable ways.) + + AC_EGREP_CPP([AX_PTHREAD_ZOS_MISSING], + [ +# if !defined(_OPEN_THREADS) && !defined(_UNIX03_THREADS) + AX_PTHREAD_ZOS_MISSING +# endif + ], + [AC_MSG_WARN([IBM z/OS requires -D_OPEN_THREADS or -D_UNIX03_THREADS to enable pthreads support.])]) + ;; + + solaris*) + + # On Solaris (at least, for some versions), libc contains stubbed + # (non-functional) versions of the pthreads routines, so link-based + # tests will erroneously succeed. (N.B.: The stubs are missing + # pthread_cleanup_push, or rather a function called by this macro, + # so we could check for that, but who knows whether they'll stub + # that too in a future libc.) So we'll check first for the + # standard Solaris way of linking pthreads (-mt -lpthread). + + ax_pthread_flags="-mt,pthread pthread $ax_pthread_flags" + ;; +esac + +# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC) + +AS_IF([test "x$GCC" = "xyes"], + [ax_pthread_flags="-pthread -pthreads $ax_pthread_flags"]) + +# The presence of a feature test macro requesting re-entrant function +# definitions is, on some systems, a strong hint that pthreads support is +# correctly enabled + +case $host_os in + darwin* | hpux* | linux* | osf* | solaris*) + ax_pthread_check_macro="_REENTRANT" + ;; + + aix*) + ax_pthread_check_macro="_THREAD_SAFE" + ;; + + *) + ax_pthread_check_macro="--" + ;; +esac +AS_IF([test "x$ax_pthread_check_macro" = "x--"], + [ax_pthread_check_cond=0], + [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"]) + +# Are we compiling with Clang? + +AC_CACHE_CHECK([whether $CC is Clang], + [ax_cv_PTHREAD_CLANG], + [ax_cv_PTHREAD_CLANG=no + # Note that Autoconf sets GCC=yes for Clang as well as GCC + if test "x$GCC" = "xyes"; then + AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG], + [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */ +# if defined(__clang__) && defined(__llvm__) + AX_PTHREAD_CC_IS_CLANG +# endif + ], + [ax_cv_PTHREAD_CLANG=yes]) + fi + ]) +ax_pthread_clang="$ax_cv_PTHREAD_CLANG" + +ax_pthread_clang_warning=no + +# Clang needs special handling, because older versions handle the -pthread +# option in a rather... idiosyncratic way + +if test "x$ax_pthread_clang" = "xyes"; then + + # Clang takes -pthread; it has never supported any other flag + + # (Note 1: This will need to be revisited if a system that Clang + # supports has POSIX threads in a separate library. This tends not + # to be the way of modern systems, but it's conceivable.) + + # (Note 2: On some systems, notably Darwin, -pthread is not needed + # to get POSIX threads support; the API is always present and + # active. We could reasonably leave PTHREAD_CFLAGS empty. But + # -pthread does define _REENTRANT, and while the Darwin headers + # ignore this macro, third-party headers might not.) + + PTHREAD_CFLAGS="-pthread" + PTHREAD_LIBS= + + ax_pthread_ok=yes + + # However, older versions of Clang make a point of warning the user + # that, in an invocation where only linking and no compilation is + # taking place, the -pthread option has no effect ("argument unused + # during compilation"). They expect -pthread to be passed in only + # when source code is being compiled. + # + # Problem is, this is at odds with the way Automake and most other + # C build frameworks function, which is that the same flags used in + # compilation (CFLAGS) are also used in linking. Many systems + # supported by AX_PTHREAD require exactly this for POSIX threads + # support, and in fact it is often not straightforward to specify a + # flag that is used only in the compilation phase and not in + # linking. Such a scenario is extremely rare in practice. + # + # Even though use of the -pthread flag in linking would only print + # a warning, this can be a nuisance for well-run software projects + # that build with -Werror. So if the active version of Clang has + # this misfeature, we search for an option to squash it. + + AC_CACHE_CHECK([whether Clang needs flag to prevent "argument unused" warning when linking with -pthread], + [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG], + [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=unknown + # Create an alternate version of $ac_link that compiles and + # links in two steps (.c -> .o, .o -> exe) instead of one + # (.c -> exe), because the warning occurs only in the second + # step + ax_pthread_save_ac_link="$ac_link" + ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g' + ax_pthread_link_step=`$as_echo "$ac_link" | sed "$ax_pthread_sed"` + ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)" + ax_pthread_save_CFLAGS="$CFLAGS" + for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do + AS_IF([test "x$ax_pthread_try" = "xunknown"], [break]) + CFLAGS="-Werror -Wunknown-warning-option $ax_pthread_try -pthread $ax_pthread_save_CFLAGS" + ac_link="$ax_pthread_save_ac_link" + AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])], + [ac_link="$ax_pthread_2step_ac_link" + AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])], + [break]) + ]) + done + ac_link="$ax_pthread_save_ac_link" + CFLAGS="$ax_pthread_save_CFLAGS" + AS_IF([test "x$ax_pthread_try" = "x"], [ax_pthread_try=no]) + ax_cv_PTHREAD_CLANG_NO_WARN_FLAG="$ax_pthread_try" + ]) + + case "$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG" in + no | unknown) ;; + *) PTHREAD_CFLAGS="$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG $PTHREAD_CFLAGS" ;; + esac + +fi # $ax_pthread_clang = yes + +if test "x$ax_pthread_ok" = "xno"; then +for ax_pthread_try_flag in $ax_pthread_flags; do + + case $ax_pthread_try_flag in + none) + AC_MSG_CHECKING([whether pthreads work without any flags]) + ;; + + -mt,pthread) + AC_MSG_CHECKING([whether pthreads work with -mt -lpthread]) + PTHREAD_CFLAGS="-mt" + PTHREAD_LIBS="-lpthread" + ;; + + -*) + AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag]) + PTHREAD_CFLAGS="$ax_pthread_try_flag" + ;; + + pthread-config) + AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no]) + AS_IF([test "x$ax_pthread_config" = "xno"], [continue]) + PTHREAD_CFLAGS="`pthread-config --cflags`" + PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" + ;; + + *) + AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag]) + PTHREAD_LIBS="-l$ax_pthread_try_flag" + ;; + esac + + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + + # Check for various functions. We must include pthread.h, + # since some functions may be macros. (On the Sequent, we + # need a special flag -Kthread to make this header compile.) + # We check for pthread_join because it is in -lpthread on IRIX + # while pthread_create is in libc. We check for pthread_attr_init + # due to DEC craziness with -lpthreads. We check for + # pthread_cleanup_push because it is one of the few pthread + # functions on Solaris that doesn't have a non-functional libc stub. + # We try pthread_create on general principles. + + AC_LINK_IFELSE([AC_LANG_PROGRAM([#include +# if $ax_pthread_check_cond +# error "$ax_pthread_check_macro must be defined" +# endif + static void routine(void *a) { a = 0; } + static void *start_routine(void *a) { return a; }], + [pthread_t th; pthread_attr_t attr; + pthread_create(&th, 0, start_routine, 0); + pthread_join(th, 0); + pthread_attr_init(&attr); + pthread_cleanup_push(routine, 0); + pthread_cleanup_pop(0) /* ; */])], + [ax_pthread_ok=yes], + []) + + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" + + AC_MSG_RESULT([$ax_pthread_ok]) + AS_IF([test "x$ax_pthread_ok" = "xyes"], [break]) + + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" +done +fi + +# Various other checks: +if test "x$ax_pthread_ok" = "xyes"; then + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + + # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. + AC_CACHE_CHECK([for joinable pthread attribute], + [ax_cv_PTHREAD_JOINABLE_ATTR], + [ax_cv_PTHREAD_JOINABLE_ATTR=unknown + for ax_pthread_attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do + AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ], + [int attr = $ax_pthread_attr; return attr /* ; */])], + [ax_cv_PTHREAD_JOINABLE_ATTR=$ax_pthread_attr; break], + []) + done + ]) + AS_IF([test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xunknown" && \ + test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xPTHREAD_CREATE_JOINABLE" && \ + test "x$ax_pthread_joinable_attr_defined" != "xyes"], + [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE], + [$ax_cv_PTHREAD_JOINABLE_ATTR], + [Define to necessary symbol if this constant + uses a non-standard name on your system.]) + ax_pthread_joinable_attr_defined=yes + ]) + + AC_CACHE_CHECK([whether more special flags are required for pthreads], + [ax_cv_PTHREAD_SPECIAL_FLAGS], + [ax_cv_PTHREAD_SPECIAL_FLAGS=no + case $host_os in + solaris*) + ax_cv_PTHREAD_SPECIAL_FLAGS="-D_POSIX_PTHREAD_SEMANTICS" + ;; + esac + ]) + AS_IF([test "x$ax_cv_PTHREAD_SPECIAL_FLAGS" != "xno" && \ + test "x$ax_pthread_special_flags_added" != "xyes"], + [PTHREAD_CFLAGS="$ax_cv_PTHREAD_SPECIAL_FLAGS $PTHREAD_CFLAGS" + ax_pthread_special_flags_added=yes]) + + AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT], + [ax_cv_PTHREAD_PRIO_INHERIT], + [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[int i = PTHREAD_PRIO_INHERIT;]])], + [ax_cv_PTHREAD_PRIO_INHERIT=yes], + [ax_cv_PTHREAD_PRIO_INHERIT=no]) + ]) + AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes" && \ + test "x$ax_pthread_prio_inherit_defined" != "xyes"], + [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.]) + ax_pthread_prio_inherit_defined=yes + ]) + + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" + + # More AIX lossage: compile with *_r variant + if test "x$GCC" != "xyes"; then + case $host_os in + aix*) + AS_CASE(["x/$CC"], + [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6], + [#handle absolute path differently from PATH based program lookup + AS_CASE(["x$CC"], + [x/*], + [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])], + [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])]) + ;; + esac + fi +fi + +test -n "$PTHREAD_CC" || PTHREAD_CC="$CC" + +AC_SUBST([PTHREAD_LIBS]) +AC_SUBST([PTHREAD_CFLAGS]) +AC_SUBST([PTHREAD_CC]) + +# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: +if test "x$ax_pthread_ok" = "xyes"; then + ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1]) + : +else + ax_pthread_ok=no + $2 +fi +AC_LANG_POP +])dnl AX_PTHREAD diff --git a/src/blogc-runserver/httpd-utils.c b/src/blogc-runserver/httpd-utils.c new file mode 100644 index 0000000..e935668 --- /dev/null +++ b/src/blogc-runserver/httpd-utils.c @@ -0,0 +1,100 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include +#include "../common/utils.h" +#include "httpd-utils.h" + + +char* +br_readline(int socket) +{ + bc_string_t *rv = bc_string_new(); + char buffer[READLINE_BUFFER_SIZE]; + ssize_t len; + + while ((len = read(socket, buffer, READLINE_BUFFER_SIZE)) != -1) { + for (ssize_t i = 0; i < len; i++) { + if (buffer[i] == '\r' || buffer[i] == '\n') + goto end; + bc_string_append_c(rv, buffer[i]); + } + } + +end: + return bc_string_free(rv, false); +} + + +int +br_hextoi(const char c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return -1; +} + + +char* +br_urldecode(const char *str) +{ + bc_string_t *rv = bc_string_new(); + + for (size_t i = 0; i < strlen(str); i++) { + switch (str[i]) { + case '%': + if (i + 2 < strlen(str)) { + int p1 = br_hextoi(str[i + 1]) * 16; + int p2 = br_hextoi(str[i + 2]); + if (p1 >= 0 && p2 >= 0) { + bc_string_append_c(rv, p1 + p2); + i += 2; + continue; + } + } + bc_string_append_c(rv, '%'); + break; + case '+': + bc_string_append_c(rv, ' '); + break; + default: + bc_string_append_c(rv, str[i]); + } + } + + return bc_string_free(rv, false); +} + + +const char* +br_get_extension(const char *filename) +{ + const char *ext = NULL; + unsigned int i; + for (i = strlen(filename); i > 0; i--) { + if (filename[i] == '.') { + ext = filename + i + 1; + break; + } + if ((filename[i] == '/') || (filename[i] == '\\')) + return NULL; + } + if (i == 0) + return NULL; + return ext; +} diff --git a/src/blogc-runserver/httpd-utils.h b/src/blogc-runserver/httpd-utils.h new file mode 100644 index 0000000..b9af852 --- /dev/null +++ b/src/blogc-runserver/httpd-utils.h @@ -0,0 +1,19 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _HTTPD_UTILS_H +#define _HTTPD_UTILS_H + +#define READLINE_BUFFER_SIZE 1024 + +char* br_readline(int socket); +int br_hextoi(const char c); +char* br_urldecode(const char *str); +const char* br_get_extension(const char *filename); + +#endif /* _HTTPD_UTILS_H */ diff --git a/src/blogc-runserver/httpd.c b/src/blogc-runserver/httpd.c new file mode 100644 index 0000000..c659580 --- /dev/null +++ b/src/blogc-runserver/httpd.c @@ -0,0 +1,254 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../common/error.h" +#include "../common/file.h" +#include "../common/utils.h" +#include "mime.h" +#include "httpd-utils.h" + +#define LISTEN_BACKLOG 100 + + +static void +error(int socket, int status_code, const char *error) +{ + char *str = bc_strdup_printf( + "HTTP/1.0 %d %s\r\n" + "Content-Type: text/html\r\n" + "Connection: close\r\n" + "\r\n" + "

%s

\n", status_code, error, error); + write(socket, str, strlen(str)); + free(str); +} + + +typedef struct { + int socket; + const char *docroot; +} request_data_t; + + +static void* +handle_request(void *arg) +{ + request_data_t *req = arg; + int client_socket = req->socket; + const char *docroot = req->docroot; + free(arg); + + char *conn_line = br_readline(client_socket); + char **pieces = bc_str_split(conn_line, ' ', 3); + if (bc_strv_length(pieces) != 3) { + error(client_socket, 400, "Bad Request"); + goto point1; + } + + if (strcmp(pieces[0], "GET") != 0) { + error(client_socket, 405, "Method Not Allowed"); + goto point1; + } + + char **pieces2 = bc_str_split(pieces[1], '?', 2); + char *path = br_urldecode(pieces2[0]); + bc_strv_free(pieces2); + + if (path == NULL) { + error(client_socket, 400, "Bad Request"); + goto point1; + } + + char *abs_path = bc_strdup_printf("%s/%s", docroot, path); + char *real_path = realpath(abs_path, NULL); + free(abs_path); + + if (real_path == NULL) { + error(client_socket, 404, "Not Found"); + goto point1; + } + + char *real_root = realpath(docroot, NULL); + if (real_root == NULL) { + error(client_socket, 500, "Internal Server Error"); + goto point2; + } + + if (0 != strncmp(real_root, real_path, strlen(real_root))) { + error(client_socket, 404, "Not Found"); + goto point3; + } + + struct stat st; + if (0 > stat(real_path, &st)) { + error(client_socket, 404, "Not Found"); + goto point3; + } + + bool add_slash = false; + + if (S_ISDIR(st.st_mode)) { + char *found = br_mime_guess_index(real_path); + + if (found == NULL) { + error(client_socket, 403, "Forbidden"); + goto point3; + } + + size_t path_len = strlen(path); + if (path_len > 0 && path[path_len - 1] != '/') + add_slash = true; + + free(real_path); + real_path = found; + } + + if (0 != access(real_path, F_OK)) { + error(client_socket, 500, "Internal Server Error"); + goto point3; + } + + if (add_slash) { + // production webservers usually returns 301 in such cases, but 302 is + // better for development/testing. + char *tmp = bc_strdup_printf( + "HTTP/1.0 302 Found\r\n" + "Location: %s/\r\n" + "Connection: close\r\n" + "\r\n", path); + write(client_socket, tmp, strlen(tmp)); + free(tmp); + goto point3; + } + + size_t len; + bc_error_t *err = NULL; + char* contents = bc_file_get_contents(real_path, false, &len, &err); + if (err != NULL) { + error(client_socket, 500, "Internal Server Error"); + bc_error_free(err); + goto point3; + } + + char *out = bc_strdup_printf( + "HTTP/1.0 200 OK\r\n" + "Content-Type: %s\r\n" + "Content-Length: %d\r\n" + "Connection: close\r\n" + "\r\n", br_mime_guess_content_type(real_path), len); + write(client_socket, out, strlen(out)); + free(out); + + write(client_socket, contents, len); + +point3: + free(real_root); +point2: + free(real_path); +point1: + bc_strv_free(pieces); + close(client_socket); + return NULL; +} + + +int +br_httpd_run(const char *host, unsigned short port, const char *docroot) +{ + int server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket == -1) { + fprintf(stderr, "Failed to open server socket: %s\n", strerror(errno)); + return 1; + } + + int rv = 0; + + int value = 1; + if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)) < 0) { + fprintf(stderr, "Failed to set socket option: %s\n", strerror(errno)); + rv = 1; + goto cleanup; + } + + struct sockaddr_in server_addr; + memset(&server_addr, 0, sizeof(struct sockaddr_in)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if ((server_addr.sin_addr.s_addr = inet_addr(host)) == -1) { + fprintf(stderr, "Invalid server listen address: %s\n", host); + rv = 1; + goto cleanup; + } + + if ((bind(server_socket, (struct sockaddr*) &server_addr, + sizeof(struct sockaddr_in))) == -1) + { + fprintf(stderr, "Failed to bind to server socket (%s:%d): %s\n", + host, port, strerror(errno)); + rv = 1; + goto cleanup; + } + + if (listen(server_socket, LISTEN_BACKLOG) == -1) { + fprintf(stderr, "Failed to listen to server socket: %s\n", strerror(errno)); + rv = 1; + goto cleanup; + } + + fprintf(stderr, + " * Running on http://%s:%d/\n" + "\n" + "WARNING!!! This is a development server, DO NOT RUN IT IN PRODUCTION!\n" + "\n", host, port); + + socklen_t len = sizeof(struct sockaddr_in); + + while (1) { + struct sockaddr_in client_addr; + int client_socket = accept(server_socket, + (struct sockaddr *) &client_addr, &len); + if (client_socket == -1) { + fprintf(stderr, "Failed to accept connection: %s\n", strerror(errno)); + rv = 1; + goto cleanup; + } + + request_data_t *arg = malloc(sizeof(request_data_t)); + arg->socket = client_socket; + arg->docroot = docroot; + + // this isn't really safe. the server can be easily DDoS'ed. + pthread_t thread; + if (pthread_create(&thread, NULL, handle_request, arg) != 0) { + fprintf(stderr, "Failed to create thread\n"); + rv = 1; + goto cleanup; + } + } + +cleanup: + close(server_socket); + return rv; +} diff --git a/src/blogc-runserver/httpd.h b/src/blogc-runserver/httpd.h new file mode 100644 index 0000000..a59a467 --- /dev/null +++ b/src/blogc-runserver/httpd.h @@ -0,0 +1,14 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _HTTPD_H +#define _HTTPD_H + +int br_httpd_run(const char *host, unsigned short port, const char *docroot); + +#endif /* _HTTPD_H */ diff --git a/src/blogc-runserver/main.c b/src/blogc-runserver/main.c index 685b6e3..d12fc99 100644 --- a/src/blogc-runserver/main.c +++ b/src/blogc-runserver/main.c @@ -10,324 +10,12 @@ #include #endif /* HAVE_CONFIG_H */ -#include -#include -#include #include -#include -#include #include #include #include -#include -#include -#include #include "../common/utils.h" - - -// mime types with index should be in the begin of the list. first NULL -// index aborts the lookup, for optimization -static const struct content_type_map { - const char *mimetype; - const char *extension; - const char *index; -} content_types[] = { - - // with index - {"text/html", "html", "index.html"}, - {"text/html", "htm", "index.htm"}, - {"text/html", "shtml", "index.shtml"}, - {"text/xml", "xml", "index.xml"}, - {"text/plain", "txt", "index.txt"}, - {"application/xhtml+xml", "xhtml", "index.xhtml"}, - - // without index - {"text/css", "css", NULL}, - {"image/gif", "gif", NULL}, - {"image/jpeg", "jpeg", NULL}, - {"image/jpeg", "jpg", NULL}, - {"application/javascript", "js", NULL}, - {"application/atom+xml", "atom", NULL}, - {"application/rss+xml", "rss", NULL}, - {"text/mathml", "mml", NULL}, - {"text/vnd.sun.j2me.app-descriptor", "jad", NULL}, - {"text/vnd.wap.wml", "wml", NULL}, - {"text/x-component", "htc", NULL}, - {"image/png", "png", NULL}, - {"image/tiff", "tif", NULL}, - {"image/tiff", "tiff", NULL}, - {"image/vnd.wap.wbmp", "wbmp", NULL}, - {"image/x-icon", "ico", NULL}, - {"image/x-jng", "jng", NULL}, - {"image/x-ms-bmp", "bmp", NULL}, - {"image/svg+xml", "svg", NULL}, - {"image/svg+xml", "svgz", NULL}, - {"image/webp", "webp", NULL}, - {"application/font-woff", "woff", NULL}, - {"application/java-archive", "jar", NULL}, - {"application/java-archive", "war", NULL}, - {"application/java-archive", "ear", NULL}, - {"application/json", "json", NULL}, - {"application/mac-binhex40", "hqx", NULL}, - {"application/msword", "doc", NULL}, - {"application/pdf", "pdf", NULL}, - {"application/postscript", "ps", NULL}, - {"application/postscript", "eps", NULL}, - {"application/postscript", "ai", NULL}, - {"application/rtf", "rtf", NULL}, - {"application/vnd.apple.mpegurl", "m3u8", NULL}, - {"application/vnd.ms-excel", "xls", NULL}, - {"application/vnd.ms-fontobject", "eot", NULL}, - {"application/vnd.ms-powerpoint", "ppt", NULL}, - {"application/vnd.wap.wmlc", "wmlc", NULL}, - {"application/vnd.google-earth.kml+xml", "kml", NULL}, - {"application/vnd.google-earth.kmz", "kmz", NULL}, - {"application/x-7z-compressed", "7z", NULL}, - {"application/x-cocoa", "cco", NULL}, - {"application/x-java-archive-diff", "jardiff", NULL}, - {"application/x-java-jnlp-file", "jnlp", NULL}, - {"application/x-makeself", "run", NULL}, - {"application/x-perl", "pl", NULL}, - {"application/x-perl", "pm", NULL}, - {"application/x-pilot", "prc", NULL}, - {"application/x-pilot", "pdb", NULL}, - {"application/x-rar-compressed", "rar", NULL}, - {"application/x-redhat-package-manager", "rpm", NULL}, - {"application/x-sea", "sea", NULL}, - {"application/x-shockwave-flash", "swf", NULL}, - {"application/x-stuffit", "sit", NULL}, - {"application/x-tcl", "tcl", NULL}, - {"application/x-tcl", "tk", NULL}, - {"application/x-x509-ca-cert", "der", NULL}, - {"application/x-x509-ca-cert", "pem", NULL}, - {"application/x-x509-ca-cert", "crt", NULL}, - {"application/x-xpinstall", "xpi", NULL}, - {"application/xspf+xml", "xspf", NULL}, - {"application/zip", "zip", NULL}, - {"application/octet-stream", "bin", NULL}, - {"application/octet-stream", "exe", NULL}, - {"application/octet-stream", "dll", NULL}, - {"application/octet-stream", "deb", NULL}, - {"application/octet-stream", "dmg", NULL}, - {"application/octet-stream", "iso", NULL}, - {"application/octet-stream", "img", NULL}, - {"application/octet-stream", "msi", NULL}, - {"application/octet-stream", "msp", NULL}, - {"application/octet-stream", "msm", NULL}, - {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx", NULL}, - {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx", NULL}, - {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx", NULL}, - {"audio/midi", "mid", NULL}, - {"audio/midi", "midi", NULL}, - {"audio/midi", "kar", NULL}, - {"audio/mpeg", "mp3", NULL}, - {"audio/ogg", "ogg", NULL}, - {"audio/x-m4a", "m4a", NULL}, - {"audio/x-realaudio", "ra", NULL}, - {"video/3gpp", "3gpp", NULL}, - {"video/3gpp", "3gp", NULL}, - {"video/mp2t", "ts", NULL}, - {"video/mp4", "mp4", NULL}, - {"video/mpeg", "mpeg", NULL}, - {"video/mpeg", "mpg", NULL}, - {"video/quicktime", "mov", NULL}, - {"video/webm", "webm", NULL}, - {"video/x-flv", "flv", NULL}, - {"video/x-m4v", "m4v", NULL}, - {"video/x-mng", "mng", NULL}, - {"video/x-ms-asf", "asx", NULL}, - {"video/x-ms-asf", "asf", NULL}, - {"video/x-ms-wmv", "wmv", NULL}, - {"video/x-msvideo", "avi", NULL}, - {NULL, NULL, NULL} -}; - - -static const char* -get_extension(const char *filename) -{ - const char *ext = NULL; - unsigned int i; - for (i = strlen(filename); i > 0; i--) { - if (filename[i] == '.') { - ext = filename + i + 1; - break; - } - } - if (i == 0) - return NULL; - return ext; -} - - -static const char* -guess_content_type(const char *filename) -{ - const char *extension = get_extension(filename); - if (extension == NULL) - goto default_type; - for (unsigned int i = 0; content_types[i].extension != NULL; i++) { - if (0 == strcmp(content_types[i].extension, extension)) { - return content_types[i].mimetype; - } - } -default_type: - return "application/octet-stream"; -} - - -static void -handler(struct evhttp_request *request, void *ptr) -{ - const char *root = ptr; - const char *uri = evhttp_request_get_uri(request); - - struct evhttp_uri *decoded_uri = evhttp_uri_parse(uri); - if (decoded_uri == NULL) { - evhttp_send_error(request, 400, "Bad request"); - return; - } - - const char *path = evhttp_uri_get_path(decoded_uri); - if (path == NULL) - path = "/"; - - char *decoded_path = evhttp_uridecode(path, 0, NULL); - if (decoded_path == NULL) { - evhttp_send_error(request, 400, "Bad request"); - goto point1; - } - - char *abs_path = bc_strdup_printf("%s/%s", root, decoded_path); - char *real_path = realpath(abs_path, NULL); - free(abs_path); - - if (real_path == NULL) { - evhttp_send_error(request, 404, "Not found"); - goto point2; - } - - char *real_root = realpath(root, NULL); - if (real_root == NULL) { - evhttp_send_error(request, 500, "Internal server error"); - goto point3; - } - - if (0 != strncmp(real_root, real_path, strlen(real_root))) { - evhttp_send_error(request, 404, "Not found"); - goto point4; - } - - struct stat st; - if (0 > stat(real_path, &st)) { - evhttp_send_error(request, 404, "Not found"); - goto point4; - } - - bool add_slash = false; - - if (S_ISDIR(st.st_mode)) { - char *found = NULL; - - for (unsigned int i = 0; content_types[i].index != NULL; i++) { - char *f = bc_strdup_printf("%s/%s", real_path, - content_types[i].index); - if (0 == access(f, F_OK)) { - found = f; - break; - } - free(f); - } - - if (found == NULL) { - evhttp_send_error(request, 403, "Forbidden"); - goto point4; - } - - size_t path_len = strlen(path); - if (path_len > 0 && path[path_len - 1] != '/') - add_slash = true; - - free(real_path); - real_path = found; - } - - int fd; - if ((fd = open(real_path, O_RDONLY)) < 0) { - evhttp_send_error(request, 500, "Internal server error"); - goto point4; - } - - const char *type = guess_content_type(real_path); - - if (fstat(fd, &st) < 0) { - evhttp_send_error(request, 500, "Internal server error"); - goto point4; - } - - struct evkeyvalq *headers = evhttp_request_get_output_headers(request); - - if (add_slash) { - char *tmp = bc_strdup_printf("%s/", path); - evhttp_add_header(headers, "Location", tmp); - free(tmp); - // production webservers usually returns 301 in such cases, but 302 is - // better for development/testing. - evhttp_send_reply(request, 302, "Found", NULL); - goto point4; - } - - evhttp_add_header(headers, "Content-Type", type); - char *content_length = bc_strdup_printf("%zu", st.st_size); - evhttp_add_header(headers, "Content-Length", content_length); - free(content_length); - - struct evbuffer *evb = evbuffer_new(); - evbuffer_add_file(evb, fd, 0, st.st_size); - evhttp_send_reply(request, 200, "OK", evb); - -point4: - free(real_root); -point3: - free(real_path); -point2: - free(decoded_path); -point1: - evhttp_uri_free(decoded_uri); -} - - -static int -runserver(const char *address, unsigned short port, const char *root) -{ - struct event_base *base = event_base_new(); - if (base == NULL) { - fprintf(stderr, "error: failed to initialize event base\n"); - return 1; - } - - struct evhttp *http = evhttp_new(base); - if (http == NULL) { - fprintf(stderr, "error: failed to initialize HTTP server\n"); - return 1; - } - - evhttp_set_gencb(http, handler, (char*) root); - - evhttp_set_allowed_methods(http, EVHTTP_REQ_GET | EVHTTP_REQ_HEAD); - - if (0 != evhttp_bind_socket(http, address, port)) { - fprintf(stderr, "error: failed to bind socket to %s:%d\n", address, - port); - return 1; - } - - fprintf(stderr, " * Running on http://%s:%d/\n", address, port); - - event_base_dispatch(base); - - return 0; -} +#include "httpd.h" static void @@ -421,7 +109,7 @@ main(int argc, char **argv) if (host == NULL) host = bc_strdup("127.0.0.1"); - rv = runserver(host, port, docroot); + rv = br_httpd_run(host, port, docroot); cleanup: free(host); diff --git a/src/blogc-runserver/mime.c b/src/blogc-runserver/mime.c new file mode 100644 index 0000000..b42d70a --- /dev/null +++ b/src/blogc-runserver/mime.c @@ -0,0 +1,168 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include +#include "../common/utils.h" +#include "httpd-utils.h" + + +// mime types with index should be in the begin of the list. first NULL +// index aborts the lookup, for optimization +static const struct content_type_map { + const char *mimetype; + const char *extension; + const char *index; +} content_types[] = { + + // with index + {"text/html", "html", "index.html"}, + {"text/html", "htm", "index.htm"}, + {"text/html", "shtml", "index.shtml"}, + {"text/xml", "xml", "index.xml"}, + {"text/plain", "txt", "index.txt"}, + {"application/xhtml+xml", "xhtml", "index.xhtml"}, + + // without index + {"text/css", "css", NULL}, + {"image/gif", "gif", NULL}, + {"image/jpeg", "jpeg", NULL}, + {"image/jpeg", "jpg", NULL}, + {"application/javascript", "js", NULL}, + {"application/atom+xml", "atom", NULL}, + {"application/rss+xml", "rss", NULL}, + {"text/mathml", "mml", NULL}, + {"text/vnd.sun.j2me.app-descriptor", "jad", NULL}, + {"text/vnd.wap.wml", "wml", NULL}, + {"text/x-component", "htc", NULL}, + {"image/png", "png", NULL}, + {"image/tiff", "tif", NULL}, + {"image/tiff", "tiff", NULL}, + {"image/vnd.wap.wbmp", "wbmp", NULL}, + {"image/x-icon", "ico", NULL}, + {"image/x-jng", "jng", NULL}, + {"image/x-ms-bmp", "bmp", NULL}, + {"image/svg+xml", "svg", NULL}, + {"image/svg+xml", "svgz", NULL}, + {"image/webp", "webp", NULL}, + {"application/font-woff", "woff", NULL}, + {"application/java-archive", "jar", NULL}, + {"application/java-archive", "war", NULL}, + {"application/java-archive", "ear", NULL}, + {"application/json", "json", NULL}, + {"application/mac-binhex40", "hqx", NULL}, + {"application/msword", "doc", NULL}, + {"application/pdf", "pdf", NULL}, + {"application/postscript", "ps", NULL}, + {"application/postscript", "eps", NULL}, + {"application/postscript", "ai", NULL}, + {"application/rtf", "rtf", NULL}, + {"application/vnd.apple.mpegurl", "m3u8", NULL}, + {"application/vnd.ms-excel", "xls", NULL}, + {"application/vnd.ms-fontobject", "eot", NULL}, + {"application/vnd.ms-powerpoint", "ppt", NULL}, + {"application/vnd.wap.wmlc", "wmlc", NULL}, + {"application/vnd.google-earth.kml+xml", "kml", NULL}, + {"application/vnd.google-earth.kmz", "kmz", NULL}, + {"application/x-7z-compressed", "7z", NULL}, + {"application/x-cocoa", "cco", NULL}, + {"application/x-java-archive-diff", "jardiff", NULL}, + {"application/x-java-jnlp-file", "jnlp", NULL}, + {"application/x-makeself", "run", NULL}, + {"application/x-perl", "pl", NULL}, + {"application/x-perl", "pm", NULL}, + {"application/x-pilot", "prc", NULL}, + {"application/x-pilot", "pdb", NULL}, + {"application/x-rar-compressed", "rar", NULL}, + {"application/x-redhat-package-manager", "rpm", NULL}, + {"application/x-sea", "sea", NULL}, + {"application/x-shockwave-flash", "swf", NULL}, + {"application/x-stuffit", "sit", NULL}, + {"application/x-tcl", "tcl", NULL}, + {"application/x-tcl", "tk", NULL}, + {"application/x-x509-ca-cert", "der", NULL}, + {"application/x-x509-ca-cert", "pem", NULL}, + {"application/x-x509-ca-cert", "crt", NULL}, + {"application/x-xpinstall", "xpi", NULL}, + {"application/xspf+xml", "xspf", NULL}, + {"application/zip", "zip", NULL}, + {"application/octet-stream", "bin", NULL}, + {"application/octet-stream", "exe", NULL}, + {"application/octet-stream", "dll", NULL}, + {"application/octet-stream", "deb", NULL}, + {"application/octet-stream", "dmg", NULL}, + {"application/octet-stream", "iso", NULL}, + {"application/octet-stream", "img", NULL}, + {"application/octet-stream", "msi", NULL}, + {"application/octet-stream", "msp", NULL}, + {"application/octet-stream", "msm", NULL}, + {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx", NULL}, + {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx", NULL}, + {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx", NULL}, + {"audio/midi", "mid", NULL}, + {"audio/midi", "midi", NULL}, + {"audio/midi", "kar", NULL}, + {"audio/mpeg", "mp3", NULL}, + {"audio/ogg", "ogg", NULL}, + {"audio/x-m4a", "m4a", NULL}, + {"audio/x-realaudio", "ra", NULL}, + {"video/3gpp", "3gpp", NULL}, + {"video/3gpp", "3gp", NULL}, + {"video/mp2t", "ts", NULL}, + {"video/mp4", "mp4", NULL}, + {"video/mpeg", "mpeg", NULL}, + {"video/mpeg", "mpg", NULL}, + {"video/quicktime", "mov", NULL}, + {"video/webm", "webm", NULL}, + {"video/x-flv", "flv", NULL}, + {"video/x-m4v", "m4v", NULL}, + {"video/x-mng", "mng", NULL}, + {"video/x-ms-asf", "asx", NULL}, + {"video/x-ms-asf", "asf", NULL}, + {"video/x-ms-wmv", "wmv", NULL}, + {"video/x-msvideo", "avi", NULL}, + {NULL, NULL, NULL} +}; + + +const char* +br_mime_guess_content_type(const char *filename) +{ + const char *extension = br_get_extension(filename); + if (extension == NULL) + goto default_type; + for (size_t i = 0; content_types[i].extension != NULL; i++) { + if (0 == strcmp(content_types[i].extension, extension)) { + return content_types[i].mimetype; + } + } + +default_type: + return "application/octet-stream"; +} + + +char* +br_mime_guess_index(const char *path) +{ + char *found = NULL; + for (size_t i = 0; content_types[i].index != NULL; i++) { + char *f = bc_strdup_printf("%s/%s", path, content_types[i].index); + if (0 == access(f, F_OK)) { + found = f; + break; + } + free(f); + } + return found; +} diff --git a/src/blogc-runserver/mime.h b/src/blogc-runserver/mime.h new file mode 100644 index 0000000..a4b6409 --- /dev/null +++ b/src/blogc-runserver/mime.h @@ -0,0 +1,15 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _MIME_H +#define _MIME_H + +const char* br_mime_guess_content_type(const char *filename); +char* br_mime_guess_index(const char *path); + +#endif /* _MIME_H */ diff --git a/src/blogc/loader.c b/src/blogc/loader.c index b182e0b..c3e1a4a 100644 --- a/src/blogc/loader.c +++ b/src/blogc/loader.c @@ -63,7 +63,7 @@ blogc_template_parse_from_file(const char *f, bc_error_t **err) if (err == NULL || *err != NULL) return NULL; size_t len; - char *s = bc_file_get_contents(f, &len, err); + char *s = bc_file_get_contents(f, true, &len, err); if (s == NULL) return NULL; bc_slist_t *rv = blogc_template_parse(s, len, err); @@ -78,7 +78,7 @@ blogc_source_parse_from_file(const char *f, bc_error_t **err) if (err == NULL || *err != NULL) return NULL; size_t len; - char *s = bc_file_get_contents(f, &len, err); + char *s = bc_file_get_contents(f, true, &len, err); if (s == NULL) return NULL; bc_trie_t *rv = blogc_source_parse(s, len, err); diff --git a/src/common/file.c b/src/common/file.c index 70a5631..47c97af 100644 --- a/src/common/file.c +++ b/src/common/file.c @@ -19,7 +19,7 @@ char* -bc_file_get_contents(const char *path, size_t *len, bc_error_t **err) +bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err) { if (path == NULL || err == NULL || *err != NULL) return NULL; @@ -43,7 +43,7 @@ bc_file_get_contents(const char *path, size_t *len, bc_error_t **err) tmp = buffer; - if (str->len == 0 && read_len > 0) { + if (utf8 && str->len == 0 && read_len > 0) { // skipping BOM before validation, for performance. should be safe // enough size_t skip = bc_utf8_skip_bom((uint8_t*) buffer, read_len); @@ -56,7 +56,7 @@ bc_file_get_contents(const char *path, size_t *len, bc_error_t **err) } fclose(fp); - if (!bc_utf8_validate_str(str)) { + if (utf8 && !bc_utf8_validate_str(str)) { *err = bc_error_new_printf(BC_ERROR_FILE, "File content is not valid UTF-8: %s", path); bc_string_free(str, true); diff --git a/src/common/file.h b/src/common/file.h index e095de7..498f4f4 100644 --- a/src/common/file.h +++ b/src/common/file.h @@ -10,10 +10,11 @@ #define _FILE_H #include +#include #include "error.h" #define BC_FILE_CHUNK_SIZE 1024 -char* bc_file_get_contents(const char *path, size_t *len, bc_error_t **err); +char* bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err); #endif /* _FILE_H */ diff --git a/tests/blogc-runserver/check_httpd_utils.c b/tests/blogc-runserver/check_httpd_utils.c new file mode 100644 index 0000000..18376a2 --- /dev/null +++ b/tests/blogc-runserver/check_httpd_utils.c @@ -0,0 +1,163 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include +#include +#include "../../src/common/utils.h" +#include "../../src/blogc-runserver/httpd-utils.h" + + +ssize_t +__wrap_read(int fd, void *buf, size_t count) +{ + assert_int_equal(fd, mock_type(int)); + const char *mock_buf = mock_type(const char*); + strcpy(buf, mock_buf); + assert_int_equal(count, READLINE_BUFFER_SIZE); + return strlen(mock_buf) > 0 ? strlen(mock_buf) : -1; +} + + +static void +test_readline(void **state) +{ + char *t; + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, ""); + t = br_readline(1234); + assert_string_equal(t, "bola"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola1\nguda\nxd"); + t = br_readline(1234); + assert_string_equal(t, "bola1"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola2\rguda\rxd"); + t = br_readline(1234); + assert_string_equal(t, "bola2"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola3\r\nguda\r\nxd"); + t = br_readline(1234); + assert_string_equal(t, "bola3"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "guda"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, ""); + t = br_readline(1234); + assert_string_equal(t, "bolaguda"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola1"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola\nguda"); + t = br_readline(1234); + assert_string_equal(t, "bola1bola"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola2"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola\rguda"); + t = br_readline(1234); + assert_string_equal(t, "bola2bola"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola3"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola\r\nguda"); + t = br_readline(1234); + assert_string_equal(t, "bola3bola"); + free(t); +} + + +static void +test_hextoi(void **state) +{ + assert_int_equal(br_hextoi('0'), 0); + assert_int_equal(br_hextoi('1'), 1); + assert_int_equal(br_hextoi('2'), 2); + assert_int_equal(br_hextoi('3'), 3); + assert_int_equal(br_hextoi('4'), 4); + assert_int_equal(br_hextoi('5'), 5); + assert_int_equal(br_hextoi('6'), 6); + assert_int_equal(br_hextoi('7'), 7); + assert_int_equal(br_hextoi('8'), 8); + assert_int_equal(br_hextoi('9'), 9); + assert_int_equal(br_hextoi('a'), 10); + assert_int_equal(br_hextoi('b'), 11); + assert_int_equal(br_hextoi('c'), 12); + assert_int_equal(br_hextoi('d'), 13); + assert_int_equal(br_hextoi('e'), 14); + assert_int_equal(br_hextoi('f'), 15); + assert_int_equal(br_hextoi('A'), 10); + assert_int_equal(br_hextoi('B'), 11); + assert_int_equal(br_hextoi('C'), 12); + assert_int_equal(br_hextoi('D'), 13); + assert_int_equal(br_hextoi('E'), 14); + assert_int_equal(br_hextoi('g'), -1); + assert_int_equal(br_hextoi('G'), -1); + assert_int_equal(br_hextoi('-'), -1); +} + + +static void +test_urldecode(void **state) +{ + for (size_t i = 0; i < 128; i++) { + char *t = bc_strdup_printf("%%%02x", i); + char *r = br_urldecode(t); + assert_int_equal(r[0], i); + assert_int_equal(r[1], 0); + free(r); + free(t); + } + char *r = br_urldecode("%Ab"); + assert_string_equal(r, "\xab"); + free(r); + r = br_urldecode("%xb"); + assert_string_equal(r, "%xb"); + free(r); + r = br_urldecode("%C3%BC"); + assert_string_equal(r, "\xc3\xbc"); + free(r); +} + + +static void +test_get_extension(void **state) +{ + assert_null(br_get_extension("bola")); + assert_string_equal(br_get_extension("bola.txt"), "txt"); + assert_string_equal(br_get_extension("bola.txt.jpg"), "jpg"); + assert_null(br_get_extension("bola.txt/foo")); + assert_string_equal(br_get_extension("bola.txt/foo.jpg"), "jpg"); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_readline), + unit_test(test_hextoi), + unit_test(test_urldecode), + unit_test(test_get_extension), + }; + return run_tests(tests); +} diff --git a/tests/blogc-runserver/check_mime.c b/tests/blogc-runserver/check_mime.c new file mode 100644 index 0000000..5d11766 --- /dev/null +++ b/tests/blogc-runserver/check_mime.c @@ -0,0 +1,92 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include +#include +#include "../../src/blogc-runserver/mime.h" + + +int +__wrap_access(const char *pathname, int mode) +{ + assert_string_equal(pathname, mock_type(const char*)); + assert_int_equal(mode, F_OK); + return mock_type(int); +} + + +static void +test_guess_content_type(void **state) +{ + assert_string_equal(br_mime_guess_content_type("foo.html"), "text/html"); + assert_string_equal(br_mime_guess_content_type("foo.jpg"), "image/jpeg"); + assert_string_equal(br_mime_guess_content_type("foo.mp4"), "video/mp4"); + assert_string_equal(br_mime_guess_content_type("foo.bola"), "application/octet-stream"); +} + + +static void +test_guess_index(void **state) +{ + char *t; + will_return(__wrap_access, "dir/index.html"); + will_return(__wrap_access, 0); + t = br_mime_guess_index("dir"); + assert_string_equal(t, "dir/index.html"); + free(t); + will_return(__wrap_access, "dir/index.html"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.htm"); + will_return(__wrap_access, 0); + t = br_mime_guess_index("dir"); + assert_string_equal(t, "dir/index.htm"); + free(t); + will_return(__wrap_access, "dir/index.html"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.htm"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.shtml"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.xml"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.txt"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.xhtml"); + will_return(__wrap_access, 0); + t = br_mime_guess_index("dir"); + assert_string_equal(t, "dir/index.xhtml"); + free(t); + will_return(__wrap_access, "dir/index.html"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.htm"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.shtml"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.xml"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.txt"); + will_return(__wrap_access, 1); + will_return(__wrap_access, "dir/index.xhtml"); + will_return(__wrap_access, 1); + assert_null(br_mime_guess_index("dir")); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_guess_content_type), + unit_test(test_guess_index), + }; + return run_tests(tests); +} diff --git a/tests/blogc/check_loader.c b/tests/blogc/check_loader.c index 52521e2..36a3471 100644 --- a/tests/blogc/check_loader.c +++ b/tests/blogc/check_loader.c @@ -50,8 +50,9 @@ test_get_filename(void **state) char* -__wrap_bc_file_get_contents(const char *path, size_t *len, bc_error_t **err) +__wrap_bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err) { + assert_true(utf8); assert_null(*err); const char *_path = mock_type(const char*); if (_path != NULL) -- cgit v1.2.3-18-g5258