shrpx_config.cc 8.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Spdylay - SPDY 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.
 */
#include "shrpx_config.h"

27 28 29 30 31 32 33 34
#include <pwd.h>
#include <netdb.h>

#include <cstring>
#include <cerrno>
#include <limits>
#include <fstream>

35
#include "shrpx_log.h"
36 37 38 39
#include "util.h"

using namespace spdylay;

40 41
namespace shrpx {

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
const char SHRPX_OPT_PRIVATE_KEY_FILE[] = "private-key-file";
const char SHRPX_OPT_CERTIFICATE_FILE[] = "certificate-file";

const char SHRPX_OPT_BACKEND[] = "backend";
const char SHRPX_OPT_FRONTEND[] = "frontend";
const char SHRPX_OPT_WORKERS[] = "workers";
const char
SHRPX_OPT_SPDY_MAX_CONCURRENT_STREAMS[] = "spdy-max-concurrent-streams";
const char SHRPX_OPT_LOG_LEVEL[] = "log-level";
const char SHRPX_OPT_DAEMON[] = "daemon";
const char SHRPX_OPT_SPDY_PROXY[] = "spdy-proxy";
const char SHRPX_OPT_ADD_X_FORWARDED_FOR[] = "add-x-forwarded-for";
const char
SHRPX_OPT_FRONTEND_SPDY_READ_TIMEOUT[] = "frontend-spdy-read-timeout";
const char SHRPX_OPT_FRONTEND_READ_TIMEOUT[] = "frontend-read-timeout";
const char SHRPX_OPT_FRONTEND_WRITE_TIMEOUT[] = "frontend-write-timeout";
const char SHRPX_OPT_BACKEND_READ_TIMEOUT[] = "backend-read-timeout";
const char SHRPX_OPT_BACKEND_WRITE_TIMEOUT[] = "backend-write-timeout";
const char SHRPX_OPT_ACCESSLOG[] = "accesslog";
const char
SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT[] = "backend-keep-alive-timeout";
const char SHRPX_OPT_FRONTEND_SPDY_WINDOW_BITS[] = "frontend-spdy-window-bits";
const char SHRPX_OPT_PID_FILE[] = "pid-file";
const char SHRPX_OPT_USER[] = "user";

67 68 69 70 71 72 73 74 75 76 77 78
Config::Config()
  : verbose(false),
    daemon(false),
    host(0),
    port(0),
    private_key_file(0),
    cert_file(0),
    verify_client(false),
    server_name(0),
    downstream_host(0),
    downstream_port(0),
    downstream_hostport(0),
79
    downstream_addrlen(0),
80
    num_worker(0),
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
81
    spdy_max_concurrent_streams(0),
82
    spdy_proxy(false),
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
83
    add_x_forwarded_for(false),
84
    accesslog(false),
85 86 87
    spdy_upstream_window_bits(0),
    pid_file(0),
    uid(0),
88 89
    gid(0),
    conf_path(0)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
{}

namespace {
Config *config = 0;
} // namespace

const Config* get_config()
{
  return config;
}

Config* mod_config()
{
  return config;
}

void create_config()
{
  config = new Config();
}

111 112 113 114 115 116 117
namespace {
int split_host_port(char *host, size_t hostlen, uint16_t *port_ptr,
                    const char *hostport)
{
  // host and port in |hostport| is separated by single ','.
  const char *p = strchr(hostport, ',');
  if(!p) {
118
    LOG(ERROR) << "Invalid host, port: " << hostport;
119 120 121 122
    return -1;
  }
  size_t len = p-hostport;
  if(hostlen < len+1) {
123
    LOG(ERROR) << "Hostname too long: " << hostport;
124 125 126 127 128 129 130 131 132 133 134
    return -1;
  }
  memcpy(host, hostport, len);
  host[len] = '\0';

  errno = 0;
  unsigned long d = strtoul(p+1, 0, 10);
  if(errno == 0 && 1 <= d && d <= std::numeric_limits<uint16_t>::max()) {
    *port_ptr = d;
    return 0;
  } else {
135
    LOG(ERROR) << "Port is invalid: " << p+1;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    return -1;
  }
}
} // namespace

void set_config_str(char **destp, const char *val)
{
  if(*destp) {
    free(*destp);
  }
  *destp = strdup(val);
}

int parse_config(const char *opt, const char *optarg)
{
  char host[NI_MAXHOST];
  uint16_t port;
  if(util::strieq(opt, SHRPX_OPT_BACKEND)) {
    if(split_host_port(host, sizeof(host), &port, optarg) == -1) {
      return -1;
    } else {
      set_config_str(&mod_config()->downstream_host, host);
      mod_config()->downstream_port = port;
    }
  } else if(util::strieq(opt, SHRPX_OPT_FRONTEND)) {
    if(split_host_port(host, sizeof(host), &port, optarg) == -1) {
      return -1;
    } else {
      set_config_str(&mod_config()->host, host);
      mod_config()->port = port;
    }
  } else if(util::strieq(opt, SHRPX_OPT_WORKERS)) {
    mod_config()->num_worker = strtol(optarg, 0, 10);
  } else if(util::strieq(opt, SHRPX_OPT_SPDY_MAX_CONCURRENT_STREAMS)) {
    mod_config()->spdy_max_concurrent_streams = strtol(optarg, 0, 10);
  } else if(util::strieq(opt, SHRPX_OPT_LOG_LEVEL)) {
    if(Log::set_severity_level_by_name(optarg) == -1) {
173
      LOG(ERROR) << "Invalid severity level: " << optarg;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      return -1;
    }
  } else if(util::strieq(opt, SHRPX_OPT_DAEMON)) {
    mod_config()->daemon = util::strieq(optarg, "yes");
  } else if(util::strieq(opt, SHRPX_OPT_SPDY_PROXY)) {
    mod_config()->spdy_proxy = util::strieq(optarg, "yes");
  } else if(util::strieq(opt, SHRPX_OPT_ADD_X_FORWARDED_FOR)) {
    mod_config()->add_x_forwarded_for = util::strieq(optarg, "yes");
  } else if(util::strieq(opt, SHRPX_OPT_FRONTEND_SPDY_READ_TIMEOUT)) {
    timeval tv = {strtol(optarg, 0, 10), 0};
    mod_config()->spdy_upstream_read_timeout = tv;
  } else if(util::strieq(opt, SHRPX_OPT_FRONTEND_READ_TIMEOUT)) {
    timeval tv = {strtol(optarg, 0, 10), 0};
    mod_config()->upstream_read_timeout = tv;
  } else if(util::strieq(opt, SHRPX_OPT_FRONTEND_WRITE_TIMEOUT)) {
    timeval tv = {strtol(optarg, 0, 10), 0};
    mod_config()->upstream_write_timeout = tv;
  } else if(util::strieq(opt, SHRPX_OPT_BACKEND_READ_TIMEOUT)) {
    timeval tv = {strtol(optarg, 0, 10), 0};
    mod_config()->downstream_read_timeout = tv;
  } else if(util::strieq(opt, SHRPX_OPT_BACKEND_WRITE_TIMEOUT)) {
    timeval tv = {strtol(optarg, 0, 10), 0};
    mod_config()->downstream_write_timeout = tv;
  } else if(util::strieq(opt, SHRPX_OPT_ACCESSLOG)) {
    mod_config()->accesslog = util::strieq(optarg, "yes");
  } else if(util::strieq(opt, SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT)) {
    timeval tv = {strtol(optarg, 0, 10), 0};
    mod_config()->downstream_idle_read_timeout = tv;
  } else if(util::strieq(opt, SHRPX_OPT_FRONTEND_SPDY_WINDOW_BITS)) {
    errno = 0;
    unsigned long int n = strtoul(optarg, 0, 10);
    if(errno == 0 && n < 31) {
      mod_config()->spdy_upstream_window_bits = n;
    } else {
208
      LOG(ERROR) << "-w: specify the integer in the range [0, 30], inclusive";
209 210 211 212 213 214 215
      return -1;
    }
  } else if(util::strieq(opt, SHRPX_OPT_PID_FILE)) {
    set_config_str(&mod_config()->pid_file, optarg);
  } else if(util::strieq(opt, SHRPX_OPT_USER)) {
    passwd *pwd = getpwnam(optarg);
    if(pwd == 0) {
216 217
      LOG(ERROR) << "--user: failed to get uid from " << optarg
                 << ": " << strerror(errno);
218 219 220 221 222 223 224 225 226
      return -1;
    }
    mod_config()->uid = pwd->pw_uid;
    mod_config()->gid = pwd->pw_gid;
  } else if(util::strieq(opt, SHRPX_OPT_PRIVATE_KEY_FILE)) {
    set_config_str(&mod_config()->private_key_file, optarg);
  } else if(util::strieq(opt, SHRPX_OPT_CERTIFICATE_FILE)) {
    set_config_str(&mod_config()->cert_file, optarg);
  } else if(util::strieq(opt, "conf")) {
227
    LOG(WARNING) << "conf is ignored";
228
  } else {
229
    LOG(ERROR) << "Unknown option: " << opt;
230 231 232 233 234 235 236 237 238
    return -1;
  }
  return 0;
}

int load_config(const char *filename)
{
  std::ifstream in(filename, std::ios::binary);
  if(!in) {
239
    LOG(ERROR) << "Could not open config file " << filename;
240 241 242 243 244 245 246 247 248 249 250 251 252
    return -1;
  }
  std::string line;
  int linenum = 0;
  while(std::getline(in, line)) {
    ++linenum;
    if(line.empty() || line[0] == '#') {
      continue;
    }
    size_t i;
    size_t size = line.size();
    for(i = 0; i < size && line[i] != '='; ++i);
    if(i == size) {
253
      LOG(ERROR) << "Bad configuration format at line " << linenum;
254 255 256 257 258 259 260 261 262 263 264
      return -1;
    }
    line[i] = '\0';
    const char *s = line.c_str();
    if(parse_config(s, s+i+1) == -1) {
      return -1;
    }
  }
  return 0;
}

265
} // namespace shrpx