Statistics
| Branch: | Revision:

chunker-player / chunker_player / chunk_puller.c @ e11386c0

History | View | Annotate | Download (3.36 KB)

1
#include <stdlib.h>
2
#include <limits.h>
3
#include <stdarg.h>
4
#include <string.h>
5
#include <stdio.h>
6
#include <stdint.h>
7
#include <memory.h>
8
#include <sys/types.h>
9
#include <sys/time.h>
10
#include <microhttpd.h>
11

    
12
struct connection_info_struct {
13
  uint8_t *block;
14
  int block_size;
15
};
16
static int listen_port = 0;
17
static char listen_path[256];
18

    
19
void request_completed(void *cls, struct MHD_Connection *connection,
20
                       void **con_cls, enum MHD_RequestTerminationCode toe) {
21
  struct connection_info_struct *con_info = (struct connection_info_struct *)*con_cls;
22

    
23
  if(NULL == con_info)
24
    return;
25
  if(con_info->block)
26
    free (con_info->block);
27
  free(con_info);
28

    
29
  *con_cls = NULL;
30
}
31

    
32
int send_response(struct MHD_Connection *connection, unsigned int code) {
33
  int ret = MHD_NO;
34

    
35
  struct MHD_Response *response;
36

    
37
  response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
38
  if(!response)
39
    return MHD_NO;
40

    
41
  ret = MHD_queue_response(connection, code, response);
42
  MHD_destroy_response (response);
43

    
44
  return ret;
45
}
46

    
47
int answer_to_connection(void *cls, struct MHD_Connection *connection,
48
                         const char *url, const char *method,
49
                         const char *version, const char *upload_data,
50
                         size_t *upload_data_size, void **con_cls) {
51
  struct connection_info_struct *con_info = NULL;
52
  uint8_t *block = NULL;
53

    
54
  if(*con_cls==NULL) {
55
    con_info = malloc(sizeof(struct connection_info_struct));
56
    if(con_info == NULL)
57
      return MHD_NO;
58
    con_info->block = NULL;
59
    con_info->block_size = 0; 
60
    *con_cls = (void *)con_info;
61

    
62
    return MHD_YES;
63
  }
64

    
65
  if(0 == strcmp (method, "GET")) {
66
    return send_response(connection, MHD_HTTP_BAD_REQUEST);
67
  }
68

    
69
  if(0 == strcmp(method, "POST")) {
70
    if(0 == strcmp(url, listen_path)) {
71
      con_info = (struct connection_info_struct *)*con_cls;
72
      if(*upload_data_size > 0) {
73
        block = (uint8_t *)malloc(con_info->block_size+*upload_data_size);
74

    
75
        if(!block)
76
          return MHD_NO;
77

    
78
        memcpy(block, con_info->block, con_info->block_size);
79
        memcpy(block+con_info->block_size, upload_data, *upload_data_size);
80
        free(con_info->block); //free the old referenced memory
81
        con_info->block = block;
82
        con_info->block_size += *upload_data_size;
83
        *upload_data_size = 0;
84
        return MHD_YES;
85
      }
86
      else {
87
                                // i do not mind about return value or problems into the enqueueBlock()
88
        enqueueBlock(con_info->block, con_info->block_size); //this might take some time
89
        free(con_info->block); //the enqueueBlock makes a copy of block into a chunk->data
90
        con_info->block = NULL;
91
        return send_response(connection, MHD_HTTP_OK);
92
      }
93
    }
94
    else
95
      return send_response(connection, MHD_HTTP_NOT_FOUND);
96
  }
97
  return send_response(connection, MHD_HTTP_BAD_REQUEST);
98
}
99

    
100

    
101
struct MHD_Daemon *initChunkPuller(const char *path, const int port) {
102
  sprintf(listen_path, "%s", path);
103
  listen_port = port;
104
printf("starting HTTPD on %s port %d\n", listen_path, listen_port);
105
  return MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, listen_port,
106
                          NULL, NULL,
107
                          &answer_to_connection, NULL, MHD_OPTION_NOTIFY_COMPLETED,
108
                          request_completed, NULL, MHD_OPTION_END);
109
}
110

    
111
void finalizeChunkPuller(struct MHD_Daemon *daemon) {
112
  MHD_stop_daemon(daemon);
113
}
114