2013-07-22 14:30:40 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2.0 C Library
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#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"
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2013-12-08 16:00:12 +01:00
|
|
|
namespace {
|
|
|
|
int parse_push_config(Config& config, const char *optarg)
|
|
|
|
{
|
|
|
|
const char *eq = strchr(optarg, '=');
|
|
|
|
if(eq == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
auto paths = std::vector<std::string>();
|
|
|
|
auto optarg_end = optarg + strlen(optarg);
|
|
|
|
const char *i = eq + 1;
|
|
|
|
for(;;) {
|
|
|
|
const char *j = strchr(i, ',');
|
|
|
|
if(j == NULL) {
|
|
|
|
j = optarg_end;
|
|
|
|
}
|
|
|
|
paths.emplace_back(i, j);
|
|
|
|
if(j == optarg_end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = j;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
config.push[std::string(optarg, eq)] = std::move(paths);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
namespace {
|
|
|
|
void print_usage(std::ostream& out)
|
|
|
|
{
|
2013-08-18 15:04:09 +02:00
|
|
|
out << "Usage: nghttpd [-DVfhv] [-d <PATH>] [--no-tls] <PORT> [<PRIVATE_KEY> <CERT>]"
|
2013-07-22 14:30:40 +02:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void print_help(std::ostream& out)
|
|
|
|
{
|
|
|
|
print_usage(out);
|
|
|
|
out << "\n"
|
|
|
|
<< "OPTIONS:\n"
|
|
|
|
<< " -D, --daemon Run in a background. If -D is used, the\n"
|
|
|
|
<< " current working directory is changed to '/'.\n"
|
|
|
|
<< " Therefore if this option is used, -d option\n"
|
|
|
|
<< " must be specified.\n"
|
|
|
|
<< " -V, --verify-client\n"
|
|
|
|
<< " The server sends a client certificate\n"
|
|
|
|
<< " request. If the client did not return a\n"
|
|
|
|
<< " certificate, the handshake is terminated.\n"
|
|
|
|
<< " Currently, this option just requests a\n"
|
|
|
|
<< " client certificate and does not verify it.\n"
|
|
|
|
<< " -d, --htdocs=<PATH>\n"
|
|
|
|
<< " Specify document root. If this option is\n"
|
|
|
|
<< " not specified, the document root is the\n"
|
|
|
|
<< " current working directory.\n"
|
|
|
|
<< " -v, --verbose Print debug information such as reception/\n"
|
|
|
|
<< " transmission of frames and name/value pairs.\n"
|
|
|
|
<< " --no-tls Disable SSL/TLS.\n"
|
2013-08-18 15:04:09 +02:00
|
|
|
<< " -f, --no-flow-control\n"
|
|
|
|
<< " Disables connection and stream level flow\n"
|
|
|
|
<< " controls.\n"
|
2013-11-05 15:44:20 +01:00
|
|
|
<< " -c, --header-table-size=<N>\n"
|
|
|
|
<< " Specify decoder header table size.\n"
|
2013-11-01 15:06:53 +01:00
|
|
|
<< " --color Force colored log output.\n"
|
2013-12-08 16:00:12 +01:00
|
|
|
<< " -p, --push=<PATH>=<PUSH_PATH,...>\n"
|
|
|
|
<< " Push resources PUSH_PATHs when PATH is\n"
|
|
|
|
<< " requested. This option can be used\n"
|
|
|
|
<< " repeatedly to specify multiple push\n"
|
|
|
|
<< " configurations. For example,\n"
|
|
|
|
<< " -p/=/foo.png -p/doc=/bar.css\n"
|
|
|
|
<< " PATH and PUSH_PATHs are relative to document\n"
|
|
|
|
<< " root. See --htdocs option.\n"
|
2013-07-22 14:30:40 +02:00
|
|
|
<< " -h, --help Print this help.\n"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Config config;
|
2013-11-01 15:06:53 +01:00
|
|
|
bool color = false;
|
2013-07-22 14:30:40 +02:00
|
|
|
while(1) {
|
2013-09-07 09:38:21 +02:00
|
|
|
int flag = 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
static option long_options[] = {
|
2013-08-18 15:08:40 +02: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'},
|
|
|
|
{"no-flow-control", no_argument, nullptr, 'f'},
|
2013-11-05 15:44:20 +01:00
|
|
|
{"header-table-size", required_argument, nullptr, 'c'},
|
2013-12-08 16:00:12 +01:00
|
|
|
{"push", required_argument, nullptr, 'p'},
|
2013-11-01 15:06:53 +01:00
|
|
|
{"no-tls", no_argument, &flag, 1},
|
|
|
|
{"color", no_argument, &flag, 2},
|
2013-08-18 15:08:40 +02:00
|
|
|
{nullptr, 0, nullptr, 0}
|
2013-07-22 14:30:40 +02:00
|
|
|
};
|
|
|
|
int option_index = 0;
|
2013-12-08 16:00:12 +01:00
|
|
|
int c = getopt_long(argc, argv, "DVc:d:fhp:v", long_options, &option_index);
|
2013-11-05 15:44:20 +01:00
|
|
|
char *end;
|
2013-07-22 14:30:40 +02:00
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(c) {
|
|
|
|
case 'D':
|
|
|
|
config.daemon = true;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
config.verify_client = true;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
config.htdocs = optarg;
|
|
|
|
break;
|
2013-07-28 12:28:41 +02:00
|
|
|
case 'f':
|
2013-08-18 15:04:09 +02:00
|
|
|
config.no_flow_control = true;
|
2013-07-28 12:28:41 +02: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':
|
|
|
|
config.header_table_size = strtol(optarg, &end, 10);
|
|
|
|
if(errno == ERANGE || *end != '\0') {
|
|
|
|
std::cerr << "-c: Bad option value: " << optarg << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-08 16:00:12 +01:00
|
|
|
case 'p':
|
|
|
|
if(parse_push_config(config, optarg) != 0) {
|
|
|
|
std::cerr << "-p: Bad option value: " << optarg << std::endl;
|
|
|
|
}
|
|
|
|
break;
|
2013-07-22 14:30:40 +02:00
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
case 0:
|
|
|
|
switch(flag) {
|
|
|
|
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;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(argc - optind < (config.no_tls ? 1 : 3)) {
|
|
|
|
print_usage(std::cerr);
|
|
|
|
std::cerr << "Too few arguments" << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
config.port = strtol(argv[optind++], nullptr, 10);
|
|
|
|
|
|
|
|
if(!config.no_tls) {
|
|
|
|
config.private_key_file = argv[optind++];
|
|
|
|
config.cert_file = argv[optind++];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(config.daemon) {
|
|
|
|
if(config.htdocs.empty()) {
|
|
|
|
print_usage(std::cerr);
|
|
|
|
std::cerr << "-d option must be specified when -D is used." << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if(daemon(0, 0) == -1) {
|
|
|
|
perror("daemon");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(config.htdocs.empty()) {
|
|
|
|
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);
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
reset_timer();
|
|
|
|
config.on_request_recv_callback = htdocs_on_request_recv_callback;
|
|
|
|
|
|
|
|
HttpServer server(&config);
|
|
|
|
server.run();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return nghttp2::main(argc, argv);
|
|
|
|
}
|