httpsrv.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2013-2014 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 <stdarg.h>
  14. #include <stddef.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #ifndef WIN32
  19. #include <sys/types.h>
  20. #include <sys/select.h>
  21. #include <sys/socket.h>
  22. #endif
  23. #include <microhttpd.h>
  24. #include "logging.h"
  25. #include "miner.h"
  26. #include "util.h"
  27. static struct MHD_Daemon *httpsrv;
  28. extern int handle_getwork(struct MHD_Connection *, bytes_t *);
  29. void httpsrv_prepare_resp(struct MHD_Response *resp)
  30. {
  31. MHD_add_response_header(resp, MHD_HTTP_HEADER_SERVER, bfgminer_name_slash_ver);
  32. }
  33. static
  34. int httpsrv_handle_req(struct MHD_Connection *conn, const char *url, const char *method, bytes_t *upbuf)
  35. {
  36. return handle_getwork(conn, upbuf);
  37. }
  38. static
  39. #if MHD_VERSION < 0x00097002
  40. int
  41. #else
  42. enum MHD_Result
  43. #endif
  44. 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)
  45. {
  46. bytes_t *upbuf;
  47. if (!*con_cls)
  48. {
  49. *con_cls = upbuf = malloc(sizeof(bytes_t));
  50. bytes_init(upbuf);
  51. return MHD_YES;
  52. }
  53. upbuf = *con_cls;
  54. if (*upload_data_size)
  55. {
  56. bytes_append(upbuf, upload_data, *upload_data_size);
  57. *upload_data_size = 0;
  58. return MHD_YES;
  59. }
  60. return httpsrv_handle_req(conn, url, method, *con_cls);
  61. }
  62. static
  63. void httpsrv_cleanup_request(void *cls, struct MHD_Connection *conn, void **con_cls, enum MHD_RequestTerminationCode toe)
  64. {
  65. if (*con_cls)
  66. {
  67. bytes_t *upbuf = *con_cls;
  68. bytes_free(upbuf);
  69. free(upbuf);
  70. *con_cls = NULL;
  71. }
  72. }
  73. static
  74. void httpsrv_log(void *arg, const char *fmt, va_list ap)
  75. {
  76. if (!opt_debug)
  77. return;
  78. char tmp42[LOGBUFSIZ] = "HTTPSrv: ";
  79. vsnprintf(&tmp42[9], sizeof(tmp42)-9, fmt, ap);
  80. _applog(LOG_DEBUG, tmp42);
  81. }
  82. void httpsrv_start(unsigned short port)
  83. {
  84. httpsrv = MHD_start_daemon(
  85. MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
  86. port, NULL, NULL,
  87. &httpsrv_handle_access, NULL,
  88. MHD_OPTION_NOTIFY_COMPLETED, &httpsrv_cleanup_request, NULL,
  89. MHD_OPTION_EXTERNAL_LOGGER, &httpsrv_log, NULL,
  90. MHD_OPTION_END);
  91. if (httpsrv)
  92. applog(LOG_NOTICE, "HTTP server listening on port %d", (int)port);
  93. else
  94. applog(LOG_ERR, "Failed to start HTTP server on port %d", (int)port);
  95. }
  96. void httpsrv_stop()
  97. {
  98. if (!httpsrv)
  99. return;
  100. applog(LOG_DEBUG, "Stopping HTTP server");
  101. MHD_stop_daemon(httpsrv);
  102. httpsrv = NULL;
  103. }