httpsrv.c 2.4 KB

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