petit_httpd

pub package Null Safety Codecov Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

This is a simple HTTP file server integrated with Let's Encrypt, gzip and CORS.

Motivation

Since Dart can run in many native platforms (Linux/x64, macOS/x64/arm64, Windows/x86), it can be an awesome way to have an HTTP Server running in any place, including all the basic features needed today.

API Documentation

See the API Documentation for a full list of functions, classes and extension.

Usage

import 'dart:io';

import 'package:petit_httpd/petit_httpd.dart';

void main() async {
  var petitHTTPD = PetitHTTPD(Directory('/var/www'),
      port: 8080,
      securePort: 443,
      bindingAddress: '0.0.0.0',
      letsEncryptDirectory: Directory('/etc/letsencrypt/live'),
      domains: {'mydomain.com': 'contact@mydomain.com'});

  var ok = await petitHTTPD.start();

  if (!ok) {
    print('** ERROR Starting: $petitHTTPD');
    exit(1);
  }

  print('-- STARTED: $petitHTTPD');
}

CLI Tool

petit_httpd

The petit_httpd is a CLI for the PetitHTTPD class.

First activate the petit_httpd command:

$> dart pub global activate petit_httpd

To run an HTTP Daemon:

$> petit_httpd ./www --port 8080 --secure-port 443 --address 0.0.0.0 --letsencrypt-path /etc/letsencrypt/live --domain mydomain.com

Options:

  • --port: the HTTP port (default: 80).
  • --secure-port: the HTTPS port (default: 443).
  • --address: the binding address (default: localhost).
  • --domain and --email: the domain for Let's Encrypt, and its contact e-mail (default: contact@<domain>).
  • --letsencrypt-path: the Let's Encrypt certificates directory (default: /etc/letsencrypt/live).
  • --cache-control: the Cache-Control header for successful responses.
  • -cors: sets the CORS headers.
  • -force-https: redirects HTTP requests of the domain to HTTPS.
  • -serve-hidden-files: serves hidden files and directories (see below).
  • -verbose: shows the configuration at startup.
  • -version: shows the version and exits.

Hidden files

By default (since 1.3.0) hidden files and directories are not served: a request with any path segment starting with . (such as /.env, /.git/config or /sub/.secret, also when encoded as %2e) gets a 404 Not Found, as if it didn't exist. A visible symbolic link to a hidden entry is not served either.

The exception is /.well-known/ (RFC 8615), used by Let's Encrypt challenges, security.txt and others (PetitHTTPD.allowedHiddenDirectories). Hidden entries inside it (/.well-known/.secret) are still not served.

To serve hidden files (the behaviour before 1.3.0), use serveHiddenFiles: true or the CLI flag -serve-hidden-files.

A BASH script with all together:

#!/bin/bash

dart pub global activate petit_httpd

export PATH="$PATH":"$HOME/.pub-cache/bin"

mkdir -p /var/log/petit_httpd

petit_httpd /var/www --address 0.0.0.0 --letsencrypt-path /etc/letsencrypt/live --domain mydomain.com -verbose  >> /var/log/petit_httpd/requests.log

Source

The official source code is hosted @ GitHub:

Features and bugs

Please file feature requests and bugs at the issue tracker.

Contribution

Any help from the open-source community is always welcome and needed:

  • Found an issue?
    • Please fill a bug report with details.
  • Wish a feature?
    • Open a feature request with use cases.
  • Are you using and liking the project?
    • Promote the project: create an article, do a post or make a donation.
  • Are you a developer?
    • Fix a bug and send a pull request.
    • Implement a new feature, like other training algorithms and activation functions.
    • Improve the Unit Tests.
  • Have you already helped in any way?
    • Many thanks from me, the contributors and everybody that uses this project!

If you donate 1 hour of your time, you can contribute a lot, because others will do the same, just be part and start with your 1 hour.

Author

Graciliano M. Passos: gmpassos@GitHub.

License

Apache License - Version 2.0

Libraries

petit_httpd
PetitHTTPD library.