Statistics
| Branch: | Revision:

chunker-player / event_http / event_http_server.c @ e11386c0

History | View | Annotate | Download (3.75 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
static char input_http_path[255];
19

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

    
24
  evh = evhttp_new(eventbase);
25

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

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

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

    
47
  sprintf(input_http_path, "/%s", path);
48
  debug("Setting up static path to: %s", input_http_path);
49

    
50
  return UL_RETURN_OK;
51
}
52

    
53
int ulEventHttpServerProcessRequest(struct evhttp_request *req, void *context) {
54
  struct evbuffer* input_buffer;
55
  const uint8_t *data;
56
  int data_len;
57
  char *path = req->uri;
58
  int ret;
59

    
60
  //cast the context as it is a data_processor_function
61
  data_processor_function data_processor = (data_processor_function)context;
62

    
63
  debug("HTTP request received for %s of type %d", req->uri, req->type);
64
  input_buffer = evhttp_request_get_input_buffer(req);
65
  data_len = evbuffer_get_length(input_buffer);
66
  if(req->type == EVHTTP_REQ_POST) {
67
    //extract the path of request
68
    if(!strncmp(path, UL_HTTP_PREFIX , strlen(UL_HTTP_PREFIX))) {  //if it begins by "http://"
69
      path = strchr(path + strlen(UL_HTTP_PREFIX),'/'); //skip "http://host:port" part
70
    }
71
    debug("HTTP POST request is for path %s, and should be for %s\n", path, input_http_path);
72

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