Statistics
| Branch: | Revision:

chunker-player / chunker_player / chunk_puller.c @ 47943338

History | View | Annotate | Download (3.57 KB)

1 01f952d0 GiuseppeTropea
/*
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 1830eea9 GiuseppeTropea
#include <stdlib.h>
9
#include <limits.h>
10
#include <stdarg.h>
11
#include <string.h>
12 9240dba2 GiuseppeTropea
#include <stdio.h>
13 1830eea9 GiuseppeTropea
#include <stdint.h>
14 9240dba2 GiuseppeTropea
#include <memory.h>
15
#include <sys/types.h>
16 1830eea9 GiuseppeTropea
#include <sys/time.h>
17 1e69ae95 GiuseppeTropea
#include <microhttpd.h>
18
19 e576e0e8 GiuseppeTropea
struct connection_info_struct {
20 1e69ae95 GiuseppeTropea
  uint8_t *block;
21
  int block_size;
22
};
23 64144d8f GiuseppeTropea
static int listen_port = 0;
24
static char listen_path[256];
25 1e69ae95 GiuseppeTropea
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 3d45943a GiuseppeTropea
30 1e69ae95 GiuseppeTropea
  if(NULL == con_info)
31
    return;
32
  if(con_info->block)
33
    free (con_info->block);
34
  free(con_info);
35 3d45943a GiuseppeTropea
36 1e69ae95 GiuseppeTropea
  *con_cls = NULL;
37
}
38
39
int send_response(struct MHD_Connection *connection, unsigned int code) {
40 d87132b9 GiuseppeTropea
  int ret = MHD_NO;
41
42 1e69ae95 GiuseppeTropea
  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 64144d8f GiuseppeTropea
    if(0 == strcmp(url, listen_path)) {
78 1e69ae95 GiuseppeTropea
      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 b9426438 GiuseppeTropea
        free(con_info->block); //free the old referenced memory
88 1e69ae95 GiuseppeTropea
        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 b9426438 GiuseppeTropea
        free(con_info->block); //the enqueueBlock makes a copy of block into a chunk->data
97 3d45943a GiuseppeTropea
        con_info->block = NULL;
98 1e69ae95 GiuseppeTropea
        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 64144d8f GiuseppeTropea
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 1e69ae95 GiuseppeTropea
                          NULL, NULL,
114 3d45943a GiuseppeTropea
                          &answer_to_connection, NULL, MHD_OPTION_NOTIFY_COMPLETED,
115
                          request_completed, NULL, MHD_OPTION_END);
116 1e69ae95 GiuseppeTropea
}
117
118
void finalizeChunkPuller(struct MHD_Daemon *daemon) {
119
  MHD_stop_daemon(daemon);
120
}