xref: /unit/src/nxt_http_static.c (revision 2139:99d792169ffb)
11183Svbart@nginx.com 
21183Svbart@nginx.com /*
31183Svbart@nginx.com  * Copyright (C) NGINX, Inc.
41183Svbart@nginx.com  */
51183Svbart@nginx.com 
61183Svbart@nginx.com #include <nxt_router.h>
71183Svbart@nginx.com #include <nxt_http.h>
81183Svbart@nginx.com 
91183Svbart@nginx.com 
101923Sz.hong@f5.com typedef struct {
111961Sz.hong@f5.com     nxt_var_t                   *var;
121961Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
131961Sz.hong@f5.com     u_char                      *fname;
141961Sz.hong@f5.com #endif
151961Sz.hong@f5.com     uint8_t                     is_const;  /* 1 bit */
161961Sz.hong@f5.com } nxt_http_static_share_t;
171961Sz.hong@f5.com 
181961Sz.hong@f5.com 
191961Sz.hong@f5.com typedef struct {
201961Sz.hong@f5.com     nxt_uint_t                  nshares;
211961Sz.hong@f5.com     nxt_http_static_share_t     *shares;
222108Salx.manpages@gmail.com     nxt_str_t                   index;
231959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
241959Sz.hong@f5.com     nxt_var_t                   *chroot;
251959Sz.hong@f5.com     nxt_uint_t                  resolve;
261959Sz.hong@f5.com #endif
271959Sz.hong@f5.com     nxt_http_route_rule_t       *types;
281923Sz.hong@f5.com } nxt_http_static_conf_t;
291923Sz.hong@f5.com 
301923Sz.hong@f5.com 
311959Sz.hong@f5.com typedef struct {
321959Sz.hong@f5.com     nxt_http_action_t           *action;
331960Sz.hong@f5.com     nxt_str_t                   share;
341959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
351959Sz.hong@f5.com     nxt_str_t                   chroot;
361959Sz.hong@f5.com #endif
372096Salx.manpages@gmail.com     uint32_t                    share_idx;
381959Sz.hong@f5.com     uint8_t                     need_body;  /* 1 bit */
391959Sz.hong@f5.com } nxt_http_static_ctx_t;
401959Sz.hong@f5.com 
411959Sz.hong@f5.com 
421183Svbart@nginx.com #define NXT_HTTP_STATIC_BUF_COUNT  2
431183Svbart@nginx.com #define NXT_HTTP_STATIC_BUF_SIZE   (128 * 1024)
441183Svbart@nginx.com 
451183Svbart@nginx.com 
461923Sz.hong@f5.com static nxt_http_action_t *nxt_http_static(nxt_task_t *task,
471923Sz.hong@f5.com     nxt_http_request_t *r, nxt_http_action_t *action);
481961Sz.hong@f5.com static void nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
491961Sz.hong@f5.com     nxt_http_static_ctx_t *ctx);
501959Sz.hong@f5.com static void nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data);
511959Sz.hong@f5.com static void nxt_http_static_var_error(nxt_task_t *task, void *obj, void *data);
521961Sz.hong@f5.com static void nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r,
531961Sz.hong@f5.com     nxt_http_static_ctx_t *ctx, nxt_http_status_t status);
541959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
551959Sz.hong@f5.com static u_char *nxt_http_static_chroot_match(u_char *chr, u_char *shr);
561959Sz.hong@f5.com #endif
571183Svbart@nginx.com static void nxt_http_static_extract_extension(nxt_str_t *path,
581923Sz.hong@f5.com     nxt_str_t *exten);
591183Svbart@nginx.com static void nxt_http_static_body_handler(nxt_task_t *task, void *obj,
601183Svbart@nginx.com     void *data);
611183Svbart@nginx.com static void nxt_http_static_buf_completion(nxt_task_t *task, void *obj,
621183Svbart@nginx.com     void *data);
631183Svbart@nginx.com 
641183Svbart@nginx.com static nxt_int_t nxt_http_static_mtypes_hash_test(nxt_lvlhsh_query_t *lhq,
651183Svbart@nginx.com     void *data);
661183Svbart@nginx.com static void *nxt_http_static_mtypes_hash_alloc(void *data, size_t size);
671183Svbart@nginx.com static void nxt_http_static_mtypes_hash_free(void *data, void *p);
681183Svbart@nginx.com 
691183Svbart@nginx.com 
701183Svbart@nginx.com static const nxt_http_request_state_t  nxt_http_static_send_state;
711183Svbart@nginx.com 
721183Svbart@nginx.com 
731923Sz.hong@f5.com nxt_int_t
nxt_http_static_init(nxt_task_t * task,nxt_router_temp_conf_t * tmcf,nxt_http_action_t * action,nxt_http_action_conf_t * acf)741923Sz.hong@f5.com nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
751923Sz.hong@f5.com     nxt_http_action_t *action, nxt_http_action_conf_t *acf)
761923Sz.hong@f5.com {
771961Sz.hong@f5.com     uint32_t                i;
781923Sz.hong@f5.com     nxt_mp_t                *mp;
792108Salx.manpages@gmail.com     nxt_str_t               str, *ret;
801961Sz.hong@f5.com     nxt_var_t               *var;
811961Sz.hong@f5.com     nxt_conf_value_t        *cv;
821923Sz.hong@f5.com     nxt_http_static_conf_t  *conf;
831923Sz.hong@f5.com 
841923Sz.hong@f5.com     mp = tmcf->router_conf->mem_pool;
851923Sz.hong@f5.com 
861923Sz.hong@f5.com     conf = nxt_mp_zget(mp, sizeof(nxt_http_static_conf_t));
871923Sz.hong@f5.com     if (nxt_slow_path(conf == NULL)) {
881923Sz.hong@f5.com         return NXT_ERROR;
891923Sz.hong@f5.com     }
901923Sz.hong@f5.com 
911923Sz.hong@f5.com     action->handler = nxt_http_static;
921923Sz.hong@f5.com     action->u.conf = conf;
931923Sz.hong@f5.com 
942077Salx.manpages@gmail.com     conf->nshares = nxt_conf_array_elements_count_or_1(acf->share);
951961Sz.hong@f5.com     conf->shares = nxt_mp_zget(mp, sizeof(nxt_http_static_share_t)
961961Sz.hong@f5.com                                    * conf->nshares);
971961Sz.hong@f5.com     if (nxt_slow_path(conf->shares == NULL)) {
981923Sz.hong@f5.com         return NXT_ERROR;
991923Sz.hong@f5.com     }
1001923Sz.hong@f5.com 
1012077Salx.manpages@gmail.com     for (i = 0; i < conf->nshares; i++) {
1022077Salx.manpages@gmail.com         cv = nxt_conf_get_array_element_or_itself(acf->share, i);
1032077Salx.manpages@gmail.com         nxt_conf_get_string(cv, &str);
1041961Sz.hong@f5.com 
1051961Sz.hong@f5.com         var = nxt_var_compile(&str, mp, 1);
1061961Sz.hong@f5.com         if (nxt_slow_path(var == NULL)) {
1071961Sz.hong@f5.com             return NXT_ERROR;
1081961Sz.hong@f5.com         }
1091961Sz.hong@f5.com 
1102077Salx.manpages@gmail.com         conf->shares[i].var = var;
1112077Salx.manpages@gmail.com         conf->shares[i].is_const = nxt_var_is_const(var);
1121961Sz.hong@f5.com     }
1131959Sz.hong@f5.com 
1142108Salx.manpages@gmail.com     if (acf->index == NULL) {
1152108Salx.manpages@gmail.com         nxt_str_set(&conf->index, "index.html");
1162108Salx.manpages@gmail.com 
1172108Salx.manpages@gmail.com     } else {
1182108Salx.manpages@gmail.com         nxt_conf_get_string(acf->index, &str);
1192108Salx.manpages@gmail.com 
1202108Salx.manpages@gmail.com         ret = nxt_str_dup(mp, &conf->index, &str);
1212108Salx.manpages@gmail.com         if (nxt_slow_path(ret == NULL)) {
1222108Salx.manpages@gmail.com             return NXT_ERROR;
1232108Salx.manpages@gmail.com         }
1242108Salx.manpages@gmail.com     }
1252108Salx.manpages@gmail.com 
1261923Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
1271923Sz.hong@f5.com     if (acf->chroot.length > 0) {
1281961Sz.hong@f5.com         nxt_str_t   chr, shr;
1291961Sz.hong@f5.com         nxt_bool_t  is_const;
1301923Sz.hong@f5.com 
1311959Sz.hong@f5.com         conf->chroot = nxt_var_compile(&acf->chroot, mp, 1);
1321959Sz.hong@f5.com         if (nxt_slow_path(conf->chroot == NULL)) {
1331923Sz.hong@f5.com             return NXT_ERROR;
1341923Sz.hong@f5.com         }
1351960Sz.hong@f5.com 
1361961Sz.hong@f5.com         is_const = nxt_var_is_const(conf->chroot);
1371961Sz.hong@f5.com 
1381961Sz.hong@f5.com         for (i = 0; i < conf->nshares; i++) {
1391961Sz.hong@f5.com             conf->shares[i].is_const &= is_const;
1401960Sz.hong@f5.com 
1411961Sz.hong@f5.com             if (conf->shares[i].is_const) {
1421961Sz.hong@f5.com                 nxt_var_raw(conf->chroot, &chr);
1431961Sz.hong@f5.com                 nxt_var_raw(conf->shares[i].var, &shr);
1441961Sz.hong@f5.com 
1451961Sz.hong@f5.com                 conf->shares[i].fname = nxt_http_static_chroot_match(chr.start,
1461961Sz.hong@f5.com                                                                      shr.start);
1471961Sz.hong@f5.com             }
1481960Sz.hong@f5.com         }
1491923Sz.hong@f5.com     }
1501923Sz.hong@f5.com 
1511923Sz.hong@f5.com     if (acf->follow_symlinks != NULL
1521923Sz.hong@f5.com         && !nxt_conf_get_boolean(acf->follow_symlinks))
1531923Sz.hong@f5.com     {
1541923Sz.hong@f5.com         conf->resolve |= RESOLVE_NO_SYMLINKS;
1551923Sz.hong@f5.com     }
1561923Sz.hong@f5.com 
1571923Sz.hong@f5.com     if (acf->traverse_mounts != NULL
1581923Sz.hong@f5.com         && !nxt_conf_get_boolean(acf->traverse_mounts))
1591923Sz.hong@f5.com     {
1601923Sz.hong@f5.com         conf->resolve |= RESOLVE_NO_XDEV;
1611923Sz.hong@f5.com     }
1621923Sz.hong@f5.com #endif
1631923Sz.hong@f5.com 
1641923Sz.hong@f5.com     if (acf->types != NULL) {
1651923Sz.hong@f5.com         conf->types = nxt_http_route_types_rule_create(task, mp, acf->types);
1661923Sz.hong@f5.com         if (nxt_slow_path(conf->types == NULL)) {
1671923Sz.hong@f5.com             return NXT_ERROR;
1681923Sz.hong@f5.com         }
1691923Sz.hong@f5.com     }
1701923Sz.hong@f5.com 
1711923Sz.hong@f5.com     if (acf->fallback != NULL) {
1721923Sz.hong@f5.com         action->fallback = nxt_mp_alloc(mp, sizeof(nxt_http_action_t));
1731923Sz.hong@f5.com         if (nxt_slow_path(action->fallback == NULL)) {
1741923Sz.hong@f5.com             return NXT_ERROR;
1751923Sz.hong@f5.com         }
1761923Sz.hong@f5.com 
1771923Sz.hong@f5.com         return nxt_http_action_init(task, tmcf, acf->fallback,
1781923Sz.hong@f5.com                                     action->fallback);
1791923Sz.hong@f5.com     }
1801923Sz.hong@f5.com 
1811923Sz.hong@f5.com     return NXT_OK;
1821923Sz.hong@f5.com }
1831923Sz.hong@f5.com 
1841923Sz.hong@f5.com 
1851923Sz.hong@f5.com static nxt_http_action_t *
nxt_http_static(nxt_task_t * task,nxt_http_request_t * r,nxt_http_action_t * action)1861923Sz.hong@f5.com nxt_http_static(nxt_task_t *task, nxt_http_request_t *r,
1871264Sigor@sysoev.ru     nxt_http_action_t *action)
1881183Svbart@nginx.com {
1891961Sz.hong@f5.com     nxt_bool_t             need_body;
1901961Sz.hong@f5.com     nxt_http_static_ctx_t  *ctx;
1911923Sz.hong@f5.com 
1921183Svbart@nginx.com     if (nxt_slow_path(!nxt_str_eq(r->method, "GET", 3))) {
1931183Svbart@nginx.com 
1941183Svbart@nginx.com         if (!nxt_str_eq(r->method, "HEAD", 4)) {
1951923Sz.hong@f5.com             if (action->fallback != NULL) {
1961923Sz.hong@f5.com                 return action->fallback;
1971378Svbart@nginx.com             }
1981378Svbart@nginx.com 
1991183Svbart@nginx.com             nxt_http_request_error(task, r, NXT_HTTP_METHOD_NOT_ALLOWED);
2001183Svbart@nginx.com             return NULL;
2011183Svbart@nginx.com         }
2021183Svbart@nginx.com 
2031183Svbart@nginx.com         need_body = 0;
2041183Svbart@nginx.com 
2051183Svbart@nginx.com     } else {
2061183Svbart@nginx.com         need_body = 1;
2071183Svbart@nginx.com     }
2081183Svbart@nginx.com 
2091961Sz.hong@f5.com     ctx = nxt_mp_zget(r->mem_pool, sizeof(nxt_http_static_ctx_t));
2101961Sz.hong@f5.com     if (nxt_slow_path(ctx == NULL)) {
2111961Sz.hong@f5.com         nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
2121961Sz.hong@f5.com         return NULL;
2131961Sz.hong@f5.com     }
2141961Sz.hong@f5.com 
2151961Sz.hong@f5.com     ctx->action = action;
2161961Sz.hong@f5.com     ctx->need_body = need_body;
2171961Sz.hong@f5.com 
2181961Sz.hong@f5.com     nxt_http_static_iterate(task, r, ctx);
2191961Sz.hong@f5.com 
2201961Sz.hong@f5.com     return NULL;
2211961Sz.hong@f5.com }
2221961Sz.hong@f5.com 
2231961Sz.hong@f5.com 
2241961Sz.hong@f5.com static void
nxt_http_static_iterate(nxt_task_t * task,nxt_http_request_t * r,nxt_http_static_ctx_t * ctx)2251961Sz.hong@f5.com nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
2261961Sz.hong@f5.com     nxt_http_static_ctx_t *ctx)
2271961Sz.hong@f5.com {
2281961Sz.hong@f5.com     nxt_int_t                ret;
2291961Sz.hong@f5.com     nxt_http_static_conf_t   *conf;
2301961Sz.hong@f5.com     nxt_http_static_share_t  *share;
2311961Sz.hong@f5.com 
2321961Sz.hong@f5.com     conf = ctx->action->u.conf;
2331961Sz.hong@f5.com 
2342096Salx.manpages@gmail.com     share = &conf->shares[ctx->share_idx];
2351959Sz.hong@f5.com 
2361960Sz.hong@f5.com #if (NXT_DEBUG)
2371960Sz.hong@f5.com     nxt_str_t  shr;
2382108Salx.manpages@gmail.com     nxt_str_t  idx;
2391960Sz.hong@f5.com 
2401961Sz.hong@f5.com     nxt_var_raw(share->var, &shr);
2412108Salx.manpages@gmail.com     idx = conf->index;
2421960Sz.hong@f5.com 
2431960Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
2441959Sz.hong@f5.com     nxt_str_t  chr;
2451959Sz.hong@f5.com 
2461959Sz.hong@f5.com     if (conf->chroot != NULL) {
2471959Sz.hong@f5.com         nxt_var_raw(conf->chroot, &chr);
2481959Sz.hong@f5.com 
2491959Sz.hong@f5.com     } else {
2501959Sz.hong@f5.com         nxt_str_set(&chr, "");
2511959Sz.hong@f5.com     }
2521959Sz.hong@f5.com 
2532108Salx.manpages@gmail.com     nxt_debug(task, "http static: \"%V\", index: \"%V\" (chroot: \"%V\")",
2542108Salx.manpages@gmail.com               &shr, &idx, &chr);
2551959Sz.hong@f5.com #else
2562108Salx.manpages@gmail.com     nxt_debug(task, "http static: \"%V\", index: \"%V\"", &shr, &idx);
2571959Sz.hong@f5.com #endif
2581960Sz.hong@f5.com #endif /* NXT_DEBUG */
2591959Sz.hong@f5.com 
2601961Sz.hong@f5.com     if (share->is_const) {
2611961Sz.hong@f5.com         nxt_var_raw(share->var, &ctx->share);
2621960Sz.hong@f5.com 
2631959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
2642096Salx.manpages@gmail.com         if (conf->chroot != NULL && ctx->share_idx == 0) {
2651959Sz.hong@f5.com             nxt_var_raw(conf->chroot, &ctx->chroot);
2661959Sz.hong@f5.com         }
2671959Sz.hong@f5.com #endif
2681959Sz.hong@f5.com 
2691959Sz.hong@f5.com         nxt_http_static_send_ready(task, r, ctx);
2701959Sz.hong@f5.com 
2711959Sz.hong@f5.com     } else {
2721959Sz.hong@f5.com         ret = nxt_var_query_init(&r->var_query, r, r->mem_pool);
2731959Sz.hong@f5.com         if (nxt_slow_path(ret != NXT_OK)) {
2741961Sz.hong@f5.com             nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
2751961Sz.hong@f5.com             return;
2761959Sz.hong@f5.com         }
2771959Sz.hong@f5.com 
2781961Sz.hong@f5.com         nxt_var_query(task, r->var_query, share->var, &ctx->share);
2791960Sz.hong@f5.com 
2801959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
2812096Salx.manpages@gmail.com         if (conf->chroot != NULL && ctx->share_idx == 0) {
2821960Sz.hong@f5.com             nxt_var_query(task, r->var_query, conf->chroot, &ctx->chroot);
2831960Sz.hong@f5.com         }
2841959Sz.hong@f5.com #endif
2851959Sz.hong@f5.com 
2861959Sz.hong@f5.com         nxt_var_query_resolve(task, r->var_query, ctx,
2871959Sz.hong@f5.com                               nxt_http_static_send_ready,
2881959Sz.hong@f5.com                               nxt_http_static_var_error);
2891961Sz.hong@f5.com      }
2901959Sz.hong@f5.com }
2911959Sz.hong@f5.com 
2921959Sz.hong@f5.com 
2931959Sz.hong@f5.com static void
nxt_http_static_send_ready(nxt_task_t * task,void * obj,void * data)2941959Sz.hong@f5.com nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
2951959Sz.hong@f5.com {
2961959Sz.hong@f5.com     size_t                  length, encode;
2971959Sz.hong@f5.com     u_char                  *p, *fname;
2981959Sz.hong@f5.com     struct tm               tm;
2991959Sz.hong@f5.com     nxt_buf_t               *fb;
3001959Sz.hong@f5.com     nxt_int_t               ret;
3012108Salx.manpages@gmail.com     nxt_str_t               *shr, *index, exten, *mtype;
3021959Sz.hong@f5.com     nxt_uint_t              level;
3031959Sz.hong@f5.com     nxt_file_t              *f, file;
3041959Sz.hong@f5.com     nxt_file_info_t         fi;
3051959Sz.hong@f5.com     nxt_http_field_t        *field;
3061959Sz.hong@f5.com     nxt_http_status_t       status;
3071959Sz.hong@f5.com     nxt_router_conf_t       *rtcf;
3081959Sz.hong@f5.com     nxt_http_action_t       *action;
3091959Sz.hong@f5.com     nxt_http_request_t      *r;
3101959Sz.hong@f5.com     nxt_work_handler_t      body_handler;
3111959Sz.hong@f5.com     nxt_http_static_ctx_t   *ctx;
3121959Sz.hong@f5.com     nxt_http_static_conf_t  *conf;
3131959Sz.hong@f5.com 
3141959Sz.hong@f5.com     r = obj;
3151959Sz.hong@f5.com     ctx = data;
3161959Sz.hong@f5.com     action = ctx->action;
3171959Sz.hong@f5.com     conf = action->u.conf;
3181960Sz.hong@f5.com     rtcf = r->conf->socket_conf->router_conf;
3191183Svbart@nginx.com 
3201855Sz.hong@f5.com     f = NULL;
3211960Sz.hong@f5.com     mtype = NULL;
3221183Svbart@nginx.com 
3231960Sz.hong@f5.com     shr = &ctx->share;
3242108Salx.manpages@gmail.com     index = &conf->index;
3251859So.canty@f5.com 
3261960Sz.hong@f5.com     if (shr->start[shr->length - 1] == '/') {
3272108Salx.manpages@gmail.com         nxt_http_static_extract_extension(index, &exten);
3281859So.canty@f5.com 
3292108Salx.manpages@gmail.com         length = shr->length + index->length;
3301960Sz.hong@f5.com 
3311960Sz.hong@f5.com         fname = nxt_mp_nget(r->mem_pool, length + 1);
3321960Sz.hong@f5.com         if (nxt_slow_path(fname == NULL)) {
3331883So.canty@f5.com             goto fail;
3341883So.canty@f5.com         }
3351883So.canty@f5.com 
3361960Sz.hong@f5.com         p = fname;
3371960Sz.hong@f5.com         p = nxt_cpymem(p, shr->start, shr->length);
3382108Salx.manpages@gmail.com         p = nxt_cpymem(p, index->start, index->length);
3391960Sz.hong@f5.com         *p = '\0';
3401859So.canty@f5.com 
3411960Sz.hong@f5.com     } else {
3421960Sz.hong@f5.com         if (conf->types == NULL) {
3431960Sz.hong@f5.com             nxt_str_null(&exten);
3441960Sz.hong@f5.com 
3451960Sz.hong@f5.com         } else {
3461960Sz.hong@f5.com             nxt_http_static_extract_extension(shr, &exten);
3471960Sz.hong@f5.com             mtype = nxt_http_static_mtype_get(&rtcf->mtypes_hash, &exten);
3481855Sz.hong@f5.com 
3491960Sz.hong@f5.com             ret = nxt_http_route_test_rule(r, conf->types, mtype->start,
3501960Sz.hong@f5.com                                            mtype->length);
3511960Sz.hong@f5.com             if (nxt_slow_path(ret == NXT_ERROR)) {
3521960Sz.hong@f5.com                 goto fail;
3531960Sz.hong@f5.com             }
3541183Svbart@nginx.com 
3551960Sz.hong@f5.com             if (ret == 0) {
3561961Sz.hong@f5.com                 nxt_http_static_next(task, r, ctx, NXT_HTTP_FORBIDDEN);
3571961Sz.hong@f5.com                 return;
3581960Sz.hong@f5.com             }
3591960Sz.hong@f5.com         }
3601960Sz.hong@f5.com 
3611960Sz.hong@f5.com         fname = ctx->share.start;
3621960Sz.hong@f5.com     }
3631183Svbart@nginx.com 
3641855Sz.hong@f5.com     nxt_memzero(&file, sizeof(nxt_file_t));
3651855Sz.hong@f5.com 
3661855Sz.hong@f5.com     file.name = fname;
3671855Sz.hong@f5.com 
3681959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
3691959Sz.hong@f5.com     if (conf->resolve != 0 || ctx->chroot.length > 0) {
3701961Sz.hong@f5.com         nxt_str_t                *chr;
3711961Sz.hong@f5.com         nxt_uint_t               resolve;
3721961Sz.hong@f5.com         nxt_http_static_share_t  *share;
3731961Sz.hong@f5.com 
3742096Salx.manpages@gmail.com         share = &conf->shares[ctx->share_idx];
3751855Sz.hong@f5.com 
3761959Sz.hong@f5.com         resolve = conf->resolve;
3771959Sz.hong@f5.com         chr = &ctx->chroot;
3781855Sz.hong@f5.com 
3791959Sz.hong@f5.com         if (chr->length > 0) {
3801959Sz.hong@f5.com             resolve |= RESOLVE_IN_ROOT;
3811855Sz.hong@f5.com 
3821961Sz.hong@f5.com             fname = share->is_const
3831961Sz.hong@f5.com                     ? share->fname
3841960Sz.hong@f5.com                     : nxt_http_static_chroot_match(chr->start, file.name);
3851959Sz.hong@f5.com 
3861959Sz.hong@f5.com             if (fname != NULL) {
3871959Sz.hong@f5.com                 file.name = chr->start;
3881855Sz.hong@f5.com                 ret = nxt_file_open(task, &file, NXT_FILE_SEARCH, NXT_FILE_OPEN,
3891855Sz.hong@f5.com                                     0);
3901855Sz.hong@f5.com 
3911855Sz.hong@f5.com             } else {
3921855Sz.hong@f5.com                 file.error = NXT_EACCES;
3931855Sz.hong@f5.com                 ret = NXT_ERROR;
3941855Sz.hong@f5.com             }
3951855Sz.hong@f5.com 
3961855Sz.hong@f5.com         } else if (fname[0] == '/') {
3971855Sz.hong@f5.com             file.name = (u_char *) "/";
3981855Sz.hong@f5.com             ret = nxt_file_open(task, &file, NXT_FILE_SEARCH, NXT_FILE_OPEN, 0);
3991855Sz.hong@f5.com 
4001855Sz.hong@f5.com         } else {
4011855Sz.hong@f5.com             file.name = (u_char *) ".";
4021855Sz.hong@f5.com             file.fd = AT_FDCWD;
4031855Sz.hong@f5.com             ret = NXT_OK;
4041855Sz.hong@f5.com         }
4051855Sz.hong@f5.com 
4061855Sz.hong@f5.com         if (nxt_fast_path(ret == NXT_OK)) {
4071856Sz.hong@f5.com             nxt_file_t  af;
4081856Sz.hong@f5.com 
4091855Sz.hong@f5.com             af = file;
4101855Sz.hong@f5.com             nxt_memzero(&file, sizeof(nxt_file_t));
4111855Sz.hong@f5.com             file.name = fname;
4121855Sz.hong@f5.com 
4131855Sz.hong@f5.com             ret = nxt_file_openat2(task, &file, NXT_FILE_RDONLY,
4141959Sz.hong@f5.com                                    NXT_FILE_OPEN, 0, af.fd, resolve);
4151855Sz.hong@f5.com 
4161855Sz.hong@f5.com             if (af.fd != AT_FDCWD) {
4171855Sz.hong@f5.com                 nxt_file_close(task, &af);
4181855Sz.hong@f5.com             }
4191855Sz.hong@f5.com         }
4201855Sz.hong@f5.com 
4211855Sz.hong@f5.com     } else {
4221855Sz.hong@f5.com         ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, 0);
4231855Sz.hong@f5.com     }
4241855Sz.hong@f5.com 
4251855Sz.hong@f5.com #else
4261855Sz.hong@f5.com     ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, 0);
4271855Sz.hong@f5.com #endif
4281183Svbart@nginx.com 
4291183Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
4301855Sz.hong@f5.com 
4311855Sz.hong@f5.com         switch (file.error) {
4321183Svbart@nginx.com 
4331200Svbart@nginx.com         /*
4341200Svbart@nginx.com          * For Unix domain sockets "errno" is set to:
4351200Svbart@nginx.com          *  - ENXIO on Linux;
4361200Svbart@nginx.com          *  - EOPNOTSUPP on *BSD, MacOSX, and Solaris.
4371200Svbart@nginx.com          */
4381200Svbart@nginx.com 
4391183Svbart@nginx.com         case NXT_ENOENT:
4401183Svbart@nginx.com         case NXT_ENOTDIR:
4411183Svbart@nginx.com         case NXT_ENAMETOOLONG:
4421200Svbart@nginx.com #if (NXT_LINUX)
4431200Svbart@nginx.com         case NXT_ENXIO:
4441200Svbart@nginx.com #else
4451200Svbart@nginx.com         case NXT_EOPNOTSUPP:
4461200Svbart@nginx.com #endif
4471183Svbart@nginx.com             level = NXT_LOG_ERR;
4481183Svbart@nginx.com             status = NXT_HTTP_NOT_FOUND;
4491183Svbart@nginx.com             break;
4501183Svbart@nginx.com 
4511183Svbart@nginx.com         case NXT_EACCES:
4521855Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
4531855Sz.hong@f5.com         case NXT_ELOOP:
4541855Sz.hong@f5.com         case NXT_EXDEV:
4551855Sz.hong@f5.com #endif
4561183Svbart@nginx.com             level = NXT_LOG_ERR;
4571183Svbart@nginx.com             status = NXT_HTTP_FORBIDDEN;
4581183Svbart@nginx.com             break;
4591183Svbart@nginx.com 
4601183Svbart@nginx.com         default:
4611183Svbart@nginx.com             level = NXT_LOG_ALERT;
4621183Svbart@nginx.com             status = NXT_HTTP_INTERNAL_SERVER_ERROR;
4631183Svbart@nginx.com             break;
4641183Svbart@nginx.com         }
4651183Svbart@nginx.com 
4661183Svbart@nginx.com         if (status != NXT_HTTP_NOT_FOUND) {
4671959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
4681959Sz.hong@f5.com             nxt_str_t  *chr = &ctx->chroot;
4691959Sz.hong@f5.com 
4701959Sz.hong@f5.com             if (chr->length > 0) {
4711857Sz.hong@f5.com                 nxt_log(task, level, "opening \"%s\" at \"%V\" failed %E",
4721959Sz.hong@f5.com                         fname, chr, file.error);
4731855Sz.hong@f5.com 
4741855Sz.hong@f5.com             } else {
4751857Sz.hong@f5.com                 nxt_log(task, level, "opening \"%s\" failed %E",
4761855Sz.hong@f5.com                         fname, file.error);
4771855Sz.hong@f5.com             }
4781959Sz.hong@f5.com 
4791959Sz.hong@f5.com #else
4801959Sz.hong@f5.com             nxt_log(task, level, "opening \"%s\" failed %E", fname, file.error);
4811959Sz.hong@f5.com #endif
4821183Svbart@nginx.com         }
4831183Svbart@nginx.com 
4841961Sz.hong@f5.com         if (level == NXT_LOG_ERR) {
4851961Sz.hong@f5.com             nxt_http_static_next(task, r, ctx, status);
4861961Sz.hong@f5.com             return;
4871961Sz.hong@f5.com         }
4881961Sz.hong@f5.com 
4891959Sz.hong@f5.com         goto fail;
4901183Svbart@nginx.com     }
4911183Svbart@nginx.com 
4921855Sz.hong@f5.com     f = nxt_mp_get(r->mem_pool, sizeof(nxt_file_t));
4931855Sz.hong@f5.com     if (nxt_slow_path(f == NULL)) {
4941962Svbart@nginx.com         nxt_file_close(task, &file);
4951855Sz.hong@f5.com         goto fail;
4961855Sz.hong@f5.com     }
4971855Sz.hong@f5.com 
4981855Sz.hong@f5.com     *f = file;
4991855Sz.hong@f5.com 
5001183Svbart@nginx.com     ret = nxt_file_info(f, &fi);
5011183Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
5021183Svbart@nginx.com         goto fail;
5031183Svbart@nginx.com     }
5041183Svbart@nginx.com 
5051183Svbart@nginx.com     if (nxt_fast_path(nxt_is_file(&fi))) {
5061183Svbart@nginx.com         r->status = NXT_HTTP_OK;
5071183Svbart@nginx.com         r->resp.content_length_n = nxt_file_size(&fi);
5081183Svbart@nginx.com 
5091183Svbart@nginx.com         field = nxt_list_zero_add(r->resp.fields);
5101183Svbart@nginx.com         if (nxt_slow_path(field == NULL)) {
5111183Svbart@nginx.com             goto fail;
5121183Svbart@nginx.com         }
5131183Svbart@nginx.com 
5141183Svbart@nginx.com         nxt_http_field_name_set(field, "Last-Modified");
5151183Svbart@nginx.com 
5161183Svbart@nginx.com         p = nxt_mp_nget(r->mem_pool, NXT_HTTP_DATE_LEN);
5171183Svbart@nginx.com         if (nxt_slow_path(p == NULL)) {
5181183Svbart@nginx.com             goto fail;
5191183Svbart@nginx.com         }
5201183Svbart@nginx.com 
5211183Svbart@nginx.com         nxt_localtime(nxt_file_mtime(&fi), &tm);
5221183Svbart@nginx.com 
5231183Svbart@nginx.com         field->value = p;
5241183Svbart@nginx.com         field->value_length = nxt_http_date(p, &tm) - p;
5251183Svbart@nginx.com 
5261183Svbart@nginx.com         field = nxt_list_zero_add(r->resp.fields);
5271183Svbart@nginx.com         if (nxt_slow_path(field == NULL)) {
5281183Svbart@nginx.com             goto fail;
5291183Svbart@nginx.com         }
5301183Svbart@nginx.com 
5311183Svbart@nginx.com         nxt_http_field_name_set(field, "ETag");
5321183Svbart@nginx.com 
5331855Sz.hong@f5.com         length = NXT_TIME_T_HEXLEN + NXT_OFF_T_HEXLEN + 3;
5341183Svbart@nginx.com 
5351855Sz.hong@f5.com         p = nxt_mp_nget(r->mem_pool, length);
5361183Svbart@nginx.com         if (nxt_slow_path(p == NULL)) {
5371183Svbart@nginx.com             goto fail;
5381183Svbart@nginx.com         }
5391183Svbart@nginx.com 
5401183Svbart@nginx.com         field->value = p;
5411855Sz.hong@f5.com         field->value_length = nxt_sprintf(p, p + length, "\"%xT-%xO\"",
5421183Svbart@nginx.com                                           nxt_file_mtime(&fi),
5431183Svbart@nginx.com                                           nxt_file_size(&fi))
5441183Svbart@nginx.com                               - p;
5451183Svbart@nginx.com 
5461923Sz.hong@f5.com         if (exten.start == NULL) {
5471960Sz.hong@f5.com             nxt_http_static_extract_extension(shr, &exten);
5481183Svbart@nginx.com         }
5491183Svbart@nginx.com 
5501859So.canty@f5.com         if (mtype == NULL) {
5511923Sz.hong@f5.com             mtype = nxt_http_static_mtype_get(&rtcf->mtypes_hash, &exten);
5521859So.canty@f5.com         }
5531183Svbart@nginx.com 
5541883So.canty@f5.com         if (mtype->length != 0) {
5551183Svbart@nginx.com             field = nxt_list_zero_add(r->resp.fields);
5561183Svbart@nginx.com             if (nxt_slow_path(field == NULL)) {
5571183Svbart@nginx.com                 goto fail;
5581183Svbart@nginx.com             }
5591183Svbart@nginx.com 
5601183Svbart@nginx.com             nxt_http_field_name_set(field, "Content-Type");
5611183Svbart@nginx.com 
5621183Svbart@nginx.com             field->value = mtype->start;
5631183Svbart@nginx.com             field->value_length = mtype->length;
5641183Svbart@nginx.com         }
5651183Svbart@nginx.com 
5661959Sz.hong@f5.com         if (ctx->need_body && nxt_file_size(&fi) > 0) {
5671183Svbart@nginx.com             fb = nxt_mp_zget(r->mem_pool, NXT_BUF_FILE_SIZE);
5681183Svbart@nginx.com             if (nxt_slow_path(fb == NULL)) {
5691183Svbart@nginx.com                 goto fail;
5701183Svbart@nginx.com             }
5711183Svbart@nginx.com 
5721183Svbart@nginx.com             fb->file = f;
5731183Svbart@nginx.com             fb->file_end = nxt_file_size(&fi);
5741183Svbart@nginx.com 
5751183Svbart@nginx.com             r->out = fb;
5761183Svbart@nginx.com 
5771183Svbart@nginx.com             body_handler = &nxt_http_static_body_handler;
5781183Svbart@nginx.com 
5791183Svbart@nginx.com         } else {
5801183Svbart@nginx.com             nxt_file_close(task, f);
5811183Svbart@nginx.com             body_handler = NULL;
5821183Svbart@nginx.com         }
5831183Svbart@nginx.com 
5841183Svbart@nginx.com     } else {
5851183Svbart@nginx.com         /* Not a file. */
5861961Sz.hong@f5.com         nxt_file_close(task, f);
5871183Svbart@nginx.com 
5882106Salx.manpages@gmail.com         if (nxt_slow_path(!nxt_is_dir(&fi)
5892106Salx.manpages@gmail.com                           || shr->start[shr->length - 1] == '/'))
5902106Salx.manpages@gmail.com         {
5911961Sz.hong@f5.com             nxt_log(task, NXT_LOG_ERR, "\"%FN\" is not a regular file",
5921961Sz.hong@f5.com                     f->name);
5931378Svbart@nginx.com 
5941961Sz.hong@f5.com             nxt_http_static_next(task, r, ctx, NXT_HTTP_NOT_FOUND);
5951961Sz.hong@f5.com             return;
5961183Svbart@nginx.com         }
5971183Svbart@nginx.com 
5981198Svbart@nginx.com         f = NULL;
5991198Svbart@nginx.com 
6001183Svbart@nginx.com         r->status = NXT_HTTP_MOVED_PERMANENTLY;
6011183Svbart@nginx.com         r->resp.content_length_n = 0;
6021183Svbart@nginx.com 
6031183Svbart@nginx.com         field = nxt_list_zero_add(r->resp.fields);
6041183Svbart@nginx.com         if (nxt_slow_path(field == NULL)) {
6051183Svbart@nginx.com             goto fail;
6061183Svbart@nginx.com         }
6071183Svbart@nginx.com 
6081183Svbart@nginx.com         nxt_http_field_name_set(field, "Location");
6091183Svbart@nginx.com 
6101183Svbart@nginx.com         encode = nxt_encode_uri(NULL, r->path->start, r->path->length);
6111855Sz.hong@f5.com         length = r->path->length + encode * 2 + 1;
6121183Svbart@nginx.com 
6131183Svbart@nginx.com         if (r->args->length > 0) {
6141855Sz.hong@f5.com             length += 1 + r->args->length;
6151183Svbart@nginx.com         }
6161183Svbart@nginx.com 
6171855Sz.hong@f5.com         p = nxt_mp_nget(r->mem_pool, length);
6181183Svbart@nginx.com         if (nxt_slow_path(p == NULL)) {
6191183Svbart@nginx.com             goto fail;
6201183Svbart@nginx.com         }
6211183Svbart@nginx.com 
6221183Svbart@nginx.com         field->value = p;
6231855Sz.hong@f5.com         field->value_length = length;
6241183Svbart@nginx.com 
6251183Svbart@nginx.com         if (encode > 0) {
6261183Svbart@nginx.com             p = (u_char *) nxt_encode_uri(p, r->path->start, r->path->length);
6271183Svbart@nginx.com 
6281183Svbart@nginx.com         } else {
6291183Svbart@nginx.com             p = nxt_cpymem(p, r->path->start, r->path->length);
6301183Svbart@nginx.com         }
6311183Svbart@nginx.com 
6321183Svbart@nginx.com         *p++ = '/';
6331183Svbart@nginx.com 
6341183Svbart@nginx.com         if (r->args->length > 0) {
6351183Svbart@nginx.com             *p++ = '?';
6361183Svbart@nginx.com             nxt_memcpy(p, r->args->start, r->args->length);
6371183Svbart@nginx.com         }
6381183Svbart@nginx.com 
6391183Svbart@nginx.com         body_handler = NULL;
6401183Svbart@nginx.com     }
6411183Svbart@nginx.com 
6421270Sigor@sysoev.ru     nxt_http_request_header_send(task, r, body_handler, NULL);
6431183Svbart@nginx.com 
6441183Svbart@nginx.com     r->state = &nxt_http_static_send_state;
6451959Sz.hong@f5.com     return;
6461183Svbart@nginx.com 
6471183Svbart@nginx.com fail:
6481183Svbart@nginx.com 
6491855Sz.hong@f5.com     if (f != NULL) {
6501183Svbart@nginx.com         nxt_file_close(task, f);
6511183Svbart@nginx.com     }
6521183Svbart@nginx.com 
6531961Sz.hong@f5.com     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
6541959Sz.hong@f5.com }
6551959Sz.hong@f5.com 
6561959Sz.hong@f5.com 
6571959Sz.hong@f5.com static void
nxt_http_static_var_error(nxt_task_t * task,void * obj,void * data)6581959Sz.hong@f5.com nxt_http_static_var_error(nxt_task_t *task, void *obj, void *data)
6591959Sz.hong@f5.com {
6601959Sz.hong@f5.com     nxt_http_request_t  *r;
6611959Sz.hong@f5.com 
6621959Sz.hong@f5.com     r = obj;
6631959Sz.hong@f5.com 
6641959Sz.hong@f5.com     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
6651183Svbart@nginx.com }
6661183Svbart@nginx.com 
6671183Svbart@nginx.com 
6681961Sz.hong@f5.com static void
nxt_http_static_next(nxt_task_t * task,nxt_http_request_t * r,nxt_http_static_ctx_t * ctx,nxt_http_status_t status)6691961Sz.hong@f5.com nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r,
6701961Sz.hong@f5.com     nxt_http_static_ctx_t *ctx, nxt_http_status_t status)
6711961Sz.hong@f5.com {
6721961Sz.hong@f5.com     nxt_http_action_t       *action;
6731961Sz.hong@f5.com     nxt_http_static_conf_t  *conf;
6741961Sz.hong@f5.com 
6751961Sz.hong@f5.com     action = ctx->action;
6761961Sz.hong@f5.com     conf = action->u.conf;
6771961Sz.hong@f5.com 
6782096Salx.manpages@gmail.com     ctx->share_idx++;
6791961Sz.hong@f5.com 
6802096Salx.manpages@gmail.com     if (ctx->share_idx < conf->nshares) {
6811961Sz.hong@f5.com         nxt_http_static_iterate(task, r, ctx);
6821961Sz.hong@f5.com         return;
6831961Sz.hong@f5.com     }
6841961Sz.hong@f5.com 
6851961Sz.hong@f5.com     if (action->fallback != NULL) {
6861961Sz.hong@f5.com         nxt_http_request_action(task, r, action->fallback);
6871961Sz.hong@f5.com         return;
6881961Sz.hong@f5.com     }
6891961Sz.hong@f5.com 
6901961Sz.hong@f5.com     nxt_http_request_error(task, r, status);
6911961Sz.hong@f5.com }
6921961Sz.hong@f5.com 
6931961Sz.hong@f5.com 
6941959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
6951959Sz.hong@f5.com 
6961959Sz.hong@f5.com static u_char *
nxt_http_static_chroot_match(u_char * chr,u_char * shr)6971959Sz.hong@f5.com nxt_http_static_chroot_match(u_char *chr, u_char *shr)
6981959Sz.hong@f5.com {
6991959Sz.hong@f5.com     if (*chr != *shr) {
7001959Sz.hong@f5.com         return NULL;
7011959Sz.hong@f5.com     }
7021959Sz.hong@f5.com 
7031959Sz.hong@f5.com     chr++;
7041959Sz.hong@f5.com     shr++;
7051959Sz.hong@f5.com 
7061959Sz.hong@f5.com     for ( ;; ) {
7071959Sz.hong@f5.com         if (*shr == '\0') {
7081959Sz.hong@f5.com             return NULL;
7091959Sz.hong@f5.com         }
7101959Sz.hong@f5.com 
7111959Sz.hong@f5.com         if (*chr == *shr) {
7121959Sz.hong@f5.com             chr++;
7131959Sz.hong@f5.com             shr++;
7141959Sz.hong@f5.com             continue;
7151959Sz.hong@f5.com         }
7161959Sz.hong@f5.com 
7171959Sz.hong@f5.com         if (*chr == '\0') {
7181959Sz.hong@f5.com             break;
7191959Sz.hong@f5.com         }
7201959Sz.hong@f5.com 
7211959Sz.hong@f5.com         if (*chr == '/') {
7221959Sz.hong@f5.com             if (chr[-1] == '/') {
7231959Sz.hong@f5.com                 chr++;
7241959Sz.hong@f5.com                 continue;
7251959Sz.hong@f5.com             }
7261959Sz.hong@f5.com 
7271959Sz.hong@f5.com         } else if (*shr == '/') {
7281959Sz.hong@f5.com             if (shr[-1] == '/') {
7291959Sz.hong@f5.com                 shr++;
7301959Sz.hong@f5.com                 continue;
7311959Sz.hong@f5.com             }
7321959Sz.hong@f5.com         }
7331959Sz.hong@f5.com 
7341959Sz.hong@f5.com         return NULL;
7351959Sz.hong@f5.com     }
7361959Sz.hong@f5.com 
7371959Sz.hong@f5.com     if (shr[-1] != '/' && *shr != '/') {
7381959Sz.hong@f5.com         return NULL;
7391959Sz.hong@f5.com     }
7401959Sz.hong@f5.com 
7411959Sz.hong@f5.com     while (*shr == '/') {
7421959Sz.hong@f5.com         shr++;
7431959Sz.hong@f5.com     }
7441959Sz.hong@f5.com 
7451959Sz.hong@f5.com     return (*shr != '\0') ? shr : NULL;
7461959Sz.hong@f5.com }
7471959Sz.hong@f5.com 
7481959Sz.hong@f5.com #endif
7491959Sz.hong@f5.com 
7501959Sz.hong@f5.com 
7511183Svbart@nginx.com static void
nxt_http_static_extract_extension(nxt_str_t * path,nxt_str_t * exten)7521923Sz.hong@f5.com nxt_http_static_extract_extension(nxt_str_t *path, nxt_str_t *exten)
7531183Svbart@nginx.com {
7541183Svbart@nginx.com     u_char  ch, *p, *end;
7551183Svbart@nginx.com 
7561183Svbart@nginx.com     end = path->start + path->length;
7571183Svbart@nginx.com     p = end;
7581183Svbart@nginx.com 
7592137Salx.manpages@gmail.com     while (p > path->start) {
7601183Svbart@nginx.com         p--;
7611183Svbart@nginx.com         ch = *p;
7621183Svbart@nginx.com 
7631183Svbart@nginx.com         switch (ch) {
7641183Svbart@nginx.com         case '/':
7651183Svbart@nginx.com             p++;
7661183Svbart@nginx.com             /* Fall through. */
7671183Svbart@nginx.com         case '.':
7682137Salx.manpages@gmail.com             goto extension;
7691183Svbart@nginx.com         }
7701183Svbart@nginx.com     }
7712137Salx.manpages@gmail.com 
7722137Salx.manpages@gmail.com extension:
7732137Salx.manpages@gmail.com 
7742137Salx.manpages@gmail.com     exten->length = end - p;
7752137Salx.manpages@gmail.com     exten->start = p;
7761183Svbart@nginx.com }
7771183Svbart@nginx.com 
7781183Svbart@nginx.com 
7791183Svbart@nginx.com static void
nxt_http_static_body_handler(nxt_task_t * task,void * obj,void * data)7801183Svbart@nginx.com nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data)
7811183Svbart@nginx.com {
7821183Svbart@nginx.com     size_t              alloc;
7831183Svbart@nginx.com     nxt_buf_t           *fb, *b, **next, *out;
7841183Svbart@nginx.com     nxt_off_t           rest;
7851183Svbart@nginx.com     nxt_int_t           n;
7861183Svbart@nginx.com     nxt_work_queue_t    *wq;
7871183Svbart@nginx.com     nxt_http_request_t  *r;
7881183Svbart@nginx.com 
7891183Svbart@nginx.com     r = obj;
7901183Svbart@nginx.com     fb = r->out;
7911183Svbart@nginx.com 
7921183Svbart@nginx.com     rest = fb->file_end - fb->file_pos;
7931183Svbart@nginx.com     out = NULL;
7941183Svbart@nginx.com     next = &out;
7951183Svbart@nginx.com     n = 0;
7961183Svbart@nginx.com 
7971183Svbart@nginx.com     do {
7981183Svbart@nginx.com         alloc = nxt_min(rest, NXT_HTTP_STATIC_BUF_SIZE);
7991183Svbart@nginx.com 
8001183Svbart@nginx.com         b = nxt_buf_mem_alloc(r->mem_pool, alloc, 0);
8011183Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
8021183Svbart@nginx.com             goto fail;
8031183Svbart@nginx.com         }
8041183Svbart@nginx.com 
8051183Svbart@nginx.com         b->completion_handler = nxt_http_static_buf_completion;
8061183Svbart@nginx.com         b->parent = r;
8071183Svbart@nginx.com 
8081183Svbart@nginx.com         nxt_mp_retain(r->mem_pool);
8091183Svbart@nginx.com 
8101183Svbart@nginx.com         *next = b;
8111183Svbart@nginx.com         next = &b->next;
8121183Svbart@nginx.com 
8131183Svbart@nginx.com         rest -= alloc;
8141183Svbart@nginx.com 
8151183Svbart@nginx.com     } while (rest > 0 && ++n < NXT_HTTP_STATIC_BUF_COUNT);
8161183Svbart@nginx.com 
8171183Svbart@nginx.com     wq = &task->thread->engine->fast_work_queue;
8181183Svbart@nginx.com 
8191183Svbart@nginx.com     nxt_sendbuf_drain(task, wq, out);
8201183Svbart@nginx.com     return;
8211183Svbart@nginx.com 
8221183Svbart@nginx.com fail:
8231183Svbart@nginx.com 
8241183Svbart@nginx.com     while (out != NULL) {
8251183Svbart@nginx.com         b = out;
8261183Svbart@nginx.com         out = b->next;
8271183Svbart@nginx.com 
8281183Svbart@nginx.com         nxt_mp_free(r->mem_pool, b);
8291183Svbart@nginx.com         nxt_mp_release(r->mem_pool);
8301183Svbart@nginx.com     }
8311183Svbart@nginx.com }
8321183Svbart@nginx.com 
8331183Svbart@nginx.com 
8341183Svbart@nginx.com static const nxt_http_request_state_t  nxt_http_static_send_state
8351183Svbart@nginx.com     nxt_aligned(64) =
8361183Svbart@nginx.com {
8371183Svbart@nginx.com     .error_handler = nxt_http_request_error_handler,
8381183Svbart@nginx.com };
8391183Svbart@nginx.com 
8401183Svbart@nginx.com 
8411183Svbart@nginx.com static void
nxt_http_static_buf_completion(nxt_task_t * task,void * obj,void * data)8421183Svbart@nginx.com nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data)
8431183Svbart@nginx.com {
8441183Svbart@nginx.com     ssize_t             n, size;
8451760Smax.romanov@nginx.com     nxt_buf_t           *b, *fb, *next;
8461183Svbart@nginx.com     nxt_off_t           rest;
8471183Svbart@nginx.com     nxt_http_request_t  *r;
8481183Svbart@nginx.com 
8491183Svbart@nginx.com     b = obj;
8501183Svbart@nginx.com     r = data;
8511760Smax.romanov@nginx.com 
8521760Smax.romanov@nginx.com complete_buf:
8531760Smax.romanov@nginx.com 
8541183Svbart@nginx.com     fb = r->out;
8551183Svbart@nginx.com 
8561183Svbart@nginx.com     if (nxt_slow_path(fb == NULL || r->error)) {
8571183Svbart@nginx.com         goto clean;
8581183Svbart@nginx.com     }
8591183Svbart@nginx.com 
8601183Svbart@nginx.com     rest = fb->file_end - fb->file_pos;
8611183Svbart@nginx.com     size = nxt_buf_mem_size(&b->mem);
8621183Svbart@nginx.com 
8631183Svbart@nginx.com     size = nxt_min(rest, (nxt_off_t) size);
8641183Svbart@nginx.com 
8651183Svbart@nginx.com     n = nxt_file_read(fb->file, b->mem.start, size, fb->file_pos);
8661183Svbart@nginx.com 
8671183Svbart@nginx.com     if (n != size) {
8681183Svbart@nginx.com         if (n >= 0) {
8691183Svbart@nginx.com             nxt_log(task, NXT_LOG_ERR, "file \"%FN\" has changed "
8701183Svbart@nginx.com                     "while sending response to a client", fb->file->name);
8711183Svbart@nginx.com         }
8721183Svbart@nginx.com 
8731183Svbart@nginx.com         nxt_http_request_error_handler(task, r, r->proto.any);
8741183Svbart@nginx.com         goto clean;
8751183Svbart@nginx.com     }
8761183Svbart@nginx.com 
8771760Smax.romanov@nginx.com     next = b->next;
8781760Smax.romanov@nginx.com 
8791183Svbart@nginx.com     if (n == rest) {
8801183Svbart@nginx.com         nxt_file_close(task, fb->file);
8811183Svbart@nginx.com         r->out = NULL;
8821183Svbart@nginx.com 
8831183Svbart@nginx.com         b->next = nxt_http_buf_last(r);
8841183Svbart@nginx.com 
8851183Svbart@nginx.com     } else {
8861183Svbart@nginx.com         fb->file_pos += n;
8871183Svbart@nginx.com         b->next = NULL;
8881183Svbart@nginx.com     }
8891183Svbart@nginx.com 
8901183Svbart@nginx.com     b->mem.pos = b->mem.start;
8911183Svbart@nginx.com     b->mem.free = b->mem.pos + n;
8921183Svbart@nginx.com 
8931183Svbart@nginx.com     nxt_http_request_send(task, r, b);
8941760Smax.romanov@nginx.com 
8951760Smax.romanov@nginx.com     if (next != NULL) {
8961760Smax.romanov@nginx.com         b = next;
8971760Smax.romanov@nginx.com         goto complete_buf;
8981760Smax.romanov@nginx.com     }
8991760Smax.romanov@nginx.com 
9001183Svbart@nginx.com     return;
9011183Svbart@nginx.com 
9021183Svbart@nginx.com clean:
9031183Svbart@nginx.com 
9041760Smax.romanov@nginx.com     do {
9051760Smax.romanov@nginx.com         next = b->next;
9061760Smax.romanov@nginx.com 
9071760Smax.romanov@nginx.com         nxt_mp_free(r->mem_pool, b);
9081760Smax.romanov@nginx.com         nxt_mp_release(r->mem_pool);
9091760Smax.romanov@nginx.com 
9101760Smax.romanov@nginx.com         b = next;
9111760Smax.romanov@nginx.com     } while (b != NULL);
9121183Svbart@nginx.com 
9131183Svbart@nginx.com     if (fb != NULL) {
9141183Svbart@nginx.com         nxt_file_close(task, fb->file);
9151183Svbart@nginx.com         r->out = NULL;
9161183Svbart@nginx.com     }
9171183Svbart@nginx.com }
9181183Svbart@nginx.com 
9191183Svbart@nginx.com 
9201183Svbart@nginx.com nxt_int_t
nxt_http_static_mtypes_init(nxt_mp_t * mp,nxt_lvlhsh_t * hash)9211183Svbart@nginx.com nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash)
9221183Svbart@nginx.com {
9231923Sz.hong@f5.com     nxt_str_t   *type, exten;
9241183Svbart@nginx.com     nxt_int_t   ret;
9251183Svbart@nginx.com     nxt_uint_t  i;
9261183Svbart@nginx.com 
9271183Svbart@nginx.com     static const struct {
9281183Svbart@nginx.com         nxt_str_t   type;
9291923Sz.hong@f5.com         const char  *exten;
9301183Svbart@nginx.com     } default_types[] = {
9311183Svbart@nginx.com 
9321183Svbart@nginx.com         { nxt_string("text/html"),      ".html"  },
9331183Svbart@nginx.com         { nxt_string("text/html"),      ".htm"   },
9341183Svbart@nginx.com         { nxt_string("text/css"),       ".css"   },
9351183Svbart@nginx.com 
9361183Svbart@nginx.com         { nxt_string("image/svg+xml"),  ".svg"   },
9371183Svbart@nginx.com         { nxt_string("image/webp"),     ".webp"  },
9381183