-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3tohttp.c
54 lines (40 loc) · 1.26 KB
/
mp3tohttp.c
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
#include <stdio.h>
#include <stdlib.h>
#include <microhttpd.h>
int handle_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
struct MHD_Response *response;
int ret;
char *input_file = (char*)cls;
FILE *fp = fopen(input_file, "rb");
if (!fp) {
return MHD_NO;
}
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *mp3_data = malloc(fsize + 1);
fread(mp3_data, fsize, 1, fp);
fclose(fp);
response = MHD_create_response_from_buffer(fsize, (void*)mp3_data, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", "audio/mpeg");
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
int main(int argc, char *argv[]) {
struct MHD_Daemon *daemon;
int port;
if (argc != 3) {
printf("Usage: %s <input_file.mp3> <port>\n", argv[0]);
return 1;
}
char *input_file = argv[1];
port = atoi(argv[2]);
daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, port, NULL, NULL, &handle_request, input_file, MHD_OPTION_END);
if (!daemon) {
return 1;
}
getchar();
MHD_stop_daemon(daemon);
return 0;
}