Revision c039490c src/path_handlers.h

View differences:

src/path_handlers.h
17 17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 18
*******************************************************************/
19 19

  
20
#ifndef __PATH_HANDLERS__ 
21
#define __PATH_HANDLERS__ 
22

  
20 23
#include <stdint.h>
21 24

  
22 25
#include<router.h>
23
#include<tokens.h>
24
#include<name_lengths.h>
25
#include<debug.h>
26 26
#include<pschannel.h>
27
#include<context.h>
28
#include<mongoose.h>
29

  
30
char * mg_uri_field(struct http_message *hm, uint8_t pos)
31
{
32
	char * uri;
33
	char ** tokens;
34
	uint32_t ntok;
35

  
36
	uri = malloc((hm->uri.len + 1) * sizeof(char));
37
	strncpy(uri, hm->uri.p, hm->uri.len);
38
	uri[hm->uri.len] = '\0';
39

  
40
	tokens = tokens_create(uri, '/', &ntok);
41
	
42
	strcpy(uri, tokens[pos]);
43
	tokens_destroy(&tokens, ntok);
44

  
45
	return uri;
46
}
47

  
48
void channel_index(struct mg_connection *nc, struct http_message *hm)
49
{
50
	char * channels;
51
	const struct context * c;
52

  
53
	c = (const struct context *) nc->user_data;
54 27

  
55
	debug("GET request for channels\n");
56
	channels = pschannel_bucket_to_json(c->pb);
57
	debug("\t%s\n", channels);
58 28

  
59
	mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
60
	mg_printf_http_chunk(nc, "%s", channels);
61
	mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
29
char * mg_uri_field(struct http_message *hm, uint8_t pos);
62 30

  
63
	free(channels);
64
}
65

  
66
void streamer_create(struct mg_connection *nc, struct http_message *hm)
67
{
68
	const struct context * c;
69
	char ipaddr[MAX_IPADDR_LENGTH];
70
	char port[MAX_PORT_LENGTH];
71
	char * id, *json;
72
	const struct pstreamer * ps;
73

  
74
	c = (const struct context *) nc->user_data;
75
	mg_get_http_var(&hm->body, "ipaddr", ipaddr, MAX_IPADDR_LENGTH);
76
	mg_get_http_var(&hm->body, "port", port, MAX_PORT_LENGTH);
77

  
78
	id = mg_uri_field(hm, 2);
31
void channel_index(struct mg_connection *nc, struct http_message *hm);
79 32

  
80
	debug("POST request for resource %s\n", id);
81

  
82
	ps = pstreamer_manager_create_streamer(c->psm, ipaddr, port, id); 
83

  
84
	if(ps)
85
	{
86
		json = pstreamer_to_json(ps);
87
		mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
88
		mg_printf_http_chunk(nc, "%s", json);
89
		mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
90
		free(json);
91
	} else {
92
		mg_printf(nc, "%s", "HTTP/1.1 409 Conflict\r\nTransfer-Encoding: chunked\r\n\r\n");
93
		mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
94
	}
95

  
96
	free(id);
97
}
33
void streamer_create(struct mg_connection *nc, struct http_message *hm);
98 34

  
99 35
uint8_t load_path_handlers(struct router *r)
100 36
{
......
105 41

  
106 42
	return res;
107 43
}
44

  
45
#endif

Also available in: Unified diff