File: httpsrv.c

package info (click to toggle)
bfgminer 4.7.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,116 kB
  • ctags: 9,129
  • sloc: ansic: 62,470; lisp: 5,221; sh: 4,572; php: 2,645; asm: 667; makefile: 427; python: 85; ruby: 23
file content (117 lines) | stat: -rw-r--r-- 2,665 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright 2013-2014 Luke Dashjr
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.  See COPYING for more details.
 */

#include "config.h"

#ifdef WIN32
#include <winsock2.h>
#endif

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef WIN32
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#endif

#include <microhttpd.h>

#include "logging.h"
#include "util.h"
#include "version.h"

static struct MHD_Daemon *httpsrv;

extern int handle_getwork(struct MHD_Connection *, bytes_t *);

void httpsrv_prepare_resp(struct MHD_Response *resp)
{
	MHD_add_response_header(resp, MHD_HTTP_HEADER_SERVER, PACKAGE"/"VERSION" getwork server");
}

static
int httpsrv_handle_req(struct MHD_Connection *conn, const char *url, const char *method, bytes_t *upbuf)
{
	return handle_getwork(conn, upbuf);
}

static
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)
{
	bytes_t *upbuf;
	
	if (!*con_cls)
	{
		*con_cls = upbuf = malloc(sizeof(bytes_t));
		bytes_init(upbuf);
		return MHD_YES;
	}
	
	upbuf = *con_cls;
	if (*upload_data_size)
	{
		bytes_append(upbuf, upload_data, *upload_data_size);
		*upload_data_size = 0;
		return MHD_YES;
	}
	return httpsrv_handle_req(conn, url, method, *con_cls);
}

static
void httpsrv_cleanup_request(void *cls, struct MHD_Connection *conn, void **con_cls, enum MHD_RequestTerminationCode toe)
{
	if (*con_cls)
	{
		bytes_t *upbuf = *con_cls;
		bytes_free(upbuf);
		free(upbuf);
		*con_cls = NULL;
	}
}

static
void httpsrv_log(void *arg, const char *fmt, va_list ap)
{
	if (!opt_debug)
		return;
	
	char tmp42[LOGBUFSIZ] = "HTTPSrv: ";
	vsnprintf(&tmp42[9], sizeof(tmp42)-9, fmt, ap);
	_applog(LOG_DEBUG, tmp42);
}

void httpsrv_start(unsigned short port)
{
	httpsrv = MHD_start_daemon(
		MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
		port, NULL, NULL,
		&httpsrv_handle_access, NULL,
		MHD_OPTION_NOTIFY_COMPLETED, &httpsrv_cleanup_request, NULL,
		MHD_OPTION_EXTERNAL_LOGGER, &httpsrv_log, NULL,
	MHD_OPTION_END);
	if (httpsrv)
		applog(LOG_NOTICE, "HTTP server listening on port %d", (int)port);
	else
		applog(LOG_ERR, "Failed to start HTTP server on port %d", (int)port);
}

void httpsrv_stop()
{
	if (!httpsrv)
		return;
	
	applog(LOG_DEBUG, "Stopping HTTP server");
	MHD_stop_daemon(httpsrv);
	httpsrv = NULL;
}