chunker-player / chunk_puller.c @ 64144d8f
History | View | Annotate | Download (3.09 KB)
1 |
#include <platform.h> |
---|---|
2 |
#include <microhttpd.h> |
3 |
|
4 |
|
5 |
static struct connection_info_struct { |
6 |
uint8_t *block; |
7 |
int block_size;
|
8 |
}; |
9 |
static int listen_port = 0; |
10 |
static char listen_path[256]; |
11 |
|
12 |
void request_completed(void *cls, struct MHD_Connection *connection, |
13 |
void **con_cls, enum MHD_RequestTerminationCode toe) { |
14 |
struct connection_info_struct *con_info = (struct connection_info_struct *)*con_cls; |
15 |
if(NULL == con_info) |
16 |
return;
|
17 |
if(con_info->block)
|
18 |
free (con_info->block); |
19 |
free(con_info); |
20 |
*con_cls = NULL;
|
21 |
} |
22 |
|
23 |
int send_response(struct MHD_Connection *connection, unsigned int code) { |
24 |
int ret;
|
25 |
struct MHD_Response *response;
|
26 |
|
27 |
response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO); |
28 |
if(!response)
|
29 |
return MHD_NO;
|
30 |
|
31 |
ret = MHD_queue_response(connection, code, response); |
32 |
MHD_destroy_response (response); |
33 |
|
34 |
return ret;
|
35 |
} |
36 |
|
37 |
int answer_to_connection(void *cls, struct MHD_Connection *connection, |
38 |
const char *url, const char *method, |
39 |
const char *version, const char *upload_data, |
40 |
size_t *upload_data_size, void **con_cls) {
|
41 |
struct connection_info_struct *con_info = NULL; |
42 |
uint8_t *block = NULL;
|
43 |
|
44 |
if(*con_cls==NULL) { |
45 |
con_info = malloc(sizeof(struct connection_info_struct)); |
46 |
if(con_info == NULL) |
47 |
return MHD_NO;
|
48 |
|
49 |
con_info->block = NULL;
|
50 |
con_info->block_size = 0;
|
51 |
*con_cls = (void *)con_info;
|
52 |
|
53 |
return MHD_YES;
|
54 |
} |
55 |
|
56 |
if(0 == strcmp (method, "GET")) { |
57 |
return send_response(connection, MHD_HTTP_BAD_REQUEST);
|
58 |
} |
59 |
|
60 |
if(0 == strcmp(method, "POST")) { |
61 |
if(0 == strcmp(url, listen_path)) { |
62 |
con_info = (struct connection_info_struct *)*con_cls;
|
63 |
if(*upload_data_size > 0) { |
64 |
block = (uint8_t *)malloc(con_info->block_size+*upload_data_size); |
65 |
|
66 |
if(!block)
|
67 |
return MHD_NO;
|
68 |
|
69 |
memcpy(block, con_info->block, con_info->block_size); |
70 |
memcpy(block+con_info->block_size, upload_data, *upload_data_size); |
71 |
free(con_info->block); //free the old referenced memory
|
72 |
con_info->block = block; |
73 |
con_info->block_size += *upload_data_size; |
74 |
*upload_data_size = 0;
|
75 |
return MHD_YES;
|
76 |
} |
77 |
else {
|
78 |
// i do not mind about return value or problems into the enqueueBlock()
|
79 |
enqueueBlock(con_info->block, con_info->block_size); //this might take some time
|
80 |
free(con_info->block); //the enqueueBlock makes a copy of block into a chunk->data
|
81 |
return send_response(connection, MHD_HTTP_OK);
|
82 |
} |
83 |
} |
84 |
else
|
85 |
return send_response(connection, MHD_HTTP_NOT_FOUND);
|
86 |
} |
87 |
return send_response(connection, MHD_HTTP_BAD_REQUEST);
|
88 |
} |
89 |
|
90 |
|
91 |
struct MHD_Daemon *initChunkPuller(const char *path, const int port) { |
92 |
sprintf(listen_path, "%s", path);
|
93 |
listen_port = port; |
94 |
printf("starting HTTPD on %s port %d\n", listen_path, listen_port);
|
95 |
return MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, listen_port,
|
96 |
NULL, NULL, |
97 |
&answer_to_connection, NULL, MHD_OPTION_END);
|
98 |
} |
99 |
|
100 |
void finalizeChunkPuller(struct MHD_Daemon *daemon) { |
101 |
MHD_stop_daemon(daemon); |
102 |
} |
103 |
|