xref: /unit/src/nxt_http_static.c (revision 2633:e3a60b76964a)
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 {
112246Sz.hong@f5.com     nxt_tstr_t                  *tstr;
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)
242246Sz.hong@f5.com     nxt_tstr_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);
512246Sz.hong@f5.com static void nxt_http_static_send_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;
802246Sz.hong@f5.com     nxt_tstr_t              *tstr;
811961Sz.hong@f5.com     nxt_conf_value_t        *cv;
822147Sz.hong@f5.com     nxt_router_conf_t       *rtcf;
831923Sz.hong@f5.com     nxt_http_static_conf_t  *conf;
841923Sz.hong@f5.com 
852147Sz.hong@f5.com     rtcf = tmcf->router_conf;
862147Sz.hong@f5.com     mp = rtcf->mem_pool;
871923Sz.hong@f5.com 
881923Sz.hong@f5.com     conf = nxt_mp_zget(mp, sizeof(nxt_http_static_conf_t));
891923Sz.hong@f5.com     if (nxt_slow_path(conf == NULL)) {
901923Sz.hong@f5.com         return NXT_ERROR;
911923Sz.hong@f5.com     }
921923Sz.hong@f5.com 
931923Sz.hong@f5.com     action->handler = nxt_http_static;
941923Sz.hong@f5.com     action->u.conf = conf;
951923Sz.hong@f5.com 
962077Salx.manpages@gmail.com     conf->nshares = nxt_conf_array_elements_count_or_1(acf->share);
971961Sz.hong@f5.com     conf->shares = nxt_mp_zget(mp, sizeof(nxt_http_static_share_t)
981961Sz.hong@f5.com                                    * conf->nshares);
991961Sz.hong@f5.com     if (nxt_slow_path(conf->shares == NULL)) {
1001923Sz.hong@f5.com         return NXT_ERROR;
1011923Sz.hong@f5.com     }
1021923Sz.hong@f5.com 
1032077Salx.manpages@gmail.com     for (i = 0; i < conf->nshares; i++) {
1042077Salx.manpages@gmail.com         cv = nxt_conf_get_array_element_or_itself(acf->share, i);
1052077Salx.manpages@gmail.com         nxt_conf_get_string(cv, &str);
1061961Sz.hong@f5.com 
1072246Sz.hong@f5.com         tstr = nxt_tstr_compile(rtcf->tstr_state, &str, NXT_TSTR_STRZ);
1082246Sz.hong@f5.com         if (nxt_slow_path(tstr == NULL)) {
1091961Sz.hong@f5.com             return NXT_ERROR;
1101961Sz.hong@f5.com         }
1111961Sz.hong@f5.com 
1122246Sz.hong@f5.com         conf->shares[i].tstr = tstr;
1132246Sz.hong@f5.com         conf->shares[i].is_const = nxt_tstr_is_const(tstr);
1141961Sz.hong@f5.com     }
1151959Sz.hong@f5.com 
1162108Salx.manpages@gmail.com     if (acf->index == NULL) {
1172108Salx.manpages@gmail.com         nxt_str_set(&conf->index, "index.html");
1182108Salx.manpages@gmail.com 
1192108Salx.manpages@gmail.com     } else {
120*2633Salx@kernel.org         ret = nxt_conf_get_string_dup(acf->index, mp, &conf->index);
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 
1312246Sz.hong@f5.com         conf->chroot = nxt_tstr_compile(rtcf->tstr_state, &acf->chroot,
1322246Sz.hong@f5.com                                         NXT_TSTR_STRZ);
1331959Sz.hong@f5.com         if (nxt_slow_path(conf->chroot == NULL)) {
1341923Sz.hong@f5.com             return NXT_ERROR;
1351923Sz.hong@f5.com         }
1361960Sz.hong@f5.com 
1372246Sz.hong@f5.com         is_const = nxt_tstr_is_const(conf->chroot);
1381961Sz.hong@f5.com 
1391961Sz.hong@f5.com         for (i = 0; i < conf->nshares; i++) {
1401961Sz.hong@f5.com             conf->shares[i].is_const &= is_const;
1411960Sz.hong@f5.com 
1421961Sz.hong@f5.com             if (conf->shares[i].is_const) {
1432246Sz.hong@f5.com                 nxt_tstr_str(conf->chroot, &chr);
1442246Sz.hong@f5.com                 nxt_tstr_str(conf->shares[i].tstr, &shr);
1451961Sz.hong@f5.com 
1461961Sz.hong@f5.com                 conf->shares[i].fname = nxt_http_static_chroot_match(chr.start,
1471961Sz.hong@f5.com                                                                      shr.start);
1481961Sz.hong@f5.com             }
1491960Sz.hong@f5.com         }
1501923Sz.hong@f5.com     }
1511923Sz.hong@f5.com 
1521923Sz.hong@f5.com     if (acf->follow_symlinks != NULL
1531923Sz.hong@f5.com         && !nxt_conf_get_boolean(acf->follow_symlinks))
1541923Sz.hong@f5.com     {
1551923Sz.hong@f5.com         conf->resolve |= RESOLVE_NO_SYMLINKS;
1561923Sz.hong@f5.com     }
1571923Sz.hong@f5.com 
1581923Sz.hong@f5.com     if (acf->traverse_mounts != NULL
1591923Sz.hong@f5.com         && !nxt_conf_get_boolean(acf->traverse_mounts))
1601923Sz.hong@f5.com     {
1611923Sz.hong@f5.com         conf->resolve |= RESOLVE_NO_XDEV;
1621923Sz.hong@f5.com     }
1631923Sz.hong@f5.com #endif
1641923Sz.hong@f5.com 
1651923Sz.hong@f5.com     if (acf->types != NULL) {
1661923Sz.hong@f5.com         conf->types = nxt_http_route_types_rule_create(task, mp, acf->types);
1671923Sz.hong@f5.com         if (nxt_slow_path(conf->types == NULL)) {
1681923Sz.hong@f5.com             return NXT_ERROR;
1691923Sz.hong@f5.com         }
1701923Sz.hong@f5.com     }
1711923Sz.hong@f5.com 
1721923Sz.hong@f5.com     if (acf->fallback != NULL) {
1731923Sz.hong@f5.com         action->fallback = nxt_mp_alloc(mp, sizeof(nxt_http_action_t));
1741923Sz.hong@f5.com         if (nxt_slow_path(action->fallback == NULL)) {
1751923Sz.hong@f5.com             return NXT_ERROR;
1761923Sz.hong@f5.com         }
1771923Sz.hong@f5.com 
1781923Sz.hong@f5.com         return nxt_http_action_init(task, tmcf, acf->fallback,
1791923Sz.hong@f5.com                                     action->fallback);
1801923Sz.hong@f5.com     }
1811923Sz.hong@f5.com 
1821923Sz.hong@f5.com     return NXT_OK;
1831923Sz.hong@f5.com }
1841923Sz.hong@f5.com 
1851923Sz.hong@f5.com 
1861923Sz.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)1871923Sz.hong@f5.com nxt_http_static(nxt_task_t *task, nxt_http_request_t *r,
1881264Sigor@sysoev.ru     nxt_http_action_t *action)
1891183Svbart@nginx.com {
1901961Sz.hong@f5.com     nxt_bool_t             need_body;
1911961Sz.hong@f5.com     nxt_http_static_ctx_t  *ctx;
1921923Sz.hong@f5.com 
1931183Svbart@nginx.com     if (nxt_slow_path(!nxt_str_eq(r->method, "GET", 3))) {
1941183Svbart@nginx.com 
1951183Svbart@nginx.com         if (!nxt_str_eq(r->method, "HEAD", 4)) {
1961923Sz.hong@f5.com             if (action->fallback != NULL) {
1972381Salx@nginx.com                 if (nxt_slow_path(r->log_route)) {
1982381Salx@nginx.com                     nxt_log(task, NXT_LOG_NOTICE, "\"fallback\" taken");
1992381Salx@nginx.com                 }
2001923Sz.hong@f5.com                 return action->fallback;
2011378Svbart@nginx.com             }
2021378Svbart@nginx.com 
2031183Svbart@nginx.com             nxt_http_request_error(task, r, NXT_HTTP_METHOD_NOT_ALLOWED);
2041183Svbart@nginx.com             return NULL;
2051183Svbart@nginx.com         }
2061183Svbart@nginx.com 
2071183Svbart@nginx.com         need_body = 0;
2081183Svbart@nginx.com 
2091183Svbart@nginx.com     } else {
2101183Svbart@nginx.com         need_body = 1;
2111183Svbart@nginx.com     }
2121183Svbart@nginx.com 
2131961Sz.hong@f5.com     ctx = nxt_mp_zget(r->mem_pool, sizeof(nxt_http_static_ctx_t));
2141961Sz.hong@f5.com     if (nxt_slow_path(ctx == NULL)) {
2151961Sz.hong@f5.com         nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
2161961Sz.hong@f5.com         return NULL;
2171961Sz.hong@f5.com     }
2181961Sz.hong@f5.com 
2191961Sz.hong@f5.com     ctx->action = action;
2201961Sz.hong@f5.com     ctx->need_body = need_body;
2211961Sz.hong@f5.com 
2221961Sz.hong@f5.com     nxt_http_static_iterate(task, r, ctx);
2231961Sz.hong@f5.com 
2241961Sz.hong@f5.com     return NULL;
2251961Sz.hong@f5.com }
2261961Sz.hong@f5.com 
2271961Sz.hong@f5.com 
2281961Sz.hong@f5.com static void
nxt_http_static_iterate(nxt_task_t * task,nxt_http_request_t * r,nxt_http_static_ctx_t * ctx)2291961Sz.hong@f5.com nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
2301961Sz.hong@f5.com     nxt_http_static_ctx_t *ctx)
2311961Sz.hong@f5.com {
2321961Sz.hong@f5.com     nxt_int_t                ret;
2332246Sz.hong@f5.com     nxt_router_conf_t        *rtcf;
2341961Sz.hong@f5.com     nxt_http_static_conf_t   *conf;
2351961Sz.hong@f5.com     nxt_http_static_share_t  *share;
2361961Sz.hong@f5.com 
2371961Sz.hong@f5.com     conf = ctx->action->u.conf;
2381961Sz.hong@f5.com 
2392096Salx.manpages@gmail.com     share = &conf->shares[ctx->share_idx];
2401959Sz.hong@f5.com 
2411960Sz.hong@f5.com #if (NXT_DEBUG)
2421960Sz.hong@f5.com     nxt_str_t  shr;
2432108Salx.manpages@gmail.com     nxt_str_t  idx;
2441960Sz.hong@f5.com 
2452246Sz.hong@f5.com     nxt_tstr_str(share->tstr, &shr);
2462108Salx.manpages@gmail.com     idx = conf->index;
2471960Sz.hong@f5.com 
2481960Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
2491959Sz.hong@f5.com     nxt_str_t  chr;
2501959Sz.hong@f5.com 
2511959Sz.hong@f5.com     if (conf->chroot != NULL) {
2522246Sz.hong@f5.com         nxt_tstr_str(conf->chroot, &chr);
2531959Sz.hong@f5.com 
2541959Sz.hong@f5.com     } else {
2551959Sz.hong@f5.com         nxt_str_set(&chr, "");
2561959Sz.hong@f5.com     }
2571959Sz.hong@f5.com 
2582108Salx.manpages@gmail.com     nxt_debug(task, "http static: \"%V\", index: \"%V\" (chroot: \"%V\")",
2592108Salx.manpages@gmail.com               &shr, &idx, &chr);
2601959Sz.hong@f5.com #else
2612108Salx.manpages@gmail.com     nxt_debug(task, "http static: \"%V\", index: \"%V\"", &shr, &idx);
2621959Sz.hong@f5.com #endif
2631960Sz.hong@f5.com #endif /* NXT_DEBUG */
2641959Sz.hong@f5.com 
2651961Sz.hong@f5.com     if (share->is_const) {
2662246Sz.hong@f5.com         nxt_tstr_str(share->tstr, &ctx->share);
2671960Sz.hong@f5.com 
2681959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
2692096Salx.manpages@gmail.com         if (conf->chroot != NULL && ctx->share_idx == 0) {
2702246Sz.hong@f5.com             nxt_tstr_str(conf->chroot, &ctx->chroot);
2711959Sz.hong@f5.com         }
2721959Sz.hong@f5.com #endif
2731959Sz.hong@f5.com 
2741959Sz.hong@f5.com         nxt_http_static_send_ready(task, r, ctx);
2751959Sz.hong@f5.com 
2761959Sz.hong@f5.com     } else {
2772246Sz.hong@f5.com         rtcf = r->conf->socket_conf->router_conf;
2782246Sz.hong@f5.com 
2792246Sz.hong@f5.com         ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state,
2802247Sz.hong@f5.com                                   &r->tstr_cache, r, r->mem_pool);
2811959Sz.hong@f5.com         if (nxt_slow_path(ret != NXT_OK)) {
2821961Sz.hong@f5.com             nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
2831961Sz.hong@f5.com             return;
2841959Sz.hong@f5.com         }
2851959Sz.hong@f5.com 
2862246Sz.hong@f5.com         nxt_tstr_query(task, r->tstr_query, share->tstr, &ctx->share);
2871960Sz.hong@f5.com 
2881959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
2892096Salx.manpages@gmail.com         if (conf->chroot != NULL && ctx->share_idx == 0) {
2902246Sz.hong@f5.com             nxt_tstr_query(task, r->tstr_query, conf->chroot, &ctx->chroot);
2911960Sz.hong@f5.com         }
2921959Sz.hong@f5.com #endif
2931959Sz.hong@f5.com 
2942246Sz.hong@f5.com         nxt_tstr_query_resolve(task, r->tstr_query, ctx,
2952246Sz.hong@f5.com                                nxt_http_static_send_ready,
2962246Sz.hong@f5.com                                nxt_http_static_send_error);
2971961Sz.hong@f5.com      }
2981959Sz.hong@f5.com }
2991959Sz.hong@f5.com 
3001959Sz.hong@f5.com 
3011959Sz.hong@f5.com static void
nxt_http_static_send_ready(nxt_task_t * task,void * obj,void * data)3021959Sz.hong@f5.com nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
3031959Sz.hong@f5.com {
3041959Sz.hong@f5.com     size_t                  length, encode;
3051959Sz.hong@f5.com     u_char                  *p, *fname;
3061959Sz.hong@f5.com     struct tm               tm;
3071959Sz.hong@f5.com     nxt_buf_t               *fb;
3081959Sz.hong@f5.com     nxt_int_t               ret;
3092108Salx.manpages@gmail.com     nxt_str_t               *shr, *index, exten, *mtype;
3101959Sz.hong@f5.com     nxt_uint_t              level;
3111959Sz.hong@f5.com     nxt_file_t              *f, file;
3121959Sz.hong@f5.com     nxt_file_info_t         fi;
3131959Sz.hong@f5.com     nxt_http_field_t        *field;
3141959Sz.hong@f5.com     nxt_http_status_t       status;
3151959Sz.hong@f5.com     nxt_router_conf_t       *rtcf;
3161959Sz.hong@f5.com     nxt_http_action_t       *action;
3171959Sz.hong@f5.com     nxt_http_request_t      *r;
3181959Sz.hong@f5.com     nxt_work_handler_t      body_handler;
3191959Sz.hong@f5.com     nxt_http_static_ctx_t   *ctx;
3201959Sz.hong@f5.com     nxt_http_static_conf_t  *conf;
3211959Sz.hong@f5.com 
3221959Sz.hong@f5.com     r = obj;
3231959Sz.hong@f5.com     ctx = data;
3241959Sz.hong@f5.com     action = ctx->action;
3251959Sz.hong@f5.com     conf = action->u.conf;
3261960Sz.hong@f5.com     rtcf = r->conf->socket_conf->router_conf;
3271183Svbart@nginx.com 
3281855Sz.hong@f5.com     f = NULL;
3291960Sz.hong@f5.com     mtype = NULL;
3301183Svbart@nginx.com 
3311960Sz.hong@f5.com     shr = &ctx->share;
3322108Salx.manpages@gmail.com     index = &conf->index;
3331859So.canty@f5.com 
3341960Sz.hong@f5.com     if (shr->start[shr->length - 1] == '/') {
3352108Salx.manpages@gmail.com         nxt_http_static_extract_extension(index, &exten);
3361859So.canty@f5.com 
3372108Salx.manpages@gmail.com         length = shr->length + index->length;
3381960Sz.hong@f5.com 
3391960Sz.hong@f5.com         fname = nxt_mp_nget(r->mem_pool, length + 1);
3401960Sz.hong@f5.com         if (nxt_slow_path(fname == NULL)) {
3411883So.canty@f5.com             goto fail;
3421883So.canty@f5.com         }
3431883So.canty@f5.com 
3441960Sz.hong@f5.com         p = fname;
3451960Sz.hong@f5.com         p = nxt_cpymem(p, shr->start, shr->length);
3462108Salx.manpages@gmail.com         p = nxt_cpymem(p, index->start, index->length);
3471960Sz.hong@f5.com         *p = '\0';
3481859So.canty@f5.com 
3491960Sz.hong@f5.com     } else {
3501960Sz.hong@f5.com         if (conf->types == NULL) {
3511960Sz.hong@f5.com             nxt_str_null(&exten);
3521960Sz.hong@f5.com 
3531960Sz.hong@f5.com         } else {
3541960Sz.hong@f5.com             nxt_http_static_extract_extension(shr, &exten);
3551960Sz.hong@f5.com             mtype = nxt_http_static_mtype_get(&rtcf->mtypes_hash, &exten);
3561855Sz.hong@f5.com 
3571960Sz.hong@f5.com             ret = nxt_http_route_test_rule(r, conf->types, mtype->start,
3581960Sz.hong@f5.com                                            mtype->length);
3591960Sz.hong@f5.com             if (nxt_slow_path(ret == NXT_ERROR)) {
3601960Sz.hong@f5.com                 goto fail;
3611960Sz.hong@f5.com             }
3621183Svbart@nginx.com 
3631960Sz.hong@f5.com             if (ret == 0) {
3641961Sz.hong@f5.com                 nxt_http_static_next(task, r, ctx, NXT_HTTP_FORBIDDEN);
3651961Sz.hong@f5.com                 return;
3661960Sz.hong@f5.com             }
3671960Sz.hong@f5.com         }
3681960Sz.hong@f5.com 
3691960Sz.hong@f5.com         fname = ctx->share.start;
3701960Sz.hong@f5.com     }
3711183Svbart@nginx.com 
3721855Sz.hong@f5.com     nxt_memzero(&file, sizeof(nxt_file_t));
3731855Sz.hong@f5.com 
3741855Sz.hong@f5.com     file.name = fname;
3751855Sz.hong@f5.com 
3761959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
3771959Sz.hong@f5.com     if (conf->resolve != 0 || ctx->chroot.length > 0) {
3781961Sz.hong@f5.com         nxt_str_t                *chr;
3791961Sz.hong@f5.com         nxt_uint_t               resolve;
3801961Sz.hong@f5.com         nxt_http_static_share_t  *share;
3811961Sz.hong@f5.com 
3822096Salx.manpages@gmail.com         share = &conf->shares[ctx->share_idx];
3831855Sz.hong@f5.com 
3841959Sz.hong@f5.com         resolve = conf->resolve;
3851959Sz.hong@f5.com         chr = &ctx->chroot;
3861855Sz.hong@f5.com 
3871959Sz.hong@f5.com         if (chr->length > 0) {
3881959Sz.hong@f5.com             resolve |= RESOLVE_IN_ROOT;
3891855Sz.hong@f5.com 
3901961Sz.hong@f5.com             fname = share->is_const
3911961Sz.hong@f5.com                     ? share->fname
3921960Sz.hong@f5.com                     : nxt_http_static_chroot_match(chr->start, file.name);
3931959Sz.hong@f5.com 
3941959Sz.hong@f5.com             if (fname != NULL) {
3951959Sz.hong@f5.com                 file.name = chr->start;
3961855Sz.hong@f5.com                 ret = nxt_file_open(task, &file, NXT_FILE_SEARCH, NXT_FILE_OPEN,
3971855Sz.hong@f5.com                                     0);
3981855Sz.hong@f5.com 
3991855Sz.hong@f5.com             } else {
4001855Sz.hong@f5.com                 file.error = NXT_EACCES;
4011855Sz.hong@f5.com                 ret = NXT_ERROR;
4021855Sz.hong@f5.com             }
4031855Sz.hong@f5.com 
4041855Sz.hong@f5.com         } else if (fname[0] == '/') {
4051855Sz.hong@f5.com             file.name = (u_char *) "/";
4061855Sz.hong@f5.com             ret = nxt_file_open(task, &file, NXT_FILE_SEARCH, NXT_FILE_OPEN, 0);
4071855Sz.hong@f5.com 
4081855Sz.hong@f5.com         } else {
4091855Sz.hong@f5.com             file.name = (u_char *) ".";
4101855Sz.hong@f5.com             file.fd = AT_FDCWD;
4111855Sz.hong@f5.com             ret = NXT_OK;
4121855Sz.hong@f5.com         }
4131855Sz.hong@f5.com 
4141855Sz.hong@f5.com         if (nxt_fast_path(ret == NXT_OK)) {
4151856Sz.hong@f5.com             nxt_file_t  af;
4161856Sz.hong@f5.com 
4171855Sz.hong@f5.com             af = file;
4181855Sz.hong@f5.com             nxt_memzero(&file, sizeof(nxt_file_t));
4191855Sz.hong@f5.com             file.name = fname;
4201855Sz.hong@f5.com 
4211855Sz.hong@f5.com             ret = nxt_file_openat2(task, &file, NXT_FILE_RDONLY,
4221959Sz.hong@f5.com                                    NXT_FILE_OPEN, 0, af.fd, resolve);
4231855Sz.hong@f5.com 
4241855Sz.hong@f5.com             if (af.fd != AT_FDCWD) {
4251855Sz.hong@f5.com                 nxt_file_close(task, &af);
4261855Sz.hong@f5.com             }
4271855Sz.hong@f5.com         }
4281855Sz.hong@f5.com 
4291855Sz.hong@f5.com     } else {
4301855Sz.hong@f5.com         ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, 0);
4311855Sz.hong@f5.com     }
4321855Sz.hong@f5.com 
4331855Sz.hong@f5.com #else
4341855Sz.hong@f5.com     ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, 0);
4351855Sz.hong@f5.com #endif
4361183Svbart@nginx.com 
4371183Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
4381855Sz.hong@f5.com 
4391855Sz.hong@f5.com         switch (file.error) {
4401183Svbart@nginx.com 
4411200Svbart@nginx.com         /*
4421200Svbart@nginx.com          * For Unix domain sockets "errno" is set to:
4431200Svbart@nginx.com          *  - ENXIO on Linux;
4441200Svbart@nginx.com          *  - EOPNOTSUPP on *BSD, MacOSX, and Solaris.
4451200Svbart@nginx.com          */
4461200Svbart@nginx.com 
4471183Svbart@nginx.com         case NXT_ENOENT:
4481183Svbart@nginx.com         case NXT_ENOTDIR:
4491183Svbart@nginx.com         case NXT_ENAMETOOLONG:
4501200Svbart@nginx.com #if (NXT_LINUX)
4511200Svbart@nginx.com         case NXT_ENXIO:
4521200Svbart@nginx.com #else
4531200Svbart@nginx.com         case NXT_EOPNOTSUPP:
4541200Svbart@nginx.com #endif
4551183Svbart@nginx.com             level = NXT_LOG_ERR;
4561183Svbart@nginx.com             status = NXT_HTTP_NOT_FOUND;
4571183Svbart@nginx.com             break;
4581183Svbart@nginx.com 
4591183Svbart@nginx.com         case NXT_EACCES:
4601855Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
4611855Sz.hong@f5.com         case NXT_ELOOP:
4621855Sz.hong@f5.com         case NXT_EXDEV:
4631855Sz.hong@f5.com #endif
4641183Svbart@nginx.com             level = NXT_LOG_ERR;
4651183Svbart@nginx.com             status = NXT_HTTP_FORBIDDEN;
4661183Svbart@nginx.com             break;
4671183Svbart@nginx.com 
4681183Svbart@nginx.com         default:
4691183Svbart@nginx.com             level = NXT_LOG_ALERT;
4701183Svbart@nginx.com             status = NXT_HTTP_INTERNAL_SERVER_ERROR;
4711183Svbart@nginx.com             break;
4721183Svbart@nginx.com         }
4731183Svbart@nginx.com 
4741183Svbart@nginx.com         if (status != NXT_HTTP_NOT_FOUND) {
4751959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
4761959Sz.hong@f5.com             nxt_str_t  *chr = &ctx->chroot;
4771959Sz.hong@f5.com 
4781959Sz.hong@f5.com             if (chr->length > 0) {
4791857Sz.hong@f5.com                 nxt_log(task, level, "opening \"%s\" at \"%V\" failed %E",
4801959Sz.hong@f5.com                         fname, chr, file.error);
4811855Sz.hong@f5.com 
4821855Sz.hong@f5.com             } else {
4831857Sz.hong@f5.com                 nxt_log(task, level, "opening \"%s\" failed %E",
4841855Sz.hong@f5.com                         fname, file.error);
4851855Sz.hong@f5.com             }
4861959Sz.hong@f5.com 
4871959Sz.hong@f5.com #else
4881959Sz.hong@f5.com             nxt_log(task, level, "opening \"%s\" failed %E", fname, file.error);
4891959Sz.hong@f5.com #endif
4901183Svbart@nginx.com         }
4911183Svbart@nginx.com 
4921961Sz.hong@f5.com         if (level == NXT_LOG_ERR) {
4931961Sz.hong@f5.com             nxt_http_static_next(task, r, ctx, status);
4941961Sz.hong@f5.com             return;
4951961Sz.hong@f5.com         }
4961961Sz.hong@f5.com 
4971959Sz.hong@f5.com         goto fail;
4981183Svbart@nginx.com     }
4991183Svbart@nginx.com 
5001855Sz.hong@f5.com     f = nxt_mp_get(r->mem_pool, sizeof(nxt_file_t));
5011855Sz.hong@f5.com     if (nxt_slow_path(f == NULL)) {
5021962Svbart@nginx.com         nxt_file_close(task, &file);
5031855Sz.hong@f5.com         goto fail;
5041855Sz.hong@f5.com     }
5051855Sz.hong@f5.com 
5061855Sz.hong@f5.com     *f = file;
5071855Sz.hong@f5.com 
5081183Svbart@nginx.com     ret = nxt_file_info(f, &fi);
5091183Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
5101183Svbart@nginx.com         goto fail;
5111183Svbart@nginx.com     }
5121183Svbart@nginx.com 
5131183Svbart@nginx.com     if (nxt_fast_path(nxt_is_file(&fi))) {
5141183Svbart@nginx.com         r->status = NXT_HTTP_OK;
5151183Svbart@nginx.com         r->resp.content_length_n = nxt_file_size(&fi);
5161183Svbart@nginx.com 
5171183Svbart@nginx.com         field = nxt_list_zero_add(r->resp.fields);
5181183Svbart@nginx.com         if (nxt_slow_path(field == NULL)) {
5191183Svbart@nginx.com             goto fail;
5201183Svbart@nginx.com         }
5211183Svbart@nginx.com 
5221183Svbart@nginx.com         nxt_http_field_name_set(field, "Last-Modified");
5231183Svbart@nginx.com 
5241183Svbart@nginx.com         p = nxt_mp_nget(r->mem_pool, NXT_HTTP_DATE_LEN);
5251183Svbart@nginx.com         if (nxt_slow_path(p == NULL)) {
5261183Svbart@nginx.com             goto fail;
5271183Svbart@nginx.com         }
5281183Svbart@nginx.com 
5291183Svbart@nginx.com         nxt_localtime(nxt_file_mtime(&fi), &tm);
5301183Svbart@nginx.com 
5311183Svbart@nginx.com         field->value = p;
5321183Svbart@nginx.com         field->value_length = nxt_http_date(p, &tm) - p;
5331183Svbart@nginx.com 
5341183Svbart@nginx.com         field = nxt_list_zero_add(r->resp.fields);
5351183Svbart@nginx.com         if (nxt_slow_path(field == NULL)) {
5361183Svbart@nginx.com             goto fail;
5371183Svbart@nginx.com         }
5381183Svbart@nginx.com 
5391183Svbart@nginx.com         nxt_http_field_name_set(field, "ETag");
5401183Svbart@nginx.com 
5411855Sz.hong@f5.com         length = NXT_TIME_T_HEXLEN + NXT_OFF_T_HEXLEN + 3;
5421183Svbart@nginx.com 
5431855Sz.hong@f5.com         p = nxt_mp_nget(r->mem_pool, length);
5441183Svbart@nginx.com         if (nxt_slow_path(p == NULL)) {
5451183Svbart@nginx.com             goto fail;
5461183Svbart@nginx.com         }
5471183Svbart@nginx.com 
5481183Svbart@nginx.com         field->value = p;
5491855Sz.hong@f5.com         field->value_length = nxt_sprintf(p, p + length, "\"%xT-%xO\"",
5501183Svbart@nginx.com                                           nxt_file_mtime(&fi),
5511183Svbart@nginx.com                                           nxt_file_size(&fi))
5521183Svbart@nginx.com                               - p;
5531183Svbart@nginx.com 
5541923Sz.hong@f5.com         if (exten.start == NULL) {
5551960Sz.hong@f5.com             nxt_http_static_extract_extension(shr, &exten);
5561183Svbart@nginx.com         }
5571183Svbart@nginx.com 
5581859So.canty@f5.com         if (mtype == NULL) {
5591923Sz.hong@f5.com             mtype = nxt_http_static_mtype_get(&rtcf->mtypes_hash, &exten);
5601859So.canty@f5.com         }
5611183Svbart@nginx.com 
5621883So.canty@f5.com         if (mtype->length != 0) {
5631183Svbart@nginx.com             field = nxt_list_zero_add(r->resp.fields);
5641183Svbart@nginx.com             if (nxt_slow_path(field == NULL)) {
5651183Svbart@nginx.com                 goto fail;
5661183Svbart@nginx.com             }
5671183Svbart@nginx.com 
5681183Svbart@nginx.com             nxt_http_field_name_set(field, "Content-Type");
5691183Svbart@nginx.com 
5701183Svbart@nginx.com             field->value = mtype->start;
5711183Svbart@nginx.com             field->value_length = mtype->length;
5721183Svbart@nginx.com         }
5731183Svbart@nginx.com 
5741959Sz.hong@f5.com         if (ctx->need_body && nxt_file_size(&fi) > 0) {
5751183Svbart@nginx.com             fb = nxt_mp_zget(r->mem_pool, NXT_BUF_FILE_SIZE);
5761183Svbart@nginx.com             if (nxt_slow_path(fb == NULL)) {
5771183Svbart@nginx.com                 goto fail;
5781183Svbart@nginx.com             }
5791183Svbart@nginx.com 
5801183Svbart@nginx.com             fb->file = f;
5811183Svbart@nginx.com             fb->file_end = nxt_file_size(&fi);
5821183Svbart@nginx.com 
5831183Svbart@nginx.com             r->out = fb;
5841183Svbart@nginx.com 
5851183Svbart@nginx.com             body_handler = &nxt_http_static_body_handler;
5861183Svbart@nginx.com 
5871183Svbart@nginx.com         } else {
5881183Svbart@nginx.com             nxt_file_close(task, f);
5891183Svbart@nginx.com             body_handler = NULL;
5901183Svbart@nginx.com         }
5911183Svbart@nginx.com 
5921183Svbart@nginx.com     } else {
5931183Svbart@nginx.com         /* Not a file. */
5941961Sz.hong@f5.com         nxt_file_close(task, f);
5951183Svbart@nginx.com 
5962106Salx.manpages@gmail.com         if (nxt_slow_path(!nxt_is_dir(&fi)
5972106Salx.manpages@gmail.com                           || shr->start[shr->length - 1] == '/'))
5982106Salx.manpages@gmail.com         {
5991961Sz.hong@f5.com             nxt_log(task, NXT_LOG_ERR, "\"%FN\" is not a regular file",
6001961Sz.hong@f5.com                     f->name);
6011378Svbart@nginx.com 
6021961Sz.hong@f5.com             nxt_http_static_next(task, r, ctx, NXT_HTTP_NOT_FOUND);
6031961Sz.hong@f5.com             return;
6041183Svbart@nginx.com         }
6051183Svbart@nginx.com 
6061198Svbart@nginx.com         f = NULL;
6071198Svbart@nginx.com 
6081183Svbart@nginx.com         r->status = NXT_HTTP_MOVED_PERMANENTLY;
6091183Svbart@nginx.com         r->resp.content_length_n = 0;
6101183Svbart@nginx.com 
6111183Svbart@nginx.com         field = nxt_list_zero_add(r->resp.fields);
6121183Svbart@nginx.com         if (nxt_slow_path(field == NULL)) {
6131183Svbart@nginx.com             goto fail;
6141183Svbart@nginx.com         }
6151183Svbart@nginx.com 
6161183Svbart@nginx.com         nxt_http_field_name_set(field, "Location");
6171183Svbart@nginx.com 
6181183Svbart@nginx.com         encode = nxt_encode_uri(NULL, r->path->start, r->path->length);
6191855Sz.hong@f5.com         length = r->path->length + encode * 2 + 1;
6201183Svbart@nginx.com 
6211183Svbart@nginx.com         if (r->args->length > 0) {
6221855Sz.hong@f5.com             length += 1 + r->args->length;
6231183Svbart@nginx.com         }
6241183Svbart@nginx.com 
6251855Sz.hong@f5.com         p = nxt_mp_nget(r->mem_pool, length);
6261183Svbart@nginx.com         if (nxt_slow_path(p == NULL)) {
6271183Svbart@nginx.com             goto fail;
6281183Svbart@nginx.com         }
6291183Svbart@nginx.com 
6301183Svbart@nginx.com         field->value = p;
6311855Sz.hong@f5.com         field->value_length = length;
6321183Svbart@nginx.com 
6331183Svbart@nginx.com         if (encode > 0) {
6341183Svbart@nginx.com             p = (u_char *) nxt_encode_uri(p, r->path->start, r->path->length);
6351183Svbart@nginx.com 
6361183Svbart@nginx.com         } else {
6371183Svbart@nginx.com             p = nxt_cpymem(p, r->path->start, r->path->length);
6381183Svbart@nginx.com         }
6391183Svbart@nginx.com 
6401183Svbart@nginx.com         *p++ = '/';
6411183Svbart@nginx.com 
6421183Svbart@nginx.com         if (r->args->length > 0) {
6431183Svbart@nginx.com             *p++ = '?';
6441183Svbart@nginx.com             nxt_memcpy(p, r->args->start, r->args->length);
6451183Svbart@nginx.com         }
6461183Svbart@nginx.com 
6471183Svbart@nginx.com         body_handler = NULL;
6481183Svbart@nginx.com     }
6491183Svbart@nginx.com 
6501270Sigor@sysoev.ru     nxt_http_request_header_send(task, r, body_handler, NULL);
6511183Svbart@nginx.com 
6521183Svbart@nginx.com     r->state = &nxt_http_static_send_state;
6531959Sz.hong@f5.com     return;
6541183Svbart@nginx.com 
6551183Svbart@nginx.com fail:
6561183Svbart@nginx.com 
6571855Sz.hong@f5.com     if (f != NULL) {
6581183Svbart@nginx.com         nxt_file_close(task, f);
6591183Svbart@nginx.com     }
6601183Svbart@nginx.com 
6611961Sz.hong@f5.com     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
6621959Sz.hong@f5.com }
6631959Sz.hong@f5.com 
6641959Sz.hong@f5.com 
6651959Sz.hong@f5.com static void
nxt_http_static_send_error(nxt_task_t * task,void * obj,void * data)6662246Sz.hong@f5.com nxt_http_static_send_error(nxt_task_t *task, void *obj, void *data)
6671959Sz.hong@f5.com {
6681959Sz.hong@f5.com     nxt_http_request_t  *r;
6691959Sz.hong@f5.com 
6701959Sz.hong@f5.com     r = obj;
6711959Sz.hong@f5.com 
6721959Sz.hong@f5.com     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
6731183Svbart@nginx.com }
6741183Svbart@nginx.com 
6751183Svbart@nginx.com 
6761961Sz.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)6771961Sz.hong@f5.com nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r,
6781961Sz.hong@f5.com     nxt_http_static_ctx_t *ctx, nxt_http_status_t status)
6791961Sz.hong@f5.com {
6801961Sz.hong@f5.com     nxt_http_action_t       *action;
6811961Sz.hong@f5.com     nxt_http_static_conf_t  *conf;
6821961Sz.hong@f5.com 
6831961Sz.hong@f5.com     action = ctx->action;
6841961Sz.hong@f5.com     conf = action->u.conf;
6851961Sz.hong@f5.com 
6862096Salx.manpages@gmail.com     ctx->share_idx++;
6871961Sz.hong@f5.com 
6882096Salx.manpages@gmail.com     if (ctx->share_idx < conf->nshares) {
6891961Sz.hong@f5.com         nxt_http_static_iterate(task, r, ctx);
6901961Sz.hong@f5.com         return;
6911961Sz.hong@f5.com     }
6921961Sz.hong@f5.com 
6931961Sz.hong@f5.com     if (action->fallback != NULL) {
6942381Salx@nginx.com         if (nxt_slow_path(r->log_route)) {
6952381Salx@nginx.com             nxt_log(task, NXT_LOG_NOTICE, "\"fallback\" taken");
6962381Salx@nginx.com         }
6972511Sz.hong@f5.com 
6982511Sz.hong@f5.com         r->action = action->fallback;
6991961Sz.hong@f5.com         nxt_http_request_action(task, r, action->fallback);
7001961Sz.hong@f5.com         return;
7011961Sz.hong@f5.com     }
7021961Sz.hong@f5.com 
7031961Sz.hong@f5.com     nxt_http_request_error(task, r, status);
7041961Sz.hong@f5.com }
7051961Sz.hong@f5.com 
7061961Sz.hong@f5.com 
7071959Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
7081959Sz.hong@f5.com 
7091959Sz.hong@f5.com static u_char *
nxt_http_static_chroot_match(u_char * chr,u_char * shr)7101959Sz.hong@f5.com nxt_http_static_chroot_match(u_char *chr, u_char *shr)
7111959Sz.hong@f5.com {
7121959Sz.hong@f5.com     if (*chr != *shr) {
7131959Sz.hong@f5.com         return NULL;
7141959Sz.hong@f5.com     }
7151959Sz.hong@f5.com 
7161959Sz.hong@f5.com     chr++;
7171959Sz.hong@f5.com     shr++;
7181959Sz.hong@f5.com 
7191959Sz.hong@f5.com     for ( ;; ) {
7201959Sz.hong@f5.com         if (*shr == '\0') {
7211959Sz.hong@f5.com             return NULL;
7221959Sz.hong@f5.com         }
7231959Sz.hong@f5.com 
7241959Sz.hong@f5.com         if (*chr == *shr) {
7251959Sz.hong@f5.com             chr++;
7261959Sz.hong@f5.com             shr++;
7271959Sz.hong@f5.com             continue;
7281959Sz.hong@f5.com         }
7291959Sz.hong@f5.com 
7301959Sz.hong@f5.com         if (*chr == '\0') {
7311959Sz.hong@f5.com             break;
7321959Sz.hong@f5.com         }
7331959Sz.hong@f5.com 
7341959Sz.hong@f5.com         if (*chr == '/') {
7351959Sz.hong@f5.com             if (chr[-1] == '/') {
7361959Sz.hong@f5.com                 chr++;
7371959Sz.hong@f5.com                 continue;
7381959Sz.hong@f5.com             }
7391959Sz.hong@f5.com 
7401959Sz.hong@f5.com         } else if (*shr == '/') {
7411959Sz.hong@f5.com             if (shr[-1] == '/') {
7421959Sz.hong@f5.com                 shr++;
7431959Sz.hong@f5.com                 continue;
7441959Sz.hong@f5.com             }
7451959Sz.hong@f5.com         }
7461959Sz.hong@f5.com 
7471959Sz.hong@f5.com         return NULL;
7481959Sz.hong@f5.com     }
7491959Sz.hong@f5.com 
7501959Sz.hong@f5.com     if (shr[-1] != '/' && *shr != '/') {
7511959Sz.hong@f5.com         return NULL;
7521959Sz.hong@f5.com     }
7531959Sz.hong@f5.com 
7541959Sz.hong@f5.com     while (*shr == '/') {
7551959Sz.hong@f5.com         shr++;
7561959Sz.hong@f5.com     }
7571959Sz.hong@f5.com 
7581959Sz.hong@f5.com     return (*shr != '\0') ? shr : NULL;
7591959Sz.hong@f5.com }
7601959Sz.hong@f5.com 
7611959Sz.hong@f5.com #endif
7621959Sz.hong@f5.com 
7631959Sz.hong@f5.com 
7641183Svbart@nginx.com static void
nxt_http_static_extract_extension(nxt_str_t * path,nxt_str_t * exten)7651923Sz.hong@f5.com nxt_http_static_extract_extension(nxt_str_t *path, nxt_str_t *exten)
7661183Svbart@nginx.com {
7671183Svbart@nginx.com     u_char  ch, *p, *end;
7681183Svbart@nginx.com 
7691183Svbart@nginx.com     end = path->start + path->length;
7701183Svbart@nginx.com     p = end;
7711183Svbart@nginx.com 
7722137Salx.manpages@gmail.com     while (p > path->start) {
7731183Svbart@nginx.com         p--;
7741183Svbart@nginx.com         ch = *p;
7751183Svbart@nginx.com 
7761183Svbart@nginx.com         switch (ch) {
7771183Svbart@nginx.com         case '/':
7781183Svbart@nginx.com             p++;
7791183Svbart@nginx.com             /* Fall through. */
7801183Svbart@nginx.com         case '.':
7812137Salx.manpages@gmail.com             goto extension;
7821183Svbart@nginx.com         }
7831183Svbart@nginx.com     }
7842137Salx.manpages@gmail.com 
7852137Salx.manpages@gmail.com extension:
7862137Salx.manpages@gmail.com 
7872137Salx.manpages@gmail.com     exten->length = end - p;
7882137Salx.manpages@gmail.com     exten->start = p;
7891183Svbart@nginx.com }
7901183Svbart@nginx.com 
7911183Svbart@nginx.com 
7921183Svbart@nginx.com static void
nxt_http_static_body_handler(nxt_task_t * task,void * obj,void * data)7931183Svbart@nginx.com nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data)
7941183Svbart@nginx.com {
7951183Svbart@nginx.com     size_t              alloc;
7961183Svbart@nginx.com     nxt_buf_t           *fb, *b, **next, *out;
7971183Svbart@nginx.com     nxt_off_t           rest;
7981183Svbart@nginx.com     nxt_int_t           n;
7991183Svbart@nginx.com     nxt_work_queue_t    *wq;
8001183Svbart@nginx.com     nxt_http_request_t  *r;
8011183Svbart@nginx.com 
8021183Svbart@nginx.com     r = obj;
8031183Svbart@nginx.com     fb = r->out;
8041183Svbart@nginx.com 
8051183Svbart@nginx.com     rest = fb->file_end - fb->file_pos;
8061183Svbart@nginx.com     out = NULL;
8071183Svbart@nginx.com     next = &out;
8081183Svbart@nginx.com     n = 0;
8091183Svbart@nginx.com 
8101183Svbart@nginx.com     do {
8111183Svbart@nginx.com         alloc = nxt_min(rest, NXT_HTTP_STATIC_BUF_SIZE);
8121183Svbart@nginx.com 
8131183Svbart@nginx.com         b = nxt_buf_mem_alloc(r->mem_pool, alloc, 0);
8141183Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
8151183Svbart@nginx.com             goto fail;
8161183Svbart@nginx.com         }
8171183Svbart@nginx.com 
8181183Svbart@nginx.com         b->completion_handler = nxt_http_static_buf_completion;
8191183Svbart@nginx.com         b->parent = r;
8201183Svbart@nginx.com 
8211183Svbart@nginx.com         nxt_mp_retain(r->mem_pool);
8221183Svbart@nginx.com 
8231183Svbart@nginx.com         *next = b;
8241183Svbart@nginx.com         next = &b->next;
8251183Svbart@nginx.com 
8261183Svbart@nginx.com         rest -= alloc;
8271183Svbart@nginx.com 
8281183Svbart@nginx.com     } while (rest > 0 && ++n < NXT_HTTP_STATIC_BUF_COUNT);
8291183Svbart@nginx.com 
8301183Svbart@nginx.com     wq = &task->thread->engine->fast_work_queue;
8311183Svbart@nginx.com 
8321183Svbart@nginx.com     nxt_sendbuf_drain(task, wq, out);
8331183Svbart@nginx.com     return;
8341183Svbart@nginx.com 
8351183Svbart@nginx.com fail:
8361183Svbart@nginx.com 
8371183Svbart@nginx.com     while (out != NULL) {
8381183Svbart@nginx.com         b = out;
8391183Svbart@nginx.com         out = b->next;
8401183Svbart@nginx.com 
8411183Svbart@nginx.com         nxt_mp_free(r->mem_pool, b);
8421183Svbart@nginx.com         nxt_mp_release(r->mem_pool);
8431183Svbart@nginx.com     }
8441183Svbart@nginx.com }
8451183Svbart@nginx.com 
8461183Svbart@nginx.com 
8471183Svbart@nginx.com static const nxt_http_request_state_t  nxt_http_static_send_state
8481183Svbart@nginx.com     nxt_aligned(64) =
8491183Svbart@nginx.com {
8501183Svbart@nginx.com     .error_handler = nxt_http_request_error_handler,
8511183Svbart@nginx.com };
8521183Svbart@nginx.com 
8531183Svbart@nginx.com 
8541183Svbart@nginx.com static void
nxt_http_static_buf_completion(nxt_task_t * task,void * obj,void * data)8551183Svbart@nginx.com nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data)
8561183Svbart@nginx.com {
8571183Svbart@nginx.com     ssize_t             n, size;
8581760Smax.romanov@nginx.com     nxt_buf_t           *b, *fb, *next;
8591183Svbart@nginx.com     nxt_off_t           rest;
8601183Svbart@nginx.com     nxt_http_request_t  *r;
8611183Svbart@nginx.com 
8621183Svbart@nginx.com     b = obj;
8631183Svbart@nginx.com     r = data;
8641760Smax.romanov@nginx.com 
8651760Smax.romanov@nginx.com complete_buf:
8661760Smax.romanov@nginx.com 
8671183Svbart@nginx.com     fb = r->out;
8681183Svbart@nginx.com 
8691183Svbart@nginx.com     if (nxt_slow_path(fb == NULL || r->error)) {
8701183Svbart@nginx.com         goto clean;
8711183Svbart@nginx.com     }
8721183Svbart@nginx.com 
8731183Svbart@nginx.com     rest = fb->file_end - fb->file_pos;
8741183Svbart@nginx.com     size = nxt_buf_mem_size(&b->mem);
8751183Svbart@nginx.com 
8761183Svbart@nginx.com     size = nxt_min(rest, (nxt_off_t) size);
8771183Svbart@nginx.com 
8781183Svbart@nginx.com     n = nxt_file_read(fb->file, b->mem.start, size, fb->file_pos);
8791183Svbart@nginx.com 
8802619Sa.clayton@nginx.com     if (nxt_slow_path(n == NXT_ERROR)) {
8811183Svbart@nginx.com         nxt_http_request_error_handler(task, r, r->proto.any);
8821183Svbart@nginx.com         goto clean;
8831183Svbart@nginx.com     }
8841183Svbart@nginx.com 
8851760Smax.romanov@nginx.com     next = b->next;
8861760Smax.romanov@nginx.com 
8871183Svbart@nginx.com     if (n == rest) {
8881183Svbart@nginx.com         nxt_file_close(task, fb->file);
8891183Svbart@nginx.com         r->out = NULL;
8901183Svbart@nginx.com 
8911183Svbart@nginx.com         b->next = nxt_http_buf_last(r);
8921183Svbart@nginx.com 
8931183Svbart@nginx.com     } else {
8941183Svbart@nginx.com         fb->file_pos += n;
8951183Svbart@nginx.com         b->next = NULL;
8961183Svbart@nginx.com     }
8971183Svbart@nginx.com 
8981183Svbart@nginx.com     b->mem.pos = b->mem.start;
8991183Svbart@nginx.com     b->mem.free = b->mem.pos + n;
9001183Svbart@nginx.com 
9011183Svbart@nginx.com     nxt_http_request_send(task, r, b);
9021760Smax.romanov@nginx.com 
9031760Smax.romanov@nginx.com     if (next != NULL) {
9041760Smax.romanov@nginx.com         b = next;
9051760Smax.romanov@nginx.com         goto complete_buf;
9061760Smax.romanov@nginx.com     }
9071760Smax.romanov@nginx.com 
9081183Svbart@nginx.com     return;
9091183Svbart@nginx.com 
9101183Svbart@nginx.com clean:
9111183Svbart@nginx.com 
9121760Smax.romanov@nginx.com     do {
9131760Smax.romanov@nginx.com         next = b->next;
9141760Smax.romanov@nginx.com 
9151760Smax.romanov@nginx.com         nxt_mp_free(r->mem_pool, b);
9161760Smax.romanov@nginx.com         nxt_mp_release(r->mem_pool);
9171760Smax.romanov@nginx.com 
9181760Smax.romanov@nginx.com         b = next;
9191760Smax.romanov@nginx.com     } while (b != NULL);
9201183Svbart@nginx.com 
9211183Svbart@nginx.com     if (fb != NULL) {
9221183Svbart@nginx.com         nxt_file_close(task, fb->file);
9231183Svbart@nginx.com         r->out = NULL;
9241183Svbart@nginx.com     }
9251183Svbart@nginx.com }
9261183Svbart@nginx.com 
9271183Svbart@nginx.com 
9281183Svbart@nginx.com nxt_int_t
nxt_http_static_mtypes_init(nxt_mp_t * mp,nxt_lvlhsh_t * hash)9291183Svbart@nginx.com nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash)
9301183Svbart@nginx.com {
9311923Sz.hong@f5.com     nxt_str_t   *type, exten;
9321183Svbart@nginx.com     nxt_int_t   ret;
9331183Svbart@nginx.com     nxt_uint_t  i;
9341183Svbart@nginx.com 
9351183Svbart@nginx.com     static const struct {
9361183Svbart@nginx.com         nxt_str_t   type;
9371923Sz.hong@f5.com         const char  *exten;
9381183Svbart@nginx.com     } default_types[] = {
9391183Svbart@nginx.com 
9401183Svbart@nginx.com         { nxt_string("text/html"),      ".html"  },
9411183Svbart@nginx.com         { nxt_string("text/html"),      ".htm"   },
9421183Svbart@nginx.com         { nxt_string("text/css"),       ".css"   },
9431183Svbart@nginx.com 
9441183Svbart@nginx.com         { nxt_string("image/svg+xml"),  ".svg"   },
9451183Svbart@nginx.com         { nxt_string("image/webp"),     ".webp"  },
9461183Svbart@nginx.com         { nxt_string("image/png"),      ".png"   },
9471617Svbart@nginx.com         { nxt_string("image/apng"),     ".apng"  },
9481183Svbart@nginx.com         { nxt_string("image/jpeg"),     ".jpeg"  },
9491183Svbart@nginx.com         { nxt_string("image/jpeg"),     ".jpg"   },
9501183Svbart@nginx.com         { nxt_string("image/gif"),      ".gif"   },
9511183Svbart@nginx.com         { nxt_string("image/x-icon"),   ".ico"   },
9521183Svbart@nginx.com 
9531617Svbart@nginx.com         { nxt_string("image/avif"),           ".avif"  },
9541617Svbart@nginx.com         { nxt_string("image/avif-sequence"),  ".avifs" },
9551617Svbart@nginx.com 
9561183Svbart@nginx.com         { nxt_string("font/woff"),      ".woff"  },
9571183Svbart@nginx.com         { nxt_string("font/woff2"),     ".woff2" },
9581183Svbart@nginx.com         { nxt_string("font/otf"),       ".otf"   },
9591183Svbart@nginx.com         { nxt_string("font/ttf"),       ".ttf"   },
9601183Svbart@nginx.com 
9611183Svbart@nginx.com         { nxt_string("text/plain"),     ".txt"   },
9621183Svbart@nginx.com         { nxt_string("text/markdown"),  ".md"    },
9631183Svbart@nginx.com         { nxt_string("text/x-rst"),     ".rst"   },
9641183Svbart@nginx.com 
9651183Svbart@nginx.com         { nxt_string("application/javascript"),  ".js"   },
9661183Svbart@nginx.com         { nxt_string("application/json"),        ".json" },
9671183Svbart@nginx.com         { nxt_string("application/xml"),         ".xml"  },
9681183Svbart@nginx.com         { nxt_string("application/rss+xml"),     ".rss"  },
9691183Svbart@nginx.com         { nxt_string("application/atom+xml"),    ".atom" },
9701183Svbart@nginx.com         { nxt_string("application/pdf"),         ".pdf"  },
9711183Svbart@nginx.com 
9721183Svbart@nginx.com         { nxt_string("application/zip"),         ".zip"  },
9731183Svbart@nginx.com 
9741183Svbart@nginx.com         { nxt_string("audio/mpeg"),       ".mp3"  },
9751183Svbart@nginx.com         { nxt_string("audio/ogg"),        ".ogg"  },
9761183Svbart@nginx.com         { nxt_string("audio/midi"),       ".midi" },
9771183Svbart@nginx.com         { nxt_string("audio/midi"),       ".mid"  },
9781183Svbart@nginx.com         { nxt_string("audio/flac"),       ".flac" },
9791183Svbart@nginx.com         { nxt_string("audio/aac"),        ".aac"  },
9801183Svbart@nginx.com         { nxt_string("audio/wav"),        ".wav"  },
9811183Svbart@nginx.com 
9821183Svbart@nginx.com         { nxt_string("video/mpeg"),       ".mpeg" },
9831183Svbart@nginx.com         { nxt_string("video/mpeg"),       ".mpg"  },
9841183Svbart@nginx.com         { nxt_string("video/mp4"),        ".mp4"  },
9851183Svbart@nginx.com         { nxt_string("video/webm"),       ".webm" },
9861183Svbart@nginx.com         { nxt_string("video/x-msvideo"),  ".avi"  },
9871183Svbart@nginx.com 
9881183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".exe" },
9891183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".bin" },
9901183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".dll" },
9911183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".iso" },
9921183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".img" },
9931183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".msi" },
9941183Svbart@nginx.com 
9951183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".deb" },
9961183Svbart@nginx.com         { nxt_string("application/octet-stream"),  ".rpm" },
9971882So.canty@f5.com 
9981882So.canty@f5.com         { nxt_string("application/x-httpd-php"),   ".php" },
9991183Svbart@nginx.com     };
10001183Svbart@nginx.com 
10011183Svbart@nginx.com     for (i = 0; i < nxt_nitems(default_types); i++) {
10021183Svbart@nginx.com         type = (nxt_str_t *) &default_types[i].type;
10031183Svbart@nginx.com 
10041923Sz.hong@f5.com         exten.start = (u_char *) default_types[i].exten;
10051923Sz.hong@f5.com         exten.length = nxt_strlen(exten.start);
10061183Svbart@nginx.com 
10071923Sz.hong@f5.com         ret = nxt_http_static_mtypes_hash_add(mp, hash, &exten, type);
10081183Svbart@nginx.com         if (nxt_slow_path(ret != NXT_OK)) {
10091183Svbart@nginx.com             return NXT_ERROR;
10101183Svbart@nginx.com         }
10111183Svbart@nginx.com     }
10121183Svbart@nginx.com 
10131183Svbart@nginx.com     return NXT_OK;
10141183Svbart@nginx.com }
10151183Svbart@nginx.com 
10161183Svbart@nginx.com 
10171183Svbart@nginx.com static const nxt_lvlhsh_proto_t  nxt_http_static_mtypes_hash_proto
10181183Svbart@nginx.com     nxt_aligned(64) =
10191183Svbart@nginx.com {
10201183Svbart@nginx.com     NXT_LVLHSH_DEFAULT,
10211183Svbart@nginx.com     nxt_http_static_mtypes_hash_test,
10221183Svbart@nginx.com     nxt_http_static_mtypes_hash_alloc,
10231183Svbart@nginx.com     nxt_http_static_mtypes_hash_free,
10241183Svbart@nginx.com };
10251183Svbart@nginx.com 
10261183Svbart@nginx.com 
10271183Svbart@nginx.com typedef struct {
10281923Sz.hong@f5.com     nxt_str_t  exten;
10291183Svbart@nginx.com     nxt_str_t  *type;
10301183Svbart@nginx.com } nxt_http_static_mtype_t;
10311183Svbart@nginx.com 
10321183Svbart@nginx.com 
10331183Svbart@nginx.com nxt_int_t
nxt_http_static_mtypes_hash_add(nxt_mp_t * mp,nxt_lvlhsh_t * hash,const nxt_str_t * exten,nxt_str_t * type)10341183Svbart@nginx.com nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash,
10352139Sandrew@digital-domain.net     const nxt_str_t *exten, nxt_str_t *type)
10361183Svbart@nginx.com {
10371183Svbart@nginx.com     nxt_lvlhsh_query_t       lhq;
10381183Svbart@nginx.com     nxt_http_static_mtype_t  *mtype;
10391183Svbart@nginx.com 
10401183Svbart@nginx.com     mtype = nxt_mp_get(mp, sizeof(nxt_http_static_mtype_t));
10411183Svbart@nginx.com     if (nxt_slow_path(mtype == NULL)) {
10421183Svbart@nginx.com         return NXT_ERROR;
10431183Svbart@nginx.com     }
10441183Svbart@nginx.com 
10451923Sz.hong@f5.com     mtype->exten = *exten;
10461183Svbart@nginx.com     mtype->type = type;
10471183Svbart@nginx.com 
10481923Sz.hong@f5.com     lhq.key = *exten;
10491183Svbart@nginx.com     lhq.key_hash = nxt_djb_hash_lowcase(lhq.key.start, lhq.key.length);
10501183Svbart@nginx.com     lhq.replace = 1;
10511183Svbart@nginx.com     lhq.value = mtype;
10521183Svbart@nginx.com     lhq.proto = &nxt_http_static_mtypes_hash_proto;
10531183Svbart@nginx.com     lhq.pool = mp;
10541183Svbart@nginx.com 
10551183Svbart@nginx.com     return nxt_lvlhsh_insert(hash, &lhq);
10561183Svbart@nginx.com }
10571183Svbart@nginx.com 
10581183Svbart@nginx.com 
10591183Svbart@nginx.com nxt_str_t *
nxt_http_static_mtype_get(nxt_lvlhsh_t * hash,const nxt_str_t * exten)10602139Sandrew@digital-domain.net nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, const nxt_str_t *exten)
10611183Svbart@nginx.com {
10621183Svbart@nginx.com     nxt_lvlhsh_query_t       lhq;
10631183Svbart@nginx.com     nxt_http_static_mtype_t  *mtype;
10641183Svbart@nginx.com 
10651883So.canty@f5.com     static nxt_str_t  empty = nxt_string("");
10661883So.canty@f5.com 
10671923Sz.hong@f5.com     lhq.key = *exten;
10681183Svbart@nginx.com     lhq.key_hash = nxt_djb_hash_lowcase(lhq.key.start, lhq.key.length);
10691183Svbart@nginx.com     lhq.proto = &nxt_http_static_mtypes_hash_proto;
10701183Svbart@nginx.com 
10711183Svbart@nginx.com     if (nxt_lvlhsh_find(hash, &lhq) == NXT_OK) {
10721183Svbart@nginx.com         mtype = lhq.value;
10731183Svbart@nginx.com         return mtype->type;
10741183Svbart@nginx.com     }
10751183Svbart@nginx.com 
10761883So.canty@f5.com     return &empty;
10771183Svbart@nginx.com }
10781183Svbart@nginx.com 
10791183Svbart@nginx.com 
10801183Svbart@nginx.com static nxt_int_t
nxt_http_static_mtypes_hash_test(nxt_lvlhsh_query_t * lhq,void * data)10811183Svbart@nginx.com nxt_http_static_mtypes_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
10821183Svbart@nginx.com {
10831183Svbart@nginx.com     nxt_http_static_mtype_t  *mtype;
10841183Svbart@nginx.com 
10851183Svbart@nginx.com     mtype = data;
10861183Svbart@nginx.com 
10871923Sz.hong@f5.com     return nxt_strcasestr_eq(&lhq->key, &mtype->exten) ? NXT_OK : NXT_DECLINED;
10881183Svbart@nginx.com }
10891183Svbart@nginx.com 
10901183Svbart@nginx.com 
10911183Svbart@nginx.com static void *
nxt_http_static_mtypes_hash_alloc(void * data,size_t size)10921183Svbart@nginx.com nxt_http_static_mtypes_hash_alloc(void *data, size_t size)
10931183Svbart@nginx.com {
10941183Svbart@nginx.com     return nxt_mp_align(data, size, size);
10951183Svbart@nginx.com }
10961183Svbart@nginx.com 
10971183Svbart@nginx.com 
10981183Svbart@nginx.com static void
nxt_http_static_mtypes_hash_free(void * data,void * p)10991183Svbart@nginx.com nxt_http_static_mtypes_hash_free(void *data, void *p)
11001183Svbart@nginx.com {
11011183Svbart@nginx.com     nxt_mp_free(data, p);
11021183Svbart@nginx.com }
1103