File: chunked.c

package info (click to toggle)
uwsgi 2.0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: ansic: 87,027; python: 7,001; cpp: 1,131; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (58 lines) | stat: -rw-r--r-- 1,475 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <uwsgi.h>

#if defined(UWSGI_ROUTING)

/*

	transfer-encoding is added to the headers

*/

static int transform_chunked(struct wsgi_request *wsgi_req, struct uwsgi_transformation *ut) {
	struct uwsgi_buffer *ub = ut->chunk;

	if (ut->is_final) {
		if (uwsgi_buffer_append(ub, "0\r\n\r\n", 5)) return -1;
		return 0;
	}

	if (ut->round == 1) {
		// do not check for errors !!!
        	uwsgi_response_add_header(wsgi_req, "Transfer-Encoding", 17, "chunked", 7);
	}

	if (ub->pos > 0) {
		if (uwsgi_buffer_insert_chunked(ub, 0, ub->pos)) return -1;
		if (uwsgi_buffer_append(ub, "\r\n", 2)) return -1;
	}

	return 0;
}

static int uwsgi_routing_func_chunked(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
	struct uwsgi_transformation *ut = uwsgi_add_transformation(wsgi_req, transform_chunked, NULL);
	ut->can_stream = 1;
	// add a "final" transformation to add the trailing chunk
	ut = uwsgi_add_transformation(wsgi_req, transform_chunked, NULL);
	ut->is_final = 1;
	return UWSGI_ROUTE_NEXT;
}

static int uwsgi_router_chunked(struct uwsgi_route *ur, char *args) {
	ur->func = uwsgi_routing_func_chunked;
	return 0;
}

static void router_chunked_register(void) {
	uwsgi_register_router("chunked", uwsgi_router_chunked);
}

struct uwsgi_plugin transformation_chunked_plugin = {
	.name = "transformation_chunked",
	.on_load = router_chunked_register,
};
#else
struct uwsgi_plugin transformation_chunked_plugin = {
	.name = "transformation_chunked",
};
#endif