httpsrv.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "util.h"
  26. #include "version.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, PACKAGE"/"VERSION" getwork server");
  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. 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)
  40. {
  41. bytes_t *upbuf;
  42. if (!*con_cls)
  43. {
  44. *con_cls = upbuf = malloc(sizeof(bytes_t));
  45. bytes_init(upbuf);
  46. return MHD_YES;
  47. }
  48. upbuf = *con_cls;
  49. if (*upload_data_size)
  50. {
  51. bytes_append(upbuf, upload_data, *upload_data_size);
  52. *upload_data_size = 0;
  53. return MHD_YES;
  54. }
  55. return httpsrv_handle_req(conn, url, method, *con_cls);
  56. }
  57. static
  58. void httpsrv_cleanup_request(void *cls, struct MHD_Connection *conn, void **con_cls, enum MHD_RequestTerminationCode toe)
  59. {
  60. if (*con_cls)
  61. {
  62. bytes_t *upbuf = *con_cls;
  63. bytes_free(upbuf);
  64. free(upbuf);
  65. *con_cls = NULL;
  66. }
  67. }
  68. static
  69. void httpsrv_log(void *arg, const char *fmt, va_list ap)
  70. {
  71. if (!opt_debug)
  72. return;
  73. char tmp42[LOGBUFSIZ] = "HTTPSrv: ";
  74. vsnprintf(&tmp42[9], sizeof(tmp42)-9, fmt, ap);
  75. _applog(LOG_DEBUG, tmp42);
  76. }
  77. void httpsrv_start(unsigned short port)
  78. {
  79. httpsrv = MHD_start_daemon(
  80. MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
  81. port, NULL, NULL,
  82. &httpsrv_handle_access, NULL,
  83. MHD_OPTION_NOTIFY_COMPLETED, &httpsrv_cleanup_request, NULL,
  84. MHD_OPTION_EXTERNAL_LOGGER, &httpsrv_log, NULL,
  85. MHD_OPTION_END);
  86. if (httpsrv)
  87. applog(LOG_NOTICE, "HTTP server listening on port %d", (int)port);
  88. else
  89. applog(LOG_ERR, "Failed to start HTTP server on port %d", (int)port);
  90. }
  91. void httpsrv_stop()
  92. {
  93. if (!httpsrv)
  94. return;
  95. applog(LOG_DEBUG, "Stopping HTTP server");
  96. MHD_stop_daemon(httpsrv);
  97. httpsrv = NULL;
  98. }