httpsrv.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #ifdef WIN32
  11. #include <winsock2.h>
  12. #endif
  13. #include <stdint.h>
  14. #ifndef WIN32
  15. #include <sys/types.h>
  16. #include <sys/select.h>
  17. #include <sys/socket.h>
  18. #endif
  19. #include <microhttpd.h>
  20. #include "logging.h"
  21. #include "util.h"
  22. static struct MHD_Daemon *httpsrv;
  23. extern int handle_getwork(struct MHD_Connection *, bytes_t *);
  24. void httpsrv_prepare_resp(struct MHD_Response *resp)
  25. {
  26. MHD_add_response_header(resp, MHD_HTTP_HEADER_SERVER, PACKAGE"/"VERSION" getwork server");
  27. }
  28. static
  29. int httpsrv_handle_req(struct MHD_Connection *conn, const char *url, const char *method, bytes_t *upbuf)
  30. {
  31. return handle_getwork(conn, upbuf);
  32. }
  33. static
  34. int httpsrv_handle_access(void *cls, struct MHD_Connection *conn, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
  35. {
  36. bytes_t *upbuf;
  37. if (!*con_cls)
  38. {
  39. *con_cls = upbuf = malloc(sizeof(bytes_t));
  40. bytes_init(upbuf);
  41. return MHD_YES;
  42. }
  43. upbuf = *con_cls;
  44. if (*upload_data_size)
  45. {
  46. bytes_append(upbuf, upload_data, *upload_data_size);
  47. *upload_data_size = 0;
  48. return MHD_YES;
  49. }
  50. return httpsrv_handle_req(conn, url, method, *con_cls);
  51. }
  52. static
  53. void httpsrv_cleanup_request(void *cls, struct MHD_Connection *conn, void **con_cls, enum MHD_RequestTerminationCode toe)
  54. {
  55. if (*con_cls)
  56. {
  57. bytes_t *upbuf = *con_cls;
  58. bytes_free(upbuf);
  59. free(upbuf);
  60. *con_cls = NULL;
  61. }
  62. }
  63. static
  64. void httpsrv_log(void *arg, const char *fmt, va_list ap)
  65. {
  66. if (!opt_debug)
  67. return;
  68. char tmp42[LOGBUFSIZ] = "HTTPSrv: ";
  69. vsnprintf(&tmp42[9], sizeof(tmp42)-9, fmt, ap);
  70. _applog(LOG_DEBUG, tmp42);
  71. }
  72. void httpsrv_start(unsigned short port)
  73. {
  74. httpsrv = MHD_start_daemon(
  75. MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
  76. port, NULL, NULL,
  77. &httpsrv_handle_access, NULL,
  78. MHD_OPTION_NOTIFY_COMPLETED, &httpsrv_cleanup_request, NULL,
  79. MHD_OPTION_EXTERNAL_LOGGER, &httpsrv_log, NULL,
  80. MHD_OPTION_END);
  81. if (httpsrv)
  82. applog(LOG_NOTICE, "HTTP server listening on port %d", (int)port);
  83. else
  84. applog(LOG_ERR, "Failed to start HTTP server on port %d", (int)port);
  85. }
  86. void httpsrv_stop()
  87. {
  88. if (!httpsrv)
  89. return;
  90. applog(LOG_DEBUG, "Stopping HTTP server");
  91. MHD_stop_daemon(httpsrv);
  92. httpsrv = NULL;
  93. }