httpsrv.c 2.4 KB

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