forked from devgs/mod_zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_zip_headers.c
211 lines (162 loc) · 5.84 KB
/
ngx_http_zip_headers.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "ngx_http_zip_module.h"
#include "ngx_http_zip_headers.h"
#include "ngx_http_zip_file.h"
ngx_int_t
ngx_http_zip_add_cache_control(ngx_http_request_t *r)
{
ngx_table_elt_t **ccp, *cc;
ngx_uint_t i;
/* convoluted way of adding Cache-Control: max-age=0 */
/* The header is necessary so IE doesn't barf */
ccp = r->headers_out.cache_control.elts;
if (ccp == NULL) {
if (ngx_array_init(&r->headers_out.cache_control, r->pool,
1, sizeof(ngx_table_elt_t *))
!= NGX_OK)
{
return NGX_ERROR;
}
ccp = ngx_array_push(&r->headers_out.cache_control);
if (ccp == NULL) {
return NGX_ERROR;
}
cc = ngx_list_push(&r->headers_out.headers);
if (cc == NULL) {
return NGX_ERROR;
}
cc->hash = 1;
cc->key.len = sizeof("Cache-Control") - 1;
cc->key.data = (u_char *) "Cache-Control";
*ccp = cc;
} else {
for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
ccp[i]->hash = 0;
}
cc = ccp[0];
}
cc->value.len = sizeof("max-age=0") - 1;
cc->value.data = (u_char *) "max-age=0";
return NGX_OK;
}
ngx_int_t
ngx_http_zip_add_content_range_header(ngx_http_request_t *r)
{
ngx_table_elt_t *content_range;
content_range = ngx_list_push(&r->headers_out.headers);
if (content_range == NULL) {
return NGX_ERROR;
}
r->headers_out.content_range = content_range;
content_range->hash = 1;
content_range->key.len = sizeof("Content-Range") - 1;
content_range->key.data = (u_char *) "Content-Range";
if (r->headers_out.content_length) {
r->headers_out.content_length->hash = 0;
r->headers_out.content_length = NULL;
}
return NGX_OK;
}
ngx_int_t
ngx_http_zip_add_full_content_range(ngx_http_request_t *r)
{
ngx_table_elt_t *content_range;
if (ngx_http_zip_add_content_range_header(r) == NGX_ERROR) {
return NGX_ERROR;
}
content_range = r->headers_out.content_range;
if (content_range == NULL) {
return NGX_ERROR;
}
content_range->value.data = ngx_palloc(r->pool,
sizeof("bytes */") - 1 + NGX_OFF_T_LEN);
if (content_range->value.data == NULL) {
return NGX_ERROR;
}
content_range->value.len = ngx_sprintf(content_range->value.data,
"bytes */%O", r->headers_out.content_length_n)
- content_range->value.data;
return NGX_OK;
}
ngx_int_t
ngx_http_zip_init_multipart_range(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx)
{
ngx_uint_t i;
ngx_http_zip_range_t *range;
size_t len, message_len = 0;
len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
+ sizeof(CRLF "Content-Type: " NGX_ZIP_MIME_TYPE) - 1
+ sizeof(CRLF "Content-Range: bytes ") - 1
+ sizeof(/* start */ "-" /* end */ "/" /* size */ CRLF CRLF) - 1
+ 3 * NGX_OFF_T_LEN;
ctx->boundary = ngx_next_temp_number(0);
r->headers_out.content_type.data = ngx_palloc(r->pool,
sizeof("Content-Type: multipart/byteranges; boundary=") - 1
+ NGX_ATOMIC_T_LEN);
if (r->headers_out.content_type.data == NULL) {
return NGX_ERROR;
}
r->headers_out.content_type.len =
ngx_sprintf(r->headers_out.content_type.data,
"multipart/byteranges; boundary=%0muA", ctx->boundary)
- r->headers_out.content_type.data;
for (i=0; i < ctx->ranges.nelts; i++) {
range = &((ngx_http_zip_range_t *)ctx->ranges.elts)[i];
range->boundary_header.data = ngx_palloc(r->pool, len);
if (range->boundary_header.data == NULL) {
return NGX_ERROR;
}
range->boundary_header.len = ngx_sprintf(range->boundary_header.data,
CRLF "--%0muA" CRLF
"Content-Type: " NGX_ZIP_MIME_TYPE CRLF
"Content-Range: bytes %O-%O/%O" CRLF CRLF,
ctx->boundary, range->start, range->end - 1, ctx->archive_size)
- range->boundary_header.data;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Allocating boundary for range start=%O end=%O (size %d)",
range->start, range->end, range->boundary_header.len);
message_len += range->boundary_header.len + (range->end - range->start);
}
message_len += sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN + sizeof("--" CRLF) - 1;
r->headers_out.content_length_n = message_len;
return NGX_OK;
}
ngx_int_t
ngx_http_zip_add_partial_content_range(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx)
{
ngx_table_elt_t *content_range;
ngx_http_zip_range_t *range;
if (ngx_http_zip_add_content_range_header(r) == NGX_ERROR) {
return NGX_ERROR;
}
content_range = r->headers_out.content_range;
range = &((ngx_http_zip_range_t *)ctx->ranges.elts)[0];
if (content_range == NULL) {
return NGX_ERROR;
}
content_range->value.data = ngx_palloc(r->pool,
sizeof("bytes " /* start */ "-" /* end */ "/" /* total */) - 1
+ 3 * NGX_OFF_T_LEN);
if (content_range->value.data == NULL) {
return NGX_ERROR;
}
content_range->value.len = ngx_sprintf(content_range->value.data,
"bytes %O-%O/%O", range->start, range->end - 1,
r->headers_out.content_length_n)
- content_range->value.data;
r->headers_out.content_length_n = range->end - range->start;
return NGX_OK;
}
ngx_int_t
ngx_http_zip_strip_range_header(ngx_http_request_t *r)
{
ngx_table_elt_t *header;
header = r->headers_in.range;
if (header) {
header->key.data = (u_char *)"X-Range";
header->key.len = sizeof("X-Range") - 1;
header->lowcase_key = (u_char *)"x-range";
}
return NGX_OK;
}