int main(int argc, char *argv[]) {
  struct ns_mgr mgr;
  struct ns_connection *nc;
  int i;
  char *cp;

  ns_mgr_init(&mgr, NULL);
  nc = ns_bind(&mgr, s_http_port, ev_handler);
  ns_set_protocol_http_websocket(nc);
  s_http_server_opts.document_root = ".";
  s_http_server_opts.enable_directory_listing = "yes";

  /* Use current binary directory as document root */
  if (argc > 0 && ((cp = strrchr(argv[0], '/')) != NULL ||
      (cp = strrchr(argv[0], '/')) != NULL)) {
    *cp = '\0';
    s_http_server_opts.document_root = argv[0];
  }

  /* Process command line options to customize HTTP server */
  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-D") == 0 && i + 1 < argc) {
      mgr.hexdump_file = argv[++i];
    } else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) {
      s_http_server_opts.document_root = argv[++i];
    } else if (strcmp(argv[i], "-a") == 0 && i + 1 < argc) {
      s_http_server_opts.auth_domain = argv[++i];
    } else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) {
      s_http_server_opts.global_auth_file = argv[++i];
    } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
      s_http_server_opts.per_directory_auth_file = argv[++i];
    } else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
      s_http_server_opts.url_rewrites = argv[++i];
#ifndef NS_DISABLE_CGI
    } else if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) {
      s_http_server_opts.cgi_interpreter = argv[++i];
#endif
#ifdef NS_ENABLE_SSL
    } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) {
      const char *ssl_cert = argv[++i];
      const char *err_str = ns_set_ssl(nc, ssl_cert, NULL);
      if (err_str != NULL) {
        fprintf(stderr, "Error loading SSL cert: %s\n", err_str);
        exit(1);
      }
#endif
    }
  }

  printf("Starting RESTful server on port %s\n", s_http_port);
  for (;;) {
    ns_mgr_poll(&mgr, 1000);
  }
  ns_mgr_free(&mgr);

  return 0;
}
Пример #2
0
static const char *test_mgr_with_ssl(int use_ssl) {
  char addr[100] = "127.0.0.1:0", ip[sizeof(addr)], buf[100] = "";
  struct ns_mgr mgr;
  struct ns_connection *nc;
  int port, port2;
#ifndef NS_ENABLE_SSL
  (void)use_ssl;
#endif

  ns_mgr_init(&mgr, NULL);
  /* mgr.hexdump_file = "/dev/stdout"; */

  ASSERT((nc = ns_bind(&mgr, addr, eh1)) != NULL);
  port2 = htons(nc->sa.sin.sin_port);
  ASSERT(port2 > 0);
#ifdef NS_ENABLE_SSL
  if (use_ssl) {
    ASSERT(ns_set_ssl(nc, S_PEM, CA_PEM) == NULL);
  }
#endif

  ns_sock_to_str(nc->sock, addr, sizeof(addr), 3);
  ASSERT(sscanf(addr, "%[^:]:%d", ip, &port) == 2);
  ASSERT(strcmp(ip, "127.0.0.1") == 0);
  ASSERT(port == port2);

  ASSERT((nc = ns_connect(&mgr, addr, eh1)) != NULL);
#ifdef NS_ENABLE_SSL
  if (use_ssl) {
    ASSERT(ns_set_ssl(nc, C_PEM, CA_PEM) == NULL);
  }
#endif
  nc->user_data = buf;
  poll_mgr(&mgr, 50);

  ASSERT(strcmp(buf, "ok!") == 0);

  ns_mgr_free(&mgr);
  return NULL;
}
Пример #3
0
/*
* Construct a new WebSocket object:
*
* url: url where to connect to
* protocol: websocket subprotocol
*
* Example:
* ws = new WebSocket('wss://localhost:1234');
* ws.onopen = function(ev) {
*     print("ON OPEN", ev);
* }
*
* ws.onclose = function(ev) {
*     print("ON CLOSE", ev);
* }
*
* ws.onmessage = function(ev) {
*     print("ON MESSAGE", ev);
* }
*
* ws.onerror = function(ev) {
*     print("ON ERROR", ev);
* }
*
*/
static v7_val_t sj_ws_ctor(struct v7 *v7, v7_val_t this_obj, v7_val_t args) {
  struct ns_connection *nc;
  struct user_data *ud;
  v7_val_t urlv = v7_array_get(v7, args, 0);
  v7_val_t subprotov = v7_array_get(v7, args, 1);
  (void) this_obj;
  (void) args;

  if (!v7_is_string(urlv)) {
    v7_throw(v7, "invalid ws url string");
  }

  if (v7_is_object(this_obj) && this_obj != v7_get_global_object(v7)) {
    int use_ssl = 0;
    size_t len;
    const char *url = v7_to_string(v7, &urlv, &len);

    if (strncmp(url, "ws://", 5) == 0) {
      url += 5;
    } else if (strncmp(url, "wss://", 6) == 0) {
      url += 6;
      use_ssl = 1;
    }

    nc = ns_connect(&sj_mgr, url, ws_ev_handler);
    if (nc == NULL) v7_throw(v7, "error creating the connection");
#ifdef NS_ENABLE_SSL
    if (use_ssl) {
      ns_set_ssl(nc, NULL, NULL);
    }
#endif

    ns_set_protocol_http_websocket(nc);

    ud = calloc(1, sizeof(*ud));
    ud->v7 = v7;
    ud->ws = this_obj;
    nc->user_data = ud;
    v7_own(v7, &ud->ws);

    if (v7_is_string(subprotov)) {
      size_t len;
      const char *proto = v7_to_string(v7, &subprotov, &len);
      ud->proto = strdup(proto);
    }

  } else {
    v7_throw(v7, "WebSocket ctor called without new");
  }

  return v7_create_undefined();
}
Пример #4
0
int main(int argc, char *argv[]) {
    struct ns_mgr mgr;
    struct ns_connection *nc;
    int i, redirect = 0;
    const char *vhost = NULL;

    ns_mgr_init(&mgr, NULL);

    /* Parse command line arguments */
    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-D") == 0) {
            mgr.hexdump_file = argv[i + 1];
            i++;
        } else if (strcmp(argv[i], "-k") == 0) {
            s_backend_keepalive = 1;
        } else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
            if (strcmp(argv[i + 1], "-") == 0) {
                s_log_file = stdout;
            } else {
                s_log_file = fopen(argv[i + 1], "a");
                if (s_log_file == NULL) {
                    perror("fopen");
                    exit(EXIT_FAILURE);
                }
            }
            i++;
        } else if (strcmp(argv[i], "-p") == 0) {
            s_http_port = argv[i + 1];
            i++;
        } else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
            redirect = 1;
        } else if (strcmp(argv[i], "-v") == 0 && i + 1 < argc) {
            if (strcmp(argv[i + 1], "") == 0) {
                vhost = NULL;
            } else {
                vhost = argv[i + 1];
            }
            i++;
        } else if (strcmp(argv[i], "-b") == 0 && i + 2 < argc) {
            struct http_backend *be =
                    vhost != NULL ? &s_vhost_backends[s_num_vhost_backends++]
                    : &s_default_backends[s_num_default_backends++];
            STAILQ_INIT(&be->conns);
            char *r = NULL;
            be->vhost = vhost;
            be->uri_prefix = argv[i + 1];
            be->host_port = argv[i + 2];
            be->redirect = redirect;
            be->uri_prefix_replacement = be->uri_prefix;
            if ((r = strchr(be->uri_prefix, '=')) != NULL) {
                *r = '\0';
                be->uri_prefix_replacement = r + 1;
            }
            printf(
                "Adding backend for %s%s : %s "
                "[redirect=%d,prefix_replacement=%s]\n",
                be->vhost == NULL ? "" : be->vhost, be->uri_prefix, be->host_port,
                be->redirect, be->uri_prefix_replacement);
            vhost = NULL;
            redirect = 0;
            i += 2;
#ifdef NS_ENABLE_SSL
        } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) {
            s_ssl_cert = argv[++i];
#endif
        } else {
            print_usage_and_exit(argv[0]);
        }
    }

    /* Open listening socket */
    if ((nc = ns_bind(&mgr, s_http_port, ev_handler)) == NULL) {
        fprintf(stderr, "ns_bind(%s) failed\n", s_http_port);
        exit(EXIT_FAILURE);
    }

#if NS_ENABLE_SSL
    if (s_ssl_cert != NULL) {
        const char *err_str = ns_set_ssl(nc, s_ssl_cert, NULL);
        if (err_str != NULL) {
            fprintf(stderr, "Error loading SSL cert: %s\n", err_str);
            exit(1);
        }
    }
#endif
    ns_set_protocol_http_websocket(nc);

    if (s_num_vhost_backends + s_num_default_backends == 0) {
        print_usage_and_exit(argv[0]);
    }

    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    /* Run event loop until signal is received */
    printf("Starting LB on port %s\n", s_http_port);
    while (s_sig_num == 0) {
        ns_mgr_poll(&mgr, 1000);
    }

    /* Cleanup */
    ns_mgr_free(&mgr);

    printf("Exiting on signal %d\n", s_sig_num);

    return EXIT_SUCCESS;
}
Пример #5
0
int main(int argc, char *argv[]) {
  struct ns_mgr mgr;
  struct ns_connection *nc;
  int i;
  int redirect;
  const char *vhost = NULL;

  ns_mgr_init(&mgr, NULL);

  /* Parse command line arguments */
  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-D") == 0) {
      mgr.hexdump_file = argv[i + 1];
      i++;
    } else if (strcmp(argv[i], "-p") == 0) {
      s_http_port = argv[i + 1];
      i++;
    } else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
      redirect = 1;
      i++;
    } else if (strcmp(argv[i], "-v") == 0 && i + 1 < argc) {
      vhost = argv[i + 1];
      i++;
    } else if (strcmp(argv[i], "-b") == 0 && i + 2 < argc) {
      s_http_backends[s_num_http_backends].vhost = vhost;
      s_http_backends[s_num_http_backends].uri_prefix = argv[i + 1];
      s_http_backends[s_num_http_backends].host_port = argv[i + 2];
      s_http_backends[s_num_http_backends].redirect = redirect;
      s_num_http_backends++;
      vhost = NULL;
      redirect = 0;
      i += 2;
#ifdef NS_ENABLE_SSL
    } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) {
      s_ssl_cert = argv[++i];
#endif
    }
  }

  /* Open listening socket */
  if ((nc = ns_bind(&mgr, s_http_port, ev_handler)) == NULL) {
    fprintf(stderr, "ns_bind(%s) failed\n", s_http_port);
    exit(EXIT_FAILURE);
  }

#if NS_ENABLE_SSL
  if (s_ssl_cert != NULL) {
    const char *err_str = ns_set_ssl(nc, s_ssl_cert, NULL);
    if (err_str != NULL) {
      fprintf(stderr, "Error loading SSL cert: %s\n", err_str);
      exit(1);
    }
  }
#endif

  if (s_num_http_backends == 0) {
    fprintf(stderr, "Usage: %s [-D debug_dump_file] [-p http_port] "
#if NS_ENABLE_SSL
            "[-s ssl_cert] "
#endif
            "<[-r] [-v vhost] -b uri_prefix host_port> ... \n", argv[0]);
    exit(EXIT_FAILURE);
  }

  signal(SIGINT, signal_handler);
  signal(SIGTERM, signal_handler);

  /* Run event loop until signal is received */
  printf("Starting LB on port %s\n", s_http_port);
  while (s_sig_num == 0) {
    ns_mgr_poll(&mgr, 1000);
  }

  /* Cleanup */
  ns_mgr_free(&mgr);

  printf("Exiting on signal %d\n", s_sig_num);

  return EXIT_SUCCESS;
}