Statistics
| Branch: | Revision:

chunker-player / event_http / event_http_server.c @ 159ff6bc

History | View | Annotate | Download (3.61 KB)

1
/**
2
 * @file event_http_server.c
3
 *
4
 * Simple http server for internal use.
5
 *
6
 * No threads, thus it is based on callbacks and timer events.
7
 *
8
 * Napa-Wine project 2009-2010
9
 * @author: Giuseppe Tropea <giuseppe.tropea@lightcomm.it>
10
 * @author: Bakay Árpád <arpad.bakay@netvisor.hu>
11
 */
12

    
13
#include <string.h>
14

    
15
#include <http_default_urls.h>
16
#include "chunk_external_interface.h"
17

    
18
int ulEventHttpServerSetup(const char *address, unsigned short port, data_processor_function data_processor) {
19
  struct evhttp* evh = NULL;
20
  debug("Setting up event-based http server listening at %s:%d", address, port);
21

    
22
  evh = evhttp_new(eventbase);
23

    
24
  if(evh != NULL) {
25
    info("Event-based http server at %s:%d has been setup", address, port);
26
    fprintf(stderr, "Event-based http server at %s:%d has been setup\n", address, port);
27
  }
28
  else {
29
    error("Setup of event-based http server at %s:%d FAILED", address, port);
30
    return UL_RETURN_FAIL;
31
  }
32

    
33
  if(evhttp_bind_socket(evh, address, port) != -1) {
34
    info("Event-based http server socket bind with %s:%d OK", address, port);
35
  }
36
  else {
37
    error("Bind of event-based http server with %s:%d FAILED", address, port);
38
    return UL_RETURN_FAIL;
39
  }
40

    
41
  //when a request for a generic path comes to the server, trigger the ulEventHttpServerProcessRequest
42
  //function and also pass to it a pointer to an external data_processor function, which is
43
  //able to handle the received data specifically
44
  evhttp_set_gencb(evh, ulEventHttpServerProcessRequest, data_processor);
45
  return UL_RETURN_OK;
46
}
47

    
48
int ulEventHttpServerProcessRequest(struct evhttp_request *req, void *context) {
49
  struct evbuffer* input_buffer;
50
  const uint8_t *data;
51
  int data_len;
52
  char *path = req->uri;
53
  int ret;
54

    
55
  //cast the context as it is a data_processor_function
56
  data_processor_function data_processor = (data_processor_function)context;
57

    
58
  debug("HTTP request received for %s of type %d", req->uri, req->type);
59
  input_buffer = evhttp_request_get_input_buffer(req);
60
  data_len = evbuffer_get_length(input_buffer);
61
  if(req->type == EVHTTP_REQ_POST) {
62
    //extract the path of request
63
    if(!strncmp(path, UL_HTTP_PREFIX , strlen(UL_HTTP_PREFIX))) {  //if it begins by "http://"
64
      path = strchr(path + strlen(UL_HTTP_PREFIX),'/'); //skip "http://host:port" part
65
    }
66
    debug("HTTP POST request is for path %s", path);
67

    
68
    if(!strcmp(path, UL_DEFAULT_CHUNKBUFFER_PATH)) {
69
      //give back the data
70
      //should i copy it in order to "free" the req pointer??
71
      //no, because the data processr makes a copy
72
      //data = (uint8_t*)req->input_buffer;
73
      data = (const uint8_t *)evbuffer_pullup(input_buffer, data_len);
74
 
75
      //sending a reply to the client
76
      evhttp_send_reply(req, 200, "OK", NULL);
77
      //debug("HTTP REPLY OK");
78
      if(context != NULL) {
79
        //invoke the data_processor function, which returns -1 and 0 as well depending on outcome
80
        debug("HTTP server invoking the data processor");
81
        ret = (*data_processor)(data, data_len);
82
      }
83
      else {
84
        debug("HTTP server NOT invoking the data processor: NULL");
85
        ret = UL_RETURN_OK;
86
      }
87
    }
88
    else {
89
      evhttp_send_reply(req, 400, "BAD URI", NULL);
90
      error("HTTP REPLY BAD URI ");
91
      ret = UL_RETURN_FAIL;
92
    }
93
  }
94
  else if(req->type == EVHTTP_REQ_GET || req->type == EVHTTP_REQ_PUT) {
95
    error("Received GET or PUT HTTP request");
96
    evhttp_send_reply(req, 404, "NOT FOUND", NULL);
97
    ret = UL_RETURN_FAIL;
98
  }
99
  else {
100
    error("HTTP Unknown request");
101
    evhttp_send_reply(req, 404, "NOT FOUND", NULL);
102
    ret = UL_RETURN_FAIL;
103
  }
104
  //evbuffer_free(input_buffer);
105
  //evhttp_request_free(req);
106
  return ret;
107
}