├── .gitattributes ├── .gitignore ├── .travis.yml ├── Changes ├── README.markdown ├── config ├── src ├── ddebug.h ├── ngx_http_srcache_fetch.c ├── ngx_http_srcache_fetch.h ├── ngx_http_srcache_filter_module.c ├── ngx_http_srcache_filter_module.h ├── ngx_http_srcache_headers.c ├── ngx_http_srcache_headers.h ├── ngx_http_srcache_store.c ├── ngx_http_srcache_store.h ├── ngx_http_srcache_util.c ├── ngx_http_srcache_util.h ├── ngx_http_srcache_var.c └── ngx_http_srcache_var.h ├── t ├── 000_init.t ├── access.t ├── bugs.t ├── conditional-get.t ├── content-length.t ├── content-type.t ├── disk.t ├── drizzle-main.t ├── drizzle-sub.t ├── empty-resp.t ├── err-page.t ├── etag.t ├── eval.t ├── expire-var.t ├── expires.t ├── fetch-header.t ├── fetch-skip.t ├── gzip.t ├── header-buf-size.t ├── main-req.t ├── max-age.t ├── methods.t ├── no-cache.t ├── no-store.t ├── postgres-main.t ├── private.t ├── proxy.t ├── ranges.t ├── redis.t ├── req-cache-control.t ├── satisfy.t ├── static.t ├── status.t ├── store-hide-headers.t ├── store-max-size.t ├── store-pass-headers.t ├── store-skip.t ├── sub-req.t ├── timeout.t └── unused.t ├── util └── build.sh └── valgrind.suppress /.gitattributes: -------------------------------------------------------------------------------- 1 | *.t linguist-language=Text 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.mobi 2 | genmobi.sh 3 | .libs 4 | *.swp 5 | *.slo 6 | *.la 7 | *.swo 8 | *.lo 9 | *~ 10 | *.o 11 | print.txt 12 | .rsync 13 | *.tar.gz 14 | dist 15 | build[78] 16 | build 17 | tags 18 | update-readme 19 | *.tmp 20 | test/Makefile 21 | test/blib 22 | test.sh 23 | t/t.sh 24 | test/t/servroot/ 25 | releng 26 | reset 27 | *.t_ 28 | src/handler.h 29 | src/util.c 30 | src/module.h 31 | src/module.c 32 | src/drizzle.c 33 | src/processor.h 34 | src/handler.c 35 | src/util.h 36 | src/drizzle.h 37 | src/processor.c 38 | src/output.c 39 | src/output.h 40 | libdrizzle 41 | ctags 42 | src/stream.h 43 | nginx 44 | keepalive 45 | reindex 46 | src/keepalive.c 47 | src/keepalive.h 48 | src/checker.h 49 | src/checker.c 50 | src/quoting.h 51 | src/quoting.c 52 | src/module.h 53 | src/module.c 54 | src/util.h 55 | src/util.c 56 | src/processor.h 57 | src/processor.c 58 | src/rds.h 59 | src/utils.h 60 | src/handler.c 61 | src/handler.h 62 | src/var.[ch] 63 | util/bench 64 | *.html 65 | trace.out* 66 | try.sh 67 | build9 68 | src/fetch.[ch] 69 | src/store.[ch] 70 | t/servroot/ 71 | headers.[ch] 72 | buildroot/ 73 | build1[0-9] 74 | go 75 | all 76 | analyze 77 | addr2line 78 | *.plist 79 | Makefile 80 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | dist: focal 3 | 4 | branches: 5 | only: 6 | - "master" 7 | 8 | os: linux 9 | 10 | language: c 11 | 12 | cache: 13 | directories: 14 | - download-cache 15 | 16 | compiler: 17 | - gcc 18 | 19 | env: 20 | global: 21 | - LUAJIT_PREFIX=/opt/luajit21 22 | - LUAJIT_LIB=$LUAJIT_PREFIX/lib 23 | - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH 24 | - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1 25 | - LUA_INCLUDE_DIR=$LUAJIT_INC 26 | - LUA_CMODULE_DIR=/lib 27 | - JOBS=3 28 | - NGX_BUILD_JOBS=$JOBS 29 | - LIBDRIZZLE_PREFIX=/opt/drizzle 30 | - LIBDRIZZLE_INC=$LIBDRIZZLE_PREFIX/include/libdrizzle-1.0 31 | - LIBDRIZZLE_LIB=$LIBDRIZZLE_PREFIX/lib 32 | matrix: 33 | - NGINX_VERSION=1.25.3 34 | 35 | addons: 36 | postgresql: "13" 37 | 38 | services: 39 | - memcache 40 | - redis-server 41 | - postgresql 42 | - mysql 43 | 44 | before_install: 45 | - sudo apt-get install -qq -y axel cpanminus libtest-base-perl libtext-diff-perl liburi-perl libwww-perl libtest-longstring-perl liblist-moreutils-perl > build.log 2>&1 || (cat build.log && exit 1) 46 | 47 | install: 48 | - echo $HOME 49 | - if [ ! -d download-cache ]; then mkdir download-cache; fi 50 | - rm -fr download-cache/drizzle7-2011.07.21.tar.gz 51 | - if [ ! -f download-cache/drizzle7-2011.07.21.tar.gz ]; then wget -P download-cache https://github.com/openresty/openresty-deps-prebuild/releases/download/v20230902/drizzle7-2011.07.21.tar.gz; fi 52 | - mkdir -p ~/work/nginx 53 | - git clone https://github.com/openresty/ngx_http_redis ~/work/nginx/ngx_http_redis 54 | - git clone https://github.com/openresty/openresty-devel-utils.git 55 | - git clone https://github.com/openresty/openresty.git ../openresty 56 | - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx 57 | - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module 58 | - git clone https://github.com/openresty/test-nginx.git 59 | - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git 60 | - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module 61 | - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core 62 | - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache 63 | - git clone https://github.com/openresty/nginx-eval-module.git ../eval-nginx-module 64 | - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module 65 | - git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module 66 | - git clone https://github.com/openresty/xss-nginx-module.git ../xss-nginx-module 67 | - git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module 68 | - git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module 69 | - git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module 70 | - git clone https://github.com/openresty/drizzle-nginx-module.git ../drizzle-nginx-module 71 | - git clone https://github.com/openresty/memc-nginx-module.git ../memc-nginx-module 72 | - git clone https://github.com/openresty/ngx_postgres.git ../postgres-nginx-module 73 | - git clone https://github.com/openresty/openresty.git ../ngx_openresty 74 | 75 | before_script: 76 | - mysql -uroot -e "create database ngx_test; CREATE USER 'ngx_test'@'%' IDENTIFIED WITH mysql_native_password BY 'ngx_test'; grant all on ngx_test.* to 'ngx_test'@'%'; flush privileges;" 77 | - psql -c "create database ngx_test;" -U postgres 78 | - psql -c "create user ngx_test with password 'ngx_test';" -U postgres 79 | - psql -c "grant all privileges on database ngx_test to ngx_test;" -U postgres 80 | 81 | script: 82 | - tar xzf download-cache/drizzle7-2011.07.21.tar.gz && cd drizzle7-2011.07.21 83 | - ./configure --prefix=$LIBDRIZZLE_PREFIX --without-server > build.log 2>&1 || (cat build.log && exit 1) 84 | - sudo PATH=$PATH make libdrizzle-1.0 install-libdrizzle-1.0 > build.log 2>&1 || (cat build.log && exit 1) 85 | - cd .. 86 | - cd luajit2 87 | - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1) 88 | - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1) 89 | - cd .. 90 | - cd test-nginx && sudo cpanm . && cd .. 91 | - export PATH=$PWD/work/nginx/sbin:$PWD/openresty-devel-utils:$PATH 92 | - export NGX_BUILD_CC=$CC 93 | - sh util/build.sh $NGINX_VERSION > build.log 2>&1 || (cat build.log && exit 1) 94 | - nginx -V 95 | - ldd `which nginx`|grep luajit 96 | - prove -I. -r t 97 | -------------------------------------------------------------------------------- /Changes: -------------------------------------------------------------------------------- 1 | v0.12 - 11 July 2011 2 | * now we properly support fetch/store subrequests with internal redirection in them. main requests with internal redirection will still not be stored into the cache if there is a cache miss. thanks Liseen Wan for reporting it. 3 | * fixed spots that trigger the unused-but-set-variable warning by gcc 4.6. 4 | * added srcache_store_skip and srcache_fetch_skip directives to skip cache fetching or storing based on variables that are set and not empty nor 0. thanks Andre. Examples of using Lua to set $nocache to avoid storing URIs that contain /tmp: 5 | set_by_lua $nocache ' 6 | if string.match(ngx.var.request_uri, "/tmp") then 7 | return "true" 8 | else 9 | return "" 10 | end'; 11 | 12 | srcache_store_skip $nocache; 13 | * added new directive srcache_store_max_size. thanks Andre. 14 | * made our filter optimization work with nginx HUP by clearing the ngx_http_srcache_used flag at nginx pre-config callback. thanks Marcus Clyne. 15 | * now we skip NULL chains in our output filters and also removed the SUBREQUEST_IN_MEMORY flag for our srcache_store subrequests because it will cause mysterious hanging issues when memcached returns CLIENT_ERROR for "get". 16 | 17 | -------------------------------------------------------------------------------- /config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_srcache_filter_module 2 | 3 | HTTP_SRCACHE_FILTER_SRCS=" \ 4 | $ngx_addon_dir/src/ngx_http_srcache_filter_module.c \ 5 | $ngx_addon_dir/src/ngx_http_srcache_util.c \ 6 | $ngx_addon_dir/src/ngx_http_srcache_var.c \ 7 | $ngx_addon_dir/src/ngx_http_srcache_store.c \ 8 | $ngx_addon_dir/src/ngx_http_srcache_fetch.c \ 9 | $ngx_addon_dir/src/ngx_http_srcache_headers.c \ 10 | " 11 | 12 | HTTP_SRCACHE_FILTER_DEPS=" \ 13 | $ngx_addon_dir/src/ddebug.h \ 14 | $ngx_addon_dir/src/ngx_http_srcache_filter_module.h \ 15 | $ngx_addon_dir/src/ngx_http_srcache_util.h \ 16 | $ngx_addon_dir/src/ngx_http_srcache_var.h \ 17 | $ngx_addon_dir/src/ngx_http_srcache_fetch.h \ 18 | $ngx_addon_dir/src/ngx_http_srcache_store.h \ 19 | $ngx_addon_dir/src/ngx_http_srcache_headers.h \ 20 | " 21 | 22 | # nginx 1.17.0+ unconditionally enables the postpone filter 23 | if [ ! -z "$HTTP_POSTPONE" ]; then 24 | # nginx won't have HTTP_POSTPONE_FILTER_MODULE & HTTP_POSTPONE_FILTER_SRCS 25 | # defined since 1.9.11 26 | if [ -z "$HTTP_POSTPONE_FILTER_MODULE" ]; then 27 | HTTP_POSTPONE_FILTER_MODULE=ngx_http_postpone_filter_module 28 | HTTP_POSTPONE_FILTER_SRCS=src/http/ngx_http_postpone_filter_module.c 29 | fi 30 | 31 | # This module depends upon the postpone filter being activated 32 | if [ "$HTTP_POSTPONE" != YES ]; then 33 | HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES $HTTP_POSTPONE_FILTER_MODULE" 34 | HTTP_SRCS="$HTTP_SRCS $HTTP_POSTPONE_FILTER_SRCS" 35 | HTTP_POSTPONE=YES 36 | fi 37 | fi 38 | 39 | if [ -n "$ngx_module_link" ]; then 40 | ngx_module_type=HTTP_AUX_FILTER 41 | ngx_module_name=$ngx_addon_name 42 | ngx_module_srcs="$HTTP_SRCACHE_FILTER_SRCS" 43 | ngx_module_deps="$HTTP_SRCACHE_FILTER_DEPS" 44 | 45 | . auto/module 46 | else 47 | HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES $ngx_addon_name" 48 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $HTTP_SRCACHE_FILTER_SRCS" 49 | NGX_ADDON_DEPS="$NGX_ADDON_DEPS $HTTP_SRCACHE_FILTER_DEPS" 50 | fi 51 | -------------------------------------------------------------------------------- /src/ddebug.h: -------------------------------------------------------------------------------- 1 | #ifndef DDEBUG_H 2 | #define DDEBUG_H 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | #if defined(DDEBUG) && (DDEBUG) 12 | 13 | # if (NGX_HAVE_VARIADIC_MACROS) 14 | 15 | # define dd(...) fprintf(stderr, "srcache *** %s: ", __func__); \ 16 | fprintf(stderr, __VA_ARGS__); \ 17 | fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__) 18 | 19 | # else 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | static ngx_inline void 27 | dd(const char * fmt, ...) { 28 | } 29 | 30 | # endif 31 | 32 | # if DDEBUG > 1 33 | 34 | # define dd_enter() dd_enter_helper(r, __func__) 35 | 36 | 37 | # if defined(nginx_version) && nginx_version >= 8011 38 | # define dd_main_req_count r->main->count 39 | # else 40 | # define dd_main_req_count 0 41 | # endif 42 | 43 | static ngx_inline void 44 | dd_enter_helper(ngx_http_request_t *r, const char *func) { 45 | ngx_http_posted_request_t *pr; 46 | 47 | fprintf(stderr, ">enter %s %.*s %.*s?%.*s c:%d m:%p r:%p ar:%p pr:%p", 48 | func, 49 | (int) r->method_name.len, r->method_name.data, 50 | (int) r->uri.len, r->uri.data, 51 | (int) r->args.len, r->args.data, 52 | (int) dd_main_req_count, r->main, 53 | r, r->connection->data, r->parent); 54 | 55 | if (r->posted_requests) { 56 | fprintf(stderr, " posted:"); 57 | 58 | for (pr = r->posted_requests; pr; pr = pr->next) { 59 | fprintf(stderr, "%p,", pr); 60 | } 61 | } 62 | 63 | fprintf(stderr, "\n"); 64 | } 65 | 66 | # else 67 | 68 | # define dd_enter() 69 | 70 | # endif 71 | 72 | #else 73 | 74 | # if (NGX_HAVE_VARIADIC_MACROS) 75 | 76 | # define dd(...) 77 | 78 | # define dd_enter() 79 | 80 | # else 81 | 82 | #include 83 | 84 | static ngx_inline void 85 | dd(const char * fmt, ...) { 86 | } 87 | 88 | static ngx_inline void 89 | dd_enter() { 90 | } 91 | 92 | # endif 93 | 94 | #endif 95 | 96 | #if defined(DDEBUG) && (DDEBUG) 97 | 98 | #define dd_check_read_event_handler(r) \ 99 | dd("r->read_event_handler = %s", \ 100 | r->read_event_handler == ngx_http_block_reading ? \ 101 | "ngx_http_block_reading" : \ 102 | r->read_event_handler == ngx_http_test_reading ? \ 103 | "ngx_http_test_reading" : \ 104 | r->read_event_handler == ngx_http_request_empty_handler ? \ 105 | "ngx_http_request_empty_handler" : "UNKNOWN") 106 | 107 | #define dd_check_write_event_handler(r) \ 108 | dd("r->write_event_handler = %s", \ 109 | r->write_event_handler == ngx_http_handler ? \ 110 | "ngx_http_handler" : \ 111 | r->write_event_handler == ngx_http_core_run_phases ? \ 112 | "ngx_http_core_run_phases" : \ 113 | r->write_event_handler == ngx_http_request_empty_handler ? \ 114 | "ngx_http_request_empty_handler" : "UNKNOWN") 115 | 116 | #else 117 | 118 | #define dd_check_read_event_handler(r) 119 | #define dd_check_write_event_handler(r) 120 | 121 | #endif 122 | 123 | #endif /* DDEBUG_H */ 124 | 125 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_fetch.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef _NGX_HTTP_SRCACHE_FETCH_H_INCLUDED_ 8 | #define _NGX_HTTP_SRCACHE_FETCH_H_INCLUDED_ 9 | 10 | 11 | #include "ngx_http_srcache_filter_module.h" 12 | 13 | 14 | ngx_int_t ngx_http_srcache_access_handler(ngx_http_request_t *r); 15 | 16 | ngx_int_t ngx_http_srcache_fetch_post_subrequest(ngx_http_request_t *r, 17 | void *data, ngx_int_t rc); 18 | 19 | 20 | #endif /* _NGX_HTTP_SRCACHE_FETCH_H_INCLUDED_ */ 21 | 22 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 23 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_filter_module.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef _NGX_HTTP_SRCACHE_FILTER_MODULE_H_INCLUDED_ 8 | #define _NGX_HTTP_SRCACHE_FILTER_MODULE_H_INCLUDED_ 9 | 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | enum { 17 | NGX_HTTP_SRCACHE_FETCH_BYPASS = 0, 18 | NGX_HTTP_SRCACHE_FETCH_MISS = 1, 19 | NGX_HTTP_SRCACHE_FETCH_HIT = 2 20 | }; 21 | 22 | 23 | enum { 24 | NGX_HTTP_SRCACHE_STORE_BYPASS = 0, 25 | NGX_HTTP_SRCACHE_STORE_STORE = 1 26 | }; 27 | 28 | 29 | extern ngx_module_t ngx_http_srcache_filter_module; 30 | 31 | 32 | typedef struct { 33 | ngx_uint_t method; 34 | ngx_str_t method_name; 35 | ngx_http_complex_value_t location; 36 | ngx_http_complex_value_t args; 37 | 38 | } ngx_http_srcache_request_t; 39 | 40 | 41 | typedef struct { 42 | ngx_uint_t method; 43 | ngx_str_t method_name; 44 | ngx_str_t location; 45 | ngx_str_t args; 46 | ngx_http_request_body_t *request_body; 47 | ssize_t content_length_n; 48 | 49 | } ngx_http_srcache_parsed_request_t; 50 | 51 | 52 | typedef struct { 53 | ngx_http_srcache_request_t *fetch; 54 | ngx_http_srcache_request_t *store; 55 | size_t buf_size; 56 | size_t store_max_size; 57 | size_t header_buf_size; 58 | 59 | ngx_http_complex_value_t *fetch_skip; 60 | ngx_http_complex_value_t *store_skip; 61 | 62 | ngx_uint_t cache_methods; 63 | 64 | ngx_int_t *store_statuses; 65 | 66 | ngx_flag_t req_cache_control; 67 | ngx_flag_t resp_cache_control; 68 | 69 | ngx_flag_t store_private; 70 | ngx_flag_t store_no_store; 71 | ngx_flag_t store_no_cache; 72 | ngx_flag_t store_ranges; 73 | 74 | ngx_flag_t ignore_content_encoding; 75 | 76 | ngx_hash_t hide_headers_hash; 77 | ngx_array_t *hide_headers; 78 | ngx_array_t *pass_headers; 79 | 80 | time_t max_expire; 81 | time_t default_expire; 82 | 83 | unsigned postponed_to_access_phase_end:1; 84 | unsigned hide_content_type:1; 85 | unsigned hide_last_modified:1; 86 | } ngx_http_srcache_loc_conf_t; 87 | 88 | 89 | typedef struct { 90 | unsigned postponed_to_access_phase_end; 91 | unsigned module_used; 92 | ngx_hash_t headers_in_hash; 93 | 94 | } ngx_http_srcache_main_conf_t; 95 | 96 | 97 | typedef struct ngx_http_srcache_ctx_s ngx_http_srcache_ctx_t; 98 | 99 | typedef struct ngx_http_srcache_postponed_request_s 100 | ngx_http_srcache_postponed_request_t; 101 | 102 | 103 | struct ngx_http_srcache_ctx_s { 104 | ngx_chain_t *body_from_cache; 105 | ngx_chain_t *body_to_cache; 106 | size_t response_length; 107 | size_t response_body_length; 108 | void *store_wev_handler_ctx; 109 | 110 | ngx_int_t (*process_header)(ngx_http_request_t *r, 111 | ngx_buf_t *b); 112 | 113 | time_t valid_sec; 114 | 115 | ngx_http_status_t status; 116 | ngx_buf_t *header_buf; 117 | 118 | ngx_http_srcache_postponed_request_t *postponed_requests; 119 | 120 | ngx_uint_t http_status; /* the HTTP status code that has been 121 | actually sent */ 122 | 123 | unsigned waiting_subrequest:1; 124 | unsigned request_done:1; 125 | unsigned from_cache:1; 126 | /* unsigned fetch_error:1; */ 127 | unsigned in_fetch_subrequest:1; 128 | unsigned in_store_subrequest:1; 129 | unsigned ignore_body:1; 130 | unsigned parsing_cached_headers:1; 131 | unsigned store_response:1; 132 | unsigned store_skip:1; 133 | unsigned issued_fetch_subrequest:1; 134 | unsigned seen_subreq_eof:1; 135 | unsigned waiting_request_body:1; 136 | unsigned request_body_done:1; 137 | }; 138 | 139 | 140 | struct ngx_http_srcache_postponed_request_s { 141 | ngx_http_srcache_postponed_request_t *next; 142 | 143 | ngx_http_request_t *request; 144 | ngx_http_srcache_ctx_t *ctx; 145 | unsigned ready:1; 146 | unsigned done:1; 147 | }; 148 | 149 | 150 | #endif /* _NGX_HTTP_SRCACHE_FILTER_MODULE_H_INCLUDED_ */ 151 | 152 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 153 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_headers.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef DDEBUG 8 | #define DDEBUG 0 9 | #endif 10 | #include "ddebug.h" 11 | 12 | 13 | #include "ngx_http_srcache_headers.h" 14 | 15 | 16 | static ngx_int_t ngx_http_srcache_process_content_type(ngx_http_request_t *r, 17 | ngx_table_elt_t *h, ngx_uint_t offset); 18 | static ngx_int_t ngx_http_srcache_process_content_length(ngx_http_request_t *r, 19 | ngx_table_elt_t *h, ngx_uint_t offset); 20 | static ngx_int_t ngx_http_srcache_process_last_modified(ngx_http_request_t *r, 21 | ngx_table_elt_t *h, ngx_uint_t offset); 22 | static ngx_int_t 23 | ngx_http_srcache_process_multi_header_lines(ngx_http_request_t *r, 24 | ngx_table_elt_t *h, ngx_uint_t offset); 25 | static ngx_int_t ngx_http_srcache_process_allow_ranges(ngx_http_request_t *r, 26 | ngx_table_elt_t *h, ngx_uint_t offset); 27 | static ngx_int_t ngx_http_srcache_process_accept_ranges(ngx_http_request_t *r, 28 | ngx_table_elt_t *h, ngx_uint_t offset); 29 | static ngx_int_t ngx_http_srcache_ignore_header_line(ngx_http_request_t *r, 30 | ngx_table_elt_t *h, ngx_uint_t offset); 31 | static ngx_int_t ngx_http_srcache_process_location(ngx_http_request_t *r, 32 | ngx_table_elt_t *h, ngx_uint_t offset); 33 | 34 | #if (NGX_HTTP_GZIP) 35 | static ngx_int_t 36 | ngx_http_srcache_process_content_encoding(ngx_http_request_t *r, 37 | ngx_table_elt_t *h, ngx_uint_t offset); 38 | #endif 39 | 40 | 41 | ngx_http_srcache_header_t ngx_http_srcache_headers_in[] = { 42 | 43 | { ngx_string("Content-Type"), 44 | ngx_http_srcache_process_content_type, 0 }, 45 | 46 | { ngx_string("Content-Length"), 47 | ngx_http_srcache_process_content_length, 0 }, 48 | 49 | { ngx_string("Date"), 50 | ngx_http_srcache_process_header_line, 51 | offsetof(ngx_http_headers_out_t, date) }, 52 | 53 | { ngx_string("Last-Modified"), 54 | ngx_http_srcache_process_last_modified, 0 }, 55 | 56 | { ngx_string("ETag"), 57 | ngx_http_srcache_process_header_line, 58 | offsetof(ngx_http_headers_out_t, etag) }, 59 | 60 | { ngx_string("Server"), 61 | ngx_http_srcache_process_header_line, 62 | offsetof(ngx_http_headers_out_t, server) }, 63 | 64 | { ngx_string("WWW-Authenticate"), 65 | ngx_http_srcache_process_header_line, 66 | offsetof(ngx_http_headers_out_t, www_authenticate) }, 67 | 68 | { ngx_string("Location"), 69 | ngx_http_srcache_process_location, 70 | 0 }, 71 | 72 | { ngx_string("Refresh"), 73 | ngx_http_srcache_process_header_line, 74 | offsetof(ngx_http_headers_out_t, refresh) }, 75 | 76 | { ngx_string("Cache-Control"), 77 | ngx_http_srcache_process_multi_header_lines, 78 | offsetof(ngx_http_headers_out_t, cache_control) }, 79 | 80 | { ngx_string("Expires"), 81 | ngx_http_srcache_process_header_line, 82 | offsetof(ngx_http_headers_out_t, expires) }, 83 | 84 | { ngx_string("X-SRCache-Allow-Ranges"), 85 | ngx_http_srcache_process_allow_ranges, 86 | offsetof(ngx_http_headers_out_t, accept_ranges) }, 87 | 88 | { ngx_string("Accept-Ranges"), 89 | ngx_http_srcache_process_accept_ranges, 90 | offsetof(ngx_http_headers_out_t, accept_ranges) }, 91 | 92 | { ngx_string("Connection"), 93 | ngx_http_srcache_ignore_header_line, 0 }, 94 | 95 | { ngx_string("Keep-Alive"), 96 | ngx_http_srcache_ignore_header_line, 0 }, 97 | 98 | #if (NGX_HTTP_GZIP) 99 | { ngx_string("Content-Encoding"), 100 | ngx_http_srcache_process_content_encoding, 101 | offsetof(ngx_http_headers_out_t, content_encoding) }, 102 | #endif 103 | 104 | { ngx_null_string, NULL, 0 } 105 | }; 106 | 107 | 108 | ngx_int_t 109 | ngx_http_srcache_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, 110 | ngx_uint_t offset) 111 | { 112 | ngx_table_elt_t *ho, **ph; 113 | 114 | ho = ngx_list_push(&r->headers_out.headers); 115 | if (ho == NULL) { 116 | return NGX_ERROR; 117 | } 118 | 119 | *ho = *h; 120 | 121 | if (offset) { 122 | ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset); 123 | *ph = ho; 124 | } 125 | 126 | return NGX_OK; 127 | } 128 | 129 | 130 | static ngx_int_t 131 | ngx_http_srcache_process_location(ngx_http_request_t *r, ngx_table_elt_t *h, 132 | ngx_uint_t offset) 133 | { 134 | ngx_table_elt_t *ho; 135 | 136 | ho = ngx_list_push(&r->headers_out.headers); 137 | if (ho == NULL) { 138 | return NGX_ERROR; 139 | } 140 | 141 | *ho = *h; 142 | 143 | if (ho->value.data[0] != '/') { 144 | r->headers_out.location = ho; 145 | } 146 | 147 | /* 148 | * we do not set r->headers_out.location here to avoid the handling 149 | * the local redirects without a host name by ngx_http_header_filter() 150 | */ 151 | 152 | return NGX_OK; 153 | } 154 | 155 | 156 | static ngx_int_t 157 | ngx_http_srcache_process_content_type(ngx_http_request_t *r, ngx_table_elt_t *h, 158 | ngx_uint_t offset) 159 | { 160 | u_char *p, *last; 161 | 162 | r->headers_out.content_type_len = h->value.len; 163 | r->headers_out.content_type = h->value; 164 | r->headers_out.content_type_lowcase = NULL; 165 | 166 | for (p = h->value.data; *p; p++) { 167 | 168 | if (*p != ';') { 169 | continue; 170 | } 171 | 172 | last = p; 173 | 174 | while (*++p == ' ') { /* void */ } 175 | 176 | if (*p == '\0') { 177 | return NGX_OK; 178 | } 179 | 180 | if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) { 181 | continue; 182 | } 183 | 184 | p += 8; 185 | 186 | r->headers_out.content_type_len = last - h->value.data; 187 | 188 | if (*p == '"') { 189 | p++; 190 | } 191 | 192 | last = h->value.data + h->value.len; 193 | 194 | if (*(last - 1) == '"') { 195 | last--; 196 | } 197 | 198 | r->headers_out.charset.len = last - p; 199 | r->headers_out.charset.data = p; 200 | 201 | return NGX_OK; 202 | } 203 | 204 | return NGX_OK; 205 | } 206 | 207 | 208 | static ngx_int_t 209 | ngx_http_srcache_process_content_length(ngx_http_request_t *r, 210 | ngx_table_elt_t *h, ngx_uint_t offset) 211 | { 212 | ngx_table_elt_t *ho; 213 | 214 | ho = ngx_list_push(&r->headers_out.headers); 215 | if (ho == NULL) { 216 | return NGX_ERROR; 217 | } 218 | 219 | *ho = *h; 220 | 221 | r->headers_out.content_length = ho; 222 | r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len); 223 | 224 | return NGX_OK; 225 | } 226 | 227 | 228 | static ngx_int_t 229 | ngx_http_srcache_process_last_modified(ngx_http_request_t *r, 230 | ngx_table_elt_t *h, ngx_uint_t offset) 231 | { 232 | #if 1 233 | ngx_table_elt_t *ho; 234 | 235 | ho = ngx_list_push(&r->headers_out.headers); 236 | if (ho == NULL) { 237 | return NGX_ERROR; 238 | } 239 | 240 | *ho = *h; 241 | 242 | r->headers_out.last_modified = ho; 243 | #endif 244 | 245 | r->headers_out.last_modified_time = ngx_http_parse_time(h->value.data, 246 | h->value.len); 247 | 248 | dd("setting last-modified-time: %d", 249 | (int) r->headers_out.last_modified_time); 250 | 251 | return NGX_OK; 252 | } 253 | 254 | 255 | static ngx_int_t 256 | ngx_http_srcache_process_multi_header_lines(ngx_http_request_t *r, 257 | ngx_table_elt_t *h, ngx_uint_t offset) 258 | { 259 | #if defined(nginx_version) && nginx_version < 1023000 260 | ngx_array_t *pa; 261 | #endif 262 | ngx_table_elt_t *ho, **ph; 263 | 264 | #if defined(nginx_version) && nginx_version >= 1023000 265 | ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset); 266 | while (*ph) { 267 | ph = &(*ph)->next; 268 | } 269 | #else 270 | pa = (ngx_array_t *) ((char *) &r->headers_out + offset); 271 | 272 | if (pa->elts == NULL) { 273 | if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) 274 | { 275 | return NGX_ERROR; 276 | } 277 | } 278 | 279 | ph = ngx_array_push(pa); 280 | if (ph == NULL) { 281 | return NGX_ERROR; 282 | } 283 | #endif 284 | 285 | ho = ngx_list_push(&r->headers_out.headers); 286 | if (ho == NULL) { 287 | return NGX_ERROR; 288 | } 289 | 290 | *ho = *h; 291 | *ph = ho; 292 | #if defined(nginx_version) && nginx_version >= 1023000 293 | ho->next = NULL; 294 | #endif 295 | 296 | return NGX_OK; 297 | } 298 | 299 | 300 | static ngx_int_t 301 | ngx_http_srcache_process_accept_ranges(ngx_http_request_t *r, 302 | ngx_table_elt_t *h, ngx_uint_t offset) 303 | { 304 | ngx_table_elt_t *ho; 305 | 306 | ho = ngx_list_push(&r->headers_out.headers); 307 | if (ho == NULL) { 308 | return NGX_ERROR; 309 | } 310 | 311 | *ho = *h; 312 | 313 | r->headers_out.accept_ranges = ho; 314 | 315 | return NGX_OK; 316 | } 317 | 318 | 319 | static ngx_int_t 320 | ngx_http_srcache_process_allow_ranges(ngx_http_request_t *r, 321 | ngx_table_elt_t *h, ngx_uint_t offset) 322 | { 323 | r->allow_ranges = 1; 324 | 325 | return NGX_OK; 326 | } 327 | 328 | 329 | static ngx_int_t 330 | ngx_http_srcache_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, 331 | ngx_uint_t offset) 332 | { 333 | return NGX_OK; 334 | } 335 | 336 | 337 | #if (NGX_HTTP_GZIP) 338 | 339 | static ngx_int_t 340 | ngx_http_srcache_process_content_encoding(ngx_http_request_t *r, 341 | ngx_table_elt_t *h, ngx_uint_t offset) 342 | { 343 | ngx_table_elt_t *ho; 344 | 345 | ho = ngx_list_push(&r->headers_out.headers); 346 | if (ho == NULL) { 347 | return NGX_ERROR; 348 | } 349 | 350 | *ho = *h; 351 | 352 | r->headers_out.content_encoding = ho; 353 | 354 | return NGX_OK; 355 | } 356 | 357 | #endif 358 | 359 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 360 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_headers.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef _NGX_HTTP_SRCACHE_HEADERS_H_INCLUDED_ 8 | #define _NGX_HTTP_SRCACHE_HEADERS_H_INCLUDED_ 9 | 10 | 11 | #include "ngx_http_srcache_filter_module.h" 12 | 13 | 14 | typedef struct { 15 | ngx_str_t name; 16 | ngx_http_header_handler_pt handler; 17 | ngx_uint_t offset; 18 | } ngx_http_srcache_header_t; 19 | 20 | 21 | extern ngx_http_srcache_header_t ngx_http_srcache_headers_in[]; 22 | 23 | 24 | ngx_int_t ngx_http_srcache_process_header_line(ngx_http_request_t *r, 25 | ngx_table_elt_t *h, ngx_uint_t offset); 26 | 27 | 28 | #endif /* _NGX_HTTP_SRCACHE_HEADERS_H_INCLUDED_ */ 29 | 30 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 31 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_store.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef _NGX_HTTP_SRCACHE_STORE_H_INCLUDED_ 8 | #define _NGX_HTTP_SRCACHE_STORE_H_INCLUDED_ 9 | 10 | 11 | #include "ngx_http_srcache_filter_module.h" 12 | 13 | 14 | extern ngx_http_output_header_filter_pt ngx_http_srcache_next_header_filter; 15 | extern ngx_http_output_body_filter_pt ngx_http_srcache_next_body_filter; 16 | 17 | 18 | ngx_int_t ngx_http_srcache_filter_init(ngx_conf_t *cf); 19 | 20 | 21 | #endif /* _NGX_HTTP_SRCACHE_STORE_H_INCLUDED_ */ 22 | 23 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 24 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_util.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef _NGX_HTTP_SRCACHE_UTIL_H_INCLUDED_ 8 | #define _NGX_HTTP_SRCACHE_UTIL_H_INCLUDED_ 9 | 10 | #include "ngx_http_srcache_filter_module.h" 11 | 12 | 13 | #define ngx_http_srcache_method_name(m) { sizeof(m) - 1, (u_char *) m " " } 14 | 15 | #define ngx_http_srcache_strcmp_const(a, b) \ 16 | ngx_strncmp(a, b, sizeof(b) - 1) 17 | 18 | extern ngx_str_t ngx_http_srcache_content_length_header_key; 19 | 20 | extern ngx_str_t ngx_http_srcache_get_method; 21 | extern ngx_str_t ngx_http_srcache_put_method; 22 | extern ngx_str_t ngx_http_srcache_post_method; 23 | extern ngx_str_t ngx_http_srcache_head_method; 24 | extern ngx_str_t ngx_http_srcache_copy_method; 25 | extern ngx_str_t ngx_http_srcache_move_method; 26 | extern ngx_str_t ngx_http_srcache_lock_method; 27 | extern ngx_str_t ngx_http_srcache_mkcol_method; 28 | extern ngx_str_t ngx_http_srcache_trace_method; 29 | extern ngx_str_t ngx_http_srcache_delete_method; 30 | extern ngx_str_t ngx_http_srcache_unlock_method; 31 | extern ngx_str_t ngx_http_srcache_options_method; 32 | extern ngx_str_t ngx_http_srcache_propfind_method; 33 | extern ngx_str_t ngx_http_srcache_proppatch_method; 34 | 35 | ngx_int_t ngx_http_srcache_parse_method_name(ngx_str_t **method_name_ptr); 36 | void ngx_http_srcache_discard_bufs(ngx_pool_t *pool, ngx_chain_t *in); 37 | ngx_int_t ngx_http_srcache_adjust_subrequest(ngx_http_request_t *sr, 38 | ngx_http_srcache_parsed_request_t *parsed_sr); 39 | ngx_int_t ngx_http_srcache_add_copy_chain(ngx_pool_t *pool, 40 | ngx_chain_t **chain, ngx_chain_t *in, unsigned *plast); 41 | ngx_int_t ngx_http_srcache_post_request_at_head(ngx_http_request_t *r, 42 | ngx_http_posted_request_t *pr); 43 | ngx_int_t ngx_http_srcache_request_no_cache(ngx_http_request_t *r, 44 | unsigned *no_store); 45 | ngx_int_t ngx_http_srcache_response_no_cache(ngx_http_request_t *r, 46 | ngx_http_srcache_loc_conf_t *conf, ngx_http_srcache_ctx_t *ctx); 47 | ngx_int_t ngx_http_srcache_process_status_line(ngx_http_request_t *r, 48 | ngx_buf_t *b); 49 | ngx_int_t ngx_http_srcache_process_header(ngx_http_request_t *r, 50 | ngx_buf_t *b); 51 | ngx_int_t ngx_http_srcache_store_response_header(ngx_http_request_t *r, 52 | ngx_http_srcache_ctx_t *ctx); 53 | ngx_int_t ngx_http_srcache_hide_headers_hash(ngx_conf_t *cf, 54 | ngx_http_srcache_loc_conf_t *conf, ngx_http_srcache_loc_conf_t *prev, 55 | ngx_str_t *default_hide_headers, ngx_hash_init_t *hash); 56 | ngx_int_t ngx_http_srcache_cmp_int(const void *one, const void *two); 57 | 58 | 59 | #endif /* _NGX_HTTP_SRCACHE_UTIL_H_INCLUDED_ */ 60 | 61 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 62 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_var.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef DDEBUG 8 | #define DDEBUG 0 9 | #endif 10 | #include "ddebug.h" 11 | 12 | 13 | #include "ngx_http_srcache_var.h" 14 | 15 | 16 | static ngx_int_t ngx_http_srcache_expire_variable(ngx_http_request_t *r, 17 | ngx_http_variable_value_t *v, uintptr_t data); 18 | static ngx_int_t ngx_http_srcache_fetch_status_variable(ngx_http_request_t *r, 19 | ngx_http_variable_value_t *v, uintptr_t data); 20 | static ngx_int_t ngx_http_srcache_store_status_variable(ngx_http_request_t *r, 21 | ngx_http_variable_value_t *v, uintptr_t data); 22 | 23 | 24 | static ngx_str_t ngx_http_srcache_fetch_status[] = { 25 | ngx_string("BYPASS"), 26 | ngx_string("MISS"), 27 | ngx_string("HIT") 28 | }; 29 | 30 | 31 | static ngx_str_t ngx_http_srcache_store_status[] = { 32 | ngx_string("BYPASS"), 33 | ngx_string("STORE"), 34 | }; 35 | 36 | 37 | static ngx_http_variable_t ngx_http_srcache_variables[] = { 38 | 39 | { ngx_string("srcache_expire"), NULL, 40 | ngx_http_srcache_expire_variable, 0, 41 | NGX_HTTP_VAR_NOCACHEABLE, 0 }, 42 | 43 | { ngx_string("srcache_fetch_status"), NULL, 44 | ngx_http_srcache_fetch_status_variable, 0, 45 | NGX_HTTP_VAR_NOCACHEABLE, 0 }, 46 | 47 | { ngx_string("srcache_store_status"), NULL, 48 | ngx_http_srcache_store_status_variable, 0, 49 | NGX_HTTP_VAR_NOCACHEABLE, 0 }, 50 | 51 | { ngx_null_string, NULL, NULL, 0, 0, 0 } 52 | }; 53 | 54 | 55 | static ngx_int_t 56 | ngx_http_srcache_expire_variable(ngx_http_request_t *r, 57 | ngx_http_variable_value_t *v, uintptr_t data) 58 | { 59 | ngx_http_srcache_ctx_t *ctx; 60 | u_char *p; 61 | time_t expire; 62 | ngx_http_srcache_loc_conf_t *conf; 63 | 64 | conf = ngx_http_get_module_loc_conf(r, ngx_http_srcache_filter_module); 65 | 66 | v->valid = 1; 67 | v->no_cacheable = 1; 68 | v->not_found = 0; 69 | 70 | ctx = ngx_http_get_module_ctx(r, ngx_http_srcache_filter_module); 71 | 72 | if (!ctx || !ctx->store_response) { 73 | v->not_found = 1; 74 | return NGX_OK; 75 | } 76 | 77 | if (ctx->valid_sec == 0) { 78 | expire = conf->default_expire; 79 | 80 | } else { 81 | expire = ctx->valid_sec - ngx_time(); 82 | } 83 | 84 | if (conf->max_expire > 0 && expire > conf->max_expire) { 85 | expire = conf->max_expire; 86 | } 87 | 88 | p = ngx_palloc(r->pool, NGX_TIME_T_LEN); 89 | if (p == NULL) { 90 | return NGX_ERROR; 91 | } 92 | 93 | v->data = p; 94 | p = ngx_sprintf(p, "%T", expire); 95 | v->len = p - v->data; 96 | 97 | return NGX_OK; 98 | } 99 | 100 | 101 | static ngx_int_t 102 | ngx_http_srcache_fetch_status_variable(ngx_http_request_t *r, 103 | ngx_http_variable_value_t *v, uintptr_t data) 104 | { 105 | ngx_uint_t status; 106 | ngx_http_srcache_ctx_t *ctx; 107 | 108 | ctx = ngx_http_get_module_ctx(r, ngx_http_srcache_filter_module); 109 | 110 | if (ctx == NULL) { 111 | status = NGX_HTTP_SRCACHE_FETCH_BYPASS; 112 | 113 | } else if (ctx->from_cache) { 114 | status = NGX_HTTP_SRCACHE_FETCH_HIT; 115 | 116 | } else if (ctx->issued_fetch_subrequest) { 117 | status = NGX_HTTP_SRCACHE_FETCH_MISS; 118 | 119 | } else { 120 | status = NGX_HTTP_SRCACHE_FETCH_BYPASS; 121 | } 122 | 123 | v->valid = 1; 124 | v->no_cacheable = 1; 125 | v->not_found = 0; 126 | 127 | v->len = ngx_http_srcache_fetch_status[status].len; 128 | v->data = ngx_http_srcache_fetch_status[status].data; 129 | 130 | return NGX_OK; 131 | } 132 | 133 | 134 | static ngx_int_t 135 | ngx_http_srcache_store_status_variable(ngx_http_request_t *r, 136 | ngx_http_variable_value_t *v, uintptr_t data) 137 | { 138 | ngx_uint_t status; 139 | ngx_http_srcache_ctx_t *ctx; 140 | 141 | ctx = ngx_http_get_module_ctx(r, ngx_http_srcache_filter_module); 142 | 143 | if (ctx && ctx->store_response) { 144 | status = NGX_HTTP_SRCACHE_STORE_STORE; 145 | 146 | } else { 147 | status = NGX_HTTP_SRCACHE_STORE_BYPASS; 148 | 149 | } 150 | 151 | v->valid = 1; 152 | v->no_cacheable = 1; 153 | v->not_found = 0; 154 | 155 | v->len = ngx_http_srcache_store_status[status].len; 156 | v->data = ngx_http_srcache_store_status[status].data; 157 | 158 | return NGX_OK; 159 | } 160 | 161 | 162 | ngx_int_t 163 | ngx_http_srcache_add_variables(ngx_conf_t *cf) 164 | { 165 | ngx_http_variable_t *var, *v; 166 | 167 | for (v = ngx_http_srcache_variables; v->name.len; v++) { 168 | var = ngx_http_add_variable(cf, &v->name, v->flags); 169 | if (var == NULL) { 170 | return NGX_ERROR; 171 | } 172 | 173 | var->get_handler = v->get_handler; 174 | var->data = v->data; 175 | } 176 | 177 | return NGX_OK; 178 | } 179 | 180 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 181 | -------------------------------------------------------------------------------- /src/ngx_http_srcache_var.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (C) Yichun Zhang (agentzh) 4 | */ 5 | 6 | 7 | #ifndef _NGX_HTTP_SRCACHE_VAR_H_INCLUDED_ 8 | #define _NGX_HTTP_SRCACHE_VAR_H_INCLUDED_ 9 | 10 | 11 | #include "ngx_http_srcache_filter_module.h" 12 | 13 | 14 | ngx_int_t ngx_http_srcache_add_variables(ngx_conf_t *cf); 15 | 16 | 17 | #endif /* _NGX_HTTP_SRCACHE_VAR_H_INCLUDED_ */ 18 | 19 | /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ 20 | -------------------------------------------------------------------------------- /t/000_init.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | repeat_each(1); 7 | 8 | plan tests => repeat_each() * (2 * blocks()); 9 | 10 | $ENV{TEST_NGINX_MYSQL_PORT} ||= 3306; 11 | $ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432; 12 | 13 | our $http_config = <<'_EOC_'; 14 | upstream database { 15 | drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql 16 | dbname=ngx_test user=ngx_test password=ngx_test; 17 | } 18 | 19 | upstream pg { 20 | postgres_server 127.0.0.1:$TEST_NGINX_POSTGRESQL_PORT 21 | dbname=ngx_test user=ngx_test password=ngx_test; 22 | } 23 | 24 | _EOC_ 25 | 26 | worker_connections(128); 27 | no_shuffle(); 28 | run_tests(); 29 | 30 | no_diff(); 31 | 32 | __DATA__ 33 | 34 | === TEST 1: cats - drop table 35 | --- http_config eval: $::http_config 36 | --- config 37 | location = /init { 38 | drizzle_pass database; 39 | drizzle_query "DROP TABLE IF EXISTS cats"; 40 | } 41 | --- request 42 | GET /init 43 | --- error_code: 200 44 | --- timeout: 10 45 | --- no_error_log 46 | [error] 47 | 48 | 49 | 50 | === TEST 2: cats - create table 51 | --- http_config eval: $::http_config 52 | --- config 53 | location = /init { 54 | drizzle_pass database; 55 | drizzle_query "CREATE TABLE cats (id integer, name text)"; 56 | } 57 | --- request 58 | GET /init 59 | --- error_code: 200 60 | --- timeout: 10 61 | --- no_error_log 62 | [error] 63 | 64 | 65 | 66 | === TEST 3: cats - insert value 67 | --- http_config eval: $::http_config 68 | --- config 69 | location = /init { 70 | drizzle_pass database; 71 | drizzle_query "INSERT INTO cats (id) VALUES (2)"; 72 | } 73 | --- request 74 | GET /init 75 | --- error_code: 200 76 | --- timeout: 10 77 | --- no_error_log 78 | [error] 79 | 80 | 81 | 82 | === TEST 4: cats - insert value 83 | --- http_config eval: $::http_config 84 | --- config 85 | location = /init { 86 | drizzle_pass database; 87 | drizzle_query "INSERT INTO cats (id, name) VALUES (3, 'bob')"; 88 | } 89 | --- request 90 | GET /init 91 | --- error_code: 200 92 | --- timeout: 10 93 | --- no_error_log 94 | [error] 95 | 96 | 97 | 98 | === TEST 5: cats - drop table - pg 99 | --- http_config eval: $::http_config 100 | --- config 101 | location = /init { 102 | postgres_pass pg; 103 | postgres_query "DROP TABLE IF EXISTS cats"; 104 | } 105 | --- request 106 | GET /init 107 | --- error_code: 200 108 | --- timeout: 10 109 | --- no_error_log 110 | [error] 111 | 112 | 113 | 114 | === TEST 6: cats - create table - pg 115 | --- http_config eval: $::http_config 116 | --- config 117 | location = /init { 118 | postgres_pass pg; 119 | postgres_query "CREATE TABLE cats (id integer, name text)"; 120 | } 121 | --- request 122 | GET /init 123 | --- error_code: 200 124 | --- timeout: 10 125 | --- no_error_log 126 | [error] 127 | 128 | 129 | 130 | === TEST 7: cats - insert value - pg 131 | --- http_config eval: $::http_config 132 | --- config 133 | location = /init { 134 | postgres_pass pg; 135 | postgres_query "INSERT INTO cats (id) VALUES (2)"; 136 | } 137 | --- request 138 | GET /init 139 | --- error_code: 200 140 | --- timeout: 10 141 | --- no_error_log 142 | [error] 143 | 144 | 145 | 146 | === TEST 8: cats - insert value - pg 147 | --- http_config eval: $::http_config 148 | --- config 149 | location = /init { 150 | postgres_pass pg; 151 | postgres_query "INSERT INTO cats (id, name) VALUES (3, 'bob')"; 152 | } 153 | --- request 154 | GET /init 155 | --- error_code: 200 156 | --- timeout: 10 157 | --- no_error_log 158 | [error] 159 | -------------------------------------------------------------------------------- /t/access.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 3 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | no_shuffle(); 13 | 14 | run_tests(); 15 | 16 | __DATA__ 17 | 18 | === TEST 1: flush all 19 | --- config 20 | location /flush { 21 | set $memc_cmd 'flush_all'; 22 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 23 | } 24 | --- response_headers 25 | Content-Type: text/plain 26 | --- request 27 | GET /flush 28 | --- response_body eval: "OK\r\n" 29 | 30 | 31 | 32 | === TEST 2: basic fetch 33 | --- config 34 | location /foo { 35 | default_type text/css; 36 | srcache_fetch GET /memc $uri; 37 | srcache_store PUT /memc $uri; 38 | 39 | #deny all; 40 | 41 | echo hello; 42 | } 43 | 44 | location /memc { 45 | internal; 46 | 47 | set $memc_key $query_string; 48 | set $memc_exptime 300; 49 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 50 | } 51 | --- request 52 | GET /foo 53 | --- response_headers 54 | Content-Type: text/css 55 | --- response_body 56 | hello 57 | 58 | 59 | 60 | === TEST 3: basic fetch 61 | --- config 62 | location /foo { 63 | default_type text/css; 64 | srcache_fetch GET /memc $uri; 65 | srcache_store PUT /memc $uri; 66 | 67 | deny all; 68 | 69 | echo world; 70 | } 71 | 72 | location /memc { 73 | internal; 74 | 75 | set $memc_key $query_string; 76 | set $memc_exptime 300; 77 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 78 | } 79 | --- request 80 | GET /foo 81 | --- response_headers 82 | Content-Type: text/html 83 | --- response_body_like: 403 Forbidden 84 | --- error_code: 403 85 | -------------------------------------------------------------------------------- /t/conditional-get.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(100); 7 | 8 | plan tests => repeat_each() * (6 * blocks() - 1); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | #$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306; 12 | 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | !Last-Modified 29 | --- request 30 | GET /flush 31 | --- response_body eval: "OK\r\n" 32 | --- no_error_log 33 | [error] 34 | 35 | 36 | 37 | === TEST 2: cache miss 38 | --- config 39 | location /cats { 40 | srcache_fetch GET /memc $uri; 41 | srcache_store PUT /memc $uri; 42 | 43 | default_type application/json; 44 | 45 | content_by_lua ' 46 | ngx.header.last_modified = "Thu, 10 May 2012 07:50:59 GMT" 47 | ngx.say("hello") 48 | '; 49 | } 50 | 51 | location /memc { 52 | internal; 53 | 54 | set $memc_key $query_string; 55 | set $memc_exptime 3000; 56 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 57 | } 58 | --- request 59 | GET /cats 60 | --- response_headers 61 | Content-Type: application/json 62 | Last-Modified: Thu, 10 May 2012 07:50:59 GMT 63 | !Content-Length 64 | --- response_body 65 | hello 66 | --- no_error_log 67 | [error] 68 | 69 | 70 | 71 | === TEST 3: cache hit 72 | --- config 73 | location /cats { 74 | srcache_fetch GET /memc $uri; 75 | srcache_store PUT /memc $uri; 76 | 77 | default_type text/css; 78 | 79 | proxy_pass http://agentzh.org:12345/; 80 | } 81 | 82 | location /memc { 83 | internal; 84 | 85 | set $memc_key $query_string; 86 | set $memc_exptime 3000; 87 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 88 | } 89 | --- request 90 | GET /cats 91 | --- response_headers 92 | Content-Type: application/json 93 | Content-Length: 6 94 | Last-Modified: Thu, 10 May 2012 07:50:59 GMT 95 | --- response_body 96 | hello 97 | --- no_error_log 98 | [error] 99 | 100 | 101 | 102 | === TEST 4: cache hit (I-M-S conditional GET, exact) 103 | --- config 104 | location /cats { 105 | srcache_fetch GET /memc $uri; 106 | srcache_store PUT /memc $uri; 107 | 108 | default_type text/css; 109 | 110 | proxy_pass http://agentzh.org:12345/; 111 | } 112 | 113 | location /memc { 114 | internal; 115 | 116 | set $memc_key $query_string; 117 | set $memc_exptime 3000; 118 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 119 | } 120 | --- request 121 | GET /cats 122 | --- more_headers 123 | If-Modified-Since: Thu, 10 May 2012 07:50:59 GMT 124 | --- response_headers 125 | !Content-Type 126 | !Content-Length 127 | Last-Modified: Thu, 10 May 2012 07:50:59 GMT 128 | --- stap2 129 | F(ngx_http_core_content_phase) { 130 | printf("r content handler: %s\n", usymname($r->content_handler)) 131 | } 132 | 133 | --- error_code: 304 134 | --- response_body 135 | --- no_error_log 136 | [error] 137 | 138 | 139 | 140 | === TEST 5: cache hit (I-M-S conditional GET, exact failed) 141 | --- config 142 | location /cats { 143 | srcache_fetch GET /memc $uri; 144 | srcache_store PUT /memc $uri; 145 | 146 | default_type text/css; 147 | 148 | proxy_pass http://agentzh.org:12345/; 149 | } 150 | 151 | location /memc { 152 | internal; 153 | 154 | set $memc_key $query_string; 155 | set $memc_exptime 3000; 156 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 157 | } 158 | --- request 159 | GET /cats 160 | --- more_headers 161 | If-Modified-Since: Thu, 10 May 2012 07:51:00 GMT 162 | --- response_headers 163 | Content-Type: application/json 164 | Content-Length: 6 165 | Last-Modified: Thu, 10 May 2012 07:50:59 GMT 166 | --- response_body 167 | hello 168 | --- no_error_log 169 | [error] 170 | 171 | 172 | 173 | === TEST 6: cache hit (I-M-S conditional GET, exact failed, before suceeded) 174 | --- config 175 | location /cats { 176 | if_modified_since before; 177 | srcache_fetch GET /memc $uri; 178 | srcache_store PUT /memc $uri; 179 | 180 | default_type text/css; 181 | 182 | proxy_pass http://agentzh.org:12345/; 183 | } 184 | 185 | location /memc { 186 | internal; 187 | 188 | set $memc_key $query_string; 189 | set $memc_exptime 3000; 190 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 191 | } 192 | --- request 193 | GET /cats 194 | --- more_headers 195 | If-Modified-Since: Thu, 10 May 2012 07:51:00 GMT 196 | --- response_headers 197 | !Content-Type 198 | !Content-Length 199 | Last-Modified: Thu, 10 May 2012 07:50:59 GMT 200 | --- response_body 201 | --- error_code: 304 202 | --- no_error_log 203 | [error] 204 | 205 | 206 | 207 | === TEST 7: cache hit (I-U-S conditional GET, 412) 208 | --- config 209 | server_tokens off; 210 | location /cats { 211 | srcache_fetch GET /memc $uri; 212 | srcache_store PUT /memc $uri; 213 | 214 | default_type text/css; 215 | 216 | proxy_pass http://agentzh.org:12345/; 217 | } 218 | 219 | location /memc { 220 | internal; 221 | 222 | set $memc_key $query_string; 223 | set $memc_exptime 3000; 224 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 225 | } 226 | --- request 227 | GET /cats 228 | --- more_headers 229 | If-Unmodified-Since: Thu, 10 May 2012 07:50:58 GMT 230 | --- response_headers 231 | Content-Type: text/html 232 | !Last-Modified 233 | --- response_body_like: 412 Precondition Failed 234 | --- error_code: 412 235 | --- no_error_log 236 | [error] 237 | 238 | 239 | 240 | === TEST 8: cache hit (I-U-S conditional GET, precondition succeeded) 241 | --- config 242 | location /cats { 243 | srcache_fetch GET /memc $uri; 244 | srcache_store PUT /memc $uri; 245 | 246 | default_type text/css; 247 | 248 | proxy_pass http://agentzh.org:12345/; 249 | } 250 | 251 | location /memc { 252 | internal; 253 | 254 | set $memc_key $query_string; 255 | set $memc_exptime 3000; 256 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 257 | } 258 | --- request 259 | GET /cats 260 | --- more_headers 261 | If-Unmodified-Since: Thu, 10 May 2012 07:50:59 GMT 262 | --- response_headers 263 | Content-Type: application/json 264 | Content-Length: 6 265 | Last-Modified: Thu, 10 May 2012 07:50:59 GMT 266 | --- response_body 267 | hello 268 | --- no_error_log 269 | [error] 270 | 271 | 272 | 273 | === TEST 9: cache hit (I-U-S conditional GET, precondition succeeded, newer) 274 | --- config 275 | location /cats { 276 | srcache_fetch GET /memc $uri; 277 | srcache_store PUT /memc $uri; 278 | 279 | default_type text/css; 280 | 281 | proxy_pass http://agentzh.org:12345/; 282 | } 283 | 284 | location /memc { 285 | internal; 286 | 287 | set $memc_key $query_string; 288 | set $memc_exptime 3000; 289 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 290 | } 291 | --- request 292 | GET /cats 293 | --- more_headers 294 | If-Unmodified-Since: Thu, 10 May 2012 07:51:00 GMT 295 | --- response_headers 296 | Content-Type: application/json 297 | Content-Length: 6 298 | Last-Modified: Thu, 10 May 2012 07:50:59 GMT 299 | --- response_body 300 | hello 301 | --- no_error_log 302 | [error] 303 | -------------------------------------------------------------------------------- /t/content-length.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (blocks() * 5 - 4); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | --- no_error_log 32 | [error] 33 | 34 | 35 | 36 | === TEST 2: basic fetch (cache miss) 37 | --- config 38 | location /foo { 39 | srcache_fetch GET /memc $uri; 40 | srcache_store PUT /memc $uri; 41 | 42 | proxy_pass http://127.0.0.1:$server_port/gate; 43 | } 44 | 45 | location = /gate { 46 | default_type text/css; 47 | content_by_lua ' 48 | ngx.header.content_length = 10 49 | ngx.say("hello") 50 | '; 51 | } 52 | 53 | location /memc { 54 | internal; 55 | 56 | set $memc_key $query_string; 57 | set $memc_exptime 300; 58 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 59 | } 60 | --- request 61 | GET /foo 62 | --- response_headers 63 | Content-Type: text/css 64 | Content-Length: 10 65 | --- ignore_response 66 | --- no_error_log 67 | srcache_store: subrequest returned status 68 | 69 | 70 | 71 | === TEST 3: basic fetch (cache miss) 72 | --- config 73 | location /foo { 74 | default_type text/css; 75 | srcache_fetch GET /memc $uri; 76 | srcache_store PUT /memc $uri; 77 | 78 | echo world; 79 | } 80 | 81 | location /memc { 82 | internal; 83 | 84 | set $memc_key $query_string; 85 | set $memc_exptime 300; 86 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 87 | } 88 | --- request 89 | GET /foo 90 | --- response_headers 91 | Content-Type: text/css 92 | !Content-Length 93 | --- response_body 94 | world 95 | --- no_error_log 96 | [error] 97 | -------------------------------------------------------------------------------- /t/content-type.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | plan tests => repeat_each() * (4 * blocks()); 7 | 8 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 9 | 10 | no_shuffle(); 11 | 12 | run_tests(); 13 | 14 | __DATA__ 15 | 16 | === TEST 1: flush all 17 | --- config 18 | location /flush { 19 | set $memc_cmd 'flush_all'; 20 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 21 | } 22 | --- request 23 | GET /flush 24 | --- response_body eval: "OK\r\n" 25 | 26 | 27 | 28 | === TEST 2: basic fetch (Set-Cookie and Proxy-Authenticate hide by default) 29 | --- config 30 | location /foo { 31 | charset UTF-8; 32 | charset_types application/x-javascript; 33 | default_type application/json; 34 | srcache_fetch GET /memc $uri; 35 | srcache_store PUT /memc $uri; 36 | xss_get on; 37 | xss_callback_arg c; 38 | 39 | echo hello; 40 | } 41 | 42 | location /memc { 43 | internal; 44 | 45 | set $memc_key $query_string; 46 | set $memc_exptime 300; 47 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 48 | } 49 | --- request 50 | GET /foo?c=bar 51 | --- response_headers 52 | Content-Type: application/x-javascript; charset=UTF-8 53 | Content-Length: 54 | --- response_body chop 55 | bar(hello 56 | ); 57 | 58 | 59 | 60 | === TEST 3: basic fetch (cache hit) 61 | --- config 62 | location /foo { 63 | charset UTF-8; 64 | charset_types application/x-javascript; 65 | default_type text/css; 66 | srcache_fetch GET /memc $uri; 67 | srcache_store PUT /memc $uri; 68 | xss_get on; 69 | xss_callback_arg c; 70 | 71 | echo world; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /foo?c=bar 83 | --- response_headers 84 | Content-Type: application/x-javascript; charset=UTF-8 85 | Content-Length: 86 | !Set-Cookie 87 | !Proxy-Authenticate 88 | --- response_body chop 89 | bar(hello 90 | ); 91 | -------------------------------------------------------------------------------- /t/disk.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(100); 7 | 8 | plan tests => repeat_each() * (2 * blocks() + 1); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | no_shuffle(); 13 | 14 | run_tests(); 15 | 16 | __DATA__ 17 | 18 | === TEST 1: flush all 19 | --- config 20 | location /flush { 21 | set $memc_cmd 'flush_all'; 22 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 23 | } 24 | --- request 25 | GET /flush 26 | --- response_body eval: "OK\r\n" 27 | 28 | 29 | 30 | === TEST 2: cache miss 31 | --- config 32 | location /index.html { 33 | srcache_fetch GET /memc $uri; 34 | srcache_store PUT /memc $uri; 35 | } 36 | 37 | location /memc { 38 | internal; 39 | 40 | set $memc_key $query_string; 41 | set $memc_exptime 300; 42 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 43 | } 44 | --- request 45 | GET /index.html 46 | --- response_headers 47 | Accept-Ranges: bytes 48 | --- response_body_like: It works! 49 | 50 | 51 | 52 | === TEST 3: cache hit 53 | --- config 54 | location /index.html { 55 | srcache_fetch GET /memc $uri; 56 | srcache_store PUT /memc $uri; 57 | } 58 | 59 | location /memc { 60 | internal; 61 | 62 | set $memc_key $query_string; 63 | set $memc_exptime 300; 64 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 65 | } 66 | --- request 67 | GET /index.html 68 | --- response_body_like: It works! 69 | -------------------------------------------------------------------------------- /t/drizzle-main.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(100); 7 | 8 | plan tests => repeat_each() * 4 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | $ENV{TEST_NGINX_MYSQL_PORT} ||= 3306; 12 | 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | --- request 28 | GET /flush 29 | --- response_body eval: "OK\r\n" 30 | --- no_error_log 31 | [error] 32 | 33 | 34 | 35 | === TEST 2: cache miss 36 | --- http_config 37 | upstream backend { 38 | drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql 39 | dbname=ngx_test user=ngx_test password=ngx_test; 40 | } 41 | --- config 42 | location /cats { 43 | srcache_fetch GET /memc $uri; 44 | srcache_store PUT /memc $uri; 45 | 46 | default_type text/css; 47 | 48 | drizzle_pass backend; 49 | drizzle_query 'select * from cats'; 50 | 51 | rds_json on; 52 | } 53 | 54 | location /memc { 55 | internal; 56 | 57 | set $memc_key $query_string; 58 | set $memc_exptime 300; 59 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 60 | } 61 | --- request 62 | GET /cats 63 | --- response_headers 64 | Content-Type: application/json 65 | --- response_body chomp 66 | [{"id":2,"name":null},{"id":3,"name":"bob"}] 67 | --- no_error_log 68 | [error] 69 | 70 | 71 | 72 | === TEST 3: cache hit 73 | --- http_config 74 | upstream backend { 75 | drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql 76 | dbname=ngx_test user=ngx_test password=ngx_test; 77 | } 78 | --- config 79 | location /cats { 80 | srcache_fetch GET /memc $uri; 81 | srcache_store PUT /memc $uri; 82 | 83 | default_type text/css; 84 | 85 | drizzle_pass backend; 86 | drizzle_query 'invalid sql here'; 87 | 88 | rds_json on; 89 | } 90 | 91 | location /memc { 92 | internal; 93 | 94 | set $memc_key $query_string; 95 | set $memc_exptime 300; 96 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 97 | } 98 | --- request 99 | GET /cats 100 | --- response_headers 101 | Content-Type: application/json 102 | --- response_body chomp 103 | [{"id":2,"name":null},{"id":3,"name":"bob"}] 104 | --- no_error_log 105 | [error] 106 | -------------------------------------------------------------------------------- /t/drizzle-sub.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket skip_all => 'subrequests not working'; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 3 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | $ENV{TEST_NGINX_MYSQL_PORT} ||= 3306; 12 | 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | --- request 28 | GET /flush 29 | --- response_body eval: "OK\r\n" 30 | 31 | 32 | 33 | === TEST 2: basic fetch (cache miss) 34 | --- http_config 35 | upstream backend { 36 | drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql 37 | dbname=ngx_test user=ngx_test password=ngx_test; 38 | } 39 | --- config 40 | location /main { 41 | echo_location /cats dir=asc; 42 | echo_location /cats dir=desc; 43 | } 44 | 45 | location /cats { 46 | internal; 47 | 48 | srcache_fetch GET /memc $uri-$arg_dir; 49 | srcache_store PUT /memc $uri-$arg_dir; 50 | 51 | default_type text/css; 52 | 53 | drizzle_pass backend; 54 | drizzle_query 'select * from cats order by id $arg_dir'; 55 | 56 | rds_json on; 57 | } 58 | 59 | location /memc { 60 | internal; 61 | 62 | set $memc_key $query_string; 63 | set $memc_exptime 300; 64 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 65 | } 66 | --- request 67 | GET /main 68 | --- response_headers 69 | Content-Type: text/plain 70 | --- response_body chomp 71 | [{"id":2,"name":null},{"id":3,"name":"bob"}][{"id":3,"name":"bob"},{"id":2,"name":null}] 72 | 73 | 74 | 75 | === TEST 3: basic fetch (cache hit) 76 | --- http_config 77 | upstream backend { 78 | drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql 79 | dbname=ngx_test user=ngx_test password=ngx_test; 80 | } 81 | --- config 82 | location /main { 83 | echo_location /cats dir=asc; 84 | echo_location /cats dir=desc; 85 | } 86 | 87 | location /cats { 88 | internal; 89 | 90 | srcache_fetch GET /memc $uri-$arg_dir; 91 | srcache_store PUT /memc $uri-$arg_dir; 92 | 93 | default_type text/css; 94 | 95 | drizzle_pass backend; 96 | drizzle_query 'invalid sql here'; 97 | 98 | rds_json on; 99 | } 100 | 101 | location /memc { 102 | internal; 103 | 104 | set $memc_key $query_string; 105 | set $memc_exptime 300; 106 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 107 | } 108 | --- request 109 | GET /main 110 | --- response_headers 111 | Content-Type: text/plain 112 | --- response_body chomp 113 | [{"id":2,"name":null},{"id":3,"name":"bob"}][{"id":3,"name":"bob"},{"id":2,"name":null}] 114 | -------------------------------------------------------------------------------- /t/empty-resp.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(100); 7 | 8 | plan tests => repeat_each() * (4 * blocks() + 1); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | #$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306; 12 | 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | --- request 28 | GET /flush 29 | --- response_body eval: "OK\r\n" 30 | --- no_error_log 31 | [error] 32 | 33 | 34 | 35 | === TEST 2: cache miss 36 | --- config 37 | location /cats { 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | default_type application/json; 42 | 43 | echo -n ''; 44 | } 45 | 46 | location /memc { 47 | internal; 48 | 49 | set $memc_key $query_string; 50 | set $memc_exptime 300; 51 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 52 | } 53 | --- request 54 | GET /cats 55 | --- response_headers 56 | Content-Type: application/json 57 | --- response_body: 58 | --- no_error_log 59 | [error] 60 | 61 | 62 | 63 | === TEST 3: cache hit 64 | --- config 65 | location /cats { 66 | srcache_fetch GET /memc $uri; 67 | srcache_store PUT /memc $uri; 68 | 69 | default_type text/css; 70 | 71 | echo hello; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /cats 83 | --- response_headers 84 | Content-Type: application/json 85 | Content-Length: 0 86 | --- response_body: 87 | --- no_error_log 88 | [error] 89 | -------------------------------------------------------------------------------- /t/err-page.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 2 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | no_shuffle(); 13 | 14 | run_tests(); 15 | 16 | __DATA__ 17 | 18 | === TEST 1: flush all 19 | --- config 20 | location /flush { 21 | set $memc_cmd 'flush_all'; 22 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 23 | } 24 | --- request 25 | GET /flush 26 | --- response_body eval: "OK\r\n" 27 | 28 | 29 | 30 | === TEST 2: basic fetch 31 | --- config 32 | location @err { 33 | echo "err"; 34 | } 35 | 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | content_by_lua ' 42 | ngx.exit(404) 43 | '; 44 | error_page 404 = @err; 45 | } 46 | 47 | location /memc { 48 | internal; 49 | 50 | set $memc_key $query_string; 51 | set $memc_exptime 300; 52 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 53 | } 54 | --- request 55 | GET /foo 56 | --- response_body 57 | err 58 | 59 | 60 | 61 | === TEST 3: fetch again 62 | --- config 63 | location /foo { 64 | default_type text/css; 65 | srcache_fetch GET /memc $uri; 66 | srcache_store PUT /memc $uri; 67 | 68 | error_page 404 = @err; 69 | echo hello; 70 | } 71 | 72 | location /memc { 73 | internal; 74 | 75 | set $memc_key $query_string; 76 | set $memc_exptime 300; 77 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 78 | } 79 | --- request 80 | GET /foo 81 | --- response_body 82 | hello 83 | -------------------------------------------------------------------------------- /t/etag.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (5 * blocks() + 4); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all (not using ngx_srcache) 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | add_header X-Fetch-Status $srcache_fetch_status; 25 | add_header X-Store-Status $srcache_store_status; 26 | } 27 | --- response_headers 28 | Content-Type: text/plain 29 | Content-Length: 4 30 | X-Fetch-Status: BYPASS 31 | X-Store-Status: BYPASS 32 | 33 | --- request 34 | GET /flush 35 | --- response_body eval: "OK\r\n" 36 | 37 | 38 | 39 | === TEST 2: basic fetch (cache miss) 40 | --- config 41 | location /foo { 42 | default_type text/css; 43 | srcache_fetch GET /memc $uri; 44 | srcache_store PUT /memc $uri; 45 | 46 | content_by_lua ' 47 | ngx.header["ETag"] = [["abcd1234"]] 48 | ngx.header["Last-Modified"] = "Sat, 01 Mar 2014 01:02:38 GMT" 49 | ngx.say("hello") 50 | '; 51 | #echo hello; 52 | add_header X-Fetch-Status $srcache_fetch_status; 53 | add_header X-Store-Status $srcache_store_status; 54 | } 55 | 56 | location /memc { 57 | internal; 58 | 59 | set $memc_key $query_string; 60 | set $memc_exptime 300; 61 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 62 | } 63 | --- request 64 | GET /foo 65 | --- response_headers 66 | Content-Type: text/css 67 | Content-Length: 68 | X-Fetch-Status: MISS 69 | X-Store-Status: STORE 70 | Last-Modified: Sat, 01 Mar 2014 01:02:38 GMT 71 | --- response_body 72 | hello 73 | --- wait: 0.1 74 | --- error_log 75 | srcache_store: subrequest returned status 201 76 | --- log_level: debug 77 | 78 | 79 | 80 | === TEST 3: basic fetch (cache hit) 81 | --- config 82 | location /foo { 83 | default_type text/css; 84 | srcache_fetch GET /memc $uri; 85 | srcache_store PUT /memc $uri; 86 | 87 | echo world; 88 | add_header X-Fetch-Status $srcache_fetch_status; 89 | add_header X-Store-Status $srcache_store_status; 90 | } 91 | 92 | location /memc { 93 | internal; 94 | 95 | set $memc_key $query_string; 96 | set $memc_exptime 300; 97 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 98 | } 99 | --- request 100 | GET /foo 101 | --- more_headers 102 | If-None-Match: "abcd1234" 103 | --- response_headers 104 | X-Fetch-Status: HIT 105 | X-Store-Status: BYPASS 106 | ETag: "abcd1234" 107 | Last-Modified: Sat, 01 Mar 2014 01:02:38 GMT 108 | --- response_body: 109 | --- error_code: 304 110 | 111 | 112 | 113 | === TEST 4: flush all (not using ngx_srcache) 114 | --- config 115 | location /flush { 116 | set $memc_cmd 'flush_all'; 117 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 118 | add_header X-Fetch-Status $srcache_fetch_status; 119 | add_header X-Store-Status $srcache_store_status; 120 | } 121 | --- response_headers 122 | Content-Type: text/plain 123 | Content-Length: 4 124 | X-Fetch-Status: BYPASS 125 | X-Store-Status: BYPASS 126 | 127 | --- request 128 | GET /flush 129 | --- response_body eval: "OK\r\n" 130 | 131 | 132 | 133 | === TEST 5: basic fetch (cache miss, 304) 134 | --- config 135 | location /foo { 136 | default_type text/css; 137 | srcache_fetch GET /memc $uri; 138 | srcache_store PUT /memc $uri; 139 | 140 | content_by_lua ' 141 | ngx.header["ETag"] = [["abcd1234"]] 142 | ngx.header["Last-Modified"] = "Sat, 01 Mar 2014 01:02:38 GMT" 143 | ngx.say("hello") 144 | '; 145 | #echo hello; 146 | add_header X-Fetch-Status $srcache_fetch_status; 147 | add_header X-Store-Status $srcache_store_status; 148 | } 149 | 150 | location /memc { 151 | internal; 152 | 153 | set $memc_key $query_string; 154 | set $memc_exptime 300; 155 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 156 | } 157 | --- request 158 | GET /foo 159 | --- more_headers 160 | If-None-Match: "abcd1234" 161 | --- response_headers 162 | Content-Length: 163 | X-Fetch-Status: MISS 164 | X-Store-Status: BYPASS 165 | Last-Modified: Sat, 01 Mar 2014 01:02:38 GMT 166 | --- response_body: 167 | --- error_code: 304 168 | --- wait: 0.1 169 | --- error_log 170 | --- log_level: debug 171 | 172 | 173 | 174 | === TEST 6: basic fetch (cache hit) 175 | --- config 176 | location /memc { 177 | #internal; 178 | 179 | set $memc_key $query_string; 180 | set $memc_exptime 300; 181 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 182 | } 183 | --- request 184 | GET /memc?/foo 185 | --- response_body_like: 404 Not Found 186 | --- error_code: 404 187 | -------------------------------------------------------------------------------- /t/eval.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | repeat_each(3); 7 | 8 | plan tests => repeat_each() * 2 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | $ENV{TEST_NGINX_MYSQL_PORT} ||= 3306; 12 | 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- request 26 | GET /flush 27 | --- response_body eval: "OK\r\n" 28 | 29 | 30 | 31 | === TEST 2: cache miss 32 | --- http_config 33 | upstream backend { 34 | drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql 35 | dbname=ngx_test user=ngx_test password=ngx_test; 36 | } 37 | 38 | upstream mem_backend { 39 | server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 40 | #keepalive 100 single; 41 | } 42 | --- config 43 | location /test1 { 44 | srcache_fetch GET /memc $uri; 45 | srcache_store PUT /memc $uri; 46 | default_type text/css; 47 | charset UTF-8; 48 | 49 | drizzle_query "select 1+2 as a;"; 50 | drizzle_pass backend; 51 | rds_json on; 52 | } 53 | 54 | location /memc { 55 | #internal; 56 | 57 | set $memc_key $query_string; 58 | set $memc_exptime 300; 59 | memc_pass mem_backend; 60 | } 61 | 62 | location /pt1 { 63 | echo "pt1"; 64 | } 65 | 66 | location /proxy { 67 | eval_subrequest_in_memory off; 68 | eval_override_content_type text/plain; 69 | 70 | eval $res { 71 | #proxy_pass http://127.0.0.1:$server_port/test1; 72 | proxy_pass $scheme://127.0.0.1:$server_port/pt1; 73 | #echo "ab"; 74 | } 75 | 76 | if ($res ~ '1') { 77 | #proxy_pass $scheme://127.0.0.1:$server_port/test1; 78 | #echo_exec /pi; 79 | echo_exec /test1; 80 | #echo_exec /pt1; 81 | #echo "okay $res"; 82 | break; 83 | } 84 | 85 | echo "[$res] error"; 86 | } 87 | 88 | location /pi { 89 | proxy_pass $scheme://127.0.0.1:$server_port/test1; 90 | #echo hi; 91 | } 92 | 93 | --- request 94 | GET /proxy 95 | --- response_body chomp 96 | [{"a":3}] 97 | -------------------------------------------------------------------------------- /t/expires.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 4 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss), and no store due to Expires: 35 | --- config 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | content_by_lua ' 42 | ngx.header.expires = ngx.http_time(ngx.time()) 43 | ngx.say("hello") 44 | '; 45 | } 46 | 47 | location /memc { 48 | internal; 49 | 50 | set $memc_key $query_string; 51 | set $memc_exptime 300; 52 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 53 | } 54 | --- request 55 | GET /foo 56 | --- response_headers 57 | Content-Type: text/css 58 | Content-Length: 59 | --- response_body 60 | hello 61 | 62 | 63 | 64 | === TEST 3: basic fetch (cache miss because not stored before) 65 | --- config 66 | location /foo { 67 | default_type text/css; 68 | srcache_fetch GET /memc $uri; 69 | srcache_store PUT /memc $uri; 70 | 71 | echo world; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /foo 83 | --- response_headers 84 | Content-Type: text/css 85 | Content-Length: 86 | --- response_body 87 | world 88 | 89 | 90 | 91 | === TEST 4: flush all 92 | --- config 93 | location /flush { 94 | set $memc_cmd 'flush_all'; 95 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 96 | } 97 | --- response_headers 98 | Content-Type: text/plain 99 | Content-Length: 4 100 | --- request 101 | GET /flush 102 | --- response_body eval: "OK\r\n" 103 | 104 | 105 | 106 | === TEST 5: basic fetch (cache miss), and no store due to Expires: - 1 107 | --- config 108 | location /foo { 109 | default_type text/css; 110 | srcache_fetch GET /memc $uri; 111 | srcache_store PUT /memc $uri; 112 | 113 | content_by_lua ' 114 | ngx.header.expires = ngx.http_time(ngx.time() - 1) 115 | ngx.say("hello") 116 | '; 117 | } 118 | 119 | location /memc { 120 | internal; 121 | 122 | set $memc_key $query_string; 123 | set $memc_exptime 300; 124 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 125 | } 126 | --- request 127 | GET /foo 128 | --- response_headers 129 | Content-Type: text/css 130 | Content-Length: 131 | --- response_body 132 | hello 133 | 134 | 135 | 136 | === TEST 6: basic fetch (cache miss because not stored before) 137 | --- config 138 | location /foo { 139 | default_type text/css; 140 | srcache_fetch GET /memc $uri; 141 | srcache_store PUT /memc $uri; 142 | 143 | echo world; 144 | } 145 | 146 | location /memc { 147 | internal; 148 | 149 | set $memc_key $query_string; 150 | set $memc_exptime 300; 151 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 152 | } 153 | --- request 154 | GET /foo 155 | --- response_headers 156 | Content-Type: text/css 157 | Content-Length: 158 | --- response_body 159 | world 160 | 161 | 162 | 163 | === TEST 7: flush all 164 | --- config 165 | location /flush { 166 | set $memc_cmd 'flush_all'; 167 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 168 | } 169 | --- response_headers 170 | Content-Type: text/plain 171 | Content-Length: 4 172 | --- request 173 | GET /flush 174 | --- response_body eval: "OK\r\n" 175 | 176 | 177 | 178 | === TEST 8: basic fetch (cache miss), and no store due to Expires: , and srcache_response_cache_control on 179 | --- config 180 | location /foo { 181 | default_type text/css; 182 | srcache_fetch GET /memc $uri; 183 | srcache_store PUT /memc $uri; 184 | srcache_response_cache_control on; 185 | 186 | content_by_lua ' 187 | ngx.header.expires = ngx.http_time(ngx.time()) 188 | ngx.say("hello") 189 | '; 190 | } 191 | 192 | location /memc { 193 | internal; 194 | 195 | set $memc_key $query_string; 196 | set $memc_exptime 300; 197 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 198 | } 199 | --- request 200 | GET /foo 201 | --- response_headers 202 | Content-Type: text/css 203 | Content-Length: 204 | --- response_body 205 | hello 206 | 207 | 208 | 209 | === TEST 9: basic fetch (cache miss because not stored before) 210 | --- config 211 | location /foo { 212 | default_type text/css; 213 | srcache_fetch GET /memc $uri; 214 | srcache_store PUT /memc $uri; 215 | 216 | echo world; 217 | } 218 | 219 | location /memc { 220 | internal; 221 | 222 | set $memc_key $query_string; 223 | set $memc_exptime 300; 224 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 225 | } 226 | --- request 227 | GET /foo 228 | --- response_headers 229 | Content-Type: text/css 230 | Content-Length: 231 | --- response_body 232 | world 233 | 234 | 235 | 236 | === TEST 10: flush all 237 | --- config 238 | location /flush { 239 | set $memc_cmd 'flush_all'; 240 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 241 | } 242 | --- response_headers 243 | Content-Type: text/plain 244 | Content-Length: 4 245 | --- request 246 | GET /flush 247 | --- response_body eval: "OK\r\n" 248 | 249 | 250 | 251 | === TEST 11: basic fetch (cache miss), and store due to Expires: , and srcache_response_cache_control off 252 | --- config 253 | location /foo { 254 | default_type text/css; 255 | srcache_fetch GET /memc $uri; 256 | srcache_store PUT /memc $uri; 257 | srcache_response_cache_control off; 258 | 259 | content_by_lua ' 260 | ngx.header.expires = ngx.http_time(ngx.time()) 261 | ngx.say("hello") 262 | '; 263 | } 264 | 265 | location /memc { 266 | internal; 267 | 268 | set $memc_key $query_string; 269 | set $memc_exptime 300; 270 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 271 | } 272 | --- request 273 | GET /foo 274 | --- response_headers 275 | Content-Type: text/css 276 | Content-Length: 277 | --- response_body 278 | hello 279 | 280 | 281 | 282 | === TEST 12: basic fetch (cache hit) 283 | --- config 284 | location /foo { 285 | default_type text/css; 286 | srcache_fetch GET /memc $uri; 287 | srcache_store PUT /memc $uri; 288 | 289 | echo world; 290 | } 291 | 292 | location /memc { 293 | internal; 294 | 295 | set $memc_key $query_string; 296 | set $memc_exptime 300; 297 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 298 | } 299 | --- request 300 | GET /foo 301 | --- response_headers 302 | Content-Type: text/css 303 | Content-Length: 6 304 | --- response_body 305 | hello 306 | 307 | 308 | 309 | === TEST 13: flush all 310 | --- config 311 | location /flush { 312 | set $memc_cmd 'flush_all'; 313 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 314 | } 315 | --- response_headers 316 | Content-Type: text/plain 317 | Content-Length: 4 318 | --- request 319 | GET /flush 320 | --- response_body eval: "OK\r\n" 321 | 322 | 323 | 324 | === TEST 14: basic fetch (cache miss), and store due to not expired expires 325 | --- config 326 | location /foo { 327 | default_type text/css; 328 | srcache_fetch GET /memc $uri; 329 | srcache_store PUT /memc $uri; 330 | 331 | content_by_lua ' 332 | ngx.header.expires = ngx.http_time(ngx.time() + 10) 333 | ngx.say("hello") 334 | '; 335 | } 336 | 337 | location /memc { 338 | internal; 339 | 340 | set $memc_key $query_string; 341 | set $memc_exptime 300; 342 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 343 | } 344 | --- request 345 | GET /foo 346 | --- response_headers 347 | Content-Type: text/css 348 | Content-Length: 349 | --- response_body 350 | hello 351 | 352 | 353 | 354 | === TEST 15: basic fetch (cache hit) 355 | --- config 356 | location /foo { 357 | default_type text/css; 358 | srcache_fetch GET /memc $uri; 359 | srcache_store PUT /memc $uri; 360 | 361 | echo world; 362 | } 363 | 364 | location /memc { 365 | internal; 366 | 367 | set $memc_key $query_string; 368 | set $memc_exptime 300; 369 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 370 | } 371 | --- request 372 | GET /foo 373 | --- response_headers 374 | Content-Type: text/css 375 | Content-Length: 6 376 | --- response_body 377 | hello 378 | -------------------------------------------------------------------------------- /t/fetch-header.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (5 * blocks() + 1); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | add_header X-Fetch-Status $srcache_fetch_status; 25 | } 26 | --- response_headers 27 | Content-Type: text/plain 28 | Content-Length: 4 29 | X-Fetch-Status: BYPASS 30 | --- request 31 | GET /flush 32 | --- response_body eval: "OK\r\n" 33 | 34 | 35 | 36 | === TEST 2: set key in memcached (good content) 37 | --- config 38 | location /memc { 39 | set $memc_key '/foo'; 40 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 41 | } 42 | --- response_headers 43 | Content-Type: text/plain 44 | Content-Length: 8 45 | !X-Fetch-Status 46 | --- request eval 47 | "PUT /memc 48 | HTTP/1.1 200 OK\r 49 | Content-Type: foo/bar\r 50 | Foo: Bar\r 51 | \r 52 | hello 53 | " 54 | --- response_body eval: "STORED\r\n" 55 | --- error_code: 201 56 | 57 | 58 | 59 | === TEST 3: basic fetch (cache hit) 60 | --- config 61 | location /foo { 62 | default_type text/css; 63 | srcache_fetch GET /memc $uri; 64 | srcache_store PUT /memc $uri; 65 | 66 | echo world; 67 | add_header X-Fetch-Status $srcache_fetch_status; 68 | } 69 | 70 | location /memc { 71 | internal; 72 | 73 | set $memc_key $query_string; 74 | set $memc_exptime 300; 75 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 76 | } 77 | --- request 78 | GET /foo 79 | --- response_headers 80 | Content-Type: foo/bar 81 | Content-Length: 5 82 | Foo: Bar 83 | X-Fetch-Status: HIT 84 | --- response_body chop 85 | hello 86 | 87 | 88 | 89 | === TEST 4: set key in memcached (bad content, syntax error in status line) 90 | --- config 91 | location /memc { 92 | set $memc_key '/foo'; 93 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 94 | } 95 | --- response_headers 96 | Content-Type: text/plain 97 | Content-Length: 8 98 | !X-Fetch-Status 99 | --- request eval 100 | "PUT /memc 101 | HTTP 200 OK\r 102 | Content-Type: foo/bar\r 103 | Foo: Bar\r 104 | \r 105 | hello 106 | " 107 | --- response_body eval: "STORED\r\n" 108 | --- error_code: 201 109 | 110 | 111 | 112 | === TEST 5: basic fetch (cache hit, but found syntax error in status line) 113 | --- config 114 | location /foo { 115 | default_type text/css; 116 | srcache_fetch GET /memc $uri; 117 | srcache_store PUT /memc $uri; 118 | 119 | echo world; 120 | add_header X-Fetch-Status $srcache_fetch_status; 121 | } 122 | 123 | location /memc { 124 | internal; 125 | 126 | set $memc_key $query_string; 127 | set $memc_exptime 300; 128 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 129 | } 130 | --- request 131 | GET /foo 132 | --- response_headers 133 | Content-Type: text/css 134 | Content-Length: 135 | X-Fetch-Status: MISS 136 | --- response_body 137 | world 138 | 139 | 140 | 141 | === TEST 6: set key in memcached (bad content, unexpected eof in status line) 142 | --- config 143 | location /memc { 144 | set $memc_key '/foo'; 145 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 146 | } 147 | --- response_headers 148 | Content-Type: text/plain 149 | Content-Length: 8 150 | !X-Fetch-Status 151 | --- request eval 152 | "PUT /memc 153 | HTTP/1.1 200" 154 | --- response_body eval: "STORED\r\n" 155 | --- error_code: 201 156 | 157 | 158 | 159 | === TEST 7: basic fetch (cache hit, but found unexpected eof in status line) 160 | --- config 161 | location /foo { 162 | default_type text/css; 163 | srcache_fetch GET /memc $uri; 164 | srcache_store PUT /memc $uri; 165 | 166 | echo world; 167 | add_header X-Fetch-Status $srcache_fetch_status; 168 | } 169 | 170 | location /memc { 171 | internal; 172 | 173 | set $memc_key $query_string; 174 | set $memc_exptime 300; 175 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 176 | } 177 | --- request 178 | GET /foo 179 | --- response_headers 180 | Content-Type: text/css 181 | Content-Length: 182 | X-Fetch-Status: MISS 183 | --- response_body 184 | world 185 | 186 | 187 | 188 | === TEST 8: set key in memcached (bad content, unexpected eof in header) 189 | --- config 190 | location /memc { 191 | set $memc_key '/foo'; 192 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 193 | } 194 | --- response_headers 195 | Content-Type: text/plain 196 | Content-Length: 8 197 | !X-Fetch-Status 198 | --- request eval 199 | "PUT /memc 200 | HTTP/1.1 200 OK\r 201 | Content-Ty" 202 | --- response_body eval: "STORED\r\n" 203 | --- error_code: 201 204 | 205 | 206 | 207 | === TEST 9: basic fetch (cache hit, but found unexpected eof in status line) 208 | --- config 209 | location /foo { 210 | default_type text/css; 211 | srcache_fetch GET /memc $uri; 212 | srcache_store PUT /memc $uri; 213 | add_header X-Fetch-Status $srcache_fetch_status; 214 | 215 | echo world; 216 | } 217 | 218 | location /memc { 219 | internal; 220 | 221 | set $memc_key $query_string; 222 | set $memc_exptime 300; 223 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 224 | } 225 | --- request 226 | GET /foo 227 | --- response_headers 228 | Content-Type: text/css 229 | Content-Length: 230 | X-Fetch-Status: MISS 231 | --- response_body 232 | world 233 | -------------------------------------------------------------------------------- /t/gzip.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 4 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss), and not stored due to Content-Encoding 35 | --- config 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | content_by_lua ' 42 | ngx.header.content_encoding = "gzip" 43 | ngx.say("hello") 44 | '; 45 | } 46 | 47 | location /memc { 48 | internal; 49 | 50 | set $memc_key $query_string; 51 | set $memc_exptime 300; 52 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 53 | } 54 | --- request 55 | GET /foo 56 | --- response_headers 57 | Content-Length: 58 | Content-Encoding: gzip 59 | --- response_body 60 | hello 61 | 62 | 63 | 64 | === TEST 3: basic fetch (cache miss again, not stored in the previous case) 65 | --- config 66 | location /foo { 67 | default_type text/css; 68 | srcache_fetch GET /memc $uri; 69 | srcache_store PUT /memc $uri; 70 | 71 | echo world; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /foo 83 | --- response_headers 84 | Content-Type: text/css 85 | Content-Length: 86 | --- response_body 87 | world 88 | 89 | 90 | 91 | === TEST 4: flush all 92 | --- config 93 | location /flush { 94 | set $memc_cmd 'flush_all'; 95 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 96 | } 97 | --- response_headers 98 | Content-Type: text/plain 99 | Content-Length: 4 100 | --- request 101 | GET /flush 102 | --- response_body eval: "OK\r\n" 103 | 104 | 105 | 106 | === TEST 5: basic fetch (cache miss), and stored due to Content-Encoding + srcache_ignore_content_encoding 107 | --- config 108 | location /foo { 109 | default_type text/css; 110 | srcache_fetch GET /memc $uri; 111 | srcache_store PUT /memc $uri; 112 | srcache_ignore_content_encoding on; 113 | 114 | content_by_lua ' 115 | ngx.header.content_encoding = "gzip" 116 | ngx.say("hello") 117 | '; 118 | } 119 | 120 | location /memc { 121 | internal; 122 | 123 | set $memc_key $query_string; 124 | set $memc_exptime 300; 125 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 126 | } 127 | --- request 128 | GET /foo 129 | --- response_headers 130 | Content-Length: 131 | Content-Encoding: gzip 132 | --- response_body 133 | hello 134 | 135 | 136 | 137 | === TEST 6: basic fetch (cache miss again, not stored in the previous case) 138 | --- config 139 | location /foo { 140 | default_type text/css; 141 | srcache_fetch GET /memc $uri; 142 | srcache_store PUT /memc $uri; 143 | 144 | echo world; 145 | } 146 | 147 | location /memc { 148 | internal; 149 | 150 | set $memc_key $query_string; 151 | set $memc_exptime 300; 152 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 153 | } 154 | --- request 155 | GET /foo 156 | --- response_headers 157 | Content-Type: text/css 158 | Content-Length: 6 159 | --- response_body 160 | hello 161 | -------------------------------------------------------------------------------- /t/header-buf-size.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (4 * blocks()); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: set key in memcached (header buf overflown) 35 | --- config 36 | location /memc { 37 | set $memc_key '/foo'; 38 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 39 | } 40 | --- response_headers 41 | Content-Type: text/plain 42 | Content-Length: 8 43 | --- request eval 44 | "PUT /memc 45 | HTTP/1.1 200 OK\r 46 | Foo: Bar\r 47 | Content-Type: foo/bar\r 48 | \r 49 | hello 50 | " 51 | --- response_body eval: "STORED\r\n" 52 | --- error_code: 201 53 | 54 | 55 | 56 | === TEST 3: basic fetch (cache hit, but header buf overflown) 57 | --- config 58 | location /foo { 59 | default_type text/css; 60 | srcache_fetch GET /memc $uri; 61 | srcache_store PUT /memc $uri; 62 | srcache_header_buffer_size 22; 63 | 64 | echo world; 65 | } 66 | 67 | location /memc { 68 | internal; 69 | 70 | set $memc_key $query_string; 71 | set $memc_exptime 300; 72 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 73 | } 74 | --- request 75 | GET /foo 76 | --- response_headers 77 | Content-Type: text/css 78 | Content-Length: 79 | --- response_body 80 | world 81 | 82 | 83 | 84 | === TEST 4: flush all 85 | --- config 86 | location /flush { 87 | set $memc_cmd 'flush_all'; 88 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 89 | } 90 | --- response_headers 91 | Content-Type: text/plain 92 | Content-Length: 4 93 | --- request 94 | GET /flush 95 | --- response_body eval: "OK\r\n" 96 | 97 | 98 | 99 | === TEST 5: set key in memcached 100 | --- config 101 | location /memc { 102 | set $memc_key '/foo'; 103 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 104 | } 105 | --- response_headers 106 | Content-Type: text/plain 107 | Content-Length: 8 108 | --- request eval 109 | "PUT /memc 110 | HTTP/1.1 200 OK\r 111 | Foo: Bar\r 112 | Content-Type: foo/bar\r 113 | \r 114 | hello 115 | " 116 | --- response_body eval: "STORED\r\n" 117 | --- error_code: 201 118 | 119 | 120 | 121 | === TEST 6: basic fetch (cache hit, just enough big header buffer) 122 | --- config 123 | location /foo { 124 | default_type text/css; 125 | srcache_fetch GET /memc $uri; 126 | srcache_store PUT /memc $uri; 127 | srcache_header_buffer_size 23; 128 | 129 | echo world; 130 | } 131 | 132 | location /memc { 133 | internal; 134 | 135 | set $memc_key $query_string; 136 | set $memc_exptime 300; 137 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 138 | } 139 | --- request 140 | GET /foo 141 | --- response_headers 142 | Content-Type: foo/bar 143 | Content-Length: 5 144 | --- response_body chomp 145 | hello 146 | -------------------------------------------------------------------------------- /t/main-req.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (5 * blocks() + 5); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all (not using ngx_srcache) 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | add_header X-Fetch-Status $srcache_fetch_status; 25 | add_header X-Store-Status $srcache_store_status; 26 | } 27 | --- response_headers 28 | Content-Type: text/plain 29 | Content-Length: 4 30 | X-Fetch-Status: BYPASS 31 | X-Store-Status: BYPASS 32 | 33 | --- stap 34 | F(ngx_http_srcache_access_handler) { 35 | println("srcache access handler") 36 | } 37 | 38 | F(ngx_http_srcache_header_filter) { 39 | println("srcache header filter") 40 | } 41 | 42 | F(ngx_http_srcache_body_filter) { 43 | println("srcache body filter") 44 | } 45 | 46 | --- stap_out 47 | --- request 48 | GET /flush 49 | --- response_body eval: "OK\r\n" 50 | 51 | 52 | 53 | === TEST 2: basic fetch (cache miss) 54 | --- config 55 | location /foo { 56 | default_type text/css; 57 | srcache_fetch GET /memc $uri; 58 | srcache_store PUT /memc $uri; 59 | 60 | echo hello; 61 | add_header X-Fetch-Status $srcache_fetch_status; 62 | add_header X-Store-Status $srcache_store_status; 63 | } 64 | 65 | location /memc { 66 | internal; 67 | 68 | set $memc_key $query_string; 69 | set $memc_exptime 300; 70 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 71 | } 72 | --- request 73 | GET /foo 74 | --- response_headers 75 | Content-Type: text/css 76 | Content-Length: 77 | X-Fetch-Status: MISS 78 | X-Store-Status: STORE 79 | --- response_body 80 | hello 81 | --- wait: 0.1 82 | --- error_log 83 | srcache_store: subrequest returned status 201 84 | --- log_level: debug 85 | 86 | 87 | 88 | === TEST 3: basic fetch (cache hit) 89 | --- config 90 | location /foo { 91 | default_type text/css; 92 | srcache_fetch GET /memc $uri; 93 | srcache_store PUT /memc $uri; 94 | 95 | echo world; 96 | add_header X-Fetch-Status $srcache_fetch_status; 97 | add_header X-Store-Status $srcache_store_status; 98 | } 99 | 100 | location /memc { 101 | internal; 102 | 103 | set $memc_key $query_string; 104 | set $memc_exptime 300; 105 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 106 | } 107 | --- request 108 | GET /foo 109 | --- response_headers 110 | Content-Type: text/css 111 | Content-Length: 6 112 | X-Fetch-Status: HIT 113 | X-Store-Status: BYPASS 114 | --- response_body 115 | hello 116 | 117 | 118 | 119 | === TEST 4: rewrite directives run before srcache directives 120 | --- config 121 | location /foo { 122 | default_type text/css; 123 | set $key $uri; 124 | set $loc /memc; 125 | srcache_fetch GET $loc $key; 126 | srcache_store PUT $loc $key; 127 | 128 | echo world; 129 | add_header X-Fetch-Status $srcache_fetch_status; 130 | } 131 | 132 | location /memc { 133 | internal; 134 | 135 | set $memc_key $query_string; 136 | set $memc_exptime 300; 137 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 138 | } 139 | --- request 140 | GET /foo 141 | --- response_headers 142 | Content-Type: text/css 143 | Content-Length: 6 144 | X-Fetch-Status: HIT 145 | --- response_body 146 | hello 147 | -------------------------------------------------------------------------------- /t/max-age.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 4 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss), and no store due to max-age=0 35 | --- config 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | content_by_lua ' 42 | ngx.header.cache_control = "public; max-age=0" 43 | ngx.say("hello") 44 | '; 45 | } 46 | 47 | location /memc { 48 | internal; 49 | 50 | set $memc_key $query_string; 51 | set $memc_exptime 300; 52 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 53 | } 54 | --- request 55 | GET /foo 56 | --- response_headers 57 | Content-Type: text/css 58 | Content-Length: 59 | --- response_body 60 | hello 61 | 62 | 63 | 64 | === TEST 3: basic fetch (cache miss because not stored before) 65 | --- config 66 | location /foo { 67 | default_type text/css; 68 | srcache_fetch GET /memc $uri; 69 | srcache_store PUT /memc $uri; 70 | 71 | echo world; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /foo 83 | --- response_headers 84 | Content-Type: text/css 85 | Content-Length: 86 | --- response_body 87 | world 88 | 89 | 90 | 91 | === TEST 4: flush all 92 | --- config 93 | location /flush { 94 | set $memc_cmd 'flush_all'; 95 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 96 | } 97 | --- response_headers 98 | Content-Type: text/plain 99 | Content-Length: 4 100 | --- request 101 | GET /flush 102 | --- response_body eval: "OK\r\n" 103 | 104 | 105 | 106 | === TEST 5: basic fetch (cache miss), and no store due to max-age=0, and srcache_response_cache_control on 107 | --- config 108 | location /foo { 109 | default_type text/css; 110 | srcache_fetch GET /memc $uri; 111 | srcache_store PUT /memc $uri; 112 | srcache_response_cache_control on; 113 | 114 | content_by_lua ' 115 | ngx.header["Cache-Control"] = "public; max-age=0" 116 | ngx.say("hello") 117 | '; 118 | } 119 | 120 | location /memc { 121 | internal; 122 | 123 | set $memc_key $query_string; 124 | set $memc_exptime 300; 125 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 126 | } 127 | --- request 128 | GET /foo 129 | --- response_headers 130 | Content-Type: text/css 131 | Content-Length: 132 | --- response_body 133 | hello 134 | 135 | 136 | 137 | === TEST 6: basic fetch (cache miss because not stored before) 138 | --- config 139 | location /foo { 140 | default_type text/css; 141 | srcache_fetch GET /memc $uri; 142 | srcache_store PUT /memc $uri; 143 | 144 | echo world; 145 | } 146 | 147 | location /memc { 148 | internal; 149 | 150 | set $memc_key $query_string; 151 | set $memc_exptime 300; 152 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 153 | } 154 | --- request 155 | GET /foo 156 | --- response_headers 157 | Content-Type: text/css 158 | Content-Length: 159 | --- response_body 160 | world 161 | 162 | 163 | 164 | === TEST 7: flush all 165 | --- config 166 | location /flush { 167 | set $memc_cmd 'flush_all'; 168 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 169 | } 170 | --- response_headers 171 | Content-Type: text/plain 172 | Content-Length: 4 173 | --- request 174 | GET /flush 175 | --- response_body eval: "OK\r\n" 176 | 177 | 178 | 179 | === TEST 8: basic fetch (cache miss), and store due to max-age=0, and srcache_response_cache_control off 180 | --- config 181 | location /foo { 182 | default_type text/css; 183 | srcache_fetch GET /memc $uri; 184 | srcache_store PUT /memc $uri; 185 | srcache_response_cache_control off; 186 | 187 | content_by_lua ' 188 | ngx.header["Cache-Control"] = "public; max-age=0" 189 | ngx.say("hello") 190 | '; 191 | } 192 | 193 | location /memc { 194 | internal; 195 | 196 | set $memc_key $query_string; 197 | set $memc_exptime 300; 198 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 199 | } 200 | --- request 201 | GET /foo 202 | --- response_headers 203 | Content-Type: text/css 204 | Content-Length: 205 | --- response_body 206 | hello 207 | 208 | 209 | 210 | === TEST 9: basic fetch (cache miss because not stored before) 211 | --- config 212 | location /foo { 213 | default_type text/css; 214 | srcache_fetch GET /memc $uri; 215 | srcache_store PUT /memc $uri; 216 | 217 | echo world; 218 | } 219 | 220 | location /memc { 221 | internal; 222 | 223 | set $memc_key $query_string; 224 | set $memc_exptime 300; 225 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 226 | } 227 | --- request 228 | GET /foo 229 | --- response_headers 230 | Content-Type: text/css 231 | Content-Length: 6 232 | --- response_body 233 | hello 234 | 235 | 236 | 237 | === TEST 10: flush all 238 | --- config 239 | location /flush { 240 | set $memc_cmd 'flush_all'; 241 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 242 | } 243 | --- response_headers 244 | Content-Type: text/plain 245 | Content-Length: 4 246 | --- request 247 | GET /flush 248 | --- response_body eval: "OK\r\n" 249 | 250 | 251 | 252 | === TEST 11: basic fetch (cache miss), and store due to max-age= 253 | --- config 254 | location /foo { 255 | default_type text/css; 256 | srcache_fetch GET /memc $uri; 257 | srcache_store PUT /memc $uri; 258 | 259 | content_by_lua ' 260 | ngx.header["Cache-Control"] = "public; max-age=7"; 261 | ngx.say("hello") 262 | '; 263 | } 264 | 265 | location /memc { 266 | internal; 267 | 268 | set $memc_key $query_string; 269 | set $memc_exptime 300; 270 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 271 | } 272 | --- request 273 | GET /foo 274 | --- response_headers 275 | Content-Type: text/css 276 | Content-Length: 277 | --- response_body 278 | hello 279 | 280 | 281 | 282 | === TEST 12: basic fetch (cache miss because not stored before) 283 | --- config 284 | location /foo { 285 | default_type text/css; 286 | srcache_fetch GET /memc $uri; 287 | srcache_store PUT /memc $uri; 288 | 289 | echo world; 290 | } 291 | 292 | location /memc { 293 | internal; 294 | 295 | set $memc_key $query_string; 296 | set $memc_exptime 300; 297 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 298 | } 299 | --- request 300 | GET /foo 301 | --- response_headers 302 | Content-Type: text/css 303 | Content-Length: 6 304 | --- response_body 305 | hello 306 | -------------------------------------------------------------------------------- /t/methods.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (4 * blocks() + 3); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss) 35 | --- config 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | echo hello; 42 | } 43 | 44 | location /memc { 45 | internal; 46 | 47 | set $memc_key $query_string; 48 | set $memc_exptime 300; 49 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 50 | } 51 | --- request 52 | GET /foo 53 | --- response_headers 54 | Content-Type: text/css 55 | Content-Length: 56 | --- response_body 57 | hello 58 | 59 | 60 | 61 | === TEST 3: basic fetch (cache hit) 62 | --- config 63 | location /foo { 64 | default_type text/css; 65 | srcache_fetch GET /memc $uri; 66 | srcache_store PUT /memc $uri; 67 | 68 | proxy_pass http://agentzh.org:12345/; 69 | } 70 | 71 | location /memc { 72 | internal; 73 | 74 | set $memc_key $query_string; 75 | set $memc_exptime 300; 76 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 77 | } 78 | --- request 79 | GET /foo 80 | --- response_headers 81 | Content-Type: text/css 82 | Content-Length: 6 83 | --- response_body 84 | hello 85 | 86 | 87 | 88 | === TEST 4: basic fetch (POST cache miss for POST by default) 89 | --- config 90 | location /foo { 91 | default_type text/css; 92 | srcache_fetch GET /memc $uri; 93 | srcache_store PUT /memc $uri; 94 | 95 | echo world; 96 | } 97 | 98 | location /memc { 99 | internal; 100 | 101 | set $memc_key $query_string; 102 | set $memc_exptime 300; 103 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 104 | } 105 | --- request 106 | POST /foo 107 | hiya, china 108 | --- response_headers 109 | Content-Type: text/css 110 | ! Content-Length 111 | --- response_body 112 | world 113 | 114 | 115 | 116 | === TEST 5: basic fetch (POST cache hit if we enable POST explicitly) 117 | --- config 118 | location /foo { 119 | default_type text/css; 120 | srcache_fetch GET /memc $uri; 121 | srcache_store PUT /memc $uri; 122 | srcache_methods POST; 123 | 124 | proxy_pass http://agentzh.org:12345/; 125 | } 126 | 127 | location /memc { 128 | internal; 129 | 130 | set $memc_key $query_string; 131 | set $memc_exptime 300; 132 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 133 | } 134 | --- request 135 | POST /foo 136 | hiya, china 137 | --- response_headers 138 | Content-Type: text/css 139 | Content-Length: 6 140 | --- response_body 141 | hello 142 | 143 | 144 | 145 | === TEST 6: basic fetch (GET still cache hit if we enable POST and PUT explicitly) 146 | --- config 147 | location /foo { 148 | default_type text/css; 149 | srcache_fetch GET /memc $uri; 150 | srcache_store PUT /memc $uri; 151 | srcache_methods POST PUT; 152 | 153 | proxy_pass http://agentzh.org:12345/; 154 | } 155 | 156 | location /memc { 157 | internal; 158 | 159 | set $memc_key $query_string; 160 | set $memc_exptime 300; 161 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 162 | } 163 | --- request 164 | GET /foo 165 | --- response_headers 166 | Content-Type: text/css 167 | Content-Length: 6 168 | --- response_body 169 | hello 170 | 171 | 172 | 173 | === TEST 7: basic fetch (HEAD still cache hit if we enable POST explicitly) 174 | --- config 175 | location /foo { 176 | default_type text/css; 177 | srcache_fetch GET /memc $uri; 178 | srcache_store PUT /memc $uri; 179 | srcache_methods POST; 180 | 181 | proxy_pass http://agentzh.org:12345/; 182 | } 183 | 184 | location /memc { 185 | internal; 186 | 187 | set $memc_key $query_string; 188 | set $memc_exptime 300; 189 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 190 | } 191 | --- request 192 | HEAD /foo 193 | --- response_headers 194 | Content-Type: text/css 195 | Content-Length: 6 196 | --- response_body 197 | --- no_error_log 198 | [error] 199 | 200 | 201 | 202 | === TEST 8: flush all 203 | --- config 204 | location /flush { 205 | set $memc_cmd 'flush_all'; 206 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 207 | } 208 | --- response_headers 209 | Content-Type: text/plain 210 | Content-Length: 4 211 | --- request 212 | GET /flush 213 | --- response_body eval: "OK\r\n" 214 | 215 | 216 | 217 | === TEST 9: basic fetch (cache miss), POST not store 218 | --- config 219 | location /foo { 220 | default_type text/css; 221 | srcache_fetch GET /memc $uri; 222 | srcache_store PUT /memc $uri; 223 | 224 | echo hello; 225 | } 226 | 227 | location /memc { 228 | internal; 229 | 230 | set $memc_key $query_string; 231 | set $memc_exptime 300; 232 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 233 | } 234 | --- request 235 | POST /foo 236 | howdy 237 | --- response_headers 238 | Content-Type: text/css 239 | Content-Length: 240 | --- response_body 241 | hello 242 | 243 | 244 | 245 | === TEST 10: basic fetch (cache miss) 246 | --- config 247 | location /foo { 248 | default_type text/css; 249 | srcache_fetch GET /memc $uri; 250 | srcache_store PUT /memc $uri; 251 | 252 | echo world; 253 | } 254 | 255 | location /memc { 256 | internal; 257 | 258 | set $memc_key $query_string; 259 | set $memc_exptime 300; 260 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 261 | } 262 | --- request 263 | GET /foo 264 | --- response_headers 265 | Content-Type: text/css 266 | Content-Length: 267 | --- response_body 268 | world 269 | 270 | 271 | 272 | === TEST 11: flush all 273 | --- config 274 | location /flush { 275 | set $memc_cmd 'flush_all'; 276 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 277 | } 278 | --- response_headers 279 | Content-Type: text/plain 280 | Content-Length: 4 281 | --- request 282 | GET /flush 283 | --- response_body eval: "OK\r\n" 284 | 285 | 286 | 287 | === TEST 12: basic fetch (cache miss), POST stored when POST is enabled in srcache_methods 288 | --- config 289 | location /foo { 290 | default_type text/css; 291 | srcache_fetch GET /memc $uri; 292 | srcache_store PUT /memc $uri; 293 | srcache_methods POST; 294 | 295 | echo hello; 296 | } 297 | 298 | location /memc { 299 | internal; 300 | 301 | set $memc_key $query_string; 302 | set $memc_exptime 300; 303 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 304 | } 305 | --- request 306 | POST /foo 307 | howdy 308 | --- response_headers 309 | Content-Type: text/css 310 | Content-Length: 311 | --- response_body 312 | hello 313 | 314 | 315 | 316 | === TEST 13: basic fetch (cache hit) 317 | --- config 318 | location /foo { 319 | default_type text/css; 320 | srcache_fetch GET /memc $uri; 321 | srcache_store PUT /memc $uri; 322 | 323 | proxy_pass http://agentzh.org:12345/; 324 | } 325 | 326 | location /memc { 327 | internal; 328 | 329 | set $memc_key $query_string; 330 | set $memc_exptime 300; 331 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 332 | } 333 | --- request 334 | GET /foo 335 | --- response_headers 336 | Content-Type: text/css 337 | Content-Length: 6 338 | --- response_body 339 | hello 340 | 341 | 342 | 343 | === TEST 14: flush all 344 | --- config 345 | location /flush { 346 | set $memc_cmd 'flush_all'; 347 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 348 | } 349 | --- response_headers 350 | Content-Type: text/plain 351 | Content-Length: 4 352 | --- request 353 | GET /flush 354 | --- response_body eval: "OK\r\n" 355 | 356 | 357 | 358 | === TEST 15: basic fetch (cache miss) - HEAD 359 | --- config 360 | location /foo { 361 | srcache_fetch GET /memc $uri; 362 | srcache_store PUT /memc $uri; 363 | 364 | proxy_pass http://127.0.0.1:$server_port/back; 365 | } 366 | 367 | location /back { 368 | return 200 "hello world"; 369 | } 370 | 371 | location /memc { 372 | internal; 373 | 374 | set $memc_key $query_string; 375 | set $memc_exptime 300; 376 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 377 | } 378 | --- request 379 | HEAD /foo 380 | --- response_headers 381 | Content-Type: text/plain 382 | Content-Length: 11 383 | --- response_body 384 | --- error_log 385 | srcache_store skipped due to request method HEAD 386 | --- no_error_log 387 | [error] 388 | --- log_level: debug 389 | --- wait: 0.1 390 | -------------------------------------------------------------------------------- /t/no-cache.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 4 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss), and not stored due to Cache-Control: no-cache 35 | --- config 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | content_by_lua ' 42 | ngx.header.cache_control = "no-cache" 43 | ngx.say("hello") 44 | '; 45 | } 46 | 47 | location /memc { 48 | internal; 49 | 50 | set $memc_key $query_string; 51 | set $memc_exptime 300; 52 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 53 | } 54 | --- request 55 | GET /foo 56 | --- response_headers 57 | Content-Type: text/css 58 | Content-Length: 59 | --- response_body 60 | hello 61 | 62 | 63 | 64 | === TEST 3: basic fetch (cache miss again, not stored in the previous case) 65 | --- config 66 | location /foo { 67 | default_type text/css; 68 | srcache_fetch GET /memc $uri; 69 | srcache_store PUT /memc $uri; 70 | 71 | echo world; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /foo 83 | --- response_headers 84 | Content-Type: text/css 85 | Content-Length: 86 | --- response_body 87 | world 88 | 89 | 90 | 91 | === TEST 4: flush all 92 | --- config 93 | location /flush { 94 | set $memc_cmd 'flush_all'; 95 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 96 | } 97 | --- response_headers 98 | Content-Type: text/plain 99 | Content-Length: 4 100 | --- request 101 | GET /flush 102 | --- response_body eval: "OK\r\n" 103 | 104 | 105 | 106 | === TEST 5: basic fetch (cache miss), and not stored due to Cache-Control: no-cache (srcache_store_no_cache off) 107 | --- config 108 | location /foo { 109 | default_type text/css; 110 | srcache_fetch GET /memc $uri; 111 | srcache_store PUT /memc $uri; 112 | srcache_store_no_cache off; 113 | srcache_store_no_store on; 114 | srcache_store_private on; 115 | 116 | content_by_lua ' 117 | ngx.header.cache_control = "no-cache" 118 | ngx.say("hello") 119 | '; 120 | } 121 | 122 | location /memc { 123 | internal; 124 | 125 | set $memc_key $query_string; 126 | set $memc_exptime 300; 127 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 128 | } 129 | --- request 130 | GET /foo 131 | --- response_headers 132 | Content-Type: text/css 133 | Content-Length: 134 | --- response_body 135 | hello 136 | 137 | 138 | 139 | === TEST 6: basic fetch (cache miss again, not stored in the previous case) 140 | --- config 141 | location /foo { 142 | default_type text/css; 143 | srcache_fetch GET /memc $uri; 144 | srcache_store PUT /memc $uri; 145 | 146 | echo world; 147 | } 148 | 149 | location /memc { 150 | internal; 151 | 152 | set $memc_key $query_string; 153 | set $memc_exptime 300; 154 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 155 | } 156 | --- request 157 | GET /foo 158 | --- response_headers 159 | Content-Type: text/css 160 | Content-Length: 161 | --- response_body 162 | world 163 | 164 | 165 | 166 | === TEST 7: flush all 167 | --- config 168 | location /flush { 169 | set $memc_cmd 'flush_all'; 170 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 171 | } 172 | --- response_headers 173 | Content-Type: text/plain 174 | Content-Length: 4 175 | --- request 176 | GET /flush 177 | --- response_body eval: "OK\r\n" 178 | 179 | 180 | 181 | === TEST 8: basic fetch (cache miss), and stored due to srcache_store_no_cache on 182 | --- config 183 | location /foo { 184 | default_type text/css; 185 | srcache_fetch GET /memc $uri; 186 | srcache_store PUT /memc $uri; 187 | srcache_store_no_cache on; 188 | srcache_store_no_store off; 189 | srcache_store_private off; 190 | 191 | content_by_lua ' 192 | ngx.header.cache_control = "no-cache" 193 | ngx.say("hello") 194 | '; 195 | } 196 | 197 | location /memc { 198 | internal; 199 | 200 | set $memc_key $query_string; 201 | set $memc_exptime 300; 202 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 203 | } 204 | --- request 205 | GET /foo 206 | --- response_headers 207 | Content-Type: text/css 208 | Content-Length: 209 | --- response_body 210 | hello 211 | 212 | 213 | 214 | === TEST 9: basic fetch (cache miss again, not stored in the previous case) 215 | --- config 216 | location /foo { 217 | default_type text/css; 218 | srcache_fetch GET /memc $uri; 219 | srcache_store PUT /memc $uri; 220 | 221 | echo world; 222 | } 223 | 224 | location /memc { 225 | internal; 226 | 227 | set $memc_key $query_string; 228 | set $memc_exptime 300; 229 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 230 | } 231 | --- request 232 | GET /foo 233 | --- response_headers 234 | Content-Type: text/css 235 | Content-Length: 6 236 | --- response_body 237 | hello 238 | 239 | 240 | 241 | === TEST 10: flush all 242 | --- config 243 | location /flush { 244 | set $memc_cmd 'flush_all'; 245 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 246 | } 247 | --- response_headers 248 | Content-Type: text/plain 249 | Content-Length: 4 250 | --- request 251 | GET /flush 252 | --- response_body eval: "OK\r\n" 253 | 254 | 255 | 256 | === TEST 11: basic fetch (cache miss), and not stored due to Cache-Control: no-cache 257 | --- config 258 | location /foo { 259 | default_type text/css; 260 | srcache_fetch GET /memc $uri; 261 | srcache_store PUT /memc $uri; 262 | 263 | content_by_lua ' 264 | ngx.header.cache_control = { "blah", "blah; No-Cache" } 265 | ngx.say("hello") 266 | '; 267 | } 268 | 269 | location /memc { 270 | internal; 271 | 272 | set $memc_key $query_string; 273 | set $memc_exptime 300; 274 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 275 | } 276 | --- request 277 | GET /foo 278 | --- response_headers 279 | Content-Type: text/css 280 | Content-Length: 281 | --- response_body 282 | hello 283 | 284 | 285 | 286 | === TEST 12: basic fetch (cache miss again, not stored in the previous case) 287 | --- config 288 | location /foo { 289 | default_type text/css; 290 | srcache_fetch GET /memc $uri; 291 | srcache_store PUT /memc $uri; 292 | 293 | echo world; 294 | } 295 | 296 | location /memc { 297 | internal; 298 | 299 | set $memc_key $query_string; 300 | set $memc_exptime 300; 301 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 302 | } 303 | --- request 304 | GET /foo 305 | --- response_headers 306 | Content-Type: text/css 307 | Content-Length: 308 | --- response_body 309 | world 310 | -------------------------------------------------------------------------------- /t/no-store.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 4 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss), and not stored due to Cache-Control: no-store 35 | --- config 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | content_by_lua ' 42 | ngx.header.cache_control = "no-store" 43 | ngx.say("hello") 44 | '; 45 | } 46 | 47 | location /memc { 48 | internal; 49 | 50 | set $memc_key $query_string; 51 | set $memc_exptime 300; 52 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 53 | } 54 | --- request 55 | GET /foo 56 | --- response_headers 57 | Content-Type: text/css 58 | Content-Length: 59 | --- response_body 60 | hello 61 | 62 | 63 | 64 | === TEST 3: basic fetch (cache miss again, not stored in the previous case) 65 | --- config 66 | location /foo { 67 | default_type text/css; 68 | srcache_fetch GET /memc $uri; 69 | srcache_store PUT /memc $uri; 70 | 71 | echo world; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /foo 83 | --- response_headers 84 | Content-Type: text/css 85 | Content-Length: 86 | --- response_body 87 | world 88 | 89 | 90 | 91 | === TEST 4: flush all 92 | --- config 93 | location /flush { 94 | set $memc_cmd 'flush_all'; 95 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 96 | } 97 | --- response_headers 98 | Content-Type: text/plain 99 | Content-Length: 4 100 | --- request 101 | GET /flush 102 | --- response_body eval: "OK\r\n" 103 | 104 | 105 | 106 | === TEST 5: basic fetch (cache miss), and not stored due to Cache-Control: no-store (srcache_store_no_store off) 107 | --- config 108 | location /foo { 109 | default_type text/css; 110 | srcache_fetch GET /memc $uri; 111 | srcache_store PUT /memc $uri; 112 | srcache_store_no_store off; 113 | 114 | content_by_lua ' 115 | ngx.header.cache_control = "no-store" 116 | ngx.say("hello") 117 | '; 118 | } 119 | 120 | location /memc { 121 | internal; 122 | 123 | set $memc_key $query_string; 124 | set $memc_exptime 300; 125 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 126 | } 127 | --- request 128 | GET /foo 129 | --- response_headers 130 | Content-Type: text/css 131 | Content-Length: 132 | --- response_body 133 | hello 134 | 135 | 136 | 137 | === TEST 6: basic fetch (cache miss again, not stored in the previous case) 138 | --- config 139 | location /foo { 140 | default_type text/css; 141 | srcache_fetch GET /memc $uri; 142 | srcache_store PUT /memc $uri; 143 | 144 | echo world; 145 | } 146 | 147 | location /memc { 148 | internal; 149 | 150 | set $memc_key $query_string; 151 | set $memc_exptime 300; 152 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 153 | } 154 | --- request 155 | GET /foo 156 | --- response_headers 157 | Content-Type: text/css 158 | Content-Length: 159 | --- response_body 160 | world 161 | 162 | 163 | 164 | === TEST 7: flush all 165 | --- config 166 | location /flush { 167 | set $memc_cmd 'flush_all'; 168 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 169 | } 170 | --- response_headers 171 | Content-Type: text/plain 172 | Content-Length: 4 173 | --- request 174 | GET /flush 175 | --- response_body eval: "OK\r\n" 176 | 177 | 178 | 179 | === TEST 8: basic fetch (cache miss), and stored due to srcache_store_no_store on 180 | --- config 181 | location /foo { 182 | default_type text/css; 183 | srcache_fetch GET /memc $uri; 184 | srcache_store PUT /memc $uri; 185 | srcache_store_no_store on; 186 | 187 | content_by_lua ' 188 | ngx.header.cache_control = "no-store" 189 | ngx.say("hello") 190 | '; 191 | } 192 | 193 | location /memc { 194 | internal; 195 | 196 | set $memc_key $query_string; 197 | set $memc_exptime 300; 198 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 199 | } 200 | --- request 201 | GET /foo 202 | --- response_headers 203 | Content-Type: text/css 204 | Content-Length: 205 | --- response_body 206 | hello 207 | 208 | 209 | 210 | === TEST 9: basic fetch (cache miss again, not stored in the previous case) 211 | --- config 212 | location /foo { 213 | default_type text/css; 214 | srcache_fetch GET /memc $uri; 215 | srcache_store PUT /memc $uri; 216 | 217 | echo world; 218 | } 219 | 220 | location /memc { 221 | internal; 222 | 223 | set $memc_key $query_string; 224 | set $memc_exptime 300; 225 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 226 | } 227 | --- request 228 | GET /foo 229 | --- response_headers 230 | Content-Type: text/css 231 | Content-Length: 6 232 | --- response_body 233 | hello 234 | 235 | 236 | 237 | === TEST 10: flush all 238 | --- config 239 | location /flush { 240 | set $memc_cmd 'flush_all'; 241 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 242 | } 243 | --- response_headers 244 | Content-Type: text/plain 245 | Content-Length: 4 246 | --- request 247 | GET /flush 248 | --- response_body eval: "OK\r\n" 249 | 250 | 251 | 252 | === TEST 11: basic fetch (cache miss), and not stored due to Cache-Control: no-store 253 | --- config 254 | location /foo { 255 | default_type text/css; 256 | srcache_fetch GET /memc $uri; 257 | srcache_store PUT /memc $uri; 258 | 259 | content_by_lua ' 260 | ngx.header.cache_control = { "blah", "blah; No-Store" } 261 | ngx.say("hello") 262 | '; 263 | } 264 | 265 | location /memc { 266 | internal; 267 | 268 | set $memc_key $query_string; 269 | set $memc_exptime 300; 270 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 271 | } 272 | --- request 273 | GET /foo 274 | --- response_headers 275 | Content-Type: text/css 276 | Content-Length: 277 | --- response_body 278 | hello 279 | 280 | 281 | 282 | === TEST 12: basic fetch (cache miss again, not stored in the previous case) 283 | --- config 284 | location /foo { 285 | default_type text/css; 286 | srcache_fetch GET /memc $uri; 287 | srcache_store PUT /memc $uri; 288 | 289 | echo world; 290 | } 291 | 292 | location /memc { 293 | internal; 294 | 295 | set $memc_key $query_string; 296 | set $memc_exptime 300; 297 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 298 | } 299 | --- request 300 | GET /foo 301 | --- response_headers 302 | Content-Type: text/css 303 | Content-Length: 304 | --- response_body 305 | world 306 | -------------------------------------------------------------------------------- /t/postgres-main.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(100); 7 | 8 | plan tests => repeat_each() * 3 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | $ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432; 12 | 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | --- request 28 | GET /flush 29 | --- response_body eval: "OK\r\n" 30 | 31 | 32 | 33 | === TEST 2: cache miss 34 | --- http_config 35 | upstream backend { 36 | postgres_server 127.0.0.1:$TEST_NGINX_POSTGRESQL_PORT 37 | dbname=ngx_test user=ngx_test password=ngx_test; 38 | } 39 | --- config 40 | location /cats { 41 | srcache_fetch GET /memc $uri; 42 | srcache_store PUT /memc $uri; 43 | 44 | default_type text/css; 45 | 46 | postgres_pass backend; 47 | postgres_query 'select * from cats'; 48 | 49 | rds_json on; 50 | } 51 | 52 | location /memc { 53 | internal; 54 | 55 | set $memc_key $query_string; 56 | set $memc_exptime 300; 57 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 58 | } 59 | --- request 60 | GET /cats 61 | --- response_headers 62 | Content-Type: application/json 63 | --- response_body chomp 64 | [{"id":2,"name":null},{"id":3,"name":"bob"}] 65 | 66 | 67 | 68 | === TEST 3: cache hit 69 | --- http_config 70 | upstream backend { 71 | postgres_server 127.0.0.1:$TEST_NGINX_POSTGRESQL_PORT 72 | dbname=ngx_test user=ngx_test password=ngx_test; 73 | } 74 | --- config 75 | location /cats { 76 | srcache_fetch GET /memc $uri; 77 | srcache_store PUT /memc $uri; 78 | 79 | default_type text/css; 80 | 81 | postgres_pass backend; 82 | postgres_query 'invalid sql here'; 83 | 84 | rds_json on; 85 | } 86 | 87 | location /memc { 88 | internal; 89 | 90 | set $memc_key $query_string; 91 | set $memc_exptime 300; 92 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 93 | } 94 | --- request 95 | GET /cats 96 | --- response_headers 97 | Content-Type: application/json 98 | --- response_body chomp 99 | [{"id":2,"name":null},{"id":3,"name":"bob"}] 100 | 101 | 102 | 103 | === TEST 4: SSL packet issue (bug) 104 | --- SKIP 105 | --- http_config 106 | upstream backend { 107 | postgres_server 127.0.0.1:$TEST_NGINX_POSTGRESQL_PORT 108 | dbname=ngx_test user=ngx_test password=ngx_test; 109 | } 110 | --- config 111 | location /cats { 112 | srcache_fetch GET /memc $uri; 113 | srcache_store PUT /memc $uri; 114 | 115 | default_type application/json; 116 | 117 | postgres_escape $token $arg_token; 118 | postgres_escape $limit $arg_limit; 119 | postgres_pass backend; 120 | postgres_query HEAD GET "select $token,$limit"; 121 | 122 | rds_json on; 123 | } 124 | 125 | location /memc { 126 | internal; 127 | 128 | set $memc_key $query_string; 129 | set $memc_exptime 300; 130 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 131 | } 132 | --- request 133 | GET /cats?token=3&limit=10 134 | --- response_headers 135 | Content-Type: text/css 136 | --- response_body chomp 137 | [{"id":2,"name":null},{"id":3,"name":"bob"}] 138 | --- SKIP 139 | -------------------------------------------------------------------------------- /t/proxy.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(100); 7 | 8 | plan tests => repeat_each() * 3 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | $ENV{TEST_NGINX_MYSQL_PORT} ||= 3306; 12 | 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | --- request 28 | GET /flush 29 | --- response_body eval: "OK\r\n" 30 | 31 | 32 | 33 | === TEST 2: cache miss 34 | --- SKIP 35 | --- http_config 36 | upstream backend { 37 | drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql 38 | dbname=ngx_test user=ngx_test password=ngx_test; 39 | } 40 | --- config 41 | location /cats { 42 | srcache_fetch GET /memc $uri; 43 | srcache_store PUT /memc $uri; 44 | 45 | default_type text/css; 46 | 47 | proxy_pass http://127.0.0.1:$server_port/foo; 48 | } 49 | 50 | location /foo { 51 | echo foo; 52 | } 53 | 54 | location /memc { 55 | internal; 56 | 57 | set $memc_key $query_string; 58 | set $memc_exptime 300; 59 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 60 | } 61 | --- request 62 | GET /cats 63 | --- response_headers 64 | Content-Type: application/json 65 | --- response_body chomp 66 | -------------------------------------------------------------------------------- /t/ranges.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (5 * blocks() + 12); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | no_long_string(); 15 | 16 | run_tests(); 17 | 18 | __DATA__ 19 | 20 | === TEST 1: flush all (not using ngx_srcache) 21 | --- config 22 | location = /flush { 23 | set $memc_cmd 'flush_all'; 24 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 25 | add_header X-Fetch-Status $srcache_fetch_status; 26 | add_header X-Store-Status $srcache_store_status; 27 | } 28 | --- response_headers 29 | Content-Type: text/plain 30 | Content-Length: 4 31 | X-Fetch-Status: BYPASS 32 | X-Store-Status: BYPASS 33 | 34 | --- request 35 | GET /flush 36 | --- response_body eval: "OK\r\n" 37 | 38 | 39 | 40 | === TEST 2: range fetch (cache miss) 41 | --- config 42 | location = /index.html { 43 | default_type text/css; 44 | srcache_store_ranges on; 45 | srcache_fetch GET /memc $uri$http_range; 46 | srcache_store PUT /memc $uri$http_range; 47 | 48 | add_header X-Fetch-Status $srcache_fetch_status; 49 | add_header X-Store-Status $srcache_store_status; 50 | } 51 | 52 | location = /memc { 53 | internal; 54 | 55 | set $memc_key $query_string; 56 | set $memc_exptime 300; 57 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 58 | } 59 | --- request 60 | GET /index.html 61 | --- more_headers 62 | Range: bytes=2-5 63 | --- response_headers 64 | Content-Type: text/html 65 | Content-Length: 4 66 | X-Fetch-Status: MISS 67 | X-Store-Status: STORE 68 | --- response_body chop 69 | tml> 70 | --- wait: 0.1 71 | --- error_log 72 | srcache_store: subrequest returned status 201 73 | --- error_code: 206 74 | --- log_level: debug 75 | 76 | 77 | 78 | === TEST 3: range fetch (cache hit) 79 | --- config 80 | location = /index.html { 81 | default_type text/css; 82 | srcache_fetch GET /memc $uri$http_range; 83 | srcache_store PUT /memc $uri$http_range; 84 | 85 | echo world; 86 | add_header X-Fetch-Status $srcache_fetch_status; 87 | add_header X-Store-Status $srcache_store_status; 88 | } 89 | 90 | location = /memc { 91 | internal; 92 | 93 | set $memc_key $query_string; 94 | set $memc_exptime 300; 95 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 96 | } 97 | --- request 98 | GET /index.html 99 | --- more_headers 100 | Range: bytes=2-5 101 | 102 | --- response_headers 103 | X-Fetch-Status: HIT 104 | X-Store-Status: BYPASS 105 | Content-Length: 4 106 | --- response_body chop 107 | tml> 108 | --- error_code: 206 109 | 110 | 111 | 112 | === TEST 4: full fetch (cache miss) 113 | --- config 114 | location = /index.html { 115 | default_type text/css; 116 | srcache_fetch GET /memc $uri$http_range; 117 | srcache_store PUT /memc $uri$http_range; 118 | 119 | add_header X-Fetch-Status $srcache_fetch_status; 120 | add_header X-Store-Status $srcache_store_status; 121 | } 122 | 123 | location = /memc { 124 | internal; 125 | 126 | set $memc_key $query_string; 127 | set $memc_exptime 300; 128 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 129 | } 130 | --- request 131 | GET /index.html 132 | 133 | --- response_headers 134 | X-Fetch-Status: MISS 135 | X-Store-Status: STORE 136 | Content-Length: 72 137 | --- response_body_like: It works! 138 | --- error_code: 200 139 | 140 | 141 | 142 | === TEST 5: flush all (not using ngx_srcache) 143 | --- config 144 | location = /flush { 145 | set $memc_cmd 'flush_all'; 146 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 147 | add_header X-Fetch-Status $srcache_fetch_status; 148 | add_header X-Store-Status $srcache_store_status; 149 | } 150 | --- response_headers 151 | Content-Type: text/plain 152 | Content-Length: 4 153 | X-Fetch-Status: BYPASS 154 | X-Store-Status: BYPASS 155 | 156 | --- request 157 | GET /flush 158 | --- response_body eval: "OK\r\n" 159 | 160 | 161 | 162 | === TEST 6: full fetch (cache miss) 163 | --- config 164 | location = /index.html { 165 | default_type text/css; 166 | srcache_fetch GET /memc $uri; 167 | srcache_store PUT /memc $uri; 168 | 169 | add_header X-Fetch-Status $srcache_fetch_status; 170 | add_header X-Store-Status $srcache_store_status; 171 | } 172 | 173 | location = /memc { 174 | internal; 175 | 176 | set $memc_key $query_string; 177 | set $memc_exptime 300; 178 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 179 | } 180 | --- request 181 | GET /index.html 182 | --- response_headers 183 | Content-Type: text/html 184 | Content-Length: 72 185 | X-Fetch-Status: MISS 186 | X-Store-Status: STORE 187 | --- response_body_like: It works! 188 | --- wait: 0.1 189 | --- error_log 190 | srcache_store: subrequest returned status 201 191 | --- error_code: 200 192 | --- log_level: debug 193 | 194 | 195 | 196 | === TEST 7: range fetch (cache hit) 197 | --- config 198 | location = /index.html { 199 | default_type text/css; 200 | srcache_fetch GET /memc $uri; 201 | srcache_store PUT /memc $uri; 202 | 203 | echo world; 204 | add_header X-Fetch-Status $srcache_fetch_status; 205 | add_header X-Store-Status $srcache_store_status; 206 | } 207 | 208 | location = /memc { 209 | internal; 210 | 211 | set $memc_key $query_string; 212 | set $memc_exptime 300; 213 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 214 | } 215 | --- request 216 | GET /index.html 217 | --- more_headers 218 | Range: bytes=2-5 219 | 220 | --- response_headers 221 | X-Fetch-Status: HIT 222 | X-Store-Status: BYPASS 223 | Content-Length: 4 224 | --- response_body chop 225 | tml> 226 | --- error_code: 206 227 | 228 | 229 | 230 | === TEST 8: flush all (not using ngx_srcache) 231 | --- config 232 | location = /flush { 233 | set $memc_cmd 'flush_all'; 234 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 235 | add_header X-Fetch-Status $srcache_fetch_status; 236 | add_header X-Store-Status $srcache_store_status; 237 | } 238 | --- response_headers 239 | Content-Type: text/plain 240 | Content-Length: 4 241 | X-Fetch-Status: BYPASS 242 | X-Store-Status: BYPASS 243 | 244 | --- request 245 | GET /flush 246 | --- response_body eval: "OK\r\n" 247 | 248 | 249 | 250 | === TEST 9: range fetch (cache miss) - no store ranges (default off) 251 | --- config 252 | location = /index.html { 253 | default_type text/css; 254 | #srcache_store_ranges on; 255 | srcache_fetch GET /memc $uri$http_range; 256 | srcache_store PUT /memc $uri$http_range; 257 | 258 | #add_header X-Fetch-Status $srcache_fetch_status; 259 | #add_header X-Store-Status $srcache_store_status; 260 | } 261 | 262 | location = /memc { 263 | internal; 264 | 265 | set $memc_key $query_string; 266 | set $memc_exptime 300; 267 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 268 | } 269 | --- request 270 | GET /index.html 271 | --- more_headers 272 | Range: bytes=2-5 273 | --- response_headers 274 | Content-Type: text/html 275 | Content-Length: 4 276 | !X-Fetch-Status 277 | !X-Store-Status 278 | --- response_body chop 279 | tml> 280 | --- wait: 0.1 281 | --- no_error_log 282 | srcache_store: subrequest returned status 201 283 | --- error_code: 206 284 | --- log_level: debug 285 | 286 | 287 | 288 | === TEST 10: range fetch (cache hit) 289 | --- config 290 | location = /index.html { 291 | default_type text/css; 292 | srcache_fetch GET /memc $uri$http_range; 293 | srcache_store PUT /memc $uri$http_range; 294 | 295 | echo world; 296 | add_header X-Fetch-Status $srcache_fetch_status; 297 | add_header X-Store-Status $srcache_store_status; 298 | } 299 | 300 | location = /memc { 301 | internal; 302 | 303 | set $memc_key $query_string; 304 | set $memc_exptime 300; 305 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 306 | } 307 | --- request 308 | GET /index.html 309 | --- more_headers 310 | Range: bytes=2-5 311 | 312 | --- response_headers 313 | X-Fetch-Status: MISS 314 | X-Store-Status: STORE 315 | !Content-Length 316 | --- response_body 317 | world 318 | --- error_code: 200 319 | 320 | 321 | 322 | === TEST 11: flush all (not using ngx_srcache) 323 | --- config 324 | location = /flush { 325 | set $memc_cmd 'flush_all'; 326 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 327 | add_header X-Fetch-Status $srcache_fetch_status; 328 | add_header X-Store-Status $srcache_store_status; 329 | } 330 | --- response_headers 331 | Content-Type: text/plain 332 | Content-Length: 4 333 | X-Fetch-Status: BYPASS 334 | X-Store-Status: BYPASS 335 | 336 | --- request 337 | GET /flush 338 | --- response_body eval: "OK\r\n" 339 | 340 | 341 | 342 | === TEST 12: range fetch (cache miss) - no store ranges (explicit off) 343 | --- config 344 | location = /index.html { 345 | default_type text/css; 346 | srcache_store_ranges off; 347 | srcache_fetch GET /memc $uri$http_range; 348 | srcache_store PUT /memc $uri$http_range; 349 | 350 | #add_header X-Fetch-Status $srcache_fetch_status; 351 | #add_header X-Store-Status $srcache_store_status; 352 | } 353 | 354 | location = /memc { 355 | internal; 356 | 357 | set $memc_key $query_string; 358 | set $memc_exptime 300; 359 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 360 | } 361 | --- request 362 | GET /index.html 363 | --- more_headers 364 | Range: bytes=2-5 365 | --- response_headers 366 | Content-Type: text/html 367 | Content-Length: 4 368 | !X-Fetch-Status 369 | !X-Store-Status 370 | --- response_body chop 371 | tml> 372 | --- wait: 0.1 373 | --- no_error_log 374 | srcache_store: subrequest returned status 201 375 | --- error_code: 206 376 | --- log_level: debug 377 | 378 | 379 | 380 | === TEST 13: range fetch (cache hit) 381 | --- config 382 | location = /index.html { 383 | default_type text/css; 384 | srcache_fetch GET /memc $uri$http_range; 385 | srcache_store PUT /memc $uri$http_range; 386 | 387 | echo world; 388 | add_header X-Fetch-Status $srcache_fetch_status; 389 | add_header X-Store-Status $srcache_store_status; 390 | } 391 | 392 | location = /memc { 393 | internal; 394 | 395 | set $memc_key $query_string; 396 | set $memc_exptime 300; 397 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 398 | } 399 | --- request 400 | GET /index.html 401 | --- more_headers 402 | Range: bytes=2-5 403 | 404 | --- response_headers 405 | X-Fetch-Status: MISS 406 | X-Store-Status: STORE 407 | !Content-Length 408 | --- response_body 409 | world 410 | --- error_code: 200 411 | -------------------------------------------------------------------------------- /t/redis.t: -------------------------------------------------------------------------------- 1 | # vi:ft= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 5 * blocks(); 9 | 10 | $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; 11 | 12 | #master_on(); 13 | #no_long_string(); 14 | no_shuffle(); 15 | 16 | #log_level('warn'); 17 | run_tests(); 18 | 19 | __DATA__ 20 | 21 | === TEST 1: flush all 22 | --- config 23 | location /flush { 24 | redis2_query flushall; 25 | redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT; 26 | } 27 | --- response_headers 28 | Content-Type: text/plain 29 | !Content-Length 30 | --- request 31 | GET /flush 32 | --- response_body eval: "+OK\r\n" 33 | --- no_error_log 34 | [error] 35 | 36 | 37 | 38 | === TEST 2: basic fetch (cache miss) 39 | --- timeout: 3 40 | --- config 41 | location /foo { 42 | default_type text/css; 43 | 44 | set $key $uri; 45 | set_escape_uri $escaped_key $key; 46 | srcache_fetch GET /redis $key; 47 | srcache_store POST /redis2 key=$escaped_key&exptime=120; 48 | 49 | echo hello; 50 | echo hiya; 51 | } 52 | 53 | location = /redis { 54 | internal; 55 | 56 | set $redis_key $args; 57 | redis_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT; 58 | } 59 | 60 | location = /redis2 { 61 | internal; 62 | 63 | set_unescape_uri $exptime $arg_exptime; 64 | set_unescape_uri $key $arg_key; 65 | 66 | redis2_query set $key $echo_request_body; 67 | redis2_query expire $key $exptime; 68 | redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT; 69 | } 70 | --- request 71 | GET /foo 72 | --- response_headers 73 | Content-Type: text/css 74 | !Content-Length 75 | --- response_body 76 | hello 77 | hiya 78 | --- no_error_log 79 | [error] 80 | 81 | 82 | 83 | === TEST 3: basic fetch (cache hit) 84 | --- config 85 | location /foo { 86 | default_type text/css; 87 | 88 | set $key $uri; 89 | srcache_fetch GET /redis $key; 90 | srcache_store PUT /redis2 key=$key&exptime=10; 91 | 92 | echo world; 93 | } 94 | 95 | location = /redis { 96 | #internal; 97 | 98 | #redis2_query get $args; 99 | #redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT; 100 | #echo "args = $args"; 101 | 102 | set $redis_key $args; 103 | redis_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT; 104 | } 105 | 106 | location = /redis2 { 107 | internal; 108 | 109 | set_unescape_uri $exptime $arg_exptime; 110 | set_unescape_uri $key $arg_key; 111 | 112 | redis2_query set $key $echo_request_body; 113 | redis2_query expire $key $exptime; 114 | 115 | redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT; 116 | } 117 | --- request 118 | GET /foo 119 | --- response_headers 120 | Content-Type: text/css 121 | Content-Length: 11 122 | --- response_body 123 | hello 124 | hiya 125 | --- no_error_log 126 | [error] 127 | 128 | 129 | 130 | === TEST 4: flush all - cluster 131 | --- config 132 | location /flush { 133 | redis2_query flushall; 134 | redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT; 135 | } 136 | --- response_headers 137 | Content-Type: text/plain 138 | !Content-Length 139 | --- request 140 | GET /flush 141 | --- response_body eval: "+OK\r\n" 142 | --- no_error_log 143 | [error] 144 | 145 | 146 | 147 | === TEST 5: basic fetch (cache miss) - cluster 148 | --- timeout: 3 149 | --- http_config 150 | upstream foo { 151 | server 127.0.0.1:$TEST_NGINX_REDIS_PORT; 152 | } 153 | --- config 154 | location /foo { 155 | default_type text/css; 156 | 157 | set $key $uri; 158 | set_escape_uri $escaped_key $key; 159 | #srcache_fetch GET /redis $key; 160 | srcache_store POST /redis2 key=$escaped_key&exptime=120; 161 | 162 | echo hello; 163 | echo hiya; 164 | } 165 | 166 | location = /redis { 167 | internal; 168 | 169 | set_md5 $redis_key $args; 170 | set $backend foo; 171 | redis_pass $backend; 172 | } 173 | 174 | location = /redis2 { 175 | internal; 176 | 177 | set_unescape_uri $exptime $arg_exptime; 178 | set_unescape_uri $key $arg_key; 179 | set_md5 $key; 180 | set $backend foo; 181 | 182 | redis2_query set $key $echo_request_body; 183 | redis2_query expire $key $exptime; 184 | redis2_pass $backend; 185 | } 186 | --- request 187 | GET /foo 188 | --- response_headers 189 | Content-Type: text/css 190 | !Content-Length 191 | --- response_body 192 | hello 193 | hiya 194 | --- no_error_log 195 | [error] 196 | 197 | 198 | 199 | === TEST 6: basic fetch (cache hit) - cluster 200 | --- http_config 201 | upstream foo { 202 | server 127.0.0.1:$TEST_NGINX_REDIS_PORT; 203 | } 204 | 205 | --- config 206 | location /foo { 207 | default_type text/css; 208 | 209 | set $key $uri; 210 | srcache_fetch GET /redis $key; 211 | srcache_store PUT /redis2 key=$key&exptime=10; 212 | 213 | echo world; 214 | } 215 | 216 | location = /redis { 217 | #internal; 218 | 219 | set_md5 $redis_key $args; 220 | set $backend foo; 221 | redis_pass $backend; 222 | } 223 | 224 | location = /redis2 { 225 | internal; 226 | 227 | set_unescape_uri $exptime $arg_exptime; 228 | set_unescape_uri $key $arg_key; 229 | set_md5 $key; 230 | 231 | redis2_query set $key $echo_request_body; 232 | redis2_query expire $key $exptime; 233 | 234 | set $backend foo; 235 | redis2_pass $backend; 236 | } 237 | --- request 238 | GET /foo 239 | --- response_headers 240 | Content-Type: text/css 241 | Content-Length: 11 242 | --- response_body 243 | hello 244 | hiya 245 | --- no_error_log 246 | [error] 247 | -------------------------------------------------------------------------------- /t/req-cache-control.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 4 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss) 35 | --- config 36 | location /foo { 37 | default_type text/css; 38 | srcache_fetch GET /memc $uri; 39 | srcache_store PUT /memc $uri; 40 | 41 | echo hello; 42 | } 43 | 44 | location /memc { 45 | internal; 46 | 47 | set $memc_key $query_string; 48 | set $memc_exptime 300; 49 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 50 | } 51 | --- request 52 | GET /foo 53 | --- response_headers 54 | Content-Type: text/css 55 | Content-Length: 56 | --- response_body 57 | hello 58 | 59 | 60 | 61 | === TEST 3: basic fetch (cache hit) 62 | --- config 63 | location /foo { 64 | default_type text/css; 65 | srcache_fetch GET /memc $uri; 66 | srcache_store PUT /memc $uri; 67 | 68 | echo world; 69 | } 70 | 71 | location /memc { 72 | internal; 73 | 74 | set $memc_key $query_string; 75 | set $memc_exptime 300; 76 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 77 | } 78 | --- request 79 | GET /foo 80 | --- response_headers 81 | Content-Type: text/css 82 | Content-Length: 6 83 | --- response_body 84 | hello 85 | 86 | 87 | 88 | === TEST 4: request cache-control: no-cache 89 | --- config 90 | location /foo { 91 | default_type text/css; 92 | srcache_fetch GET /memc $uri; 93 | srcache_store PUT /memc $uri; 94 | srcache_request_cache_control on; 95 | 96 | echo world; 97 | } 98 | 99 | location /memc { 100 | internal; 101 | 102 | set $memc_key $query_string; 103 | set $memc_exptime 300; 104 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 105 | } 106 | --- request 107 | GET /foo 108 | --- more_headers 109 | cache-control: No-Cache 110 | --- response_headers 111 | Content-Type: text/css 112 | Content-Length: 113 | --- response_body 114 | world 115 | 116 | 117 | 118 | === TEST 5: basic fetch (cache hit again) 119 | --- config 120 | location /foo { 121 | default_type text/css; 122 | srcache_fetch GET /memc $uri; 123 | srcache_store PUT /memc $uri; 124 | 125 | echo world; 126 | } 127 | 128 | location /memc { 129 | internal; 130 | 131 | set $memc_key $query_string; 132 | set $memc_exptime 300; 133 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 134 | } 135 | --- request 136 | GET /foo 137 | --- response_headers 138 | Content-Type: text/css 139 | Content-Length: 6 140 | --- response_body 141 | world 142 | 143 | 144 | 145 | === TEST 6: flush all 146 | --- config 147 | location /flush { 148 | set $memc_cmd 'flush_all'; 149 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 150 | } 151 | --- response_headers 152 | Content-Type: text/plain 153 | Content-Length: 4 154 | --- request 155 | GET /flush 156 | --- response_body eval: "OK\r\n" 157 | 158 | 159 | 160 | === TEST 7: basic fetch (cache miss) 161 | --- config 162 | location /foo { 163 | default_type text/css; 164 | srcache_fetch GET /memc $uri; 165 | srcache_store PUT /memc $uri; 166 | 167 | echo hello; 168 | } 169 | 170 | location /memc { 171 | internal; 172 | 173 | set $memc_key $query_string; 174 | set $memc_exptime 300; 175 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 176 | } 177 | --- request 178 | GET /foo 179 | --- response_headers 180 | Content-Type: text/css 181 | Content-Length: 182 | --- response_body 183 | hello 184 | 185 | 186 | 187 | === TEST 8: basic fetch (cache hit) 188 | --- config 189 | location /foo { 190 | default_type text/css; 191 | srcache_fetch GET /memc $uri; 192 | srcache_store PUT /memc $uri; 193 | 194 | echo world; 195 | } 196 | 197 | location /memc { 198 | internal; 199 | 200 | set $memc_key $query_string; 201 | set $memc_exptime 300; 202 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 203 | } 204 | --- request 205 | GET /foo 206 | --- response_headers 207 | Content-Type: text/css 208 | Content-Length: 6 209 | --- response_body 210 | hello 211 | 212 | 213 | 214 | === TEST 9: request cache-control: no-cache & no-store 215 | --- config 216 | location /foo { 217 | default_type text/css; 218 | srcache_fetch GET /memc $uri; 219 | srcache_store PUT /memc $uri; 220 | srcache_request_cache_control on; 221 | 222 | echo world; 223 | } 224 | 225 | location /memc { 226 | internal; 227 | 228 | set $memc_key $query_string; 229 | set $memc_exptime 300; 230 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 231 | } 232 | --- request 233 | GET /foo 234 | --- more_headers 235 | Cache-Control: no-cache 236 | Cache-Control: no-store 237 | --- response_headers 238 | Content-Type: text/css 239 | Content-Length: 240 | --- response_body 241 | world 242 | 243 | 244 | 245 | === TEST 10: basic fetch (cache hit again) 246 | --- config 247 | location /foo { 248 | default_type text/css; 249 | srcache_fetch GET /memc $uri; 250 | srcache_store PUT /memc $uri; 251 | 252 | echo world; 253 | } 254 | 255 | location /memc { 256 | internal; 257 | 258 | set $memc_key $query_string; 259 | set $memc_exptime 300; 260 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 261 | } 262 | --- request 263 | GET /foo 264 | --- response_headers 265 | Content-Type: text/css 266 | Content-Length: 6 267 | --- response_body 268 | hello 269 | 270 | 271 | 272 | === TEST 11: flush all 273 | --- config 274 | location /flush { 275 | set $memc_cmd 'flush_all'; 276 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 277 | } 278 | --- response_headers 279 | Content-Type: text/plain 280 | Content-Length: 4 281 | --- request 282 | GET /flush 283 | --- response_body eval: "OK\r\n" 284 | 285 | 286 | 287 | === TEST 12: basic fetch (cache miss) 288 | --- config 289 | location /foo { 290 | default_type text/css; 291 | srcache_fetch GET /memc $uri; 292 | srcache_store PUT /memc $uri; 293 | 294 | echo hello; 295 | } 296 | 297 | location /memc { 298 | internal; 299 | 300 | set $memc_key $query_string; 301 | set $memc_exptime 300; 302 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 303 | } 304 | --- request 305 | GET /foo 306 | --- response_headers 307 | Content-Type: text/css 308 | Content-Length: 309 | --- response_body 310 | hello 311 | 312 | 313 | 314 | === TEST 13: basic fetch (cache hit) 315 | --- config 316 | location /foo { 317 | default_type text/css; 318 | srcache_fetch GET /memc $uri; 319 | srcache_store PUT /memc $uri; 320 | 321 | echo world; 322 | } 323 | 324 | location /memc { 325 | internal; 326 | 327 | set $memc_key $query_string; 328 | set $memc_exptime 300; 329 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 330 | } 331 | --- request 332 | GET /foo 333 | --- response_headers 334 | Content-Type: text/css 335 | Content-Length: 6 336 | --- response_body 337 | hello 338 | 339 | 340 | 341 | === TEST 14: request cache-control: no-store 342 | --- config 343 | location /foo { 344 | default_type text/css; 345 | srcache_fetch GET /memc $uri; 346 | srcache_store PUT /memc $uri; 347 | srcache_request_cache_control on; 348 | 349 | echo world; 350 | } 351 | 352 | location /memc { 353 | internal; 354 | 355 | set $memc_key $query_string; 356 | set $memc_exptime 300; 357 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 358 | } 359 | --- request 360 | GET /foo 361 | --- more_headers 362 | Cache-Control: no-store 363 | --- response_headers 364 | Content-Type: text/css 365 | Content-Length: 6 366 | --- response_body 367 | hello 368 | 369 | 370 | 371 | === TEST 15: basic fetch (cache hit again) 372 | --- config 373 | location /foo { 374 | default_type text/css; 375 | srcache_fetch GET /memc $uri; 376 | srcache_store PUT /memc $uri; 377 | 378 | echo world; 379 | } 380 | 381 | location /memc { 382 | internal; 383 | 384 | set $memc_key $query_string; 385 | set $memc_exptime 300; 386 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 387 | } 388 | --- request 389 | GET /foo 390 | --- response_headers 391 | Content-Type: text/css 392 | Content-Length: 6 393 | --- response_body 394 | hello 395 | -------------------------------------------------------------------------------- /t/satisfy.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks() + 2); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | --- request 29 | GET /flush 30 | --- response_body eval: "OK\r\n" 31 | 32 | 33 | 34 | === TEST 2: basic fetch (cache miss) - deny all 35 | --- config 36 | location /foo { 37 | satisfy any; 38 | 39 | deny all; 40 | 41 | default_type text/css; 42 | srcache_fetch GET /memc $uri; 43 | srcache_store PUT /memc $uri; 44 | 45 | echo hello; 46 | } 47 | 48 | location /memc { 49 | internal; 50 | 51 | set $memc_key $query_string; 52 | set $memc_exptime 300; 53 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 54 | } 55 | --- request 56 | GET /foo 57 | --- response_headers 58 | --- response_body_like: 403 Forbidden 59 | --- error_code: 403 60 | 61 | 62 | 63 | === TEST 3: basic fetch (cache miss) 64 | --- config 65 | location /foo { 66 | satisfy any; 67 | 68 | default_type text/css; 69 | srcache_fetch GET /memc $uri; 70 | srcache_store PUT /memc $uri; 71 | 72 | echo hello; 73 | } 74 | 75 | location /memc { 76 | internal; 77 | 78 | set $memc_key $query_string; 79 | set $memc_exptime 300; 80 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 81 | } 82 | --- request 83 | GET /foo 84 | --- response_headers 85 | Content-Type: text/css 86 | Content-Length: 87 | --- response_body 88 | hello 89 | 90 | 91 | 92 | === TEST 4: basic fetch (cache hit) 93 | --- config 94 | location /foo { 95 | satisfy any; 96 | 97 | default_type text/css; 98 | 99 | srcache_fetch GET /memc $uri; 100 | srcache_store PUT /memc $uri; 101 | 102 | echo world; 103 | } 104 | 105 | location /memc { 106 | internal; 107 | 108 | set $memc_key $query_string; 109 | set $memc_exptime 300; 110 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 111 | } 112 | --- request 113 | GET /foo 114 | --- response_headers 115 | Content-Type: text/css 116 | Content-Length: 6 117 | --- response_body 118 | hello 119 | -------------------------------------------------------------------------------- /t/static.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 5 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | Last-Modified: 29 | --- request 30 | GET /flush 31 | --- response_body eval: "OK\r\n" 32 | 33 | 34 | 35 | === TEST 2: basic fetch (cache miss) 36 | --- config 37 | location /foo { 38 | default_type text/css; 39 | srcache_fetch GET /memc $uri; 40 | srcache_store PUT /memc $uri; 41 | } 42 | 43 | location /memc { 44 | internal; 45 | 46 | set $memc_key $query_string; 47 | set $memc_exptime 300; 48 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 49 | } 50 | --- user_files 51 | >>> foo 201103040521.59 52 | hello 53 | --- request 54 | GET /foo 55 | --- response_headers 56 | Content-Type: text/css 57 | Content-Length: 6 58 | Last-Modified: Fri, 04 Mar 2011 05:21:59 GMT 59 | --- response_body 60 | hello 61 | 62 | 63 | 64 | === TEST 3: basic fetch (cache hit) 65 | --- config 66 | location /foo { 67 | default_type text/css; 68 | srcache_fetch GET /memc $uri; 69 | srcache_store PUT /memc $uri; 70 | 71 | echo world; 72 | } 73 | 74 | location /memc { 75 | internal; 76 | 77 | set $memc_key $query_string; 78 | set $memc_exptime 300; 79 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 80 | } 81 | --- request 82 | GET /foo 83 | --- response_headers 84 | Content-Type: text/css 85 | Content-Length: 6 86 | Last-Modified: Fri, 04 Mar 2011 05:21:59 GMT 87 | --- response_body 88 | hello 89 | 90 | 91 | 92 | === TEST 4: flush all 93 | --- config 94 | location /flush { 95 | set $memc_cmd 'flush_all'; 96 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 97 | } 98 | --- response_headers 99 | Content-Type: text/plain 100 | Content-Length: 4 101 | Last-Modified: 102 | --- request 103 | GET /flush 104 | --- response_body eval: "OK\r\n" 105 | 106 | 107 | 108 | === TEST 5: basic fetch (cache miss), hide Last-Modified 109 | --- config 110 | location /foo { 111 | default_type text/css; 112 | srcache_fetch GET /memc $uri; 113 | srcache_store PUT /memc $uri; 114 | srcache_store_hide_header Last-Modified; 115 | } 116 | 117 | location /memc { 118 | internal; 119 | 120 | set $memc_key $query_string; 121 | set $memc_exptime 300; 122 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 123 | } 124 | --- user_files 125 | >>> foo 201103040521.59 126 | hello 127 | --- request 128 | GET /foo 129 | --- response_headers 130 | Content-Type: text/css 131 | Content-Length: 6 132 | Last-Modified: Fri, 04 Mar 2011 05:21:59 GMT 133 | --- response_body 134 | hello 135 | 136 | 137 | 138 | === TEST 6: basic fetch (cache hit) 139 | --- config 140 | location /foo { 141 | default_type text/css; 142 | srcache_fetch GET /memc $uri; 143 | srcache_store PUT /memc $uri; 144 | 145 | echo world; 146 | } 147 | 148 | location /memc { 149 | internal; 150 | 151 | set $memc_key $query_string; 152 | set $memc_exptime 300; 153 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 154 | } 155 | --- request 156 | GET /foo 157 | --- response_headers 158 | Content-Type: text/css 159 | Content-Length: 6 160 | !Last-Modified 161 | --- response_body 162 | hello 163 | -------------------------------------------------------------------------------- /t/store-hide-headers.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * 5 * blocks(); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | !Foo-Bar 29 | --- request 30 | GET /flush 31 | --- response_body eval: "OK\r\n" 32 | 33 | 34 | 35 | === TEST 2: basic fetch (cache miss) 36 | --- config 37 | location /foo { 38 | default_type text/css; 39 | srcache_fetch GET /memc $uri; 40 | srcache_store PUT /memc $uri; 41 | 42 | content_by_lua ' 43 | ngx.header["Foo-Bar"] = "hi world" 44 | ngx.say("hello") 45 | '; 46 | } 47 | 48 | location /memc { 49 | internal; 50 | 51 | set $memc_key $query_string; 52 | set $memc_exptime 300; 53 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 54 | } 55 | --- request 56 | GET /foo 57 | --- response_headers 58 | Content-Type: text/css 59 | Content-Length: 60 | Foo-Bar: hi world 61 | --- response_body 62 | hello 63 | 64 | 65 | 66 | === TEST 3: basic fetch (cache hit) 67 | --- config 68 | location /foo { 69 | default_type text/css; 70 | srcache_fetch GET /memc $uri; 71 | srcache_store PUT /memc $uri; 72 | 73 | echo world; 74 | } 75 | 76 | location /memc { 77 | internal; 78 | 79 | set $memc_key $query_string; 80 | set $memc_exptime 300; 81 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 82 | } 83 | --- request 84 | GET /foo 85 | --- response_headers 86 | Content-Type: text/css 87 | Content-Length: 6 88 | Foo-Bar: hi world 89 | --- response_body 90 | hello 91 | 92 | 93 | 94 | === TEST 4: flush all 95 | --- config 96 | location /flush { 97 | set $memc_cmd 'flush_all'; 98 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 99 | } 100 | --- response_headers 101 | Content-Type: text/plain 102 | Content-Length: 4 103 | !Foo-Bar 104 | --- request 105 | GET /flush 106 | --- response_body eval: "OK\r\n" 107 | 108 | 109 | 110 | === TEST 5: basic fetch (cache miss) 111 | --- config 112 | location /foo { 113 | default_type text/css; 114 | srcache_fetch GET /memc $uri; 115 | srcache_store PUT /memc $uri; 116 | srcache_store_hide_header "Foo-Bar"; 117 | 118 | content_by_lua ' 119 | ngx.header["Foo-Bar"] = "hi world" 120 | ngx.say("hello") 121 | '; 122 | } 123 | 124 | location /memc { 125 | internal; 126 | 127 | set $memc_key $query_string; 128 | set $memc_exptime 300; 129 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 130 | } 131 | --- request 132 | GET /foo 133 | --- response_headers 134 | Content-Type: text/css 135 | Content-Length: 136 | Foo-Bar: hi world 137 | --- response_body 138 | hello 139 | 140 | 141 | 142 | === TEST 6: basic fetch (cache hit) 143 | --- config 144 | location /foo { 145 | default_type text/css; 146 | srcache_fetch GET /memc $uri; 147 | srcache_store PUT /memc $uri; 148 | 149 | echo world; 150 | } 151 | 152 | location /memc { 153 | internal; 154 | 155 | set $memc_key $query_string; 156 | set $memc_exptime 300; 157 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 158 | } 159 | --- request 160 | GET /foo 161 | --- response_headers 162 | Content-Type: text/css 163 | Content-Length: 6 164 | !Foo-Bar 165 | --- response_body 166 | hello 167 | 168 | 169 | 170 | === TEST 7: flush all 171 | --- config 172 | location /flush { 173 | set $memc_cmd 'flush_all'; 174 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 175 | } 176 | --- response_headers 177 | Content-Type: text/plain 178 | Content-Length: 4 179 | !Foo-Bar 180 | --- request 181 | GET /flush 182 | --- response_body eval: "OK\r\n" 183 | 184 | 185 | 186 | === TEST 8: basic fetch (cache miss) 187 | --- config 188 | location /foo { 189 | default_type text/css; 190 | srcache_fetch GET /memc $uri; 191 | srcache_store PUT /memc $uri; 192 | srcache_store_hide_header "Foo-Bar"; 193 | 194 | content_by_lua ' 195 | ngx.header["Foo-Bar"] = "hi world"; 196 | ngx.say("hello") 197 | '; 198 | } 199 | 200 | location /memc { 201 | internal; 202 | 203 | set $memc_key $query_string; 204 | set $memc_exptime 300; 205 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 206 | } 207 | --- request 208 | GET /foo 209 | --- response_headers 210 | Content-Type: text/css 211 | Content-Length: 212 | Foo-Bar: hi world 213 | --- response_body 214 | hello 215 | 216 | 217 | 218 | === TEST 9: basic fetch (cache hit) 219 | --- config 220 | location /foo { 221 | default_type text/css; 222 | srcache_fetch GET /memc $uri; 223 | srcache_store PUT /memc $uri; 224 | 225 | echo world; 226 | } 227 | 228 | location /memc { 229 | internal; 230 | 231 | set $memc_key $query_string; 232 | set $memc_exptime 300; 233 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 234 | } 235 | --- request 236 | GET /foo 237 | --- response_headers 238 | Content-Type: text/css 239 | Content-Length: 6 240 | !Foo-Bar 241 | --- response_body 242 | hello 243 | 244 | 245 | 246 | === TEST 10: flush all 247 | --- config 248 | location /flush { 249 | set $memc_cmd 'flush_all'; 250 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 251 | } 252 | --- response_headers 253 | Content-Type: text/plain 254 | Content-Length: 4 255 | !Foo-Bar 256 | --- request 257 | GET /flush 258 | --- response_body eval: "OK\r\n" 259 | 260 | 261 | 262 | === TEST 11: basic fetch (hide Content-Type in store) 263 | --- config 264 | location /foo { 265 | default_type text/css; 266 | srcache_fetch GET /memc $uri; 267 | srcache_store PUT /memc $uri; 268 | srcache_store_hide_header "Content-Type"; 269 | 270 | echo hello; 271 | } 272 | 273 | location /memc { 274 | internal; 275 | 276 | set $memc_key $query_string; 277 | set $memc_exptime 300; 278 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 279 | } 280 | --- request 281 | GET /foo 282 | --- response_headers 283 | Content-Type: text/css 284 | Content-Length: 285 | !Foo-Bar 286 | --- response_body 287 | hello 288 | 289 | 290 | 291 | === TEST 12: basic fetch (cache hit) 292 | --- config 293 | location /foo { 294 | default_type text/css; 295 | srcache_fetch GET /memc $uri; 296 | srcache_store PUT /memc $uri; 297 | 298 | echo world; 299 | } 300 | 301 | location /memc { 302 | internal; 303 | 304 | set $memc_key $query_string; 305 | set $memc_exptime 300; 306 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 307 | } 308 | --- request 309 | GET /foo 310 | --- response_headers 311 | !Content-Type 312 | Content-Length: 6 313 | !Foo-Bar 314 | --- response_body 315 | hello 316 | 317 | 318 | 319 | === TEST 13: flush all 320 | --- config 321 | location /flush { 322 | set $memc_cmd 'flush_all'; 323 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 324 | } 325 | --- response_headers 326 | Content-Type: text/plain 327 | Content-Length: 4 328 | !Foo-Bar 329 | --- request 330 | GET /flush 331 | --- response_body eval: "OK\r\n" 332 | 333 | 334 | 335 | === TEST 14: basic fetch (hide and pass Content-Type in store) 336 | --- config 337 | location /foo { 338 | default_type text/css; 339 | srcache_fetch GET /memc $uri; 340 | srcache_store PUT /memc $uri; 341 | srcache_store_hide_header "Content-Type"; 342 | srcache_store_pass_header "Content-Type"; 343 | 344 | echo hello; 345 | } 346 | 347 | location /memc { 348 | internal; 349 | 350 | set $memc_key $query_string; 351 | set $memc_exptime 300; 352 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 353 | } 354 | --- request 355 | GET /foo 356 | --- response_headers 357 | Content-Type: text/css 358 | Content-Length: 359 | !Foo-Bar 360 | --- response_body 361 | hello 362 | 363 | 364 | 365 | === TEST 15: basic fetch (cache hit) 366 | --- config 367 | location /foo { 368 | default_type text/blah; 369 | srcache_fetch GET /memc $uri; 370 | srcache_store PUT /memc $uri; 371 | 372 | echo world; 373 | } 374 | 375 | location /memc { 376 | internal; 377 | 378 | set $memc_key $query_string; 379 | set $memc_exptime 300; 380 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 381 | } 382 | --- request 383 | GET /foo 384 | --- response_headers 385 | Content-Type: text/css 386 | Content-Length: 6 387 | !Foo-Bar 388 | --- response_body 389 | hello 390 | -------------------------------------------------------------------------------- /t/store-max-size.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (2 * blocks() + 5); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | no_shuffle(); 13 | 14 | run_tests(); 15 | 16 | __DATA__ 17 | 18 | === TEST 1: flush all 19 | --- config 20 | location /flush { 21 | set $memc_cmd 'flush_all'; 22 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 23 | } 24 | --- request 25 | GET /flush 26 | --- response_body eval: "OK\r\n" 27 | 28 | 29 | 30 | === TEST 2: just hit store_max_size 31 | --- config 32 | location /foo { 33 | default_type text/css; 34 | srcache_store PUT /memc $uri; 35 | srcache_store_max_size 49; 36 | 37 | echo hello; 38 | add_header X-Store-Status $srcache_store_status; 39 | } 40 | 41 | location /memc { 42 | internal; 43 | 44 | set $memc_cmd set; 45 | set $memc_key hit_store_max_size; 46 | set $memc_exptime 300; 47 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 48 | } 49 | --- request 50 | GET /foo 51 | --- response_body 52 | hello 53 | --- response_headers 54 | X-Store-Status: STORE 55 | 56 | 57 | 58 | === TEST 3: check if /memc was invoked (just equal) 59 | --- config 60 | location /memc { 61 | set $memc_cmd get; 62 | set $memc_key hit_store_max_size; 63 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 64 | } 65 | --- request 66 | GET /memc 67 | --- response_body eval 68 | "HTTP/1.1 200 OK\r 69 | Content-Type: text/css\r 70 | \r 71 | hello 72 | " 73 | 74 | 75 | 76 | === TEST 4: flush all 77 | --- config 78 | location /flush { 79 | set $memc_cmd 'flush_all'; 80 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 81 | } 82 | --- request 83 | GET /flush 84 | --- response_body eval: "OK\r\n" 85 | 86 | 87 | 88 | === TEST 5: less than store_max_size 89 | --- config 90 | location /foo { 91 | default_type text/css; 92 | srcache_store PUT /memc $uri; 93 | srcache_store_max_size 50; 94 | 95 | echo hello; 96 | add_header X-Store-Status $srcache_store_status; 97 | } 98 | 99 | location /memc { 100 | internal; 101 | 102 | set $memc_cmd set; 103 | set $memc_key less_than_store_max_size; 104 | set $memc_exptime 300; 105 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 106 | } 107 | --- request 108 | GET /foo 109 | --- response_body 110 | hello 111 | --- response_headers 112 | X-Store-Status: STORE 113 | 114 | 115 | 116 | === TEST 6: check if /memc was invoked (less than) 117 | --- config 118 | location /memc { 119 | set $memc_cmd get; 120 | set $memc_key less_than_store_max_size; 121 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 122 | } 123 | --- request 124 | GET /memc 125 | --- response_body eval 126 | "HTTP/1.1 200 OK\r 127 | Content-Type: text/css\r 128 | \r 129 | hello 130 | " 131 | 132 | 133 | 134 | === TEST 7: flush all 135 | --- config 136 | location /flush { 137 | set $memc_cmd 'flush_all'; 138 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 139 | } 140 | --- request 141 | GET /flush 142 | --- response_body eval: "OK\r\n" 143 | 144 | 145 | 146 | === TEST 8: just more than store_max_size 147 | --- config 148 | location /foo { 149 | default_type text/css; 150 | srcache_store PUT /memc $uri; 151 | srcache_store_max_size 48; 152 | 153 | echo hello; 154 | add_header X-Store-Status $srcache_store_status; 155 | log_by_lua 'ngx.log(ngx.WARN, "store status: ", ngx.var.srcache_store_status)'; 156 | } 157 | 158 | location /memc { 159 | internal; 160 | 161 | set $memc_cmd set; 162 | set $memc_key more_than_store_max_size; 163 | set $memc_exptime 300; 164 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 165 | } 166 | --- request 167 | GET /foo 168 | --- response_body 169 | hello 170 | --- wait: 0.1 171 | --- response_headers 172 | X-Store-Status: STORE 173 | --- error_log 174 | store status: BYPASS 175 | 176 | 177 | 178 | === TEST 9: check if /memc was invoked (more than) 179 | --- config 180 | location /memc { 181 | set $memc_cmd get; 182 | set $memc_key more_than_store_max_size; 183 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 184 | } 185 | --- request 186 | GET /memc 187 | --- response_body_like: 404 Not Found 188 | --- error_code: 404 189 | 190 | 191 | 192 | === TEST 10: flush all 193 | --- config 194 | location /flush { 195 | set $memc_cmd 'flush_all'; 196 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 197 | } 198 | --- request 199 | GET /flush 200 | --- response_body eval: "OK\r\n" 201 | 202 | 203 | 204 | === TEST 11: just more than store_max_size (explicit content-length) 205 | --- config 206 | location /foo.txt { 207 | default_type text/css; 208 | srcache_store PUT /memc $uri; 209 | srcache_store_max_size 48; 210 | 211 | content_by_lua ' 212 | ngx.header.content_length = 40; 213 | ngx.say("hello") 214 | '; 215 | add_header X-Store-Status $srcache_store_status; 216 | } 217 | 218 | location /memc { 219 | internal; 220 | 221 | set $memc_cmd set; 222 | set $memc_key more_than_store_max_size; 223 | set $memc_exptime 300; 224 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 225 | } 226 | --- request 227 | GET /foo.txt 228 | --- response_body 229 | hello 230 | --- response_headers 231 | X-Store-Status: BYPASS 232 | 233 | 234 | 235 | === TEST 12: check if /memc was invoked (more than) 236 | --- config 237 | location /memc { 238 | set $memc_cmd get; 239 | set $memc_key more_than_store_max_size; 240 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 241 | } 242 | --- request 243 | GET /memc 244 | --- response_body_like: 404 Not Found 245 | --- error_code: 404 246 | 247 | 248 | 249 | === TEST 13: flush all 250 | --- config 251 | location /flush { 252 | set $memc_cmd 'flush_all'; 253 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 254 | } 255 | --- request 256 | GET /flush 257 | --- response_body eval: "OK\r\n" 258 | 259 | 260 | 261 | === TEST 14: server-side config 262 | --- config 263 | srcache_store_max_size 46; 264 | location /foo.txt { 265 | default_type text/css; 266 | srcache_store PUT /memc $uri; 267 | } 268 | 269 | location /memc { 270 | internal; 271 | 272 | set $memc_cmd set; 273 | set $memc_key more_than_store_max_size; 274 | set $memc_exptime 300; 275 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 276 | } 277 | --- user_files 278 | >>> foo.txt 279 | abc 280 | --- request 281 | GET /foo.txt 282 | --- response_body 283 | abc 284 | 285 | 286 | 287 | === TEST 15: check if /memc was invoked (server-level config) 288 | --- config 289 | location /memc { 290 | set $memc_cmd get; 291 | set $memc_key more_than_store_max_size; 292 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 293 | } 294 | --- request 295 | GET /memc 296 | --- response_body_like: 404 Not Found 297 | --- error_code: 404 298 | 299 | 300 | 301 | === TEST 16: flush all 302 | --- config 303 | location /flush { 304 | set $memc_cmd 'flush_all'; 305 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 306 | } 307 | --- request 308 | GET /flush 309 | --- response_body eval: "OK\r\n" 310 | 311 | 312 | 313 | === TEST 17: 0 means unlimited 314 | --- config 315 | srcache_store_max_size 3; 316 | location /foo.txt { 317 | default_type text/css; 318 | srcache_store PUT /memc $uri; 319 | srcache_store_max_size 0; 320 | } 321 | 322 | location /memc { 323 | internal; 324 | 325 | set $memc_cmd set; 326 | set $memc_key more_than_store_max_size; 327 | set $memc_exptime 300; 328 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 329 | } 330 | --- user_files 331 | >>> foo.txt 199801171935.33 332 | hello, world 333 | --- request 334 | GET /foo.txt 335 | --- response_body 336 | hello, world 337 | 338 | 339 | 340 | === TEST 18: check if /memc was invoked (explicit unlimited) 341 | --- config 342 | location /memc { 343 | set $memc_cmd get; 344 | set $memc_key more_than_store_max_size; 345 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 346 | } 347 | --- request 348 | GET /memc 349 | --- response_body_like eval 350 | qr{^HTTP/1.1 200 OK\r 351 | Content-Type: text/css\r 352 | Last-Modified: Sat, 17 Jan 1998 19:35:33 GMT\r 353 | X-SRCache-Allow-Ranges: 1\r 354 | (?:ETag: "[^"]+"\r 355 | )?\r 356 | hello, world$} 357 | -------------------------------------------------------------------------------- /t/store-pass-headers.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (5 * blocks() + 4); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: flush all 20 | --- config 21 | location /flush { 22 | set $memc_cmd 'flush_all'; 23 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 24 | } 25 | --- response_headers 26 | Content-Type: text/plain 27 | Content-Length: 4 28 | !Foo-Bar 29 | --- request 30 | GET /flush 31 | --- response_body eval: "OK\r\n" 32 | 33 | 34 | 35 | === TEST 2: basic fetch (Set-Cookie and Proxy-Authenticate hide by default) 36 | --- config 37 | location /foo { 38 | default_type text/css; 39 | srcache_fetch GET /memc $uri; 40 | srcache_store PUT /memc $uri; 41 | 42 | content_by_lua ' 43 | ngx.header["Set-Cookie"] = "foo=baz" 44 | ngx.header["Proxy-Authenticate"] = "blah" 45 | ngx.say("hello") 46 | '; 47 | } 48 | 49 | location /memc { 50 | internal; 51 | 52 | set $memc_key $query_string; 53 | set $memc_exptime 300; 54 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 55 | } 56 | --- request 57 | GET /foo 58 | --- response_headers 59 | Content-Type: text/css 60 | Content-Length: 61 | Set-Cookie: foo=baz 62 | Proxy-Authenticate: blah 63 | --- response_body 64 | hello 65 | 66 | 67 | 68 | === TEST 3: basic fetch (cache hit) 69 | --- config 70 | location /foo { 71 | default_type text/css; 72 | srcache_fetch GET /memc $uri; 73 | srcache_store PUT /memc $uri; 74 | 75 | echo world; 76 | } 77 | 78 | location /memc { 79 | internal; 80 | 81 | set $memc_key $query_string; 82 | set $memc_exptime 300; 83 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 84 | } 85 | --- request 86 | GET /foo 87 | --- response_headers 88 | Content-Type: text/css 89 | Content-Length: 6 90 | !Set-Cookie 91 | !Proxy-Authenticate 92 | --- response_body 93 | hello 94 | 95 | 96 | 97 | === TEST 4: flush all 98 | --- config 99 | location /flush { 100 | set $memc_cmd 'flush_all'; 101 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 102 | } 103 | --- response_headers 104 | Content-Type: text/plain 105 | Content-Length: 4 106 | !Foo-Bar 107 | --- request 108 | GET /flush 109 | --- response_body eval: "OK\r\n" 110 | 111 | 112 | 113 | === TEST 5: basic fetch (Set-Cookie hide by default) 114 | --- config 115 | location /foo { 116 | default_type text/css; 117 | srcache_fetch GET /memc $uri; 118 | srcache_store PUT /memc $uri; 119 | srcache_store_pass_header Set-Cookie; 120 | srcache_store_pass_header Proxy-Authenticate; 121 | 122 | content_by_lua ' 123 | ngx.header["Set-Cookie"] = "foo=baz" 124 | ngx.header["Proxy-Authenticate"] = "blah" 125 | ngx.say("hello") 126 | '; 127 | } 128 | 129 | location /memc { 130 | internal; 131 | 132 | set $memc_key $query_string; 133 | set $memc_exptime 300; 134 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 135 | } 136 | --- request 137 | GET /foo 138 | --- response_headers 139 | Content-Type: text/css 140 | Content-Length: 141 | Set-Cookie: foo=baz 142 | Proxy-Authenticate: blah 143 | --- response_body 144 | hello 145 | 146 | 147 | 148 | === TEST 6: basic fetch (cache hit) 149 | --- config 150 | location /foo { 151 | default_type text/css; 152 | srcache_fetch GET /memc $uri; 153 | srcache_store PUT /memc $uri; 154 | 155 | echo world; 156 | } 157 | 158 | location /memc { 159 | internal; 160 | 161 | set $memc_key $query_string; 162 | set $memc_exptime 300; 163 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 164 | } 165 | --- request 166 | GET /foo 167 | --- response_headers 168 | Content-Type: text/css 169 | Content-Length: 6 170 | Set-Cookie: foo=baz 171 | Proxy-Authenticate: blah 172 | --- response_body 173 | hello 174 | -------------------------------------------------------------------------------- /t/store-skip.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (2 * blocks() + 8); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | no_shuffle(); 13 | 14 | run_tests(); 15 | 16 | __DATA__ 17 | 18 | === TEST 1: flush all 19 | --- config 20 | location /flush { 21 | set $memc_cmd 'flush_all'; 22 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 23 | } 24 | --- request 25 | GET /flush 26 | --- response_body eval: "OK\r\n" 27 | 28 | 29 | 30 | === TEST 2: skip is false 31 | --- config 32 | location /foo { 33 | default_type text/css; 34 | srcache_store PUT /memc $uri; 35 | set $skip ''; 36 | srcache_store_skip $skip; 37 | 38 | echo hello; 39 | add_header X-Store-Status $srcache_store_status; 40 | } 41 | 42 | location /memc { 43 | internal; 44 | 45 | set $memc_cmd set; 46 | set $memc_key key; 47 | set $memc_exptime 300; 48 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 49 | } 50 | --- request 51 | GET /foo 52 | --- response_body 53 | hello 54 | --- response_headers 55 | X-Store-Status: STORE 56 | 57 | 58 | 59 | === TEST 3: check if /memc was invoked (just equal) 60 | --- config 61 | location /memc { 62 | set $memc_cmd get; 63 | set $memc_key key; 64 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 65 | } 66 | --- request 67 | GET /memc 68 | --- response_body eval 69 | "HTTP/1.1 200 OK\r 70 | Content-Type: text/css\r 71 | \r 72 | hello 73 | " 74 | 75 | 76 | 77 | === TEST 4: flush all 78 | --- config 79 | location /flush { 80 | set $memc_cmd 'flush_all'; 81 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 82 | } 83 | --- request 84 | GET /flush 85 | --- response_body eval: "OK\r\n" 86 | 87 | 88 | 89 | === TEST 5: store_skip is literally false 90 | --- config 91 | location /foo { 92 | default_type text/css; 93 | srcache_store PUT /memc $uri; 94 | srcache_store_skip 0; 95 | add_header X-Store-Status $srcache_store_status; 96 | 97 | echo hello; 98 | } 99 | 100 | location /memc { 101 | internal; 102 | 103 | set $memc_cmd set; 104 | set $memc_key key; 105 | set $memc_exptime 300; 106 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 107 | } 108 | --- request 109 | GET /foo 110 | --- response_body 111 | hello 112 | --- response_headers 113 | X-Store-Status: STORE 114 | 115 | 116 | 117 | === TEST 6: check if /memc was invoked (less than) 118 | --- config 119 | location /memc { 120 | set $memc_cmd get; 121 | set $memc_key key; 122 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 123 | } 124 | --- request 125 | GET /memc 126 | --- response_body eval 127 | "HTTP/1.1 200 OK\r 128 | Content-Type: text/css\r 129 | \r 130 | hello 131 | " 132 | 133 | 134 | 135 | === TEST 7: flush all 136 | --- config 137 | location /flush { 138 | set $memc_cmd 'flush_all'; 139 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 140 | } 141 | --- request 142 | GET /flush 143 | --- response_body eval: "OK\r\n" 144 | 145 | 146 | 147 | === TEST 8: store_skip is true 148 | --- config 149 | location /foo { 150 | default_type text/css; 151 | srcache_store PUT /memc $uri; 152 | srcache_store_skip 1; 153 | 154 | echo hello; 155 | add_header X-Store-Status $srcache_store_status; 156 | } 157 | 158 | location /memc { 159 | internal; 160 | 161 | set $memc_cmd set; 162 | set $memc_key key; 163 | set $memc_exptime 300; 164 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 165 | } 166 | --- request 167 | GET /foo 168 | --- response_body 169 | hello 170 | --- response_headers 171 | X-Store-Status: BYPASS 172 | 173 | 174 | 175 | === TEST 9: check if /memc was invoked (more than) 176 | --- config 177 | location /memc { 178 | set $memc_cmd get; 179 | set $memc_key key; 180 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 181 | } 182 | --- request 183 | GET /memc 184 | --- response_body_like: 404 Not Found 185 | --- error_code: 404 186 | 187 | 188 | 189 | === TEST 10: flush all 190 | --- config 191 | location /flush { 192 | set $memc_cmd 'flush_all'; 193 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 194 | } 195 | --- request 196 | GET /flush 197 | --- response_body eval: "OK\r\n" 198 | 199 | 200 | 201 | === TEST 11: explicit "true" string 202 | --- config 203 | location /foo.txt { 204 | default_type text/css; 205 | srcache_store PUT /memc $uri; 206 | srcache_store_skip true; 207 | } 208 | 209 | location /memc { 210 | internal; 211 | 212 | set $memc_cmd set; 213 | set $memc_key key; 214 | set $memc_exptime 300; 215 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 216 | } 217 | --- user_files 218 | >>> foo.txt 219 | hello 220 | --- request 221 | GET /foo.txt 222 | --- response_body 223 | hello 224 | 225 | 226 | 227 | === TEST 12: check if /memc was invoked (explicit "true" string) 228 | --- config 229 | location /memc { 230 | set $memc_cmd get; 231 | set $memc_key key; 232 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 233 | } 234 | --- request 235 | GET /memc 236 | --- response_body_like: 404 Not Found 237 | --- error_code: 404 238 | 239 | 240 | 241 | === TEST 13: flush all 242 | --- config 243 | location /flush { 244 | set $memc_cmd 'flush_all'; 245 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 246 | } 247 | --- request 248 | GET /flush 249 | --- response_body eval: "OK\r\n" 250 | 251 | 252 | 253 | === TEST 14: server-side config 254 | --- config 255 | srcache_store_skip 1; 256 | location /foo.txt { 257 | default_type text/css; 258 | srcache_store PUT /memc $uri; 259 | } 260 | 261 | location /memc { 262 | internal; 263 | 264 | set $memc_cmd set; 265 | set $memc_key key; 266 | set $memc_exptime 300; 267 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 268 | } 269 | --- user_files 270 | >>> foo.txt 271 | abc 272 | --- request 273 | GET /foo.txt 274 | --- response_body 275 | abc 276 | 277 | 278 | 279 | === TEST 15: check if /memc was invoked (server-level config) 280 | --- config 281 | location /memc { 282 | set $memc_cmd get; 283 | set $memc_key key; 284 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 285 | } 286 | --- request 287 | GET /memc 288 | --- response_body_like: 404 Not Found 289 | --- error_code: 404 290 | 291 | 292 | 293 | === TEST 16: flush all 294 | --- config 295 | location /flush { 296 | set $memc_cmd 'flush_all'; 297 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 298 | } 299 | --- request 300 | GET /flush 301 | --- response_body eval: "OK\r\n" 302 | 303 | 304 | 305 | === TEST 17: overriding server-side config 306 | --- config 307 | srcache_store_skip 1; 308 | location /foo.txt { 309 | default_type text/css; 310 | srcache_store PUT /memc $uri; 311 | srcache_store_skip 0; 312 | } 313 | 314 | location /memc { 315 | internal; 316 | 317 | set $memc_cmd set; 318 | set $memc_key key; 319 | set $memc_exptime 300; 320 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 321 | } 322 | --- user_files 323 | >>> foo.txt 201012240310.03 324 | hello, world 325 | --- request 326 | GET /foo.txt 327 | --- response_body 328 | hello, world 329 | 330 | 331 | 332 | === TEST 18: check if /memc was invoked (overriding server config) 333 | --- config 334 | location /memc { 335 | set $memc_cmd get; 336 | set $memc_key key; 337 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 338 | } 339 | --- request 340 | GET /memc 341 | --- response_body eval 342 | qr{HTTP/1.1 200 OK\r 343 | Content-Type: text/css\r 344 | Last-Modified: Fri, 24 Dec 2010 03:10:03 GMT\r 345 | X-SRCache-Allow-Ranges: 1\r 346 | (?:ETag: "4d140f0b-d"\r 347 | )?\r 348 | hello, world$} 349 | 350 | 351 | 352 | === TEST 19: check the response 353 | --- config 354 | location /foo.txt { 355 | default_type text/css; 356 | srcache_fetch GET /memc $uri; 357 | } 358 | 359 | location /memc { 360 | internal; 361 | 362 | set $memc_key key; 363 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 364 | } 365 | --- request 366 | GET /foo.txt 367 | --- response_headers 368 | Accept-Ranges: bytes 369 | Last-Modified: Fri, 24 Dec 2010 03:10:03 GMT 370 | Content-Length: 13 371 | --- response_body 372 | hello, world 373 | 374 | 375 | 376 | === TEST 20: flush all 377 | --- config 378 | location /flush { 379 | set $memc_cmd 'flush_all'; 380 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 381 | } 382 | --- request 383 | GET /flush 384 | --- response_body eval: "OK\r\n" 385 | 386 | 387 | 388 | === TEST 21: store_skip is true in the last minute 389 | --- config 390 | location /foo { 391 | default_type text/css; 392 | srcache_store PUT /memc $uri; 393 | set $skip ''; 394 | srcache_store_skip $skip; 395 | 396 | content_by_lua ' 397 | ngx.say("hello") 398 | ngx.say("world") 399 | ngx.var.skip = 1 400 | '; 401 | add_header X-Store-Status $srcache_store_status; 402 | } 403 | 404 | location /memc { 405 | internal; 406 | 407 | set $memc_cmd set; 408 | set $memc_key key; 409 | set $memc_exptime 300; 410 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 411 | } 412 | --- request 413 | GET /foo 414 | --- response_body 415 | hello 416 | world 417 | --- response_headers 418 | X-Store-Status: STORE 419 | --- error_log 420 | srcache_store skipped due to the true value in srcache_store_skip: "1" 421 | 422 | 423 | 424 | === TEST 22: check if /memc was invoked 425 | --- config 426 | location /memc { 427 | set $memc_cmd get; 428 | set $memc_key key; 429 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 430 | } 431 | --- request 432 | GET /memc 433 | --- response_body_like: 404 Not Found 434 | --- error_code: 404 435 | -------------------------------------------------------------------------------- /t/sub-req.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket skip_all => 'subrequests not working'; 5 | ; 6 | 7 | no_long_string(); 8 | #repeat_each(2); 9 | 10 | plan tests => repeat_each() * 2 * blocks(); 11 | 12 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 13 | 14 | run_tests(); 15 | 16 | __DATA__ 17 | 18 | === TEST 1: simple fetch 19 | --- config 20 | location /main { 21 | echo_location /pre /foo; 22 | echo_location /foo; 23 | echo_location /foo; 24 | } 25 | 26 | location /foo { 27 | srcache_fetch GET /memc $uri; 28 | 29 | echo $echo_incr; 30 | } 31 | 32 | location /pre { 33 | set $memc_cmd 'set'; 34 | set $memc_key $query_string; 35 | set $memc_value "hello\n"; 36 | set $memc_exptime 300; 37 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 38 | } 39 | 40 | location /memc { 41 | set $memc_key $query_string; 42 | set $memc_exptime 300; 43 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 44 | } 45 | --- request 46 | GET /main 47 | --- response_body eval 48 | "STORED\r 49 | hello 50 | hello 51 | " 52 | 53 | 54 | 55 | === TEST 2: simple fetch (without fetch) 56 | --- config 57 | location /main { 58 | echo_location /foo?1; 59 | echo_location /foo?2; 60 | } 61 | 62 | location /foo { 63 | srcache_store PUT /memc $uri; 64 | 65 | echo $echo_incr; 66 | } 67 | 68 | location /memc { 69 | internal; 70 | 71 | set $memc_key $query_string; 72 | set $memc_exptime 300; 73 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 74 | } 75 | --- request 76 | GET /main 77 | --- response_body 78 | 1 79 | 2 80 | 81 | 82 | 83 | === TEST 3: simple fetch (flush fetch) 84 | --- config 85 | location /main { 86 | echo_location /flush; 87 | echo_location /bar; 88 | echo_location /flush; 89 | echo_location /bar; 90 | } 91 | 92 | location /bar { 93 | srcache_fetch GET /memc $uri; 94 | srcache_store PUT /memc $uri; 95 | 96 | echo $echo_incr; 97 | } 98 | 99 | location /flush { 100 | internal; 101 | set $memc_cmd 'flush_all'; 102 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 103 | } 104 | 105 | location /memc { 106 | internal; 107 | 108 | set $memc_key $query_string; 109 | set $memc_exptime 300; 110 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 111 | } 112 | --- request 113 | GET /main 114 | --- response_body eval 115 | "OK\r 116 | 1 117 | OK\r 118 | 2 119 | " 120 | 121 | 122 | 123 | === TEST 4: fetch & store 124 | --- config 125 | location /main { 126 | echo_location /flush; 127 | echo_location /bar; 128 | echo_location /bar; 129 | echo_location /bar; 130 | } 131 | 132 | location /bar { 133 | srcache_fetch GET /memc $uri; 134 | srcache_store PUT /memc $uri; 135 | 136 | echo $echo_incr; 137 | } 138 | 139 | location /flush { 140 | internal; 141 | set $memc_cmd 'flush_all'; 142 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 143 | } 144 | 145 | location /memc { 146 | internal; 147 | 148 | set $memc_key $query_string; 149 | set $memc_exptime 300; 150 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 151 | } 152 | --- request 153 | GET /main 154 | --- response_body eval 155 | "OK\r 156 | 1 157 | 1 158 | 1 159 | " 160 | --- timeout: 2 161 | 162 | 163 | 164 | === TEST 5: fetch & store (mixture of echo_location & echo_location_async) 165 | --- config 166 | location /main { 167 | echo_location /flush; 168 | #echo_location /bar?; 169 | echo_location /group?a=1&b=2; 170 | echo_location_async /group?a=3&b=4; 171 | } 172 | 173 | location /group { 174 | #echo_location /bar; 175 | echo_location /bar $arg_a; 176 | echo_location_async /bar $arg_b; 177 | } 178 | 179 | location /bar { 180 | srcache_fetch GET /memc $uri; 181 | srcache_store PUT /memc $uri; 182 | 183 | echo $query_string; 184 | } 185 | 186 | location /flush { 187 | internal; 188 | set $memc_cmd 'flush_all'; 189 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 190 | } 191 | 192 | location /memc { 193 | #internal; 194 | 195 | set $memc_key $query_string; 196 | set $memc_exptime 300; 197 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 198 | } 199 | --- request 200 | GET /main 201 | --- response_body eval 202 | "OK\r 203 | 1 204 | 1 205 | 1 206 | 1 207 | " 208 | --- timeout: 2 209 | 210 | 211 | 212 | === TEST 6: deep nested echo_location/echo_location_async 213 | --- config 214 | location /main { 215 | echo_location /flush; 216 | echo_location /bar; 217 | echo_location_async /bar; 218 | echo_location_async /bar; 219 | echo_location /group; 220 | echo_location_async /group; 221 | } 222 | 223 | location /group { 224 | echo_location /bar; 225 | echo_location_async /bar; 226 | } 227 | 228 | location /bar { 229 | srcache_fetch GET /memc $uri; 230 | srcache_store PUT /memc $uri; 231 | 232 | echo $echo_incr; 233 | } 234 | 235 | location /flush { 236 | internal; 237 | set $memc_cmd 'flush_all'; 238 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 239 | } 240 | 241 | location /memc { 242 | internal; 243 | 244 | set $memc_key $query_string; 245 | set $memc_exptime 300; 246 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 247 | } 248 | --- request 249 | GET /main 250 | --- response_body eval 251 | "OK\r 252 | 1 253 | 1 254 | 1 255 | 1 256 | 1 257 | 1 258 | 1 259 | " 260 | --- timeout: 2 261 | --- SKIP 262 | 263 | 264 | 265 | === TEST 7: deep nested pure echo_location 266 | --- config 267 | location /main { 268 | echo_location /flush; 269 | echo_location /bar; 270 | echo_location /bar; 271 | echo_location /bar; 272 | echo_location /group; 273 | echo_location /group; 274 | } 275 | 276 | location /group { 277 | echo_location /bar; 278 | echo_location /bar; 279 | } 280 | 281 | location /bar { 282 | srcache_fetch GET /memc $uri; 283 | srcache_store PUT /memc $uri; 284 | 285 | echo $echo_incr; 286 | } 287 | 288 | location /flush { 289 | internal; 290 | set $memc_cmd 'flush_all'; 291 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 292 | } 293 | 294 | location /memc { 295 | internal; 296 | 297 | set $memc_key $query_string; 298 | set $memc_exptime 300; 299 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 300 | } 301 | --- request 302 | GET /main 303 | --- response_body eval 304 | "OK\r 305 | 1 306 | 1 307 | 1 308 | 1 309 | 1 310 | 1 311 | 1 312 | " 313 | --- timeout: 2 314 | 315 | 316 | 317 | === TEST 8: deep nested echo_location/echo_location_async 318 | --- config 319 | location /main { 320 | echo_location /flush; 321 | echo_location /bar; 322 | echo_location_async /bar; 323 | echo_location_async /bar; 324 | echo_location_async /group; 325 | echo_location_async /group; 326 | } 327 | 328 | location /group { 329 | echo_location_async /bar; 330 | echo_location_async /bar; 331 | } 332 | 333 | location /bar { 334 | srcache_fetch GET /memc $uri; 335 | srcache_store PUT /memc $uri; 336 | 337 | echo $echo_incr; 338 | } 339 | 340 | location /flush { 341 | internal; 342 | set $memc_cmd 'flush_all'; 343 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 344 | } 345 | 346 | location /memc { 347 | internal; 348 | 349 | set $memc_key $query_string; 350 | set $memc_exptime 300; 351 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 352 | } 353 | --- request 354 | GET /main 355 | --- response_body eval 356 | "OK\r 357 | 1 358 | 1 359 | 1 360 | 1 361 | 1 362 | 1 363 | 1 364 | " 365 | --- timeout: 2 366 | 367 | 368 | 369 | === TEST 9: deep nested echo_location/echo_location_async 370 | --- config 371 | location /main { 372 | echo_location /flush; 373 | echo_location /bar; 374 | echo_location_async /bar; 375 | echo_location /group; 376 | } 377 | 378 | location /group { 379 | echo_location /bar; 380 | echo_location_async /bar; 381 | } 382 | 383 | location /bar { 384 | srcache_fetch GET /memc $uri; 385 | srcache_store PUT /memc $uri; 386 | 387 | echo $echo_incr; 388 | } 389 | 390 | location /flush { 391 | internal; 392 | set $memc_cmd 'flush_all'; 393 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 394 | } 395 | 396 | location /memc { 397 | internal; 398 | 399 | set $memc_key $query_string; 400 | set $memc_exptime 300; 401 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 402 | } 403 | --- request 404 | GET /main 405 | --- response_body eval 406 | "OK\r 407 | 1 408 | 1 409 | 1 410 | 1 411 | " 412 | --- timeout: 1 413 | 414 | 415 | 416 | === TEST 10: simple store 417 | --- config 418 | location /main { 419 | echo_location /flush; 420 | echo_location /bar; 421 | } 422 | 423 | location /bar { 424 | srcache_fetch GET /memc $uri; 425 | srcache_store PUT /memc $uri; 426 | 427 | echo $echo_incr; 428 | } 429 | 430 | location /flush { 431 | internal; 432 | set $memc_cmd 'flush_all'; 433 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 434 | } 435 | 436 | location /memc { 437 | internal; 438 | 439 | set $memc_key $query_string; 440 | set $memc_exptime 300; 441 | memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; 442 | } 443 | --- request 444 | GET /main 445 | --- response_body eval 446 | "OK\r 447 | 1 448 | " 449 | -------------------------------------------------------------------------------- /t/timeout.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (4 * blocks() - 2); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_shuffle(); 14 | 15 | run_tests(); 16 | 17 | __DATA__ 18 | 19 | === TEST 1: basic fetch (cache miss) 20 | --- config 21 | error_page 500 502 503 504 /50x.html; 22 | location = /50x.html { 23 | root html; 24 | } 25 | 26 | location = /foo { 27 | srcache_fetch GET /memc $uri; 28 | srcache_store PUT /memc $uri; 29 | 30 | proxy_pass http://127.0.0.1:$server_port/hello; 31 | } 32 | 33 | location = /hello { 34 | echo hello; 35 | default_type text/css; 36 | } 37 | 38 | memc_connect_timeout 1ms; 39 | location = /memc { 40 | internal; 41 | 42 | set $memc_key $query_string; 43 | set $memc_exptime 300; 44 | memc_pass www.google.com:1234; 45 | } 46 | --- user_files 47 | >>> 50x.html 48 | bad bad 49 | --- request 50 | GET /foo 51 | --- response_headers 52 | Content-Type: text/css 53 | !Content-Length 54 | --- response_body 55 | hello 56 | 57 | 58 | 59 | === TEST 2: main req upstream truncation (with content-length) 60 | --- config 61 | location = /t { 62 | srcache_store PUT /err; 63 | 64 | proxy_read_timeout 100ms; 65 | proxy_pass http://127.0.0.1:11945/echo; 66 | } 67 | 68 | location = /err { 69 | content_by_lua ' 70 | ngx.log(ngx.ERR, "location /err is called") 71 | '; 72 | } 73 | 74 | --- tcp_listen: 11945 75 | --- tcp_no_close 76 | --- tcp_reply eval 77 | "HTTP/1.0 200 OK\r\nContent-Length: 120\r\n\r\nhello world" 78 | 79 | --- request 80 | GET /t 81 | --- ignore_response 82 | --- no_error_log 83 | location /err is called 84 | --- no_error_log 85 | srcache_store: subrequest returned status 86 | 87 | 88 | 89 | === TEST 3: main req upstream truncation (without content-length) 90 | --- config 91 | location = /t { 92 | srcache_store PUT /err; 93 | 94 | proxy_read_timeout 100ms; 95 | proxy_pass http://127.0.0.1:11945/echo; 96 | } 97 | 98 | location = /err { 99 | content_by_lua ' 100 | ngx.log(ngx.ERR, "location /err is called") 101 | '; 102 | } 103 | 104 | --- tcp_listen: 11945 105 | --- tcp_no_close 106 | --- tcp_reply eval 107 | "HTTP/1.0 200 OK\r\n\r\nhello world" 108 | 109 | --- request 110 | GET /t 111 | --- ignore_response 112 | --- no_error_log 113 | location /err is called 114 | srcache_store: subrequest returned status 115 | [alert] 116 | [crit] 117 | 118 | --- error_log 119 | upstream timed out 120 | -------------------------------------------------------------------------------- /t/unused.t: -------------------------------------------------------------------------------- 1 | # vi:filetype= 2 | 3 | use lib 'lib'; 4 | use Test::Nginx::Socket; 5 | 6 | #repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 11 | 12 | #master_on(); 13 | no_long_string(); 14 | no_shuffle(); 15 | 16 | run_tests(); 17 | 18 | __DATA__ 19 | 20 | === TEST 1: module used 21 | --- config 22 | location /foo { 23 | srcache_store PUT /store; 24 | echo hello; 25 | } 26 | 27 | location /store { 28 | echo stored; 29 | } 30 | --- request 31 | GET /foo 32 | --- stap 33 | F(ngx_http_srcache_header_filter) { 34 | println("srcache header filter called") 35 | } 36 | 37 | F(ngx_http_srcache_access_handler) { 38 | println("srcache access handler called") 39 | } 40 | 41 | --- stap_out 42 | srcache access handler called 43 | srcache access handler called 44 | srcache header filter called 45 | srcache header filter called 46 | --- response_body 47 | hello 48 | 49 | 50 | 51 | === TEST 2: module unused 52 | --- config 53 | location /foo { 54 | #srcache_store PUT /store; 55 | echo hello; 56 | } 57 | 58 | location /store { 59 | echo stored; 60 | } 61 | --- request 62 | GET /foo 63 | --- stap 64 | F(ngx_http_srcache_header_filter) { 65 | println("srcache header filter called") 66 | } 67 | 68 | F(ngx_http_srcache_access_handler) { 69 | println("srcache access handler called") 70 | } 71 | 72 | --- stap_out 73 | --- response_body 74 | hello 75 | 76 | 77 | 78 | === TEST 3: module used (multiple http {} blocks) 79 | This test case won't run with nginx 1.9.3+ since duplicate http {} blocks 80 | have been prohibited since then. 81 | --- SKIP 82 | --- config 83 | location /foo { 84 | srcache_store PUT /store; 85 | echo hello; 86 | } 87 | 88 | location /store { 89 | echo stored; 90 | } 91 | 92 | --- post_main_config 93 | http { 94 | } 95 | 96 | --- request 97 | GET /foo 98 | --- stap 99 | F(ngx_http_srcache_header_filter) { 100 | println("srcache header filter called") 101 | } 102 | 103 | F(ngx_http_srcache_access_handler) { 104 | println("srcache access handler called") 105 | } 106 | 107 | --- stap_out 108 | srcache access handler called 109 | srcache access handler called 110 | srcache header filter called 111 | srcache header filter called 112 | --- response_body 113 | hello 114 | -------------------------------------------------------------------------------- /util/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # this file is only meant to be used by the module developers. 4 | 5 | root=`pwd` 6 | version=$1 7 | home=~ 8 | force=$2 9 | 10 | ngx_redis_path=$home/work/nginx/ngx_http_redis 11 | 12 | ngx-build $force $version \ 13 | --with-cc-opt="-O0" \ 14 | --with-ld-opt="-Wl,-rpath,/opt/postgres/lib:/opt/drizzle/lib:/usr/local/lib:/home/lz/lib" \ 15 | --without-mail_pop3_module \ 16 | --without-mail_imap_module \ 17 | --without-mail_smtp_module \ 18 | --without-http_upstream_ip_hash_module \ 19 | --without-http_empty_gif_module \ 20 | --without-http_referer_module \ 21 | --without-http_autoindex_module \ 22 | --without-http_auth_basic_module \ 23 | --without-http_userid_module \ 24 | --add-module=$root/../ndk-nginx-module \ 25 | --add-module=$root/../set-misc-nginx-module \ 26 | --add-module=$ngx_redis_path \ 27 | --add-module=$root/../xss-nginx-module \ 28 | --add-module=$root/../redis2-nginx-module \ 29 | --add-module=$root/../eval-nginx-module \ 30 | --add-module=$root/../echo-nginx-module \ 31 | --add-module=$root/../headers-more-nginx-module \ 32 | --add-module=$root $opts \ 33 | --add-module=$root/../lua-nginx-module \ 34 | --add-module=$root/../rds-json-nginx-module \ 35 | --add-module=$root/../drizzle-nginx-module \ 36 | --add-module=$root/../postgres-nginx-module \ 37 | --add-module=$root/../memc-nginx-module \ 38 | --add-module=$home/work/nginx/ngx_http_upstream_keepalive-0.7 \ 39 | --with-select_module \ 40 | --with-poll_module \ 41 | --without-http_ssi_module \ 42 | --with-debug 43 | #--add-module=/home/agentz/git/dodo/utils/dodo-hook \ 44 | #--add-module=$home/work/ngx_http_auth_request-0.1 #\ 45 | #--with-rtsig_module 46 | #--with-cc-opt="-g3 -O0" 47 | #--add-module=$root/../echo-nginx-module \ 48 | 49 | -------------------------------------------------------------------------------- /valgrind.suppress: -------------------------------------------------------------------------------- 1 | { 2 | 3 | Memcheck:Addr4 4 | fun:ngx_init_cycle 5 | fun:ngx_master_process_cycle 6 | fun:main 7 | } 8 | { 9 | 10 | Memcheck:Param 11 | epoll_ctl(event) 12 | fun:epoll_ctl 13 | } 14 | { 15 | 16 | Memcheck:Cond 17 | fun:memcpy 18 | fun:ngx_vslprintf 19 | fun:ngx_log_error_core 20 | fun:ngx_http_charset_header_filter 21 | } 22 | { 23 | 24 | exp-sgcheck:SorG 25 | fun:ngx_http_variables_init_vars 26 | fun:ngx_http_block 27 | } 28 | { 29 | 30 | exp-sgcheck:SorG 31 | fun:ngx_conf_parse 32 | } 33 | { 34 | 35 | exp-sgcheck:SorG 36 | fun:ngx_vslprintf 37 | fun:ngx_log_error_core 38 | } 39 | { 40 | 41 | exp-sgcheck:SorG 42 | fun:ngx_conf_parse 43 | fun:ngx_http_core_location 44 | } 45 | { 46 | 47 | Memcheck:Param 48 | socketcall.setsockopt(optval) 49 | fun:setsockopt 50 | fun:drizzle_state_connect 51 | } 52 | { 53 | 54 | Memcheck:Cond 55 | fun:ngx_conf_flush_files 56 | } 57 | { 58 | 59 | Memcheck:Leak 60 | fun:malloc 61 | fun:ngx_alloc 62 | fun:ngx_event_process_init 63 | } 64 | { 65 | 66 | Memcheck:Cond 67 | fun:index 68 | fun:expand_dynamic_string_token 69 | fun:_dl_map_object 70 | fun:map_doit 71 | fun:_dl_catch_error 72 | fun:do_preload 73 | fun:dl_main 74 | } 75 | { 76 | 77 | Memcheck:Leak 78 | match-leak-kinds: definite 79 | fun:malloc 80 | fun:ngx_alloc 81 | fun:ngx_set_environment 82 | fun:ngx_single_process_cycle 83 | } 84 | { 85 | 86 | Memcheck:Leak 87 | match-leak-kinds: definite 88 | fun:malloc 89 | fun:ngx_alloc 90 | fun:ngx_set_environment 91 | fun:ngx_worker_process_init 92 | fun:ngx_worker_process_cycle 93 | } 94 | --------------------------------------------------------------------------------