Statistics
| Branch: | Revision:

chunker-player / chunk_puller.c @ 1e69ae95

History | View | Annotate | Download (2.79 KB)

1
#include <platform.h>
2
#include <microhttpd.h>
3

    
4
#include <http_default_urls.h>
5

    
6
struct connection_info_struct {
7
  uint8_t *block;
8
  int block_size;
9
};
10

    
11
void request_completed(void *cls, struct MHD_Connection *connection,
12
                       void **con_cls, enum MHD_RequestTerminationCode toe) {
13
  struct connection_info_struct *con_info = (struct connection_info_struct *)*con_cls;
14
  if(NULL == con_info)
15
    return;
16
  if(con_info->block)
17
    free (con_info->block);
18
  free(con_info);
19
  *con_cls = NULL;
20
}
21

    
22
int send_response(struct MHD_Connection *connection, unsigned int code) {
23
  int ret;
24
  struct MHD_Response *response;
25

    
26
  response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
27
  if(!response)
28
    return MHD_NO;
29

    
30
  ret = MHD_queue_response(connection, code, response);
31
  MHD_destroy_response (response);
32

    
33
  return ret;
34
}
35

    
36
int answer_to_connection(void *cls, struct MHD_Connection *connection,
37
                         const char *url, const char *method,
38
                         const char *version, const char *upload_data,
39
                         size_t *upload_data_size, void **con_cls) {
40
  struct connection_info_struct *con_info = NULL;
41
  uint8_t *block = NULL;
42

    
43
  if(*con_cls==NULL) {
44
    con_info = malloc(sizeof(struct connection_info_struct));
45
    if(con_info == NULL)
46
      return MHD_NO;
47

    
48
    con_info->block = NULL;
49
    con_info->block_size = 0; 
50
    *con_cls = (void *)con_info;
51

    
52
    return MHD_YES;
53
  }
54

    
55
  if(0 == strcmp (method, "GET")) {
56
    return send_response(connection, MHD_HTTP_BAD_REQUEST);
57
  }
58

    
59
  if(0 == strcmp(method, "POST")) {
60
    if(0 == strcmp(url, UL_DEFAULT_EXTERNALPLAYER_PATH)) {
61
      con_info = (struct connection_info_struct *)*con_cls;
62
      if(*upload_data_size > 0) {
63
        block = (uint8_t *)malloc(con_info->block_size+*upload_data_size);
64

    
65
        if(!block)
66
          return MHD_NO;
67

    
68
        memcpy(block, con_info->block, con_info->block_size);
69
        memcpy(block+con_info->block_size, upload_data, *upload_data_size);
70
        con_info->block = block;
71
        con_info->block_size += *upload_data_size;
72
        *upload_data_size = 0;
73
        return MHD_YES;
74
      }
75
      else {
76
                                // i do not mind about return value or problems into the enqueueBlock()
77
        enqueueBlock(con_info->block, con_info->block_size); //this might take some time
78
        return send_response(connection, MHD_HTTP_OK);
79
      }
80
    }
81
    else
82
      return send_response(connection, MHD_HTTP_NOT_FOUND);
83
  }
84
  return send_response(connection, MHD_HTTP_BAD_REQUEST);
85
}
86

    
87

    
88
struct MHD_Daemon *initChunkPuller() {
89
  return MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, UL_DEFAULT_EXTERNALPLAYER_PORT,
90
                          NULL, NULL,
91
                          &answer_to_connection, NULL, MHD_OPTION_END);
92
}
93

    
94
void finalizeChunkPuller(struct MHD_Daemon *daemon) {
95
  MHD_stop_daemon(daemon);
96
}
97