Statistics
| Branch: | Revision:

chunker-player / chunker_player / chunk_puller.c @ 59e008ed

History | View | Annotate | Download (3.57 KB)

1
/*
2
 *  Copyright (c) 2009-2011 Carmelo Daniele, Dario Marchese, Diego Reforgiato, Giuseppe Tropea
3
 *  developed for the Napa-Wine EU project. See www.napa-wine.eu
4
 *
5
 *  This is free software; see lgpl-2.1.txt
6
 */
7

    
8
#include <stdlib.h>
9
#include <limits.h>
10
#include <stdarg.h>
11
#include <string.h>
12
#include <stdio.h>
13
#include <stdint.h>
14
#include <memory.h>
15
#include <sys/types.h>
16
#include <sys/time.h>
17
#include <microhttpd.h>
18

    
19
struct connection_info_struct {
20
  uint8_t *block;
21
  int block_size;
22
};
23
static int listen_port = 0;
24
static char listen_path[256];
25

    
26
void request_completed(void *cls, struct MHD_Connection *connection,
27
                       void **con_cls, enum MHD_RequestTerminationCode toe) {
28
  struct connection_info_struct *con_info = (struct connection_info_struct *)*con_cls;
29

    
30
  if(NULL == con_info)
31
    return;
32
  if(con_info->block)
33
    free (con_info->block);
34
  free(con_info);
35

    
36
  *con_cls = NULL;
37
}
38

    
39
int send_response(struct MHD_Connection *connection, unsigned int code) {
40
  int ret = MHD_NO;
41

    
42
  struct MHD_Response *response;
43

    
44
  response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
45
  if(!response)
46
    return MHD_NO;
47

    
48
  ret = MHD_queue_response(connection, code, response);
49
  MHD_destroy_response (response);
50

    
51
  return ret;
52
}
53

    
54
int answer_to_connection(void *cls, struct MHD_Connection *connection,
55
                         const char *url, const char *method,
56
                         const char *version, const char *upload_data,
57
                         size_t *upload_data_size, void **con_cls) {
58
  struct connection_info_struct *con_info = NULL;
59
  uint8_t *block = NULL;
60

    
61
  if(*con_cls==NULL) {
62
    con_info = malloc(sizeof(struct connection_info_struct));
63
    if(con_info == NULL)
64
      return MHD_NO;
65
    con_info->block = NULL;
66
    con_info->block_size = 0; 
67
    *con_cls = (void *)con_info;
68

    
69
    return MHD_YES;
70
  }
71

    
72
  if(0 == strcmp (method, "GET")) {
73
    return send_response(connection, MHD_HTTP_BAD_REQUEST);
74
  }
75

    
76
  if(0 == strcmp(method, "POST")) {
77
    if(0 == strcmp(url, listen_path)) {
78
      con_info = (struct connection_info_struct *)*con_cls;
79
      if(*upload_data_size > 0) {
80
        block = (uint8_t *)malloc(con_info->block_size+*upload_data_size);
81

    
82
        if(!block)
83
          return MHD_NO;
84

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

    
107

    
108
struct MHD_Daemon *initChunkPuller(const char *path, const int port) {
109
  sprintf(listen_path, "%s", path);
110
  listen_port = port;
111
printf("starting HTTPD on %s port %d\n", listen_path, listen_port);
112
  return MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, listen_port,
113
                          NULL, NULL,
114
                          &answer_to_connection, NULL, MHD_OPTION_NOTIFY_COMPLETED,
115
                          request_completed, NULL, MHD_OPTION_END);
116
}
117

    
118
void finalizeChunkPuller(struct MHD_Daemon *daemon) {
119
  MHD_stop_daemon(daemon);
120
}
121