2013-07-22 14:30:40 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-07-22 14:30:40 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-01-08 15:30:02 +01:00
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
2014-08-02 03:11:45 +02:00
|
|
|
#include <openssl/conf.h>
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
2013-07-22 15:12:54 +02:00
|
|
|
#include "app_helper.h"
|
2013-07-22 14:30:40 +02:00
|
|
|
#include "HttpServer.h"
|
2014-01-08 17:27:56 +01:00
|
|
|
#include "util.h"
|
2014-03-04 15:14:26 +01:00
|
|
|
#include "ssl.h"
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2013-12-08 16:00:12 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int parse_push_config(Config &config, const char *optarg) {
|
2015-02-05 14:48:55 +01:00
|
|
|
const char *eq = strchr(optarg, '=');
|
2014-11-27 15:39:04 +01:00
|
|
|
if (eq == NULL) {
|
2013-12-08 16:00:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-02-05 13:31:16 +01:00
|
|
|
auto &paths = config.push[std::string(optarg, eq)];
|
2013-12-08 16:00:12 +01:00
|
|
|
auto optarg_end = optarg + strlen(optarg);
|
2015-02-05 13:31:16 +01:00
|
|
|
auto i = eq + 1;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2015-02-05 14:48:55 +01:00
|
|
|
const char *j = strchr(i, ',');
|
2014-11-27 15:39:04 +01:00
|
|
|
if (j == NULL) {
|
2013-12-08 16:00:12 +01:00
|
|
|
j = optarg_end;
|
|
|
|
}
|
|
|
|
paths.emplace_back(i, j);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (j == optarg_end) {
|
2013-12-08 16:00:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = j;
|
|
|
|
++i;
|
|
|
|
}
|
2015-02-05 13:31:16 +01:00
|
|
|
|
2013-12-08 16:00:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-02-16 11:39:41 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_version(std::ostream &out) {
|
2014-02-16 11:39:41 +01:00
|
|
|
out << "nghttpd nghttp2/" NGHTTP2_VERSION << std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_usage(std::ostream &out) {
|
2015-01-09 16:37:42 +01:00
|
|
|
out << "Usage: nghttpd [OPTION]... <PORT> [<PRIVATE_KEY> <CERT>]\n"
|
2014-02-16 11:39:41 +01:00
|
|
|
<< "HTTP/2 experimental server" << std::endl;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_help(std::ostream &out) {
|
2013-07-22 14:30:40 +02:00
|
|
|
print_usage(out);
|
2014-03-22 13:24:21 +01:00
|
|
|
out << R"(
|
2015-01-15 16:00:38 +01:00
|
|
|
<PORT> Specify listening port number.
|
|
|
|
<PRIVATE_KEY>
|
|
|
|
Set path to server's private key. Required unless
|
|
|
|
--no-tls is specified.
|
|
|
|
<CERT> Set path to server's certificate. Required unless
|
|
|
|
--no-tls is specified.
|
2014-03-22 13:24:21 +01:00
|
|
|
Options:
|
2015-02-26 16:56:26 +01:00
|
|
|
-a, --address=<ADDR>
|
2015-01-24 21:26:49 +01:00
|
|
|
The address to bind to. If not specified the default IP
|
|
|
|
address determined by getaddrinfo is used.
|
2015-01-15 16:00:38 +01:00
|
|
|
-D, --daemon
|
|
|
|
Run in a background. If -D is used, the current working
|
|
|
|
directory is changed to '/'. Therefore if this option
|
|
|
|
is used, -d option must be specified.
|
2014-03-22 13:24:21 +01:00
|
|
|
-V, --verify-client
|
2015-01-15 16:00:38 +01:00
|
|
|
The server sends a client certificate request. If the
|
|
|
|
client did not return a certificate, the handshake is
|
|
|
|
terminated. Currently, this option just requests a
|
|
|
|
client certificate and does not verify it.
|
2014-03-22 13:24:21 +01:00
|
|
|
-d, --htdocs=<PATH>
|
2015-01-15 16:00:38 +01:00
|
|
|
Specify document root. If this option is not specified,
|
|
|
|
the document root is the current working directory.
|
|
|
|
-v, --verbose
|
|
|
|
Print debug information such as reception/ transmission
|
|
|
|
of frames and name/value pairs.
|
|
|
|
--no-tls Disable SSL/TLS.
|
|
|
|
-c, --header-table-size=<SIZE>
|
|
|
|
Specify decoder header table size.
|
|
|
|
--color Force colored log output.
|
2014-03-22 13:24:21 +01:00
|
|
|
-p, --push=<PATH>=<PUSH_PATH,...>
|
2015-01-15 16:00:38 +01:00
|
|
|
Push resources <PUSH_PATH>s when <PATH> is requested.
|
|
|
|
This option can be used repeatedly to specify multiple
|
|
|
|
push configurations. <PATH> and <PUSH_PATH>s are
|
|
|
|
relative to document root. See --htdocs option.
|
|
|
|
Example: -p/=/foo.png -p/doc=/bar.css
|
|
|
|
-b, --padding=<N>
|
|
|
|
Add at most <N> bytes to a frame payload as padding.
|
|
|
|
Specify 0 to disable padding.
|
|
|
|
-n, --workers=<N>
|
|
|
|
Set the number of worker threads.
|
|
|
|
Default: 1
|
|
|
|
-e, --error-gzip
|
|
|
|
Make error response gzipped.
|
2014-06-28 08:43:06 +02:00
|
|
|
--dh-param-file=<PATH>
|
2015-01-15 16:00:38 +01:00
|
|
|
Path to file that contains DH parameters in PEM format.
|
|
|
|
Without this option, DHE cipher suites are not
|
|
|
|
available.
|
|
|
|
--early-response
|
|
|
|
Start sending response when request HEADERS is received,
|
|
|
|
rather than complete request is received.
|
2015-03-07 09:50:03 +01:00
|
|
|
--trailer=<HEADER>
|
|
|
|
Add a trailer header to a response. <HEADER> must not
|
|
|
|
include pseudo header field (header field name starting
|
|
|
|
with ':'). The trailer is sent only if a response has
|
|
|
|
body part. Example: --trailer 'foo: bar'.
|
2015-03-26 16:36:19 +01:00
|
|
|
--hexdump Display the incoming traffic in hexadecimal (Canonical
|
|
|
|
hex+ASCII display). If SSL/TLS is used, decrypted data
|
|
|
|
are used.
|
2015-01-15 16:00:38 +01:00
|
|
|
--version Display version information and exit.
|
2015-01-15 16:06:47 +01:00
|
|
|
-h, --help Display this help and exit.
|
|
|
|
|
2015-03-30 17:21:52 +02:00
|
|
|
--
|
|
|
|
|
2015-01-15 16:06:47 +01:00
|
|
|
The <SIZE> argument is an integer and an optional unit (e.g., 10K is
|
|
|
|
10 * 1024). Units are K, M and G (powers of 1024).)" << std::endl;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int main(int argc, char **argv) {
|
2013-07-22 14:30:40 +02:00
|
|
|
Config config;
|
2013-11-01 15:06:53 +01:00
|
|
|
bool color = false;
|
2014-11-27 15:39:04 +01:00
|
|
|
while (1) {
|
2014-04-30 15:44:51 +02:00
|
|
|
static int flag = 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
static option long_options[] = {
|
2015-02-23 13:13:03 +01:00
|
|
|
{"address", required_argument, nullptr, 'a'},
|
2014-11-27 15:39:04 +01:00
|
|
|
{"daemon", no_argument, nullptr, 'D'},
|
|
|
|
{"htdocs", required_argument, nullptr, 'd'},
|
|
|
|
{"help", no_argument, nullptr, 'h'},
|
|
|
|
{"verbose", no_argument, nullptr, 'v'},
|
|
|
|
{"verify-client", no_argument, nullptr, 'V'},
|
|
|
|
{"header-table-size", required_argument, nullptr, 'c'},
|
|
|
|
{"push", required_argument, nullptr, 'p'},
|
|
|
|
{"padding", required_argument, nullptr, 'b'},
|
|
|
|
{"workers", required_argument, nullptr, 'n'},
|
|
|
|
{"error-gzip", no_argument, nullptr, 'e'},
|
|
|
|
{"no-tls", no_argument, &flag, 1},
|
|
|
|
{"color", no_argument, &flag, 2},
|
|
|
|
{"version", no_argument, &flag, 3},
|
|
|
|
{"dh-param-file", required_argument, &flag, 4},
|
|
|
|
{"early-response", no_argument, &flag, 5},
|
2015-03-07 09:50:03 +01:00
|
|
|
{"trailer", required_argument, &flag, 6},
|
2015-03-23 18:30:51 +01:00
|
|
|
{"hexdump", no_argument, &flag, 7},
|
2014-11-27 15:39:04 +01:00
|
|
|
{nullptr, 0, nullptr, 0}};
|
2013-07-22 14:30:40 +02:00
|
|
|
int option_index = 0;
|
2015-02-26 15:59:53 +01:00
|
|
|
int c = getopt_long(argc, argv, "DVb:c:d:ehn:p:va:", long_options,
|
|
|
|
&option_index);
|
2013-11-05 15:44:20 +01:00
|
|
|
char *end;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (c == -1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (c) {
|
2015-01-24 21:26:49 +01:00
|
|
|
case 'a':
|
|
|
|
config.address = optarg;
|
|
|
|
break;
|
2013-07-22 14:30:40 +02:00
|
|
|
case 'D':
|
|
|
|
config.daemon = true;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
config.verify_client = true;
|
|
|
|
break;
|
2014-02-07 15:22:17 +01:00
|
|
|
case 'b':
|
2014-02-15 08:40:32 +01:00
|
|
|
config.padding = strtol(optarg, nullptr, 10);
|
2014-02-07 15:22:17 +01:00
|
|
|
break;
|
2013-07-22 14:30:40 +02:00
|
|
|
case 'd':
|
|
|
|
config.htdocs = optarg;
|
|
|
|
break;
|
2014-03-04 15:29:30 +01:00
|
|
|
case 'e':
|
|
|
|
config.error_gzip = true;
|
|
|
|
break;
|
2014-03-04 15:14:26 +01:00
|
|
|
case 'n':
|
2014-04-23 17:47:26 +02:00
|
|
|
#ifdef NOTHREADS
|
2014-11-27 15:39:04 +01:00
|
|
|
std::cerr << "-n: WARNING: Threading disabled at build time, "
|
|
|
|
<< "no threads created." << std::endl;
|
2014-04-23 17:47:26 +02:00
|
|
|
#else
|
2014-03-04 15:14:26 +01:00
|
|
|
errno = 0;
|
|
|
|
config.num_worker = strtoul(optarg, &end, 10);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (errno == ERANGE || *end != '\0' || config.num_worker == 0) {
|
2014-03-04 15:14:26 +01:00
|
|
|
std::cerr << "-n: Bad option value: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-05-14 16:22:23 +02:00
|
|
|
#endif // NOTHREADS
|
2014-03-04 15:14:26 +01:00
|
|
|
break;
|
2013-07-22 14:30:40 +02:00
|
|
|
case 'h':
|
|
|
|
print_help(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'v':
|
|
|
|
config.verbose = true;
|
|
|
|
break;
|
2013-11-05 15:44:20 +01:00
|
|
|
case 'c':
|
2014-01-18 07:32:50 +01:00
|
|
|
errno = 0;
|
2015-01-15 16:06:47 +01:00
|
|
|
config.header_table_size = util::parse_uint_with_unit(optarg);
|
|
|
|
if (config.header_table_size == -1) {
|
2013-11-05 15:44:20 +01:00
|
|
|
std::cerr << "-c: Bad option value: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-08 16:00:12 +01:00
|
|
|
case 'p':
|
2014-11-27 15:39:04 +01:00
|
|
|
if (parse_push_config(config, optarg) != 0) {
|
2013-12-08 16:00:12 +01:00
|
|
|
std::cerr << "-p: Bad option value: " << optarg << std::endl;
|
|
|
|
}
|
|
|
|
break;
|
2013-07-22 14:30:40 +02:00
|
|
|
case '?':
|
2014-01-08 17:27:56 +01:00
|
|
|
util::show_candidates(argv[optind - 1], long_options);
|
2013-07-22 14:30:40 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
case 0:
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (flag) {
|
2013-07-22 14:30:40 +02:00
|
|
|
case 1:
|
|
|
|
// no-tls option
|
|
|
|
config.no_tls = true;
|
|
|
|
break;
|
2013-11-01 15:06:53 +01:00
|
|
|
case 2:
|
|
|
|
// color option
|
|
|
|
color = true;
|
|
|
|
break;
|
2014-02-16 11:39:41 +01:00
|
|
|
case 3:
|
|
|
|
// version
|
|
|
|
print_version(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-06-28 08:43:06 +02:00
|
|
|
case 4:
|
|
|
|
// dh-param-file
|
|
|
|
config.dh_param_file = optarg;
|
|
|
|
break;
|
2014-07-03 15:44:27 +02:00
|
|
|
case 5:
|
|
|
|
// early-response
|
|
|
|
config.early_response = true;
|
|
|
|
break;
|
2015-03-07 09:50:03 +01:00
|
|
|
case 6: {
|
|
|
|
// trailer option
|
|
|
|
auto header = optarg;
|
|
|
|
auto value = strchr(optarg, ':');
|
|
|
|
if (!value) {
|
|
|
|
std::cerr << "--trailer: invalid header: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
*value = 0;
|
|
|
|
value++;
|
|
|
|
while (isspace(*value)) {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
if (*value == 0) {
|
|
|
|
// This could also be a valid case for suppressing a header
|
|
|
|
// similar to curl
|
|
|
|
std::cerr << "--trailer: invalid header - value missing: " << optarg
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
config.trailer.emplace_back(header, value, false);
|
|
|
|
util::inp_strlower(config.trailer.back().name);
|
|
|
|
break;
|
|
|
|
}
|
2015-03-23 18:30:51 +01:00
|
|
|
case 7:
|
|
|
|
// hexdump option
|
|
|
|
config.hexdump = true;
|
|
|
|
break;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (argc - optind < (config.no_tls ? 1 : 3)) {
|
2013-07-22 14:30:40 +02:00
|
|
|
print_usage(std::cerr);
|
|
|
|
std::cerr << "Too few arguments" << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
config.port = strtol(argv[optind++], nullptr, 10);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config.no_tls) {
|
2013-07-22 14:30:40 +02:00
|
|
|
config.private_key_file = argv[optind++];
|
|
|
|
config.cert_file = argv[optind++];
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.daemon) {
|
|
|
|
if (config.htdocs.empty()) {
|
2013-07-22 14:30:40 +02:00
|
|
|
print_usage(std::cerr);
|
|
|
|
std::cerr << "-d option must be specified when -D is used." << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (daemon(0, 0) == -1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
perror("daemon");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config.htdocs.empty()) {
|
2013-07-22 14:30:40 +02:00
|
|
|
config.htdocs = "./";
|
|
|
|
}
|
|
|
|
|
2013-11-01 15:06:53 +01:00
|
|
|
set_color_output(color || isatty(fileno(stdout)));
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &act, nullptr);
|
2014-08-02 03:11:45 +02:00
|
|
|
OPENSSL_config(nullptr);
|
2013-07-22 14:30:40 +02:00
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
2014-04-23 17:47:26 +02:00
|
|
|
#ifndef NOTHREADS
|
2014-10-14 14:47:07 +02:00
|
|
|
ssl::LibsslGlobalLock lock;
|
2014-05-14 16:22:23 +02:00
|
|
|
#endif // NOTHREADS
|
2014-03-04 15:14:26 +01:00
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
reset_timer();
|
|
|
|
|
|
|
|
HttpServer server(&config);
|
2015-02-12 15:18:25 +01:00
|
|
|
if (server.run() != 0) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int main(int argc, char **argv) { return nghttp2::main(argc, argv); }
|