web.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "web.h"
  2. #include "mongoose.h"
  3. #include "thread.h"
  4. #include "cfg.h"
  5. typedef void (*http_fn_t)(void *arg);
  6. #ifdef USE_MG714
  7. typedef struct mg_tls_opts mg_tls_t;
  8. #else
  9. typedef struct mg_serve_http_opts mg_opts_t;
  10. #define MG_PATH_MAX 128
  11. #endif
  12. typedef struct {
  13. struct mg_connection *c;
  14. int ev;
  15. void *ev_data;
  16. }mg_stream_t;
  17. typedef struct {
  18. pthread_t tid;
  19. struct mg_mgr mgr;
  20. #ifdef USE_MG714
  21. mg_tls_t tls;
  22. #else
  23. mg_opts_t opts;
  24. #endif
  25. char rootDir[MG_PATH_MAX];
  26. }web_handle_t;
  27. static web_handle_t webHandle={0};
  28. static void http_fn(struct mg_connection *c, int ev, void *ev_data)
  29. {
  30. int i,j,flag=0;
  31. web_handle_t *h=&webHandle;
  32. mg_stream_t s={c,ev,ev_data};
  33. #ifdef USE_MG714
  34. if (ev == MG_EV_HTTP_MSG) {
  35. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  36. mg_http_serve_dir(c, ev_data, &opts);
  37. }
  38. else if(ev == MG_EV_ACCEPT) {
  39. if (c->fn_data) {
  40. mg_tls_init(c, &h->tls);
  41. }
  42. }
  43. #else
  44. if (ev == MG_EV_HTTP_REQUEST) {
  45. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  46. }
  47. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  48. //
  49. //
  50. }
  51. else {
  52. //
  53. }
  54. #endif
  55. }
  56. static void *web_thread(void *arg)
  57. {
  58. struct mg_connection *nc;
  59. const char *http_addr="http://[::]:80";
  60. const char *https_addr="https://[::]:443";
  61. thread_handle_t *th=(thread_handle_t*)arg;
  62. web_handle_t *h=(web_handle_t*)th->arg;
  63. #ifdef USE_MG714
  64. //mg_log_set(MG_LL_DEBUG);
  65. mg_mgr_init(&mgr);
  66. //sys_get_path(mh->rootDir, WEB_ROOT);
  67. strcpy(h->rootDir, "/web");
  68. if (mg_http_listen(&mgr, http_addr, http_fn, NULL) == NULL) {
  69. MG_ERROR(("cannot listen on %s\n", http_addr));
  70. return NULL;
  71. }
  72. if (mg_http_listen(&mgr, https_addr, http_fn, (void*)1) == NULL) {
  73. MG_ERROR(("cannot listen on %s\n", https_addr));
  74. return NULL;
  75. }
  76. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  77. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  78. //mh->tls.name = mg_url_host(https_addr);
  79. #else
  80. mg_mgr_init(&h->mgr, NULL);
  81. nc = mg_bind(&h->mgr, "80", http_fn);
  82. strcpy(h->rootDir, "/web");
  83. mg_set_protocol_http_websocket(nc);
  84. h->opts.document_root = h->rootDir;
  85. h->opts.enable_directory_listing = "yes";
  86. #endif
  87. while(th->quit==0) {
  88. mg_mgr_poll(&h->mgr, 200);
  89. }
  90. mg_mgr_free(&h->mgr);
  91. pthread_exit(NULL);
  92. }
  93. int web_init(void)
  94. {
  95. web_handle_t *h=&webHandle;
  96. thread_start(THREAD_ID_WEB, web_thread, h);
  97. return 0;
  98. }
  99. int web_send(void *data, int len, int is_bin)
  100. {
  101. struct mg_connection *c;
  102. web_handle_t *h=&webHandle;
  103. for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
  104. mg_send_websocket_frame(c, is_bin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, data, len);
  105. }
  106. return 0;
  107. }