When you need a web server in a hurry.

Overview

darkhttpd

https://unix4lyfe.org/darkhttpd/

When you need a web server in a hurry.

Features:

  • Simple to set up:
    • Single binary, no other files, no installation needed.
    • Standalone, doesn't need inetd or ucspi-tcp.
    • No messing around with config files - all you have to specify is the www root.
  • Written in C - efficient and portable.
  • Small memory footprint.
  • Event loop, single threaded - no fork() or pthreads.
  • Generates directory listings.
  • Supports HTTP GET and HEAD requests.
  • Supports Range / partial content. (try streaming music files or resuming a download)
  • Supports If-Modified-Since.
  • Supports Keep-Alive connections.
  • Supports IPv6.
  • Can serve 301 redirects based on Host header.
  • Uses sendfile() on FreeBSD, Solaris and Linux.
  • Can use acceptfilter on FreeBSD.
  • At some point worked on FreeBSD, Linux, OpenBSD, Solaris.
  • ISC license.
  • suckless.org says darkhttpd sucks less.

Security:

  • Can log accesses, including Referer and User-Agent.
  • Can chroot.
  • Can drop privileges.
  • Impervious to /../ sniffing.
  • Times out idle connections.
  • Drops overly long requests.

Limitations:

  • Only serves static content - no CGI.

How to build darkhttpd

Simply run make:

make

How to run darkhttpd

Serve /var/www/htdocs on the default port (80 if running as root, else 8080):

./darkhttpd /var/www/htdocs

Serve ~/public_html on port 8081:

./darkhttpd ~/public_html --port 8081

Only bind to one IP address (useful on multi-homed systems):

./darkhttpd ~/public_html --addr 192.168.0.1

Serve at most 4 simultaneous connections:

./darkhttpd ~/public_html --maxconn 4

Log accesses to a file:

./darkhttpd ~/public_html --log access.log

Chroot for extra security (you need root privs for chroot):

./darkhttpd /var/www/htdocs --chroot

Use default.htm instead of index.html:

./darkhttpd /var/www/htdocs --index default.htm

Add mimetypes - in this case, serve .dat files as text/plain:

$ cat extramime
text/plain  dat
$ ./darkhttpd /var/www/htdocs --mimetypes extramime

Drop privileges:

./darkhttpd /var/www/htdocs --uid www --gid www

Use acceptfilter (FreeBSD only):

kldload accf_http
./darkhttpd /var/www/htdocs --accf

Run in the background and create a pidfile:

./darkhttpd /var/www/htdocs --pidfile /var/run/httpd.pid --daemon

Web forward (301) requests for some hosts:

./darkhttpd /var/www/htdocs --forward example.com http://www.example.com \
  --forward secure.example.com https://www.example.com/secure

Web forward (301) requests for all hosts:

./darkhttpd /var/www/htdocs --forward example.com http://www.example.com \
  --forward-all http://catchall.example.com

Commandline options can be combined:

./darkhttpd ~/public_html --port 8080 --addr 127.0.0.1

To see a full list of commandline options, run darkhttpd without any arguments:

./darkhttpd

Enjoy.

Comments
  • Non-ASCII filelist compatibility

    Non-ASCII filelist compatibility

    As for 1.12 the patch was:

    --- darkhttpd.c.orig	2020-12-05 23:50:47.745138608 +0300
    +++ darkhttpd.c	2020-12-05 23:52:36.930425427 +0300
    @@ -1819,7 +1819,7 @@
         }
     
         listing = make_apbuf();
    -    append(listing, "<html>\n<head>\n <title>");
    +    append(listing, "<html>\n<head>\n<meta charset=\"UTF-8\">\n<title>");
         append(listing, conn->url);
         append(listing, "</title>\n</head>\n<body>\n<h1>");
         append(listing, conn->url);
    
    opened by tieugene 10
  • New release?

    New release?

    This is very cool. I'm looking to make a FreeBSD port for this. Looks like there's some useful new features since 1.13. Are you planning on releasing a new version that I can use in the port?

    Thank you!

    opened by hhartzer 7
  • Great open source project

    Great open source project

    If it can upload file to the server, or it can call the server-side script interface. Then this could implement dynamic language support. Used on embedded boards, instead of using the TFTP tool, very, very good. I have compiled and run on HI3518EV300. It works . low cpu and memory usage

    thanks.

    opened by zsinba 2
  • Update illumos support to the modern era

    Update illumos support to the modern era

    This PR introduces 2 commits. The primary is 95d785acc1e4bc5f1b9ff8bda17ffb51b6f7a5f6.

    Old versions of Solaris did not have vasprintf, so darkhttpd defined one
    gated behind an ifdef. Oracle Solaris 10 has had vasprintf since 2011.
    Oracle Solaris 11 has had it since release. illumos (which also reports
    as `__sun`) also has it in all current incarnations. As a result, this
    ifdef'd block creates compiler errors due to a second definition of the
    function. This commit removes the block.
    
    This commit also adds `-lsendfile` to the Makefile for systems that
    report as `SunOS` in `uname` (Solaris and Illumos), which is necessary
    to link successfully in current day
    

    This should bring darkhttpd up to date with the current state of illumos and solaris and allow it to build on those systems.

    In the second commit I added a note to the README that CC=<compiler> make may be necessary if cc isn't a command on the system. For whatever reason, some variants of illumos I'm using don't have this. I need to look into that more, but in the mean time, I think it's a helpful tip.

    opened by faithanalog 1
  • HTTP2 header compatibility

    HTTP2 header compatibility

    If drakhttpd is behind some HTTP2 reverse proxy (like haproxy), it doesn't read the HTTP Header very well, because the http2 header is all lowercase.

    Is there any compatibility we can do?

    opened by Hill-98 1
  • Forward to HTTPS if X-Forwarded-Proto is

    Forward to HTTPS if X-Forwarded-Proto is "http"

    Forward to HTTPS if X-Forwarded-Proto is equal to "http". This can be enabled with "--forward-https".

    This might be useful if darkhttpd is behind a reverse proxy that supports SSL.

    opened by FragmentedCurve 1
  • Is the fork() in line 2610 strictly necessary? Could it be changed to vfork()?

    Is the fork() in line 2610 strictly necessary? Could it be changed to vfork()?

    I ask only because I would like to use this on NuttX, and it doesn't support fork(), but using it with some architectures does support vfork(). https://nuttx.apache.org/docs/latest/reference/user/01_task_control.html#c.vfork

    Forgive my lack of knowledge in this area, I'm just a software user, not a programmer.

    opened by cogburnd02 1
  • Fix high CPU usage when timeout is disabled

    Fix high CPU usage when timeout is disabled

    When darkhttpd is running with --timeout 0 (timeout disabled), and any connection is idle, it will use 100% of the CPU. This happens because select returns immediately when its timeout is zero, causing the main httpd_poll loop to spin.

    Fix this by adding a check to httpd_poll making select always receive a NULL timeout when timeout_secs is zero.

    opened by tdryer 1
  • Fix hung connection from consecutive requests

    Fix hung connection from consecutive requests

    These commits fix a bug with keep-alive requests and substantially improve their throughput.

    Fix hung connection from consecutive requests

    A client making quick consecutive requests with keep-alive, such as ab with -k, can cause the connection to become hung.

    This happens because of an optimization in http_poll function. When a connection state becomes DONE, httpd_poll recycles the connection and immediately calls poll_recv_request. However, it doesn't handle this resulting in the connection state becoming DONE again. If this occurs, the state stays in DONE, and the further calls to httpd_poll ignore the connection.

    Fix this by calling poll_recv_request in a loop until the state is no longer DONE.

    Enable TCP_NODELAY optimization

    It looks like TCP_NODELAY was disabled due to the bug fixed in the previous commit. Enabling it substantially improves keep-alive performance with ab:

    Before:

    Time per request:       0.272 [ms] (mean)
    

    After:

    Time per request:       0.033 [ms] (mean)
    

    Remove keep-alive optimization from httpd_poll

    Benchmarking with ab shows that bypassing select for keep-alive connections in the DONE state doesn't significantly impact performance. Since this optimization previously caused a bug, remove it.

    opened by tdryer 1
  • Dockerize

    Dockerize

    Add static build option to Makefile and create Dockerfile to run it. Users could simply map the containers /var/www/htdocs directory or copy files directly to the container using standard Docker image extension.

    opened by miles-po 1
  • Compile error (patched sources)

    Compile error (patched sources)

    Building on CentOS7 from scratch:

    darkhttpd.c: In function 'base64_encode':
    darkhttpd.c:969:5: error: 'for' loop initial declarations are only allowed in C99 mode
         for (int i = 0, j = 0; i < input_length;) {
         ^
    darkhttpd.c:969:5: note: use option -std=c99 or -std=gnu99 to compile your code
    darkhttpd.c:984:14: error: redefinition of 'i'
         for (int i = 0; i < mod_table[input_length % 3]; i++)
                  ^
    darkhttpd.c:969:14: note: previous definition of 'i' was here
         for (int i = 0, j = 0; i < input_length;) {
                  ^
    darkhttpd.c:984:5: error: 'for' loop initial declarations are only allowed in C99 mode
         for (int i = 0; i < mod_table[input_length % 3]; i++)
         ^
    error: Bad exit status from /var/tmp/rpm-tmp.uQUaKJ (%build)
        Bad exit status from /var/tmp/rpm-tmp.uQUaKJ (%build)
    
    opened by tieugene 1
  • Add an option to serve a single file for all requests

    Add an option to serve a single file for all requests

    Often, I want to just share a single file over HTTP. Right now, we can only pass a directory to darkhttpd, and it will serve any file from that directory. I would love to be able to do darkhttpd ./my/single/file.html.

    opened by iliazeus 0
  • Adding initial run with mimetypes and index page

    Adding initial run with mimetypes and index page

    Initial run options after compile to get quick ride with the server. It includes index.html, mimetypes to be able to access a linked README.md and run.sh shell script to apply those initial run options.

    opened by saidbakr 0
  • Custom HTTP errors pages

    Custom HTTP errors pages

    Like mimetypes option, it would be nice to have an option such as --customerrors that points to a router like file that points to custom errors page, something like below:

    404  path/to/error404.html
    500  path/to/error500.html
    
    opened by saidbakr 0
  • Simple CORS support

    Simple CORS support

    Would you be open to merging a PR adding (very) simple support for CORS headers? My first thought is that passing an --allow-origin X argument would add the Access-Control-Allow-Origin: X header to successful HTTP responses. We don't care about pre-flight requests since we only respond to GET and HEAD requests.

    Thanks for working on darkhttpd! It's pretty sweet.

    opened by aszkid 4
  • `maxconn` option does not limit simultaneous connections

    `maxconn` option does not limit simultaneous connections

    The --maxconn N option does not limit the number of simultaneous connections as described.

    This value is being used as the backlog parameter to listen. At least on Linux, this does not limit the number of concurrent connections, because once a connection is accepted, it's no long part of the pending connection queue.

    One way to fix this would be to count the number of open connections, and avoid adding the listening socket to the file descriptor set if the count reaches the maximum.

    Here's a Python script to reproduce the issue:

    import socket
    
    PORT = 8080
    
    def main():
      request = b'GET /darkhttpd.c HTTP/1.1\r\n\r\n'
      sockets = []
      while True:
          s = socket.socket()
          s.settimeout(1)
          s.connect(("", PORT))
          s.send(request)
          data = s.recv(1024)
          sockets.append(s)
          print('{} connections open...'.format(len(sockets)))
    
    if __name__ == '__main__':
        main()
    
    opened by tdryer 1
Releases(v1.14)
  • v1.14(Oct 2, 2022)

    • Add support for logging with syslog.
    • Fix hung connection from consecutive keep-alive requests.
    • Fix high CPU usage when timeout is disabled.
    • Add --forward-https.
    • Make header parsing case insensitive, to work behind an HTTP2 reverse proxy.
    • Add trailing slash to links for directories.
    • Fix crash when a file has a large (year 10,000+) mtime.
    Source code(tar.gz)
    Source code(zip)
🌱Light and powerful C++ web framework for highly scalable and resource-efficient web application. It's zero-dependency and easy-portable.

Oat++ News Hey, meet the new oatpp version 1.2.5! See the changelog for details. Check out the new oatpp ORM - read more here. Oat++ is a modern Web F

Oat++ 5.7k Oct 2, 2022
Mongoose Embedded Web Server Library - a multi-protocol embedded networking library with TCP/UDP, HTTP, WebSocket, MQTT built-in protocols, async DNS resolver, and non-blocking API.

Mongoose - Embedded Web Server / Embedded Networking Library Mongoose is a networking library for C/C++. It implements event-driven non-blocking APIs

Cesanta Software 8.8k Sep 28, 2022
Simple, secure & standards compliant web server for the most demanding of applications

Simple, secure[1] & standards compliant[2] web server for the most demanding[3] of applications. Read more... ?? Optimized security Being meticulously

uNetworking AB 14.2k Sep 29, 2022
web server that will print hello world on the screen only for linux users

a simple http server side lib only for linux users Note: This lib is currently under development you can check the source code and even use it but dn'

notaweeb 11 Mar 14, 2021
cServe : mean and lean web server written in c

cServe =========== cServe is a web server written in c for POXIS compliant operating systems cServe is planned to be [ ] Supporting HTTP/1.x and HTTP/

Ayush Kumar 1 Dec 21, 2021
reverse proxy with web server and preview page

Reverse Proxy Dependencies Go Make Suport Termux (android/afsd kernel) linux (kernel) Install: Termux: 1 step: Install Go-lang, Git and Make pkg insta

AlbâniaSecurity-RT 7 Feb 19, 2022
Update ESP32 firmware over WiFi from a web server

esp32-firmware-update Update ESP32 firmware over WiFi from Github This includes a python script which generates the update json file based on the firm

Felix Biego 10 Sep 4, 2022
A purely bash web server, no socat, netcat, etc...

Bash-web-server A purely bash web server, no socat, netcat, etc... Requirement bash 5.1 loadable accept builtin (http://git.savannah.gnu.org/cgit/bash

null 615 Sep 25, 2022
A webserver hosting a bank system for Minecraft, able to be used from web browser or from CC/OC if you're playing modded.

CCash A webserver hosting a bank system for Minecraft, able to be used from web browser or from CC/OC if you're playing modded. Description the curren

William Katz 23 Aug 7, 2022
LAppS - Lua Application Server for micro-services with default communication over WebSockets. The fastest and most vertically scalable WebSockets server implementation ever. Low latency C++ <-> Lua stack roundtrip.

LAppS - Lua Application Server This is an attempt to provide very easy to use Lua Application Server working over WebSockets protocol (RFC 6455). LApp

null 47 Apr 25, 2022
Windows named pipe server that forwards connections to given TCP server

PipeTcp An asynchronous Windows named pipe server that forwards connections to given TCP server. Pre-built binaries can be found in Releases. Invocati

Jinoh Kang 4 May 23, 2022
A project designed for the esp8266 D1 Mini or the esp8266 D1 Mini PRO to provide a wifi http server and dns server.

PS4 Server 9.00 This is a project designed for the esp8266 D1 Mini or the esp8266 D1 Mini PRO to provide a wifi http server and dns server. this is fo

null 13 Sep 7, 2022
LibVNCServer/LibVNCClient are cross-platform C libraries that allow you to easily implement VNC server or client functionality in your program.

LibVNCServer: A library for easy implementation of a VNC server. Copyright (C) 2001-2003 Johannes E. Schindelin If you already used LibVNCServer, you

null 864 Sep 29, 2022
Provide translation, currency conversion, and voting services. First using telnet you create a connection to a TCP socket, then the server connects to 3 UDP sockets hosted on other servers to do tasks.

to run micro servers g++ translator.cpp -o translator ./translator <port 1> g++ voting.cpp -o voting ./voting <port 2> g++ currency_converter.cpp -o c

Jacob Artuso 1 Oct 29, 2021
Simple useful interoperability tests for WebRTC libraries. If you are a WebRTC library developer we'd love to include you!

Overview This project aims to be a convenient location for WebRTC library developers to perform interoperability tests. Who can Participate The projec

Aaron Clauson 103 Sep 8, 2022
About Add any Program in any language you like or add a hello world Program ❣️ if you like give us ⭐

Hello-World About Add any Program in any language you like or add a hello world Program ❣️ if you like give us ⭐ Give this Project a Star ⭐ If you lik

Lokesh Jangid 14 Aug 23, 2022
C library to create simple HTTP servers and Web Applications.

Onion http server library Travis status Coverity status Onion is a C library to create simple HTTP servers and Web Applications. master the developmen

David Moreno Montero 1.9k Sep 21, 2022
An asynchronous web framework for C++ built on top of Qt

!!! I can no longer maintain this project. If you're interessed, please contact me and I can move the projetct to you !!! Tufão - an asynchronous web

Vinícius dos Santos Oliveira 549 Sep 20, 2022
C Hypertext Library - A library for writing web applications in C

CHL C Hypertext Library - A library for writing web applications in C #include <chl/chl.h> int main() { chl_set_default_headers(); chl_print_header

null 273 Aug 15, 2022