NGINX(七)分段下载

时间:2020-12-29 20:36:56

前言

nginx分段下载通过ngx_http_range_filter_module模块进行处理,关于HTTP分段下载过程,可以参考HTTP分段下载一文,主要分为一次请求一段和一次请求多段

涉及数据结构

typedef struct {
/*文件开始位置*/
off_t start ;
/*文件结束位置*/
off_t end ;
/*一次请求多个部分时Content-Range字段 格式:SSSS-EEEE/TTTT*/
ngx_str_t content_range ;
} ngx_http_range_t; typedef struct {
/*偏移量*/
off_t offset ;
/*一次请求多个部分时内容中分割内容包含boundary(分割符), Content-Type, Content-Range三个字段*/
ngx_str_t boundary_header ;
/*ngx_http_range_t数组*/
ngx_array_t ranges ;
} ngx_http_range_filter_ctx_t;

内容进行分段

入口函数ngx_http_range_header_filter,首先判断请求中If-Range字段是否存在,If-Range字段格式 If-Range:"Etag" 或者 If-Range:modify_time,If-Range中保存文件Etag

值或者文件修改时间,如果判断Etag值和当前输出的文件值一致或者文件modify_time与If—Range中时间一致,则只返回请求的Range内容,否则返回文件所有内容。nginx判断If-Range最后一个字节如果是双引号则内容为etag,反之则是修改时间,代码如下。

static ngx_int_t
ngx_http_range_header_filter(ngx_http_request_t *r)
{
time_t if_range_time;
ngx_str_t *if_range, *etag;
ngx_uint_t ranges;
ngx_http_core_loc_conf_t *clcf;
ngx_http_range_filter_ctx_t *ctx; if (r->http_version < NGX_HTTP_VERSION_10
|| r->headers_out.status != NGX_HTTP_OK
|| r != r->main
|| r->headers_out.content_length_n == -1
|| !r->allow_ranges)
{
return ngx_http_next_header_filter(r);
} clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); if (clcf->max_ranges == 0) {
return ngx_http_next_header_filter(r);
} if (r->headers_in.range == NULL
|| r->headers_in.range->value.len < 7
|| ngx_strncasecmp(r->headers_in.range->value.data,
(u_char *) "bytes=", 6)
!= 0)
{
goto next_filter;
} /*头部存在If-Range字段*/
if (r->headers_in.if_range) { if_range = &r->headers_in.if_range->value; /*判断是否是etag*/
if (if_range->len >= 2 && if_range->data[if_range->len - 1] == '"') { if (r->headers_out.etag == NULL) {
goto next_filter;
} etag = &r->headers_out.etag->value; /*与输出的etag比较,一致则返回Range内容,否则返回所有内容*/
if (if_range->len != etag->len
|| ngx_strncmp(if_range->data, etag->data, etag->len) != 0)
{
/*不一致需要全部返回整个文件, 时间比较时一样*/
goto next_filter;
} goto parse;
} if (r->headers_out.last_modified_time == (time_t) -1) {
goto next_filter;
} if_range_time = ngx_http_parse_time(if_range->data, if_range->len); /*比较两个时间是否一致*/
if (if_range_time != r->headers_out.last_modified_time) {
goto next_filter;
}
} parse: ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_range_filter_ctx_t));
if (ctx == NULL) {
return NGX_ERROR;
} if (ngx_array_init(&ctx->ranges, r->pool, 1, sizeof(ngx_http_range_t))
!= NGX_OK)
{
return NGX_ERROR;
} ranges = r->single_range ? 1 : clcf->max_ranges; /*解析头部送的Range字段 例如 Range: bytes=0-1024 */
switch (ngx_http_range_parse(r, ctx, ranges)) { case NGX_OK:
ngx_http_set_ctx(r, ctx, ngx_http_range_body_filter_module); r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
r->headers_out.status_line.len = 0; if (ctx->ranges.nelts == 1) {
/*只包含一个段时执行*/
return ngx_http_range_singlepart_header(r, ctx);
} /*同时请求多个段时使用*/
return ngx_http_range_multipart_header(r, ctx); /*客户端请求的Range字段格式不正确, 直接返回给客户端错误*/
case NGX_HTTP_RANGE_NOT_SATISFIABLE:
return ngx_http_range_not_satisfiable(r); case NGX_ERROR:
return NGX_ERROR; default: /* NGX_DECLINED */
break;
} next_filter: r->headers_out.accept_ranges = ngx_list_push(&r->headers_out.headers);
if (r->headers_out.accept_ranges == NULL) {
return NGX_ERROR;
} r->headers_out.accept_ranges->hash = 1;
/*输出 Accept-Ranges : bytes 字段告诉客户端, 服务器支持分段下载*/
ngx_str_set(&r->headers_out.accept_ranges->key, "Accept-Ranges");
ngx_str_set(&r->headers_out.accept_ranges->value, "bytes"); return ngx_http_next_header_filter(r);
}

然后解析Range字段,如果字段中只有一个范围字段,则通过ngx_http_range_singlepart_header函数进行解析过滤,函数比较简单只是设置返回头部Content-Range字段。如果请求的多个范围通过函数ngx_http_range_multipart_header进行解析过滤,函数会生成boundary字段(一个全局变量上面每次加1生成),并设置ngx_http_range_filter_ctx_t和ngx_http_range_t中头部信息。

static ngx_int_t
ngx_http_range_multipart_header(ngx_http_request_t *r,
ngx_http_range_filter_ctx_t *ctx)
{
size_t len;
ngx_uint_t i;
ngx_http_range_t *range;
ngx_atomic_uint_t boundary; len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
+ sizeof(CRLF "Content-Type: ") - 1
+ r->headers_out.content_type.len
+ sizeof(CRLF "Content-Range: bytes ") - 1; /*判断是否需要添加charset字段, 如果添加charset字段, content_type.len 和 content_type_len值将不会相等.*/
if (r->headers_out.content_type_len == r->headers_out.content_type.len
&& r->headers_out.charset.len)
{
len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
} ctx->boundary_header.data = ngx_pnalloc(r->pool, len);
if (ctx->boundary_header.data == NULL) {
return NGX_ERROR;
} /*全局变量上加1, 作为分割符*/
boundary = ngx_next_temp_number(0); /*
* The boundary header of the range:
* CRLF
* "--0123456789" CRLF
* "Content-Type: image/jpeg" CRLF
* "Content-Range: bytes "
*/ /*由于存在多个段, 因此内容中需要有分割符区分, 下面设置分割符*/
if (r->headers_out.content_type_len == r->headers_out.content_type.len
&& r->headers_out.charset.len)
{
ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
CRLF "--%0muA" CRLF
"Content-Type: %V; charset=%V" CRLF
"Content-Range: bytes ",
boundary,
&r->headers_out.content_type,
&r->headers_out.charset)
- ctx->boundary_header.data; } else if (r->headers_out.content_type.len) {
ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
CRLF "--%0muA" CRLF
"Content-Type: %V" CRLF
"Content-Range: bytes ",
boundary,
&r->headers_out.content_type)
- ctx->boundary_header.data; } else {
ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
CRLF "--%0muA" CRLF
"Content-Range: bytes ",
boundary)
- ctx->boundary_header.data;
} r->headers_out.content_type.data =
ngx_pnalloc(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_lowcase = NULL; /* "Content-Type: multipart/byteranges; boundary=0123456789" */
/* 设置头部Content-Type类型,标识为多段内容, 并指明分隔符boundary字段 */
r->headers_out.content_type.len =
ngx_sprintf(r->headers_out.content_type.data,
"multipart/byteranges; boundary=%0muA",
boundary)
- r->headers_out.content_type.data; r->headers_out.content_type_len = r->headers_out.content_type.len; r->headers_out.charset.len = 0; /* the size of the last boundary CRLF "--0123456789--" CRLF */ len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN + sizeof("--" CRLF) - 1; /* 循环设置每个分段长度 */
range = ctx->ranges.elts;
for (i = 0; i < ctx->ranges.nelts; i++) { /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */ range[i].content_range.data =
ngx_pnalloc(r->pool, 3 * NGX_OFF_T_LEN + 2 + 4); if (range[i].content_range.data == NULL) {
return NGX_ERROR;
} range[i].content_range.len = ngx_sprintf(range[i].content_range.data,
"%O-%O/%O" CRLF CRLF,
range[i].start, range[i].end - 1,
r->headers_out.content_length_n)
- range[i].content_range.data; len += ctx->boundary_header.len + range[i].content_range.len
+ (size_t) (range[i].end - range[i].start);
} r->headers_out.content_length_n = len; if (r->headers_out.content_length) {
r->headers_out.content_length->hash = 0;
r->headers_out.content_length = NULL;
} return ngx_http_next_header_filter(r);
}

内容过滤

入口函数ngx_http_range_body_filter,同样分为两种情况处理,一次请求一段的ngx_http_range_singlepart_body比较简单,不再说明。一次请求多个部分的ngx_http_range_multipart_body,首先判断所有请求的范围是否在同一个buffer上, nginx多段请求时,只处理文件内容或文件在同一个buffer上面的情况。

static ngx_int_t
ngx_http_range_multipart_body(ngx_http_request_t *r,
ngx_http_range_filter_ctx_t *ctx, ngx_chain_t *in)
{
ngx_buf_t *b, *buf;
ngx_uint_t i;
ngx_chain_t *out, *hcl, *rcl, *dcl, **ll;
ngx_http_range_t *range; ll = &out;
buf = in->buf;
range = ctx->ranges.elts; /*
* 重组需要分段的buf, 首先是分段分隔符头部, 分段长度, 分段内容, 重组成一个新的链表
*/ for (i = 0; i < ctx->ranges.nelts; i++) { /*
* The boundary header of the range:
* CRLF
* "--0123456789" CRLF
* "Content-Type: image/jpeg" CRLF
* "Content-Range: bytes "
*/ b = ngx_calloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
} b->memory = 1;
b->pos = ctx->boundary_header.data;
b->last = ctx->boundary_header.data + ctx->boundary_header.len; hcl = ngx_alloc_chain_link(r->pool);
if (hcl == NULL) {
return NGX_ERROR;
} hcl->buf = b; /* "SSSS-EEEE/TTTT" CRLF CRLF */ b = ngx_calloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
} b->temporary = 1;
b->pos = range[i].content_range.data;
b->last = range[i].content_range.data + range[i].content_range.len; rcl = ngx_alloc_chain_link(r->pool);
if (rcl == NULL) {
return NGX_ERROR;
} rcl->buf = b; /* the range data */ b = ngx_calloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
} b->in_file = buf->in_file;
b->temporary = buf->temporary;
b->memory = buf->memory;
b->mmap = buf->mmap;
b->file = buf->file; if (buf->in_file) {
b->file_pos = buf->file_pos + range[i].start;
b->file_last = buf->file_pos + range[i].end;
} if (ngx_buf_in_memory(buf)) {
b->pos = buf->pos + (size_t) range[i].start;
b->last = buf->pos + (size_t) range[i].end;
} dcl = ngx_alloc_chain_link(r->pool);
if (dcl == NULL) {
return NGX_ERROR;
} dcl->buf = b; /*hcl(分隔符头), rcl(分段长度), dcl(分段内容) 串成新的链表*/ *ll = hcl;
hcl->next = rcl;
rcl->next = dcl;
ll = &dcl->next;
} /* the last boundary CRLF "--0123456789--" CRLF */ b = ngx_calloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
} b->temporary = 1;
b->last_buf = 1; b->pos = ngx_pnalloc(r->pool, sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
+ sizeof("--" CRLF) - 1);
if (b->pos == NULL) {
return NGX_ERROR;
} b->last = ngx_cpymem(b->pos, ctx->boundary_header.data,
sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN);
*b->last++ = '-'; *b->last++ = '-';
*b->last++ = CR; *b->last++ = LF; hcl = ngx_alloc_chain_link(r->pool);
if (hcl == NULL) {
return NGX_ERROR;
} hcl->buf = b;
hcl->next = NULL; *ll = hcl; return ngx_http_next_body_filter(r, out);
}