Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 9 | #include <ngx_event.h> |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 10 | |
| 11 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 12 | #if 0 |
| 13 | #define NGX_SENDFILE_LIMIT 4096 |
| 14 | #endif |
| 15 | |
| 16 | |
| 17 | #define NGX_NONE 1 |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 18 | |
| 19 | |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 20 | static ngx_inline ngx_int_t |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 21 | ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf); |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 22 | static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool, |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 23 | ngx_chain_t **chain, ngx_chain_t *in); |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 24 | static ngx_int_t ngx_output_chain_copy_buf(ngx_buf_t *dst, ngx_buf_t *src, |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 25 | ngx_uint_t sendfile); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 26 | |
| 27 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 28 | ngx_int_t |
| 29 | ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in) |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 30 | { |
| 31 | int rc, last; |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 32 | off_t bsize; |
| 33 | size_t size; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 34 | ngx_chain_t *cl, *out, **last_out; |
| 35 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 36 | if (ctx->in == NULL && ctx->busy == NULL) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 37 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 38 | /* |
| 39 | * the short path for the case when the ctx->in and ctx->busy chains |
| 40 | * are empty, the incoming chain is empty too or has the single buf |
| 41 | * that does not require the copy |
| 42 | */ |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 43 | |
| 44 | if (in == NULL) { |
Igor Sysoev | 89690bf | 2004-03-23 06:01:52 +0000 | [diff] [blame] | 45 | return ctx->output_filter(ctx->filter_ctx, in); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 48 | if (in->next == NULL |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 49 | #if (NGX_SENDFILE_LIMIT) |
| 50 | && !(in->buf->in_file && in->buf->file_last > NGX_SENDFILE_LIMIT) |
| 51 | #endif |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 52 | && (!ngx_output_chain_need_to_copy(ctx, in->buf))) |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 53 | { |
Igor Sysoev | 89690bf | 2004-03-23 06:01:52 +0000 | [diff] [blame] | 54 | return ctx->output_filter(ctx->filter_ctx, in); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Igor Sysoev | 67f450d | 2004-06-01 06:04:46 +0000 | [diff] [blame] | 58 | /* add the incoming buf to the chain ctx->in */ |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 59 | |
| 60 | if (in) { |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 61 | if (ngx_output_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 62 | return NGX_ERROR; |
| 63 | } |
| 64 | } |
| 65 | |
Igor Sysoev | b5faed2 | 2003-10-29 08:30:44 +0000 | [diff] [blame] | 66 | out = NULL; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 67 | last_out = &out; |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 68 | last = NGX_NONE; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 69 | |
| 70 | for ( ;; ) { |
| 71 | |
| 72 | while (ctx->in) { |
| 73 | |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 74 | /* |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 75 | * cycle while there are the ctx->in bufs |
| 76 | * or there are the free output bufs to copy in |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 77 | */ |
| 78 | |
Igor Sysoev | 00d433f | 2004-07-28 19:21:26 +0000 | [diff] [blame] | 79 | bsize = ngx_buf_size(ctx->in->buf); |
| 80 | |
| 81 | if (bsize == 0 && !ngx_buf_special(ctx->in->buf)) { |
| 82 | |
| 83 | ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0, |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 84 | "zero size buf in output " |
| 85 | "t:%d r:%d f:%d %p %p-%p %p %O-%O", |
| 86 | ctx->in->buf->temporary, |
| 87 | ctx->in->buf->recycled, |
| 88 | ctx->in->buf->in_file, |
| 89 | ctx->in->buf->start, |
| 90 | ctx->in->buf->pos, |
| 91 | ctx->in->buf->last, |
| 92 | ctx->in->buf->file, |
| 93 | ctx->in->buf->file_pos, |
| 94 | ctx->in->buf->file_last); |
Igor Sysoev | 00d433f | 2004-07-28 19:21:26 +0000 | [diff] [blame] | 95 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 96 | ngx_debug_point(); |
| 97 | |
Igor Sysoev | 00d433f | 2004-07-28 19:21:26 +0000 | [diff] [blame] | 98 | ctx->in = ctx->in->next; |
Igor Sysoev | a2c8119 | 2004-09-19 18:27:00 +0000 | [diff] [blame] | 99 | |
Igor Sysoev | 00d433f | 2004-07-28 19:21:26 +0000 | [diff] [blame] | 100 | continue; |
| 101 | } |
| 102 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 103 | if (!ngx_output_chain_need_to_copy(ctx, ctx->in->buf)) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 104 | |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 105 | /* move the chain link to the output chain */ |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 106 | |
| 107 | cl = ctx->in; |
| 108 | ctx->in = cl->next; |
| 109 | |
| 110 | *last_out = cl; |
| 111 | last_out = &cl->next; |
| 112 | cl->next = NULL; |
| 113 | |
| 114 | continue; |
| 115 | } |
| 116 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 117 | if (ctx->buf == NULL) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 118 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 119 | /* get the free buf */ |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 120 | |
| 121 | if (ctx->free) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 122 | ctx->buf = ctx->free->buf; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 123 | ctx->free = ctx->free->next; |
| 124 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 125 | } else if (out || ctx->allocated == ctx->bufs.num) { |
Igor Sysoev | 31f7f6a | 2004-05-12 05:37:55 +0000 | [diff] [blame] | 126 | |
| 127 | break; |
| 128 | |
| 129 | } else { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 130 | |
| 131 | size = ctx->bufs.size; |
| 132 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 133 | if (ctx->in->buf->last_buf) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 134 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 135 | if (bsize < (off_t) ctx->bufs.size) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 138 | * allocate small temp buf for the small last buf |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 139 | * or its small last part |
| 140 | */ |
| 141 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 142 | size = (size_t) bsize; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 143 | |
| 144 | } else if (ctx->bufs.num == 1 |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 145 | && (bsize < (off_t) (ctx->bufs.size |
| 146 | + (ctx->bufs.size >> 2)))) |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 147 | { |
| 148 | /* |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 149 | * allocate a temp buf that equals |
| 150 | * to the last buf if the last buf size is lesser |
| 151 | * than 1.25 of bufs.size and a temp buf is single |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 152 | */ |
| 153 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 154 | size = (size_t) bsize; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 158 | if (!(ctx->buf = ngx_create_temp_buf(ctx->pool, size))) { |
| 159 | return NGX_ERROR; |
| 160 | } |
| 161 | |
| 162 | ctx->buf->tag = ctx->tag; |
| 163 | ctx->buf->recycled = 1; |
| 164 | ctx->allocated++; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 168 | rc = ngx_output_chain_copy_buf(ctx->buf, ctx->in->buf, |
| 169 | ctx->sendfile); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 170 | |
| 171 | if (rc == NGX_ERROR) { |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | if (rc == NGX_AGAIN) { |
| 176 | if (out) { |
| 177 | break; |
| 178 | } |
Igor Sysoev | a2c8119 | 2004-09-19 18:27:00 +0000 | [diff] [blame] | 179 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 180 | return rc; |
| 181 | } |
| 182 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 183 | /* delete the completed buf from the ctx->in chain */ |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 184 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 185 | if (ngx_buf_size(ctx->in->buf) == 0) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 186 | ctx->in = ctx->in->next; |
| 187 | } |
| 188 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 189 | if (!(cl = ngx_alloc_chain_link(ctx->pool))) { |
| 190 | return NGX_ERROR; |
| 191 | } |
| 192 | cl->buf = ctx->buf; |
| 193 | cl->next = NULL; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 194 | *last_out = cl; |
| 195 | last_out = &cl->next; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 196 | ctx->buf = NULL; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (out == NULL && last != NGX_NONE) { |
| 200 | return last; |
| 201 | } |
| 202 | |
Igor Sysoev | 89690bf | 2004-03-23 06:01:52 +0000 | [diff] [blame] | 203 | last = ctx->output_filter(ctx->filter_ctx, out); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 204 | |
| 205 | ngx_chain_update_chains(&ctx->free, &ctx->busy, &out, ctx->tag); |
| 206 | last_out = &out; |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 207 | |
| 208 | if (last == NGX_ERROR) { |
| 209 | return last; |
| 210 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 215 | static ngx_inline ngx_int_t |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 216 | ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf) |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 217 | { |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 218 | ngx_uint_t sendfile; |
| 219 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 220 | if (ngx_buf_special(buf)) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 224 | sendfile = ctx->sendfile; |
| 225 | |
| 226 | #if (NGX_SENDFILE_LIMIT) |
| 227 | |
| 228 | if (buf->in_file && buf->file_pos >= NGX_SENDFILE_LIMIT) { |
| 229 | sendfile = 0; |
| 230 | } |
| 231 | |
| 232 | #endif |
| 233 | |
| 234 | if (!sendfile) { |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 235 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 236 | if (!ngx_buf_in_memory(buf)) { |
Igor Sysoev | 6597749 | 2003-11-02 22:56:18 +0000 | [diff] [blame] | 237 | return 1; |
| 238 | } |
| 239 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 240 | buf->in_file = 0; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 243 | if (ctx->need_in_memory && !ngx_buf_in_memory(buf)) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 244 | return 1; |
| 245 | } |
| 246 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 247 | if (ctx->need_in_temp && (buf->memory || buf->mmap)) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 255 | static ngx_int_t |
| 256 | ngx_output_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, |
| 257 | ngx_chain_t *in) |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 258 | { |
| 259 | ngx_chain_t *cl, **ll; |
| 260 | #if (NGX_SENDFILE_LIMIT) |
| 261 | ngx_buf_t *b, *buf; |
| 262 | #endif |
| 263 | |
| 264 | ll = chain; |
| 265 | |
| 266 | for (cl = *chain; cl; cl = cl->next) { |
| 267 | ll = &cl->next; |
| 268 | } |
| 269 | |
| 270 | while (in) { |
| 271 | |
| 272 | if (!(cl = ngx_alloc_chain_link(pool))) { |
| 273 | return NGX_ERROR; |
| 274 | } |
| 275 | |
| 276 | #if (NGX_SENDFILE_LIMIT) |
| 277 | |
| 278 | buf = in->buf; |
| 279 | |
| 280 | if (buf->in_file |
| 281 | && buf->file_pos < NGX_SENDFILE_LIMIT |
| 282 | && buf->file_last > NGX_SENDFILE_LIMIT) |
| 283 | { |
| 284 | if (!(b = ngx_calloc_buf(pool))) { |
| 285 | return NGX_ERROR; |
| 286 | } |
| 287 | |
| 288 | ngx_memcpy(b, buf, sizeof(ngx_buf_t)); |
| 289 | |
| 290 | if (ngx_buf_in_memory(buf)) { |
| 291 | buf->pos += (ssize_t) (NGX_SENDFILE_LIMIT - buf->file_pos); |
| 292 | b->last = buf->pos; |
| 293 | } |
| 294 | |
| 295 | buf->file_pos = NGX_SENDFILE_LIMIT; |
| 296 | b->file_last = NGX_SENDFILE_LIMIT; |
| 297 | |
| 298 | cl->buf = b; |
| 299 | |
| 300 | } else { |
| 301 | cl->buf = buf; |
| 302 | in = in->next; |
| 303 | } |
| 304 | |
| 305 | #else |
| 306 | cl->buf = in->buf; |
| 307 | in = in->next; |
| 308 | |
| 309 | #endif |
| 310 | |
| 311 | *ll = cl; |
| 312 | ll = &cl->next; |
| 313 | } |
| 314 | |
| 315 | *ll = NULL; |
| 316 | |
| 317 | return NGX_OK; |
| 318 | } |
| 319 | |
| 320 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 321 | static ngx_int_t |
| 322 | ngx_output_chain_copy_buf(ngx_buf_t *dst, ngx_buf_t *src, ngx_uint_t sendfile) |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 323 | { |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 324 | off_t size; |
Igor Sysoev | d9d0ca1 | 2003-11-21 06:30:49 +0000 | [diff] [blame] | 325 | ssize_t n; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 326 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 327 | size = ngx_buf_size(src); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 328 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 329 | if (size > dst->end - dst->pos) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 330 | size = dst->end - dst->pos; |
| 331 | } |
| 332 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 333 | #if (NGX_SENDFILE_LIMIT) |
| 334 | |
| 335 | if (src->in_file && src->file_pos >= NGX_SENDFILE_LIMIT) { |
| 336 | sendfile = 0; |
| 337 | } |
| 338 | |
| 339 | #endif |
| 340 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 341 | if (ngx_buf_in_memory(src)) { |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 342 | ngx_memcpy(dst->pos, src->pos, (size_t) size); |
| 343 | src->pos += (size_t) size; |
| 344 | dst->last += (size_t) size; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 345 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 346 | if (src->in_file) { |
| 347 | |
| 348 | if (sendfile) { |
| 349 | dst->in_file = 1; |
| 350 | dst->file = src->file; |
| 351 | dst->file_pos = src->file_pos; |
| 352 | dst->file_last = src->file_pos + size; |
| 353 | |
| 354 | } else { |
| 355 | dst->in_file = 0; |
| 356 | } |
| 357 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 358 | src->file_pos += size; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 359 | |
| 360 | } else { |
| 361 | dst->in_file = 0; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 364 | if (src->last_buf && src->pos == src->last) { |
| 365 | dst->last_buf = 1; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | } else { |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 369 | n = ngx_read_file(src->file, dst->pos, (size_t) size, src->file_pos); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 370 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 371 | if (n == NGX_ERROR) { |
| 372 | return n; |
| 373 | } |
| 374 | |
| 375 | #if (NGX_FILE_AIO_READ) |
| 376 | if (n == NGX_AGAIN) { |
| 377 | return n; |
| 378 | } |
| 379 | #endif |
| 380 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 381 | if (n != size) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 382 | ngx_log_error(NGX_LOG_ALERT, src->file->log, 0, |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 383 | ngx_read_file_n " reads only %z of %O from file", |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 384 | n, size); |
| 385 | if (n == 0) { |
| 386 | return NGX_ERROR; |
| 387 | } |
| 388 | } |
| 389 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 390 | dst->last += n; |
| 391 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 392 | if (sendfile) { |
| 393 | dst->in_file = 1; |
| 394 | dst->file = src->file; |
| 395 | dst->file_pos = src->file_pos; |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 396 | dst->file_last = src->file_pos + n; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 397 | |
| 398 | } else { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 399 | dst->in_file = 0; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 402 | src->file_pos += n; |
| 403 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 404 | if (src->last_buf && src->file_pos == src->file_last) { |
| 405 | dst->last_buf = 1; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | return NGX_OK; |
| 410 | } |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 411 | |
| 412 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 413 | ngx_int_t |
| 414 | ngx_chain_writer(void *data, ngx_chain_t *in) |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 415 | { |
Igor Sysoev | 68ee8f1 | 2003-10-30 08:51:06 +0000 | [diff] [blame] | 416 | ngx_chain_writer_ctx_t *ctx = data; |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 417 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 418 | off_t size; |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 419 | ngx_chain_t *cl; |
| 420 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 421 | for (size = 0; in; in = in->next) { |
| 422 | |
| 423 | #if 1 |
| 424 | if (ngx_buf_size(in->buf) == 0 && !ngx_buf_special(in->buf)) { |
| 425 | ngx_debug_point(); |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | size += ngx_buf_size(in->buf); |
Igor Sysoev | 967fd63 | 2004-08-27 15:40:59 +0000 | [diff] [blame] | 430 | |
| 431 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 432 | "chain writer buf size: %uz", ngx_buf_size(in->buf)); |
Igor Sysoev | 967fd63 | 2004-08-27 15:40:59 +0000 | [diff] [blame] | 433 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 434 | if (!(cl = ngx_alloc_chain_link(ctx->pool))) { |
| 435 | return NGX_ERROR; |
| 436 | } |
| 437 | cl->buf = in->buf; |
| 438 | cl->next = NULL; |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 439 | *ctx->last = cl; |
| 440 | ctx->last = &cl->next; |
| 441 | } |
| 442 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 443 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 444 | "chain writer in: %p", ctx->out); |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 445 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 446 | for (cl = ctx->out; cl; cl = cl->next) { |
| 447 | |
| 448 | #if 1 |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 449 | if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) { |
| 450 | ngx_debug_point(); |
| 451 | } |
| 452 | |
| 453 | #endif |
| 454 | |
| 455 | size += ngx_buf_size(cl->buf); |
| 456 | } |
| 457 | |
| 458 | if (size == 0) { |
| 459 | return NGX_OK; |
| 460 | } |
| 461 | |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 462 | ctx->out = ngx_send_chain(ctx->connection, ctx->out, ctx->limit); |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 463 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 464 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 465 | "chain writer out: %p", ctx->out); |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 466 | |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 467 | if (ctx->out == NGX_CHAIN_ERROR) { |
| 468 | return NGX_ERROR; |
| 469 | } |
| 470 | |
| 471 | if (ctx->out == NULL) { |
| 472 | ctx->last = &ctx->out; |
| 473 | return NGX_OK; |
| 474 | } |
| 475 | |
| 476 | return NGX_AGAIN; |
| 477 | } |