summaryrefslogtreecommitdiff
path: root/src/http_chunk.c
blob: 281e86ba6f2a259a2cf8dd02f60870e0702851f3 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
 * the HTTP chunk-API
 * 
 * 
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "server.h"
#include "chunk.h"
#include "http_chunk.h"
#include "log.h"

static int http_chunk_append_len(server *srv, connection *con, size_t len) {
	size_t i, olen = len, j;
	buffer *b;
	
	b = srv->tmp_chunk_len;
	
	if (len == 0) {
		buffer_copy_string(b, "0");
	} else {
		for (i = 0; i < 8 && len; i++) {
			len >>= 4;
		}
		
		/* i is the number of hex digits we have */
		buffer_prepare_copy(b, i + 1);
		
		for (j = i-1, len = olen; j+1 > 0; j--) {
			b->ptr[j] = (len & 0xf) + (((len & 0xf) <= 9) ? '0' : 'a' - 10);
			len >>= 4;
		}
		b->used = i;
		b->ptr[b->used++] = '\0';
	}
		
	buffer_append_string(b, "\r\n");
	chunkqueue_append_buffer(con->write_queue, b);
	
	return 0;
}


int http_chunk_append_file(server *srv, connection *con, buffer *fn, off_t offset, off_t len) {
	chunkqueue *cq;
	
	if (!con) return -1;
	
	cq = con->write_queue;
	
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
		http_chunk_append_len(srv, con, len);
	}
	
	chunkqueue_append_file(cq, fn, offset, len);
	
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED && len > 0) {
		chunkqueue_append_mem(cq, "\r\n", 2 + 1);
	}
	
	return 0;
}

int http_chunk_append_buffer(server *srv, connection *con, buffer *mem) {
	chunkqueue *cq;
	
	if (!con) return -1;
	
	cq = con->write_queue;
	
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
		http_chunk_append_len(srv, con, mem->used - 1);
	}
	
	chunkqueue_append_buffer(cq, mem);
	
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED && mem->used > 0) {
		chunkqueue_append_mem(cq, "\r\n", 2 + 1);
	}
	
	return 0;
}

int http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) {
	chunkqueue *cq;
	
	if (!con) return -1;
	
	cq = con->write_queue;
	
	if (len == 0) {
		if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
			chunkqueue_append_mem(cq, "0\r\n\r\n", 5 + 1);
		} else {
			chunkqueue_append_mem(cq, "", 1);
		}
		return 0;
	}
	
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
		http_chunk_append_len(srv, con, len - 1);
	}
	
	chunkqueue_append_mem(cq, mem, len);
	
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
		chunkqueue_append_mem(cq, "\r\n", 2 + 1);
	}
	
	return 0;
}


off_t http_chunkqueue_length(server *srv, connection *con) {
	if (!con) {
		log_error_write(srv, __FILE__, __LINE__, "s", "connection is NULL!!");
		
		return 0;
	}
	
	return chunkqueue_length(con->write_queue);
}