httpsrv.c 2.6 KB

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