minimal-ws-proxy.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * lws-minimal-ws-proxy
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * This demonstrates the most minimal http server you can make with lws,
  10. * with an added websocket proxy distributing what is received on a
  11. * dumb-increment wss connection to https://libwebsockets.org to all
  12. * browsers connected to this server.
  13. *
  14. * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of
  15. * the directory it was started in.
  16. * You can change that by changing mount.origin.
  17. */
  18. #include <libwebsockets.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #define LWS_PLUGIN_STATIC
  22. #include "protocol_lws_minimal.c"
  23. static struct lws_protocols protocols[] = {
  24. { "http", lws_callback_http_dummy, 0, 0 },
  25. LWS_PLUGIN_PROTOCOL_MINIMAL,
  26. { NULL, NULL, 0, 0 } /* terminator */
  27. };
  28. static int interrupted;
  29. static const struct lws_http_mount mount = {
  30. /* .mount_next */ NULL, /* linked-list "next" */
  31. /* .mountpoint */ "/", /* mountpoint URL */
  32. /* .origin */ "./mount-origin", /* serve from dir */
  33. /* .def */ "index.html", /* default filename */
  34. /* .protocol */ NULL,
  35. /* .cgienv */ NULL,
  36. /* .extra_mimetypes */ NULL,
  37. /* .interpret */ NULL,
  38. /* .cgi_timeout */ 0,
  39. /* .cache_max_age */ 0,
  40. /* .auth_mask */ 0,
  41. /* .cache_reusable */ 0,
  42. /* .cache_revalidate */ 0,
  43. /* .cache_intermediaries */ 0,
  44. /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
  45. /* .mountpoint_len */ 1, /* char count */
  46. /* .basic_auth_login_file */ NULL,
  47. };
  48. void sigint_handler(int sig)
  49. {
  50. interrupted = 1;
  51. }
  52. int main(int argc, const char **argv)
  53. {
  54. struct lws_context_creation_info info;
  55. struct lws_context *context;
  56. const char *p;
  57. int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
  58. /* for LLL_ verbosity above NOTICE to be built into lws,
  59. * lws must have been configured and built with
  60. * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
  61. /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
  62. /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
  63. /* | LLL_DEBUG */;
  64. signal(SIGINT, sigint_handler);
  65. if ((p = lws_cmdline_option(argc, argv, "-d")))
  66. logs = atoi(p);
  67. lws_set_log_level(logs, NULL);
  68. lwsl_user("LWS minimal ws proxy | visit http://localhost:7681\n");
  69. memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
  70. info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
  71. LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
  72. info.port = 7681;
  73. info.mounts = &mount;
  74. info.protocols = protocols;
  75. context = lws_create_context(&info);
  76. if (!context) {
  77. lwsl_err("lws init failed\n");
  78. return 1;
  79. }
  80. while (n >= 0 && !interrupted)
  81. n = lws_service(context, 0);
  82. lws_context_destroy(context);
  83. return 0;
  84. }