给nginx lua模块添加sendfile函数可代替X-Accel-Redirect(二)
->open_file_cache_events;
if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK)
{
return 0;//NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) != NGX_OK)
{
switch (of.err)
{
case 0:
return 0;//NGX_HTTP_INTERNAL_SERVER_ERROR;
case NGX_ENOENT:
case NGX_ENOTDIR:
case NGX_ENAMETOOLONG:
level = NGX_LOG_ERR;
rc = NGX_HTTP_NOT_FOUND;
break;
case NGX_EACCES:
#if (NGX_HAVE_OPENAT)
case NGX_EMLINK:
case NGX_ELOOP:
#endif
level = NGX_LOG_ERR;
rc = NGX_HTTP_FORBIDDEN;
break;
default:
level = NGX_LOG_CRIT;
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found)
{
ngx_log_error(level, log, of.err, "%s \"%s\" failed", of.failed, path.data);
}
return 0;//rc;
}
r->root_tested = !r->error_page;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
if (offset < 0) {
offset = 0;
}
if (bytes <= 0) {
bytes = of.size - offset;
}
#if !(NGX_WIN32) /* the not regular files are probably Unix specific */
if (!of.is_file)
{
ngx_log_error(NGX_LOG_CRIT, log, 0, "\"%s\" is not a regular file", path.data);
return 0;//NGX_HTTP_NOT_FOUND;
}
#endif
if (r->method & NGX_HTTP_POST)
{
return 0;//NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK)
{
return 0;//rc;
}
log->action = "sending response to client";
len = (offset + bytes) >= of.size of.size : (offset + bytes);
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = len - offset;
r->headers_out.last_modified_time = of.mtime;
if (ngx_http_set_content_type(r) != NGX_OK)
{
return 0;//NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (r != r->main && of.size == 0)
{
ngx_http_send_header(r);
return 0;//
}
r->allow_ranges = 1;
/* we need to allocate all before the header would be sent */
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL)
{
return 0;//NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
if (b->file == NULL)
{
return 0;//NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
{
return 0;//rc;
}
b->file_pos = offset;
b->file_last = (offset + bytes) >= of.size of.size : (offset + bytes);
b->in_file = 1;
b->last_buf = (r == r->main) 1: 0;
b->last_in_chain = 1;
b->file->fd = of.fd;
b->file->name = path;
b->file->log = log;
b->file->directio = of.is_directio;
out.buf = b;
out.next = NULL;
ngx_http_out
| 评论 |
|
|
