pax_global_header00006660000000000000000000000064145430407050014514gustar00rootroot0000000000000052 comment=b889a6fc3e18b784b454e1bc74e5d0b3513a07ce memc-nginx-module-0.20/000077500000000000000000000000001454304070500147625ustar00rootroot00000000000000memc-nginx-module-0.20/.gitattributes000066400000000000000000000000331454304070500176510ustar00rootroot00000000000000*.t linguist-language=Text memc-nginx-module-0.20/.gitignore000066400000000000000000000007411454304070500167540ustar00rootroot00000000000000.libs *.t_ *.swp *.slo *.la *.swo *.lo *~ *.o print.txt .rsync *.tar.gz dist build[789] build tags update-readme *.tmp test/Makefile test/blib test.sh t.sh t/t.sh test/t/servroot/ releng reset *.t_ genmobi.sh *.mobi misc/chunked reindex src/response.c src/handler.h src/util.c src/module.c src/handler.c src/response.rl src/response.h src/module.h src/util.h src/request.h src/request.c ctags nginx *.html t/servroot/ all Changes build1[0-9] go buildroot/ analyze *.plist Makefile memc-nginx-module-0.20/.travis.yml000066400000000000000000000043741454304070500171030ustar00rootroot00000000000000sudo: required dist: bionic os: linux language: c compiler: - gcc addons: apt: packages: [ axel, cpanminus, libtest-base-perl, libtext-diff-perl, liburi-perl, libwww-perl, libtest-longstring-perl, liblist-moreutils-perl ] cache: apt: true env: global: - LUAJIT_PREFIX=/opt/luajit21 - LUAJIT_LIB=$LUAJIT_PREFIX/lib - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1 - LUA_INCLUDE_DIR=$LUAJIT_INC - LUA_CMODULE_DIR=/lib - JOBS=3 - NGX_BUILD_JOBS=$JOBS matrix: - NGINX_VERSION=1.17.8 - NGINX_VERSION=1.19.9 services: - memcache install: - git clone https://github.com/openresty/nginx-devel-utils.git - git clone https://github.com/openresty/openresty.git ../openresty - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module - git clone https://github.com/openresty/test-nginx.git - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache - git clone https://github.com/openresty/nginx-eval-module.git ../eval-nginx-module - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module - git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module script: - sudo iptables -I OUTPUT -p tcp --dst 127.0.0.2 --dport 12345 -j DROP - sudo iptables -I OUTPUT -p udp --dst 127.0.0.2 --dport 12345 -j DROP - cd luajit2 - 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) - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1) - cd .. - perlbrew off && cd test-nginx && sudo cpanm . && cd .. - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH - export NGX_BUILD_CC=$CC - sh util/build.sh $NGINX_VERSION > build.log 2>&1 || (cat build.log && exit 1) - nginx -V - ldd `which nginx`|grep luajit - prove -I. -r t memc-nginx-module-0.20/README.markdown000066400000000000000000000657351454304070500175030ustar00rootroot00000000000000Name ==== **ngx_memc** - An extended version of the standard memcached module that supports set, add, delete, and many more memcached commands. *This module is not distributed with the Nginx source.* See [the installation instructions](#installation). Table of Contents ================= * [Version](#version) * [Synopsis](#synopsis) * [Description](#description) * [Keep-alive connections to memcached servers](#keep-alive-connections-to-memcached-servers) * [How it works](#how-it-works) * [Memcached commands supported](#memcached-commands-supported) * [get $memc_key](#get-memc_key) * [set $memc_key $memc_flags $memc_exptime $memc_value](#set-memc_key-memc_flags-memc_exptime-memc_value) * [add $memc_key $memc_flags $memc_exptime $memc_value](#add-memc_key-memc_flags-memc_exptime-memc_value) * [replace $memc_key $memc_flags $memc_exptime $memc_value](#replace-memc_key-memc_flags-memc_exptime-memc_value) * [append $memc_key $memc_flags $memc_exptime $memc_value](#append-memc_key-memc_flags-memc_exptime-memc_value) * [prepend $memc_key $memc_flags $memc_exptime $memc_value](#prepend-memc_key-memc_flags-memc_exptime-memc_value) * [delete $memc_key](#delete-memc_key) * [delete $memc_key $memc_exptime](#delete-memc_key-memc_exptime) * [incr $memc_key $memc_value](#incr-memc_key-memc_value) * [decr $memc_key $memc_value](#decr-memc_key-memc_value) * [flush_all](#flush_all) * [flush_all $memc_exptime](#flush_all-memc_exptime) * [stats](#stats) * [version](#version) * [Directives](#directives) * [memc_pass](#memc_pass) * [memc_cmds_allowed](#memc_cmds_allowed) * [memc_flags_to_last_modified](#memc_flags_to_last_modified) * [memc_connect_timeout](#memc_connect_timeout) * [memc_send_timeout](#memc_send_timeout) * [memc_read_timeout](#memc_read_timeout) * [memc_buffer_size](#memc_buffer_size) * [memc_ignore_client_abort](#memc_ignore_client_abort) * [Installation](#installation) * [For Developers](#for-developers) * [Compatibility](#compatibility) * [Community](#community) * [English Mailing List](#english-mailing-list) * [Chinese Mailing List](#chinese-mailing-list) * [Report Bugs](#report-bugs) * [Source Repository](#source-repository) * [Changes](#changes) * [Test Suite](#test-suite) * [TODO](#todo) * [Getting involved](#getting-involved) * [Author](#author) * [Copyright & License](#copyright--license) * [See Also](#see-also) Version ======= This document describes ngx_memc [v0.19](http://github.com/openresty/memc-nginx-module/tags) released on 19 April 2018. Synopsis ======== ```nginx # GET /foo?key=dog # # POST /foo?key=cat # Cat's value... # # PUT /foo?key=bird # Bird's value... # # DELETE /foo?key=Tiger location /foo { set $memc_key $arg_key; # $memc_cmd defaults to get for GET, # add for POST, set for PUT, and # delete for the DELETE request method. memc_pass 127.0.0.1:11211; } ``` ```nginx # GET /bar?cmd=get&key=cat # # POST /bar?cmd=set&key=dog # My value for the "dog" key... # # DELETE /bar?cmd=delete&key=dog # GET /bar?cmd=delete&key=dog location /bar { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_flags $arg_flags; # defaults to 0 set $memc_exptime $arg_exptime; # defaults to 0 memc_pass 127.0.0.1:11211; } ``` ```nginx # GET /bar?cmd=get&key=cat # GET /bar?cmd=set&key=dog&val=animal&flags=1234&exptime=2 # GET /bar?cmd=delete&key=dog # GET /bar?cmd=flush_all location /bar { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_flags $arg_flags; # defaults to 0 set $memc_exptime $arg_exptime; # defaults to 0 memc_cmds_allowed get set add delete flush_all; memc_pass 127.0.0.1:11211; } ``` ```nginx http { ... upstream backend { server 127.0.0.1:11984; server 127.0.0.1:11985; } server { location /stats { set $memc_cmd stats; memc_pass backend; } ... } } ... ``` ```nginx # read the memcached flags into the Last-Modified header # to respond 304 to conditional GET location /memc { set $memc_key $arg_key; memc_pass 127.0.0.1:11984; memc_flags_to_last_modified on; } ``` ```nginx location /memc { set $memc_key foo; set $memc_cmd get; # access the unix domain socket listend by memcached memc_pass unix:/tmp/memcached.sock; } ``` Description =========== This module extends the standard [memcached module](http://nginx.org/en/docs/http/ngx_http_memcached_module.html) to support almost the whole [memcached ascii protocol](http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt). It allows you to define a custom [REST](http://en.wikipedia.org/wiki/REST) interface to your memcached servers or access memcached in a very efficient way from within the nginx server by means of subrequests or [independent fake requests](http://github.com/srlindsay/nginx-independent-subrequest). This module is not supposed to be merged into the Nginx core because I've used [Ragel](http://www.complang.org/ragel/) to generate the memcached response parsers (in C) for joy :) If you are going to use this module to cache location responses out of the box, try [srcache-nginx-module](http://github.com/openresty/srcache-nginx-module) with this module to achieve that. When used in conjunction with [lua-nginx-module](http://github.com/openresty/lua-nginx-module), it is recommended to use the [lua-resty-memcached](http://github.com/openresty/lua-resty-memcached) library instead of this module though, because the former is much more flexible and memory-efficient. [Back to TOC](#table-of-contents) Keep-alive connections to memcached servers ------------------------------------------- You need [HttpUpstreamKeepaliveModule](http://wiki.nginx.org/HttpUpstreamKeepaliveModule) together with this module for keep-alive TCP connections to your backend memcached servers. Here's a sample configuration: ```nginx http { upstream backend { server 127.0.0.1:11211; # a pool with at most 1024 connections # and do not distinguish the servers: keepalive 1024; } server { ... location /memc { set $memc_cmd get; set $memc_key $arg_key; memc_pass backend; } } } ``` [Back to TOC](#table-of-contents) How it works ------------ It implements the memcached TCP protocol all by itself, based upon the `upstream` mechanism. Everything involving I/O is non-blocking. The module itself does not keep TCP connections to the upstream memcached servers across requests, just like other upstream modules. For a working solution, see section [Keep-alive connections to memcached servers](#keep-alive-connections-to-memcached-servers). [Back to TOC](#table-of-contents) Memcached commands supported ============================ The memcached storage commands [set](#set-memc_key-memc_flags-memc_exptime-memc_value), [add](#add-memc_key-memc_flags-memc_exptime-memc_value), [replace](#replace-memc_key-memc_flags-memc_exptime-memc_value), [prepend](#prepend-memc_key-memc_flags-memc_exptime-memc_value), and [append](#append-memc_key-memc_flags-memc_exptime-memc_value) uses the `$memc_key` as the key, `$memc_exptime` as the expiration time (or delay) (defaults to 0), `$memc_flags` as the flags (defaults to 0), to build the corresponding memcached queries. If `$memc_value` is not defined at all, then the request body will be used as the value of the `$memc_value` except for the [incr](#incr-memc_key-memc_value) and [decr](#decr-memc_key-memc_value) commands. Note that if `$memc_value` is defined as an empty string (`""`), that empty string will still be used as the value as is. The following memcached commands have been implemented and tested (with their parameters marked by corresponding nginx variables defined by this module): [Back to TOC](#table-of-contents) get $memc_key ------------- Retrieves the value using a key. ```nginx location /foo { set $memc_cmd 'get'; set $memc_key 'my_key'; memc_pass 127.0.0.1:11211; add_header X-Memc-Flags $memc_flags; } ``` Returns `200 OK` with the value put into the response body if the key is found, or `404 Not Found` otherwise. The `flags` number will be set into the `$memc_flags` variable so it's often desired to put that info into the response headers by means of the standard [add_header directive](http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header). It returns `502` for `ERROR`, `CLIENT_ERROR`, or `SERVER_ERROR`. [Back to TOC](#table-of-contents) set $memc_key $memc_flags $memc_exptime $memc_value --------------------------------------------------- To use the request body as the memcached value, just avoid setting the `$memc_value` variable: ```nginx # POST /foo # my value... location /foo { set $memc_cmd 'set'; set $memc_key 'my_key'; set $memc_flags 12345; set $memc_exptime 24; memc_pass 127.0.0.1:11211; } ``` Or let the `$memc_value` hold the value: ```nginx location /foo { set $memc_cmd 'set'; set $memc_key 'my_key'; set $memc_flags 12345; set $memc_exptime 24; set $memc_value 'my_value'; memc_pass 127.0.0.1:11211; } ``` Returns `201 Created` if the upstream memcached server replies `STORED`, `200` for `NOT_STORED`, `404` for `NOT_FOUND`, `502` for `ERROR`, `CLIENT_ERROR`, or `SERVER_ERROR`. The original memcached responses are returned as the response body except for `404 NOT FOUND`. [Back to TOC](#table-of-contents) add $memc_key $memc_flags $memc_exptime $memc_value --------------------------------------------------- Similar to the [set command](#set-memc_key-memc_flags-memc_exptime-memc_value). [Back to TOC](#table-of-contents) replace $memc_key $memc_flags $memc_exptime $memc_value ------------------------------------------------------- Similar to the [set command](#set-memc_key-memc_flags-memc_exptime-memc_value). [Back to TOC](#table-of-contents) append $memc_key $memc_flags $memc_exptime $memc_value ------------------------------------------------------ Similar to the [set command](#set-memc_key-memc_flags-memc_exptime-memc_value). Note that at least memcached version 1.2.2 does not support the "append" and "prepend" commands. At least 1.2.4 and later versions seem to supports these two commands. [Back to TOC](#table-of-contents) prepend $memc_key $memc_flags $memc_exptime $memc_value ------------------------------------------------------- Similar to the [append command](#append-memc_key-memc_flags-memc_exptime-memc_value). [Back to TOC](#table-of-contents) delete $memc_key ---------------- Deletes the memcached entry using a key. ```nginx location /foo set $memc_cmd delete; set $memc_key my_key; memc_pass 127.0.0.1:11211; } ``` Returns `200 OK` if deleted successfully, `404 Not Found` for `NOT_FOUND`, or `502` for `ERROR`, `CLIENT_ERROR`, or `SERVER_ERROR`. The original memcached responses are returned as the response body except for `404 NOT FOUND`. [Back to TOC](#table-of-contents) delete $memc_key $memc_exptime ------------------------------ Similar to the [delete $memc_key](#delete-memc_key) command except it accepts an optional `expiration` time specified by the `$memc_exptime` variable. This command is no longer available in the latest memcached version 1.4.4. [Back to TOC](#table-of-contents) incr $memc_key $memc_value -------------------------- Increments the existing value of `$memc_key` by the amount specified by `$memc_value`: ```nginx location /foo { set $memc_cmd incr; set $memc_key my_key; set $memc_value 2; memc_pass 127.0.0.1:11211; } ``` In the preceding example, every time we access `/foo` will cause the value of `my_key` increments by `2`. Returns `200 OK` with the new value associated with that key as the response body if successful, or `404 Not Found` if the key is not found. It returns `502` for `ERROR`, `CLIENT_ERROR`, or `SERVER_ERROR`. [Back to TOC](#table-of-contents) decr $memc_key $memc_value -------------------------- Similar to [incr $memc_key $memc_value](#incr-memc_key-memc_value). [Back to TOC](#table-of-contents) flush_all --------- Mark all the keys on the memcached server as expired: ```nginx location /foo { set $memc_cmd flush_all; memc_pass 127.0.0.1:11211; } ``` [Back to TOC](#table-of-contents) flush_all $memc_exptime ----------------------- Just like [flush_all](#flush_all) but also accepts an expiration time specified by the `$memc_exptime` variable. [Back to TOC](#table-of-contents) stats ----- Causes the memcached server to output general-purpose statistics and settings ```nginx location /foo { set $memc_cmd stats; memc_pass 127.0.0.1:11211; } ``` Returns `200 OK` if the request succeeds, or 502 for `ERROR`, `CLIENT_ERROR`, or `SERVER_ERROR`. The raw `stats` command output from the upstream memcached server will be put into the response body. [Back to TOC](#table-of-contents) version ------- Queries the memcached server's version number: ```nginx location /foo { set $memc_cmd version; memc_pass 127.0.0.1:11211; } ``` Returns `200 OK` if the request succeeds, or 502 for `ERROR`, `CLIENT_ERROR`, or `SERVER_ERROR`. The raw `version` command output from the upstream memcached server will be put into the response body. [Back to TOC](#table-of-contents) Directives ========== All the standard [memcached module](http://nginx.org/en/docs/http/ngx_http_memcached_module.html) directives in nginx 0.8.28 are directly inherited, with the `memcached_` prefixes replaced by `memc_`. For example, the `memcached_pass` directive is spelled `memc_pass`. Here we only document the most important two directives (the latter is a new directive introduced by this module). [Back to TOC](#table-of-contents) memc_pass --------- **syntax:** *memc_pass <memcached server IP address>:<memcached server port>* **syntax:** *memc_pass <memcached server hostname>:<memcached server port>* **syntax:** *memc_pass <upstream_backend_name>* **syntax:** *memc_pass unix:<path_to_unix_domain_socket>* **default:** *none* **context:** *http, server, location, if* **phase:** *content* Specify the memcached server backend. [Back to TOC](#table-of-contents) memc_cmds_allowed ----------------- **syntax:** *memc_cmds_allowed <cmd>...* **default:** *none* **context:** *http, server, location, if* Lists memcached commands that are allowed to access. By default, all the memcached commands supported by this module are accessible. An example is ```nginx location /foo { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:11211; memc_cmds_allowed get; } ``` [Back to TOC](#table-of-contents) memc_flags_to_last_modified --------------------------- **syntax:** *memc_flags_to_last_modified on|off* **default:** *off* **context:** *http, server, location, if* Read the memcached flags as epoch seconds and set it as the value of the `Last-Modified` header. For conditional GET, it will signal nginx to return `304 Not Modified` response to save bandwidth. [Back to TOC](#table-of-contents) memc_connect_timeout -------------------- **syntax:** *memc_connect_timeout <time>* **default:** *60s* **context:** *http, server, location* The timeout for connecting to the memcached server, in seconds by default. It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years), "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes). This time must be less than 597 hours. [Back to TOC](#table-of-contents) memc_send_timeout ----------------- **syntax:** *memc_send_timeout <time>* **default:** *60s* **context:** *http, server, location* The timeout for sending TCP requests to the memcached server, in seconds by default. It is wise to always explicitly specify the time unit to avoid confusion. Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years), "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes). This time must be less than 597 hours. [Back to TOC](#table-of-contents) memc_read_timeout ----------------- **syntax:** *memc_read_timeout <time>* **default:** *60s* **context:** *http, server, location* The timeout for reading TCP responses from the memcached server, in seconds by default. It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years), "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes). This time must be less than 597 hours. [Back to TOC](#table-of-contents) memc_buffer_size ---------------- **syntax:** *memc_buffer_size <size>* **default:** *4k/8k* **context:** *http, server, location* This buffer size is used for the memory buffer to hold * the complete response for memcached commands other than `get`, * the complete response header (i.e., the first line of the response) for the `get` memcached command. This default size is the page size, may be `4k` or `8k`. [Back to TOC](#table-of-contents) memc_ignore_client_abort ------------------------ **syntax:** *memc_ignore_client_abort on|off* **default:** *off* **context:** *location* Determines whether the connection with a memcache server should be closed when a client closes a connection without waiting for a response. This directive was first added in the `v0.14` release. [Back to TOC](#table-of-contents) Installation ============ You're recommended to install this module (as well as the Nginx core and many other goodies) via the [OpenResty bundle](http://openresty.org). See the [installation steps](http://openresty.org/#Installation) for `OpenResty`. Alternatively, you can compile this module into the standard Nginx source distribution by hand: Grab the nginx source code from [nginx.org](http://nginx.org/), for example, the version 1.13.6 (see [nginx compatibility](#compatibility)), and then build the source with this module: ```bash wget 'http://nginx.org/download/nginx-1.13.6.tar.gz' tar -xzvf nginx-1.13.6.tar.gz cd nginx-1.13.6/ # Here we assume you would install you nginx under /opt/nginx/. ./configure --prefix=/opt/nginx \ --add-module=/path/to/memc-nginx-module make -j2 make install ``` Download the latest version of the release tarball of this module from [memc-nginx-module file list](http://github.com/openresty/memc-nginx-module/tags). Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the `--add-dynamic-module=PATH` option instead of `--add-module=PATH` on the `./configure` command line above. And then you can explicitly load the module in your `nginx.conf` via the [load_module](http://nginx.org/en/docs/ngx_core_module.html#load_module) directive, for example, ```nginx load_module /path/to/modules/ngx_http_memc_module.so; ``` [Back to TOC](#table-of-contents) For Developers -------------- The memached response parsers were generated by [Ragel](http://www.complang.org/ragel/). If you want to regenerate the parser's C file, i.e., [src/ngx_http_memc_response.c](http://github.com/openresty/memc-nginx-module/blob/master/src/ngx_http_memc_response.c), use the following command from the root of the memc module's source tree: ```bash $ ragel -G2 src/ngx_http_memc_response.rl ``` [Back to TOC](#table-of-contents) Compatibility ============= The following versions of Nginx should work with this module: * **1.17.x** (last tested: 1.17.4) * **1.16.x** * **1.15.x** (last tested: 1.15.8) * **1.14.x** * **1.13.x** (last tested: 1.13.6) * **1.12.x** * **1.11.x** (last tested: 1.11.2) * **1.10.x** * **1.9.x** (last tested: 1.9.15) * **1.8.x** * **1.7.x** (last tested: 1.7.10) * **1.6.x** * **1.5.x** (last tested: 1.5.12) * **1.4.x** (last tested: 1.4.4) * **1.2.x** (last tested: 1.2.9) * **1.1.x** (last tested: 1.1.5) * **1.0.x** (last tested: 1.0.10) * **0.9.x** (last tested: 0.9.4) * **0.8.x** (last tested: 0.8.54) * **0.7.x >= 0.7.46** (last tested: 0.7.68) It's worth mentioning that some 0.7.x versions older than 0.7.46 might also work, but I can't easily test them because the test suite makes extensive use of the [echo module](http://github.com/openresty/echo-nginx-module)'s [echo_location directive](http://github.com/openresty/echo-nginx-module#echo_location), which requires at least nginx 0.7.46 :) Earlier versions of Nginx like 0.6.x and 0.5.x will *not* work. If you find that any particular version of Nginx above 0.7.46 does not work with this module, please consider [reporting a bug](#report-bugs). [Back to TOC](#table-of-contents) Community ========= [Back to TOC](#table-of-contents) English Mailing List -------------------- The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers. [Back to TOC](#table-of-contents) Chinese Mailing List -------------------- The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers. [Back to TOC](#table-of-contents) Report Bugs =========== Although a lot of effort has been put into testing and code tuning, there must be some serious bugs lurking somewhere in this module. So whenever you are bitten by any quirks, please don't hesitate to 1. create a ticket on the [issue tracking interface](http://github.com/openresty/memc-nginx-module/issues) provided by GitHub, 1. or send a bug report or even patches to the [nginx mailing list](http://mailman.nginx.org/mailman/listinfo/nginx). [Back to TOC](#table-of-contents) Source Repository ================= Available on github at [openresty/memc-nginx-module](http://github.com/openresty/memc-nginx-module). [Back to TOC](#table-of-contents) Changes ======= The changes of every release of this module can be obtained from the OpenResty bundle's change logs: [Back to TOC](#table-of-contents) Test Suite ========== This module comes with a Perl-driven test suite. The [test cases](http://github.com/openresty/memc-nginx-module/tree/master/t/) are [declarative](http://github.com/openresty/memc-nginx-module/blob/master/t/storage.t) too. Thanks to the [Test::Base](http://search.cpan.org/perldoc?Test::Base) module in the Perl world. To run it on your side: ```bash $ PATH=/path/to/your/nginx-with-memc-module:$PATH prove -r t ``` You need to terminate any Nginx processes before running the test suite if you have changed the Nginx server binary. Either [LWP::UserAgent](http://search.cpan.org/perldoc?LWP::UserAgent) or [IO::Socket](http://search.cpan.org/perldoc?IO::Socket) is used by the [test scaffold](http://github.com/openresty/memc-nginx-module/blob/master/test/lib/Test/Nginx/LWP.pm). Because a single nginx server (by default, `localhost:1984`) is used across all the test scripts (`.t` files), it's meaningless to run the test suite in parallel by specifying `-jN` when invoking the `prove` utility. You should also keep a memcached server listening on the `11211` port at localhost before running the test suite. Some parts of the test suite requires modules [rewrite](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html) and [echo](http://github.com/openresty/echo-nginx-module) to be enabled as well when building Nginx. [Back to TOC](#table-of-contents) TODO ==== * add support for the memcached commands `cas`, `gets` and `stats $memc_value`. * add support for the `noreply` option. [Back to TOC](#table-of-contents) Getting involved ================ You'll be very welcomed to submit patches to the [author](#author) or just ask for a commit bit to the [source repository](#source-repository) on GitHub. [Back to TOC](#table-of-contents) Author ====== Yichun "agentzh" Zhang (章亦春) *<agentzh@gmail.com>*, OpenResty Inc. This wiki page is also maintained by the author himself, and everybody is encouraged to improve this page as well. [Back to TOC](#table-of-contents) Copyright & License =================== The code base is borrowed directly from the standard [memcached module](http://nginx.org/en/docs/http/ngx_http_memcached_module.html) in the Nginx core. This part of code is copyrighted by Igor Sysoev and Nginx Inc. Copyright (c) 2009-2018, Yichun "agentzh" Zhang (章亦春) , OpenResty Inc. This module is licensed under the terms of the BSD license. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [Back to TOC](#table-of-contents) See Also ======== * The original announcement email on the nginx mailing list: [ngx_memc: "an extended version of ngx_memcached that supports set, add, delete, and many more commands"](http://forum.nginx.org/read.php?2,28359) * My slides demonstrating various ngx_memc usage: (use the arrow or pageup/pagedown keys on the keyboard to swith pages) * The latest [memcached TCP protocol](http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt). * The [ngx_srcache](http://github.com/openresty/srcache-nginx-module) module * The [lua-resty-memcached](https://github.com/openresty/lua-resty-memcached) library based on the [lua-nginx-module](http://github.com/openresty/lua-nginx-module) cosocket API. * The standard [memcached](http://nginx.org/en/docs/http/ngx_http_memcached_module.html) module. * The [echo module](http://github.com/openresty/echo-nginx-module) for Nginx module's automated testing. * The standard [headers](http://nginx.org/en/docs/http/ngx_http_headers_module.html) module and the 3rd-parth [headers-more](http://github.com/openresty/headers-more-nginx-module) module. memc-nginx-module-0.20/config000066400000000000000000000025771454304070500161650ustar00rootroot00000000000000ngx_addon_name=ngx_http_memc_module MEMC_SRCS=" \ $ngx_addon_dir/src/ngx_http_memc_module.c \ $ngx_addon_dir/src/ngx_http_memc_request.c \ $ngx_addon_dir/src/ngx_http_memc_response.c \ $ngx_addon_dir/src/ngx_http_memc_util.c \ $ngx_addon_dir/src/ngx_http_memc_handler.c \ " MEMC_DEPS=" \ $ngx_addon_dir/src/ddebug.h \ $ngx_addon_dir/src/ngx_http_memc_module.h \ $ngx_addon_dir/src/ngx_http_memc_request.h \ $ngx_addon_dir/src/ngx_http_memc_response.h \ $ngx_addon_dir/src/ngx_http_memc_util.h \ $ngx_addon_dir/src/ngx_http_memc_handler.h \ " if [ -n "$ngx_module_link" ]; then ngx_module_type=HTTP ngx_module_name=$ngx_addon_name ngx_module_srcs="$MEMC_SRCS" ngx_module_deps="$MEMC_DEPS" . auto/module else HTTP_MODULES="$HTTP_MODULES $ngx_addon_name" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $MEMC_SRCS" NGX_ADDON_DEPS="$NGX_ADDON_DEPS $MEMC_DEPS" fi memc-nginx-module-0.20/src/000077500000000000000000000000001454304070500155515ustar00rootroot00000000000000memc-nginx-module-0.20/src/ddebug.h000066400000000000000000000030711454304070500171550ustar00rootroot00000000000000#ifndef DDEBUG_H #define DDEBUG_H #include #include #if defined(DDEBUG) && (DDEBUG) # if (NGX_HAVE_VARIADIC_MACROS) # define dd(...) fprintf(stderr, "memc *** "); \ fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__) # else #include #include #include static ngx_inline void dd(const char* fmt, ...) { } # endif #else # if (NGX_HAVE_VARIADIC_MACROS) # define dd(...) # else #include static ngx_inline void dd(const char* fmt, ...) { } # endif #endif #if defined(DDEBUG) && (DDEBUG) #define dd_check_read_event_handler(r) \ dd("r->read_event_handler = %s", \ r->read_event_handler == ngx_http_block_reading ? \ "ngx_http_block_reading" : \ r->read_event_handler == ngx_http_test_reading ? \ "ngx_http_test_reading" : \ r->read_event_handler == ngx_http_request_empty_handler ? \ "ngx_http_request_empty_handler" : "UNKNOWN") #define dd_check_write_event_handler(r) \ dd("r->write_event_handler = %s", \ r->write_event_handler == ngx_http_handler ? \ "ngx_http_handler" : \ r->write_event_handler == ngx_http_core_run_phases ? \ "ngx_http_core_run_phases" : \ r->write_event_handler == ngx_http_request_empty_handler ? \ "ngx_http_request_empty_handler" : "UNKNOWN") #else #define dd_check_read_event_handler(r) #define dd_check_write_event_handler(r) #endif #endif /* DDEBUG_H */ memc-nginx-module-0.20/src/memc_common.rl000066400000000000000000000014501454304070500204010ustar00rootroot00000000000000%%{ machine memc_common; action catch_err { dd("caught error..."); dd("machine state: %d", cs); *status_addr = NGX_HTTP_BAD_GATEWAY; } msg = any* -- "\r\n"; error_helper = "ERROR\r\n" | "CLIENT_ERROR " msg "\r\n" | "SERVER_ERROR " msg "\r\n" ; error = error_helper @catch_err ; action finalize { dd("done it!"); *done_addr = 1; } action check { dd("state %d, left %d, reading char '%c'", cs, (int) (pe - p), *p); } action handle_stored { dd("status set to 201"); *status_addr = NGX_HTTP_CREATED; } action handle_not_found { dd("status set to 404"); *status_addr = NGX_HTTP_NOT_FOUND; } }%% memc-nginx-module-0.20/src/memc_delete.rl000066400000000000000000000004011454304070500203460ustar00rootroot00000000000000%%{ machine memc_delete; alphtype short; include memc_common "memc_common.rl"; response = "DELETED\r\n" | "NOT_FOUND\r\n" @handle_not_found | error ; main := response @finalize ; }%% memc-nginx-module-0.20/src/memc_flush_all.rl000066400000000000000000000003161454304070500210620ustar00rootroot00000000000000%%{ machine memc_flush_all; alphtype short; include memc_common "memc_common.rl"; response = "OK\r\n" | error ; main := response @finalize ; }%% memc-nginx-module-0.20/src/memc_incr_decr.rl000066400000000000000000000004761454304070500210500ustar00rootroot00000000000000%%{ machine memc_incr_decr; alphtype short; include memc_common "memc_common.rl"; value = digit+ ; response = value " "* "\r\n" @handle_stored | "NOT_FOUND\r\n" @handle_not_found | error ; main := response @finalize $check ; }%% memc-nginx-module-0.20/src/memc_stats.rl000066400000000000000000000004711454304070500202510ustar00rootroot00000000000000%%{ machine memc_stats; alphtype short; include memc_common "memc_common.rl"; content = any+ - "\r\n" ; stat_line = "STAT " content "\r\n" ; response = stat_line* "END\r\n" | error ; main := response @finalize ; }%% memc-nginx-module-0.20/src/memc_storage.rl000066400000000000000000000004671454304070500205640ustar00rootroot00000000000000%%{ machine memc_storage; include memc_common "memc_common.rl"; response = "STORED\r\n" @handle_stored | "NOT_STORED\r\n" | "EXISTS\r\n" | "NOT_FOUND\r\n" @handle_not_found | error ; main := response @finalize ; }%% memc-nginx-module-0.20/src/memc_version.rl000066400000000000000000000004101454304070500205710ustar00rootroot00000000000000%%{ machine memc_version; alphtype short; include memc_common "memc_common.rl"; version = any+ - "\r\n" ; response = "VERSION " version "\r\n" | error ; main := response @finalize ; }%% memc-nginx-module-0.20/src/ngx_http_memc_handler.c000066400000000000000000000416371454304070500222610ustar00rootroot00000000000000 /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_memc_handler.h" #include "ngx_http_memc_module.h" #include "ngx_http_memc_request.h" #include "ngx_http_memc_response.h" #include "ngx_http_memc_util.h" static ngx_int_t ngx_http_memc_flags_as_http_time_variable( ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data); static ngx_http_variable_t ngx_http_memc_variables[] = { { ngx_string("memc_flags_as_http_time"), NULL, ngx_http_memc_flags_as_http_time_variable, 0, 0, 0 }, { ngx_null_string, NULL, NULL, 0, 0, 0 } }; static ngx_str_t ngx_http_memc_key = ngx_string("memc_key"); static ngx_str_t ngx_http_memc_cmd = ngx_string("memc_cmd"); static ngx_str_t ngx_http_memc_value = ngx_string("memc_value"); static ngx_str_t ngx_http_memc_flags = ngx_string("memc_flags"); static ngx_str_t ngx_http_memc_exptime = ngx_string("memc_exptime"); static ngx_int_t ngx_http_memc_add_more_variables(ngx_conf_t *cf); static ngx_int_t ngx_http_memc_variable_not_found(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data); static ngx_int_t ngx_http_memc_add_variable(ngx_conf_t *cf, ngx_str_t *name); static ngx_flag_t ngx_http_memc_in_cmds_allowed(ngx_http_memc_loc_conf_t *mlcf, ngx_http_memc_cmd_t memc_cmd); static ngx_int_t ngx_http_memc_reinit_request(ngx_http_request_t *r); static void ngx_http_memc_abort_request(ngx_http_request_t *r); static void ngx_http_memc_finalize_request(ngx_http_request_t *r, ngx_int_t rc); static ngx_flag_t ngx_http_memc_valid_uint32_str(u_char *data, size_t len); static ngx_flag_t ngx_http_memc_valid_uint64_str(u_char *data, size_t len); ngx_int_t ngx_http_memc_handler(ngx_http_request_t *r) { ngx_int_t rc; ngx_http_upstream_t *u; ngx_http_memc_ctx_t *ctx; ngx_http_memc_loc_conf_t *mlcf; ngx_http_memc_main_conf_t *mmcf; ngx_str_t target; ngx_url_t url; ngx_http_variable_value_t *cmd_vv; ngx_http_variable_value_t *key_vv; ngx_http_variable_value_t *value_vv; ngx_http_variable_value_t *flags_vv; ngx_http_variable_value_t *exptime_vv; ngx_http_memc_cmd_t memc_cmd; ngx_flag_t is_storage_cmd = 0; dd("memc handler"); mmcf = ngx_http_get_module_main_conf(r, ngx_http_memc_module); key_vv = ngx_http_get_indexed_variable(r, mmcf->key_index); if (key_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } cmd_vv = ngx_http_get_indexed_variable(r, mmcf->cmd_index); if (cmd_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } if (cmd_vv->not_found || cmd_vv->len == 0) { dd("variable $memc_cmd not found"); cmd_vv->not_found = 0; cmd_vv->valid = 1; cmd_vv->no_cacheable = 0; if (r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD)) { cmd_vv->len = sizeof("get") - 1; cmd_vv->data = (u_char *) "get"; memc_cmd = ngx_http_memc_cmd_get; } else if (r->method == NGX_HTTP_POST) { cmd_vv->len = sizeof("add") - 1; cmd_vv->data = (u_char *) "add"; memc_cmd = ngx_http_memc_cmd_add; is_storage_cmd = 1; } else if (r->method == NGX_HTTP_PUT) { cmd_vv->len = sizeof("set") - 1; cmd_vv->data = (u_char *) "set"; memc_cmd = ngx_http_memc_cmd_set; is_storage_cmd = 1; } else if (r->method == NGX_HTTP_DELETE) { cmd_vv->len = sizeof("delete") - 1; cmd_vv->data = (u_char *) "delete"; memc_cmd = ngx_http_memc_cmd_delete; } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_memc: $memc_cmd variable not found for HTTP " "%V requests", &r->method_name); return NGX_HTTP_BAD_REQUEST; } } else { memc_cmd = ngx_http_memc_parse_cmd(cmd_vv->data, cmd_vv->len, &is_storage_cmd); if (memc_cmd == ngx_http_memc_cmd_unknown) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_memc: unknown $memc_cmd \"%v\"", cmd_vv); return NGX_HTTP_BAD_REQUEST; } } mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memc_module); dd("XXX connect timeout %d", (int) mlcf->upstream.connect_timeout); if (!ngx_http_memc_in_cmds_allowed(mlcf, memc_cmd)) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_memc: memcached command \"%v\" not allowed", cmd_vv); return NGX_HTTP_FORBIDDEN; } if (ngx_http_set_content_type(r) != NGX_OK) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } #if defined(nginx_version) && \ ((nginx_version >= 7063 && nginx_version < 8000) \ || nginx_version >= 8007) if (ngx_http_upstream_create(r) != NGX_OK) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } u = r->upstream; #else /* 0.7.x < 0.7.63, 0.8.x < 0.8.7 */ u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); if (u == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } u->peer.log = r->connection->log; u->peer.log_error = NGX_ERROR_ERR; # if (NGX_THREADS) u->peer.lock = &r->connection->lock; # endif r->upstream = u; #endif if (mlcf->complex_target) { /* variables used in the memc_pass directive */ if (ngx_http_complex_value(r, mlcf->complex_target, &target) != NGX_OK) { dd("failed to compile"); return NGX_ERROR; } if (target.len == 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memc: handler: empty \"memc_pass\" target"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } url.host = target; url.port = 0; url.no_resolve = 1; mlcf->upstream.upstream = ngx_http_memc_upstream_add(r, &url); if (mlcf->upstream.upstream == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memc: upstream \"%V\" not found", &target); return NGX_HTTP_INTERNAL_SERVER_ERROR; } } u->schema.len = sizeof("memcached://") - 1; u->schema.data = (u_char *) "memcached://"; u->output.tag = (ngx_buf_tag_t) &ngx_http_memc_module; u->conf = &mlcf->upstream; ctx = ngx_palloc(r->pool, sizeof(ngx_http_memc_ctx_t)); if (ctx == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } ctx->body_length = 0; ctx->memc_key_vv = key_vv; ctx->memc_value_vv = NULL; ctx->memc_flags_vv = NULL; ctx->parser_state = NGX_ERROR; ctx->rest = NGX_HTTP_MEMC_END; ctx->request = r; ctx->cmd_str.data = cmd_vv->data; ctx->cmd_str.len = cmd_vv->len; ctx->cmd = memc_cmd; ctx->is_storage_cmd = is_storage_cmd; ngx_http_set_ctx(r, ctx, ngx_http_memc_module); if (is_storage_cmd) { u->create_request = ngx_http_memc_create_storage_cmd_request; u->process_header = ngx_http_memc_process_simple_header; u->input_filter_init = ngx_http_memc_empty_filter_init; u->input_filter = ngx_http_memc_empty_filter; } else if (memc_cmd == ngx_http_memc_cmd_get) { u->create_request = ngx_http_memc_create_get_cmd_request; u->process_header = ngx_http_memc_process_get_cmd_header; u->input_filter_init = ngx_http_memc_get_cmd_filter_init; u->input_filter = ngx_http_memc_get_cmd_filter; } else if (memc_cmd == ngx_http_memc_cmd_flush_all) { u->create_request = ngx_http_memc_create_flush_all_cmd_request; u->process_header = ngx_http_memc_process_simple_header; u->input_filter_init = ngx_http_memc_empty_filter_init; u->input_filter = ngx_http_memc_empty_filter; } else if (memc_cmd == ngx_http_memc_cmd_version || memc_cmd == ngx_http_memc_cmd_stats) { u->create_request = ngx_http_memc_create_noarg_cmd_request; u->process_header = ngx_http_memc_process_simple_header; u->input_filter_init = ngx_http_memc_empty_filter_init; u->input_filter = ngx_http_memc_empty_filter; } else if (memc_cmd == ngx_http_memc_cmd_delete) { u->create_request = ngx_http_memc_create_delete_cmd_request; u->process_header = ngx_http_memc_process_simple_header; u->input_filter_init = ngx_http_memc_empty_filter_init; u->input_filter = ngx_http_memc_empty_filter; } else if (memc_cmd == ngx_http_memc_cmd_incr || memc_cmd == ngx_http_memc_cmd_decr) { u->create_request = ngx_http_memc_create_incr_decr_cmd_request; u->process_header = ngx_http_memc_process_simple_header; u->input_filter_init = ngx_http_memc_empty_filter_init; u->input_filter = ngx_http_memc_empty_filter; } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "assertion failed: command \"%v\" does not have proper " "handlers.", cmd_vv); return NGX_HTTP_INTERNAL_SERVER_ERROR; } u->reinit_request = ngx_http_memc_reinit_request; u->abort_request = ngx_http_memc_abort_request; u->finalize_request = ngx_http_memc_finalize_request; u->input_filter_ctx = ctx; if (is_storage_cmd || memc_cmd == ngx_http_memc_cmd_flush_all || memc_cmd == ngx_http_memc_cmd_delete) { exptime_vv = ngx_http_get_indexed_variable(r, mmcf->exptime_index); if (exptime_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } ctx->memc_exptime_vv = exptime_vv; if (!exptime_vv->not_found && exptime_vv->len && !ngx_http_memc_valid_uint32_str(exptime_vv->data, exptime_vv->len)) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "variable \"$memc_exptime\" takes invalid value: %v", exptime_vv); return NGX_HTTP_BAD_REQUEST; } } if (is_storage_cmd || memc_cmd == ngx_http_memc_cmd_get) { flags_vv = ngx_http_get_indexed_variable(r, mmcf->flags_index); if (flags_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } ctx->memc_flags_vv = flags_vv; if (is_storage_cmd && !flags_vv->not_found && flags_vv->len && !ngx_http_memc_valid_uint32_str(flags_vv->data, flags_vv->len)) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "variable \"$memc_flags\" takes invalid value: %v", flags_vv); return NGX_HTTP_BAD_REQUEST; } } if (is_storage_cmd || memc_cmd == ngx_http_memc_cmd_incr || memc_cmd == ngx_http_memc_cmd_decr) { value_vv = ngx_http_get_indexed_variable(r, mmcf->value_index); if (value_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } if (memc_cmd == ngx_http_memc_cmd_incr || memc_cmd == ngx_http_memc_cmd_decr) { if (value_vv->not_found || value_vv->len == 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "the \"$memc_value\" variable is required for " "command \"%V\"", &ctx->cmd_str); return NGX_HTTP_BAD_REQUEST; } if (!ngx_http_memc_valid_uint64_str(value_vv->data, value_vv->len)) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "variable \"$memc_value\" is invalid for " "incr/decr: %v", value_vv); return NGX_HTTP_BAD_REQUEST; } } ctx->memc_value_vv = value_vv; } rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); if (rc == NGX_ERROR || rc > NGX_OK) { return rc; } return NGX_DONE; } static ngx_int_t ngx_http_memc_reinit_request(ngx_http_request_t *r) { return NGX_OK; } static void ngx_http_memc_abort_request(ngx_http_request_t *r) { ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "abort http memcached request"); return; } static void ngx_http_memc_finalize_request(ngx_http_request_t *r, ngx_int_t rc) { ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "finalize http memcached request"); return; } static ngx_flag_t ngx_http_memc_in_cmds_allowed(ngx_http_memc_loc_conf_t *mlcf, ngx_http_memc_cmd_t memc_cmd) { ngx_uint_t i; ngx_http_memc_cmd_t *value; if (mlcf->cmds_allowed == NULL || mlcf->cmds_allowed->nelts == 0) { /* by default, all the memcached commands supported are allowed. */ return 1; } value = mlcf->cmds_allowed->elts; for (i = 0; i < mlcf->cmds_allowed->nelts; i++) { if (memc_cmd == value[i]) { return 1; } } return 0; } static ngx_flag_t ngx_http_memc_valid_uint32_str(u_char *data, size_t len) { u_char *p, *last; if (len > NGX_UINT32_LEN) { return 0; } last = data + len; for (p = data; p != last; p++) { if (*p < '0' || *p > '9') { return 0; } } return 1; } static ngx_flag_t ngx_http_memc_valid_uint64_str(u_char *data, size_t len) { u_char *p, *last; if (len > NGX_UINT64_LEN) { return 0; } last = data + len; for (p = data; p != last; p++) { if (*p < '0' || *p > '9') { return 0; } } return 1; } ngx_int_t ngx_http_memc_init(ngx_conf_t *cf) { ngx_http_memc_main_conf_t *mmcf; mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_memc_module); if (!mmcf->module_used) { return NGX_OK; } mmcf->key_index = ngx_http_memc_add_variable(cf, &ngx_http_memc_key); if (mmcf->key_index == NGX_ERROR) { return NGX_ERROR; } mmcf->cmd_index = ngx_http_memc_add_variable(cf, &ngx_http_memc_cmd); if (mmcf->cmd_index == NGX_ERROR) { return NGX_ERROR; } mmcf->flags_index = ngx_http_memc_add_variable(cf, &ngx_http_memc_flags); if (mmcf->flags_index == NGX_ERROR) { return NGX_ERROR; } mmcf->exptime_index = ngx_http_memc_add_variable(cf, &ngx_http_memc_exptime); if (mmcf->exptime_index == NGX_ERROR) { return NGX_ERROR; } mmcf->value_index = ngx_http_memc_add_variable(cf, &ngx_http_memc_value); if (mmcf->value_index == NGX_ERROR) { return NGX_ERROR; } return ngx_http_memc_add_more_variables(cf); } static ngx_int_t ngx_http_memc_add_variable(ngx_conf_t *cf, ngx_str_t *name) { ngx_http_variable_t *v; v = ngx_http_add_variable(cf, name, NGX_HTTP_VAR_CHANGEABLE); if (v == NULL) { return NGX_ERROR; } v->get_handler = ngx_http_memc_variable_not_found; return ngx_http_get_variable_index(cf, name); } static ngx_int_t ngx_http_memc_variable_not_found(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { v->not_found = 1; return NGX_OK; } static ngx_int_t ngx_http_memc_add_more_variables(ngx_conf_t *cf) { ngx_http_variable_t *var, *v; for (v = ngx_http_memc_variables; v->name.len; v++) { var = ngx_http_add_variable(cf, &v->name, v->flags); if (var == NULL) { return NGX_ERROR; } var->get_handler = v->get_handler; var->data = v->data; } return NGX_OK; } static ngx_int_t ngx_http_memc_flags_as_http_time_variable( ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { u_char *p; size_t len; time_t flags_time = 0; ngx_http_memc_ctx_t *ctx; ngx_http_variable_value_t *flags_vv; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); flags_vv = ctx->memc_flags_vv; if (flags_vv == NULL) { goto not_found; } if (flags_vv->not_found || flags_vv->len == 0) { goto not_found; } flags_time = ngx_atotm(flags_vv->data, flags_vv->len); if (flags_time == NGX_ERROR) { return NGX_ERROR; } len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; p = ngx_pnalloc(r->pool, len); if (p == NULL) { return NGX_ERROR; } ngx_http_time(p, flags_time); v->len = len; v->data = p; v->valid = 1; v->not_found = 0; v->no_cacheable = 0; return NGX_OK; not_found: v->len = 0; v->data = NULL; v->valid = 1; v->not_found = 1; return NGX_OK; } memc-nginx-module-0.20/src/ngx_http_memc_handler.h000066400000000000000000000003751454304070500222600ustar00rootroot00000000000000#ifndef NGX_HTTP_MEMC_HANDLER_H #define NGX_HTTP_MEMC_HANDLER_H #include #include ngx_int_t ngx_http_memc_handler(ngx_http_request_t *r); ngx_int_t ngx_http_memc_init(ngx_conf_t *cf); #endif /* NGX_HTTP_MEMC_HANDLER_H */ memc-nginx-module-0.20/src/ngx_http_memc_module.c000066400000000000000000000303021454304070500221140ustar00rootroot00000000000000 /* * Copyright (C) Igor Sysoev * Copyright (C) Zhang "agentzh" Yichun */ #ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_memc_module.h" #include "ngx_http_memc_handler.h" #include "ngx_http_memc_util.h" #include #include #include #include static void *ngx_http_memc_create_loc_conf(ngx_conf_t *cf); static char *ngx_http_memc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); static char *ngx_http_memc_cmds_allowed(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_memc_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_memc_upstream_max_fails_unsupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_memc_upstream_fail_timeout_unsupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static void *ngx_http_memc_create_main_conf(ngx_conf_t *cf); static ngx_conf_bitmask_t ngx_http_memc_next_upstream_masks[] = { { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, { ngx_string("invalid_response"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, { ngx_string("not_found"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, { ngx_null_string, 0 } }; static ngx_command_t ngx_http_memc_commands[] = { { ngx_string("memc_cmds_allowed"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF |NGX_HTTP_LIF_CONF|NGX_CONF_1MORE, ngx_http_memc_cmds_allowed, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL }, { ngx_string("memc_pass"), NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, ngx_http_memc_pass, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL }, { ngx_string("memc_connect_timeout"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_msec_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memc_loc_conf_t, upstream.connect_timeout), NULL }, { ngx_string("memc_send_timeout"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_msec_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memc_loc_conf_t, upstream.send_timeout), NULL }, { ngx_string("memc_read_timeout"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_msec_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memc_loc_conf_t, upstream.read_timeout), NULL }, { ngx_string("memc_buffer_size"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memc_loc_conf_t, upstream.buffer_size), NULL }, { ngx_string("memc_next_upstream"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, ngx_conf_set_bitmask_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memc_loc_conf_t, upstream.next_upstream), &ngx_http_memc_next_upstream_masks }, { ngx_string("memc_upstream_max_fails"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_http_memc_upstream_max_fails_unsupported, 0, 0, NULL }, { ngx_string("memc_upstream_fail_timeout"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_http_memc_upstream_fail_timeout_unsupported, 0, 0, NULL }, { ngx_string("memc_flags_to_last_modified"), NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memc_loc_conf_t, flags_to_last_modified), NULL }, { ngx_string("memc_ignore_client_abort"), NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memc_loc_conf_t, upstream.ignore_client_abort), NULL }, ngx_null_command }; static ngx_http_module_t ngx_http_memc_module_ctx = { NULL, /* preconfiguration */ ngx_http_memc_init, /* postconfiguration */ ngx_http_memc_create_main_conf, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_memc_create_loc_conf, /* create location configration */ ngx_http_memc_merge_loc_conf /* merge location configration */ }; ngx_module_t ngx_http_memc_module = { NGX_MODULE_V1, &ngx_http_memc_module_ctx, /* module context */ ngx_http_memc_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING }; static void * ngx_http_memc_create_loc_conf(ngx_conf_t *cf) { ngx_http_memc_loc_conf_t *conf; conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_memc_loc_conf_t)); if (conf == NULL) { return NULL; } /* * set by ngx_pcalloc(): * * conf->cmds_allowed = NULL; * conf->upstream.bufs.num = 0; * conf->upstream.next_upstream = 0; * conf->upstream.temp_path = NULL; * conf->upstream.uri = { 0, NULL }; * conf->upstream.location = NULL; */ conf->flags_to_last_modified = NGX_CONF_UNSET; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; conf->upstream.ignore_client_abort = NGX_CONF_UNSET; /* the hardcoded values */ conf->upstream.cyclic_temp_file = 0; conf->upstream.buffering = 0; conf->upstream.send_lowat = 0; conf->upstream.bufs.num = 0; conf->upstream.busy_buffers_size = 0; conf->upstream.max_temp_file_size = 0; conf->upstream.temp_file_write_size = 0; conf->upstream.intercept_errors = 1; conf->upstream.intercept_404 = 1; conf->upstream.pass_request_headers = 0; conf->upstream.pass_request_body = 0; return conf; } static char * ngx_http_memc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_http_memc_loc_conf_t *prev = parent; ngx_http_memc_loc_conf_t *conf = child; ngx_conf_merge_value(conf->flags_to_last_modified, prev->flags_to_last_modified, 0); ngx_conf_merge_msec_value(conf->upstream.connect_timeout, prev->upstream.connect_timeout, 60000); ngx_conf_merge_msec_value(conf->upstream.send_timeout, prev->upstream.send_timeout, 60000); ngx_conf_merge_msec_value(conf->upstream.read_timeout, prev->upstream.read_timeout, 60000); ngx_conf_merge_size_value(conf->upstream.buffer_size, prev->upstream.buffer_size, (size_t) ngx_pagesize); ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, prev->upstream.next_upstream, (NGX_CONF_BITMASK_SET |NGX_HTTP_UPSTREAM_FT_ERROR |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { conf->upstream.next_upstream = NGX_CONF_BITMASK_SET |NGX_HTTP_UPSTREAM_FT_OFF; } if (conf->upstream.upstream == NULL) { conf->upstream.upstream = prev->upstream.upstream; } if (conf->cmds_allowed == NULL) { conf->cmds_allowed = prev->cmds_allowed; } ngx_conf_merge_value(conf->upstream.ignore_client_abort, prev->upstream.ignore_client_abort, 0); return NGX_CONF_OK; } static char * ngx_http_memc_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_memc_loc_conf_t *mlcf = conf; ngx_http_memc_main_conf_t *mmcf; ngx_str_t *value; ngx_url_t url; ngx_http_core_loc_conf_t *clcf; ngx_uint_t n; ngx_http_compile_complex_value_t ccv; if (mlcf->upstream.upstream) { return "is duplicate"; } clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_memc_handler; if (clcf->name.data[clcf->name.len - 1] == '/') { clcf->auto_redirect = 1; } mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_memc_module); mmcf->module_used = 1; value = cf->args->elts; n = ngx_http_script_variables_count(&value[1]); if (n) { mlcf->complex_target = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t)); if (mlcf->complex_target == NULL) { return NGX_CONF_ERROR; } ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); ccv.cf = cf; ccv.value = &value[1]; ccv.complex_value = mlcf->complex_target; if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { return NGX_CONF_ERROR; } return NGX_CONF_OK; } mlcf->complex_target = NULL; ngx_memzero(&url, sizeof(ngx_url_t)); url.url = value[1]; url.no_resolve = 1; mlcf->upstream.upstream = ngx_http_upstream_add(cf, &url, 0); if (mlcf->upstream.upstream == NULL) { return NGX_CONF_ERROR; } return NGX_CONF_OK; } static char * ngx_http_memc_upstream_max_fails_unsupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"memc_upstream_max_fails\" is not supported, " "use the \"max_fails\" parameter of the \"server\" " "directive inside the \"upstream\" block"); return NGX_CONF_ERROR; } static char * ngx_http_memc_upstream_fail_timeout_unsupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"memc_upstream_fail_timeout\" is not supported, " "use the \"fail_timeout\" parameter of the \"server\" " "directive inside the \"upstream\" block"); return NGX_CONF_ERROR; } static char * ngx_http_memc_cmds_allowed(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_memc_loc_conf_t *mlcf = conf; ngx_uint_t i; ngx_str_t *value; ngx_http_memc_cmd_t memc_cmd; ngx_http_memc_cmd_t *c; ngx_flag_t is_storage_cmd; value = cf->args->elts; mlcf->cmds_allowed = ngx_array_create(cf->pool, cf->args->nelts - 1, sizeof(ngx_http_memc_cmd_t)); if (mlcf->cmds_allowed == NULL) { return NGX_CONF_ERROR; } for (i = 1; i < cf->args->nelts; i++) { memc_cmd = ngx_http_memc_parse_cmd(value[i].data, value[i].len, &is_storage_cmd); if (memc_cmd == ngx_http_memc_cmd_unknown) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Unknown memcached command \"%V\" used in " "\"memc_cmds_allowed\"", &value[i]); return NGX_CONF_ERROR; } c = ngx_array_push(mlcf->cmds_allowed); if (c == NULL) { return NGX_CONF_ERROR; } *c = memc_cmd; } return NGX_CONF_OK; } static void * ngx_http_memc_create_main_conf(ngx_conf_t *cf) { ngx_http_memc_main_conf_t *mmcf; mmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_memc_main_conf_t)); if (mmcf == NULL) { return NULL; } return mmcf; } memc-nginx-module-0.20/src/ngx_http_memc_module.h000066400000000000000000000041751454304070500221320ustar00rootroot00000000000000 /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef NGX_HTTP_MEMC_MODULE_H #define NGX_HTTP_MEMC_MODULE_H #include #include #include typedef enum { ngx_http_memc_cmd_set, ngx_http_memc_cmd_add, ngx_http_memc_cmd_replace, ngx_http_memc_cmd_append, ngx_http_memc_cmd_prepend, /* ngx_http_memc_cmd_cas, */ ngx_http_memc_cmd_get, /* ngx_http_memc_cmd_gets, */ ngx_http_memc_cmd_delete, ngx_http_memc_cmd_incr, ngx_http_memc_cmd_decr, ngx_http_memc_cmd_stats, ngx_http_memc_cmd_flush_all, ngx_http_memc_cmd_version, /* ngx_http_memc_cmd_verbosity, */ /* we do not want to support the "quit" command here */ /* ngx_http_memc_cmd_quit, */ ngx_http_memc_cmd_unknown } ngx_http_memc_cmd_t; typedef struct { ngx_flag_t flags_to_last_modified; ngx_http_upstream_conf_t upstream; ngx_array_t *cmds_allowed; ngx_http_complex_value_t *complex_target; } ngx_http_memc_loc_conf_t; typedef struct { ngx_int_t key_index; ngx_int_t cmd_index; ngx_int_t value_index; ngx_int_t flags_index; ngx_int_t exptime_index; ngx_int_t module_used; } ngx_http_memc_main_conf_t; typedef struct { #if defined(nginx_version) && nginx_version >= 1001004 off_t rest; #else size_t rest; #endif ngx_http_request_t *request; ngx_str_t key; ngx_str_t cmd_str; ngx_http_memc_cmd_t cmd; ngx_http_variable_value_t *memc_value_vv; ngx_http_variable_value_t *memc_key_vv; ngx_http_variable_value_t *memc_flags_vv; ngx_http_variable_value_t *memc_exptime_vv; ngx_flag_t is_storage_cmd; int parser_state; /* just for the subrequests in memory support */ size_t body_length; } ngx_http_memc_ctx_t; extern ngx_module_t ngx_http_memc_module; #define NGX_HTTP_MEMC_END (sizeof(CRLF "END" CRLF) - 1) #endif /* NGX_HTTP_MEMC_MODULE_H */ memc-nginx-module-0.20/src/ngx_http_memc_request.c000066400000000000000000000334201454304070500223230ustar00rootroot00000000000000 /* * Copyright (c) Yichun Zhang (agentzh) */ #ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_memc_request.h" #include "ngx_http_memc_module.h" #include "ngx_http_memc_util.h" ngx_int_t ngx_http_memc_create_storage_cmd_request(ngx_http_request_t *r) { size_t len; off_t bytes; size_t bytes_len; uintptr_t escape; ngx_buf_t *b; ngx_chain_t *cl, *in; ngx_chain_t **ll; ngx_http_memc_ctx_t *ctx; ngx_http_variable_value_t *key_vv; ngx_http_variable_value_t *flags_vv; ngx_http_variable_value_t *exptime_vv; ngx_http_variable_value_t *memc_value_vv; u_char bytes_buf[NGX_UINT32_LEN]; /* TODO add support for the "cas" command */ ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); /* prepare the "key" argument */ key_vv = ctx->memc_key_vv; if (key_vv == NULL || key_vv->not_found || key_vv->len == 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "the \"$memc_key\" variable is not set"); return NGX_ERROR; } escape = 2 * ngx_escape_uri(NULL, key_vv->data, key_vv->len, NGX_ESCAPE_MEMCACHED); /* prepare the "bytes" argument */ if (ctx->memc_value_vv && !ctx->memc_value_vv->not_found) { dd("found variable $memc_value"); memc_value_vv = ctx->memc_value_vv; bytes = memc_value_vv->len; } else if (r->request_body == NULL || r->request_body->bufs == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "neither the \"$memc_value\" variable " "nor the request body is available"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } else { memc_value_vv = NULL; bytes = 0; for (cl = r->request_body->bufs; cl; cl = cl->next) { bytes += ngx_buf_size(cl->buf); } } bytes_len = ngx_snprintf(bytes_buf, sizeof(bytes_buf), "%O", bytes) - bytes_buf; /* prepare the "flags" argument */ flags_vv = ctx->memc_flags_vv; if (flags_vv == NULL) { return NGX_ERROR; } if (flags_vv->not_found) { flags_vv->not_found = 0; flags_vv->valid = 1; flags_vv->no_cacheable = 0; flags_vv->len = sizeof("0") - 1; flags_vv->data = (u_char *) "0"; } else if (flags_vv->len == 0) { flags_vv->len = sizeof("0") - 1; flags_vv->data = (u_char *) "0"; } /* prepare the "exptime" argument */ exptime_vv = ctx->memc_exptime_vv; if (exptime_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } if (exptime_vv->not_found || exptime_vv->len == 0) { dd("setting exptime to its default value 0..."); exptime_vv->not_found = 0; exptime_vv->valid = 1; exptime_vv->no_cacheable = 0; exptime_vv->len = sizeof("0") - 1; exptime_vv->data = (u_char *) "0"; } /* calculate the total length of the command */ len = ctx->cmd_str.len + sizeof(" ") - 1 + key_vv->len + escape + sizeof(" ") - 1 + flags_vv->len + sizeof(" ") - 1 + exptime_vv->len + sizeof(" ") - 1 + bytes_len + sizeof(CRLF) - 1; b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; ll = &cl->next; r->upstream->request_bufs = cl; /* copy the memcached command over */ b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len); *b->last++ = ' '; /* copy the memcached key over */ ctx->key.data = b->last; if (escape == 0) { b->last = ngx_copy(b->last, key_vv->data, key_vv->len); } else { b->last = (u_char *) ngx_escape_uri(b->last, key_vv->data, key_vv->len, NGX_ESCAPE_MEMCACHED); } ctx->key.len = b->last - ctx->key.data; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http memcached request: \"%V\"", &ctx->key); *b->last++ = ' '; /* copy the memcached flags over */ b->last = ngx_copy(b->last, flags_vv->data, flags_vv->len); *b->last++ = ' '; /* copy the memcached exptime over */ b->last = ngx_copy(b->last, exptime_vv->data, exptime_vv->len); *b->last++ = ' '; /* copy the memcached bytes over */ b->last = ngx_copy(b->last, bytes_buf, bytes_len); *b->last++ = CR; *b->last++ = LF; if (memc_value_vv) { if (memc_value_vv->len) { dd("copy $memc_value to the request"); b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_ERROR; } b->memory = 1; b->start = b->pos = memc_value_vv->data; b->last = b->end = b->start + memc_value_vv->len; cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; *ll = cl; ll = &cl->next; } } else { /* to preserve the r->request_body->bufs untouched */ in = r->request_body->bufs; while (in) { cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = ngx_calloc_buf(r->pool); if (cl->buf == NULL) { return NGX_ERROR; } cl->buf->memory = 1; *cl->buf = *in->buf; *ll = cl; ll = &cl->next; in = in->next; } } /* append the trailing CRLF */ b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_ERROR; } b->start = b->pos = (u_char *) CRLF; b->last = b->end = b->start + sizeof(CRLF) - 1; b->memory = 1; cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; *ll = cl; return NGX_OK; } ngx_int_t ngx_http_memc_create_get_cmd_request(ngx_http_request_t *r) { size_t len; uintptr_t escape; ngx_buf_t *b; ngx_chain_t *cl; ngx_http_memc_ctx_t *ctx; ngx_http_variable_value_t *vv; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); vv = ctx->memc_key_vv; if (vv == NULL || vv->not_found || vv->len == 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "the \"$memc_key\" variable is not set"); return NGX_ERROR; } escape = 2 * ngx_escape_uri(NULL, vv->data, vv->len, NGX_ESCAPE_MEMCACHED); len = sizeof("get ") - 1 + vv->len + escape + sizeof(CRLF) - 1; b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; r->upstream->request_bufs = cl; *b->last++ = 'g'; *b->last++ = 'e'; *b->last++ = 't'; *b->last++ = ' '; ctx->key.data = b->last; if (escape == 0) { b->last = ngx_copy(b->last, vv->data, vv->len); } else { b->last = (u_char *) ngx_escape_uri(b->last, vv->data, vv->len, NGX_ESCAPE_MEMCACHED); } ctx->key.len = b->last - ctx->key.data; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http memcached request: \"%V\"", &ctx->key); *b->last++ = CR; *b->last++ = LF; ctx->parser_state = NGX_ERROR; return NGX_OK; } ngx_int_t ngx_http_memc_create_noarg_cmd_request(ngx_http_request_t *r) { size_t len; ngx_buf_t *b; ngx_http_memc_ctx_t *ctx; ngx_chain_t *cl; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); len = ctx->cmd_str.len + sizeof(CRLF) - 1; b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; r->upstream->request_bufs = cl; b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len); *b->last++ = CR; *b->last++ = LF; return NGX_OK; } ngx_int_t ngx_http_memc_create_flush_all_cmd_request(ngx_http_request_t *r) { size_t len; ngx_buf_t *b; ngx_http_memc_ctx_t *ctx; ngx_chain_t *cl; ngx_http_variable_value_t *exptime_vv; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); /* prepare the (optional) "exptime" argument */ exptime_vv = ctx->memc_exptime_vv; if (exptime_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } len = ctx->cmd_str.len; if (!exptime_vv->not_found && exptime_vv->len) { dd("found exptime: %s", exptime_vv->data); len += sizeof(" ") - 1 + exptime_vv->len; } len += sizeof(CRLF) - 1; b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; r->upstream->request_bufs = cl; b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len); if (!exptime_vv->not_found && exptime_vv->len) { *b->last++ = ' '; b->last = ngx_copy(b->last, exptime_vv->data, exptime_vv->len); } *b->last++ = CR; *b->last++ = LF; return NGX_OK; } ngx_int_t ngx_http_memc_create_delete_cmd_request(ngx_http_request_t *r) { size_t len; ngx_buf_t *b; ngx_http_memc_ctx_t *ctx; ngx_chain_t *cl; uintptr_t escape; ngx_http_variable_value_t *key_vv; ngx_http_variable_value_t *exptime_vv; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); /* prepare the "key" argument */ key_vv = ctx->memc_key_vv; if (key_vv == NULL || key_vv->not_found || key_vv->len == 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "the \"$memc_key\" variable is not set"); return NGX_ERROR; } escape = 2 * ngx_escape_uri(NULL, key_vv->data, key_vv->len, NGX_ESCAPE_MEMCACHED); /* prepare the (optional) "exptime" argument */ exptime_vv = ctx->memc_exptime_vv; if (exptime_vv == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } len = ctx->cmd_str.len + sizeof(" ") - 1 + key_vv->len + escape; if (!exptime_vv->not_found && exptime_vv->len) { dd("found exptime: %s", exptime_vv->data); len += sizeof(" ") - 1 + exptime_vv->len; } len += sizeof(CRLF) - 1; b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; r->upstream->request_bufs = cl; b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len); *b->last++ = ' '; if (escape == 0) { b->last = ngx_copy(b->last, key_vv->data, key_vv->len); } else { b->last = (u_char *) ngx_escape_uri(b->last, key_vv->data, key_vv->len, NGX_ESCAPE_MEMCACHED); } if (!exptime_vv->not_found && exptime_vv->len) { *b->last++ = ' '; b->last = ngx_copy(b->last, exptime_vv->data, exptime_vv->len); } *b->last++ = CR; *b->last++ = LF; return NGX_OK; } ngx_int_t ngx_http_memc_create_incr_decr_cmd_request(ngx_http_request_t *r) { size_t len; ngx_buf_t *b; ngx_http_memc_ctx_t *ctx; ngx_chain_t *cl; uintptr_t escape; ngx_http_variable_value_t *key_vv; ngx_http_variable_value_t *value_vv; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); /* prepare the "key" argument */ key_vv = ctx->memc_key_vv; if (key_vv == NULL || key_vv->not_found || key_vv->len == 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "the \"$memc_key\" variable is not set"); return NGX_ERROR; } escape = 2 * ngx_escape_uri(NULL, key_vv->data, key_vv->len, NGX_ESCAPE_MEMCACHED); /* prepare the "value" argument */ value_vv = ctx->memc_value_vv; /* XXX validate if $memc_value_vv is a valid uint64 string */ len = ctx->cmd_str.len + sizeof(" ") - 1 + key_vv->len + escape + sizeof(" ") - 1 + value_vv->len + sizeof(CRLF) - 1; b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; r->upstream->request_bufs = cl; b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len); *b->last++ = ' '; if (escape == 0) { b->last = ngx_copy(b->last, key_vv->data, key_vv->len); } else { b->last = (u_char *) ngx_escape_uri(b->last, key_vv->data, key_vv->len, NGX_ESCAPE_MEMCACHED); } *b->last++ = ' '; b->last = ngx_copy(b->last, value_vv->data, value_vv->len); *b->last++ = CR; *b->last++ = LF; return NGX_OK; } memc-nginx-module-0.20/src/ngx_http_memc_request.h000066400000000000000000000012141454304070500223240ustar00rootroot00000000000000 /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef NGX_HTTP_MEMC_REQUEST_H #define NGX_HTTP_MEMC_REQUEST_H #include #include ngx_int_t ngx_http_memc_create_get_cmd_request(ngx_http_request_t *r); ngx_int_t ngx_http_memc_create_storage_cmd_request(ngx_http_request_t *r); ngx_int_t ngx_http_memc_create_noarg_cmd_request(ngx_http_request_t *r); ngx_int_t ngx_http_memc_create_flush_all_cmd_request(ngx_http_request_t *r); ngx_int_t ngx_http_memc_create_delete_cmd_request(ngx_http_request_t *r); ngx_int_t ngx_http_memc_create_incr_decr_cmd_request(ngx_http_request_t *r); #endif /* NGX_HTTP_MEMC_REQUEST_H */ memc-nginx-module-0.20/src/ngx_http_memc_response.c000066400000000000000000001242311454304070500224720ustar00rootroot00000000000000 #line 1 "src/ngx_http_memc_response.rl" /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_memc_response.h" #include "ngx_http_memc_module.h" #ifdef s_char #undef s_char #endif #define s_char signed char #line 24 "src/ngx_http_memc_response.rl" #line 29 "src/ngx_http_memc_response.c" static const char _memc_storage_key_offsets[] = { 0, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 }; static const char _memc_storage_trans_keys[] = { 67, 69, 78, 83, 76, 73, 69, 78, 84, 95, 69, 82, 82, 79, 82, 32, 13, 10, 13, 82, 88, 82, 79, 82, 13, 10, 73, 83, 84, 83, 13, 10, 79, 84, 95, 70, 83, 79, 85, 78, 68, 13, 10, 84, 79, 82, 69, 68, 69, 84, 82, 86, 69, 82, 79, 82, 69, 68, 13, 10, 0 }; static const char _memc_storage_single_lengths[] = { 0, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }; static const char _memc_storage_range_lengths[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const unsigned char _memc_storage_index_offsets[] = { 0, 0, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 34, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113 }; static const char _memc_storage_trans_targs[] = { 2, 16, 28, 43, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 14, 54, 15, 14, 17, 22, 0, 18, 0, 19, 0, 20, 0, 21, 0, 54, 0, 23, 0, 24, 0, 25, 0, 26, 0, 27, 0, 54, 0, 29, 0, 30, 0, 31, 0, 32, 38, 0, 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 54, 0, 39, 0, 40, 0, 41, 0, 42, 0, 26, 0, 44, 48, 0, 45, 0, 46, 0, 47, 0, 7, 0, 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 0, 0 }; static const char _memc_storage_trans_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0 }; static const int memc_storage_start = 1; static const int memc_storage_first_final = 54; static const int memc_storage_error = 0; #line 25 "src/ngx_http_memc_response.rl" #line 27 "src/ngx_http_memc_response.rl" #line 129 "src/ngx_http_memc_response.c" static const char _memc_flush_all_key_offsets[] = { 0, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 }; static const short _memc_flush_all_trans_keys[] = { 67, 69, 79, 83, 76, 73, 69, 78, 84, 95, 69, 82, 82, 79, 82, 32, 13, 10, 13, 82, 82, 79, 82, 13, 10, 75, 13, 10, 69, 82, 86, 69, 82, 0 }; static const char _memc_flush_all_single_lengths[] = { 0, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }; static const char _memc_flush_all_range_lengths[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const char _memc_flush_all_index_offsets[] = { 0, 0, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62 }; static const char _memc_flush_all_indicies[] = { 0, 2, 3, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 16, 18, 17, 16, 19, 1, 20, 1, 21, 1, 22, 1, 23, 1, 18, 1, 24, 1, 25, 1, 26, 1, 27, 1, 28, 1, 29, 1, 30, 1, 9, 1, 1, 0 }; static const char _memc_flush_all_trans_targs[] = { 2, 0, 16, 22, 25, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 30, 17, 18, 19, 20, 21, 23, 24, 30, 26, 27, 28, 29 }; static const char _memc_flush_all_trans_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0 }; static const int memc_flush_all_start = 1; static const int memc_flush_all_first_final = 30; static const int memc_flush_all_error = 0; #line 28 "src/ngx_http_memc_response.rl" #line 30 "src/ngx_http_memc_response.rl" #line 203 "src/ngx_http_memc_response.c" static const char _memc_version_key_offsets[] = { 0, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 43, 43, 43 }; static const short _memc_version_trans_keys[] = { 67, 69, 83, 86, 76, 73, 69, 78, 84, 95, 69, 82, 82, 79, 82, 32, 13, 10, 13, 82, 82, 79, 82, 13, 10, 69, 82, 86, 69, 82, 69, 82, 83, 73, 79, 78, 32, 13, 13, 10, 13, 10, 13, 13, 0 }; static const char _memc_version_single_lengths[] = { 0, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0, 1 }; static const char _memc_version_range_lengths[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const char _memc_version_index_offsets[] = { 0, 0, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 77, 80, 81, 82 }; static const char _memc_version_indicies[] = { 0, 2, 3, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 16, 18, 17, 16, 19, 1, 20, 1, 21, 1, 22, 1, 23, 1, 18, 1, 24, 1, 25, 1, 26, 1, 27, 1, 9, 1, 28, 1, 29, 1, 30, 1, 31, 1, 32, 1, 33, 1, 34, 1, 36, 35, 37, 35, 38, 37, 35, 39, 37, 35, 35, 1, 37, 35, 0 }; static const char _memc_version_trans_targs[] = { 2, 0, 16, 22, 27, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 39, 17, 18, 19, 20, 21, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 35, 37, 36, 40, 38 }; static const char _memc_version_trans_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 }; static const int memc_version_start = 1; static const int memc_version_first_final = 39; static const int memc_version_error = 0; #line 31 "src/ngx_http_memc_response.rl" #line 33 "src/ngx_http_memc_response.rl" #line 291 "src/ngx_http_memc_response.c" static const char _memc_stats_key_offsets[] = { 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 43, 45, 47, 48, 50, 52, 52, 52 }; static const short _memc_stats_trans_keys[] = { 67, 69, 83, 76, 73, 69, 78, 84, 95, 69, 82, 82, 79, 82, 32, 13, 10, 13, 78, 82, 68, 13, 10, 82, 79, 82, 13, 10, 69, 84, 82, 86, 69, 82, 65, 84, 32, 13, 13, 10, 13, 13, 69, 13, 78, 13, 68, 13, 10, 13, 10, 13, 13, 69, 0 }; static const char _memc_stats_single_lengths[] = { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 2, 2, 0, 0, 2 }; static const char _memc_stats_range_lengths[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const unsigned char _memc_stats_index_offsets[] = { 0, 0, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36, 38, 40, 42, 44, 46, 48, 50, 52, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 76, 79, 82, 85, 87, 90, 93, 94, 95 }; static const char _memc_stats_indicies[] = { 0, 2, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 15, 17, 16, 15, 18, 19, 1, 20, 1, 21, 1, 22, 1, 23, 1, 24, 1, 25, 1, 26, 1, 17, 1, 27, 28, 1, 29, 1, 30, 1, 31, 1, 8, 1, 32, 1, 33, 1, 34, 1, 36, 35, 37, 35, 38, 37, 35, 37, 39, 35, 37, 40, 35, 37, 41, 35, 42, 35, 43, 37, 35, 44, 37, 35, 35, 1, 37, 39, 35, 0 }; static const char _memc_stats_trans_targs[] = { 2, 0, 16, 25, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 43, 17, 20, 18, 19, 43, 21, 22, 23, 24, 26, 30, 27, 28, 29, 31, 32, 33, 34, 41, 35, 36, 37, 38, 39, 40, 44, 42 }; static const char _memc_stats_trans_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 }; static const int memc_stats_start = 1; static const int memc_stats_first_final = 43; static const int memc_stats_error = 0; #line 34 "src/ngx_http_memc_response.rl" #line 36 "src/ngx_http_memc_response.rl" #line 384 "src/ngx_http_memc_response.c" static const char _memc_delete_key_offsets[] = { 0, 0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }; static const short _memc_delete_trans_keys[] = { 67, 68, 69, 78, 83, 76, 73, 69, 78, 84, 95, 69, 82, 82, 79, 82, 32, 13, 10, 13, 69, 76, 69, 84, 69, 68, 13, 10, 82, 82, 79, 82, 13, 10, 79, 84, 95, 70, 79, 85, 78, 68, 13, 10, 69, 82, 86, 69, 82, 0 }; static const char _memc_delete_single_lengths[] = { 0, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }; static const char _memc_delete_range_lengths[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const unsigned char _memc_delete_index_offsets[] = { 0, 0, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93 }; static const char _memc_delete_trans_targs[] = { 2, 16, 24, 30, 40, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 14, 45, 15, 14, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 45, 0, 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 45, 0, 31, 0, 32, 0, 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 45, 0, 41, 0, 42, 0, 43, 0, 44, 0, 7, 0, 0, 0 }; static const char _memc_delete_trans_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const int memc_delete_start = 1; static const int memc_delete_first_final = 45; static const int memc_delete_error = 0; #line 37 "src/ngx_http_memc_response.rl" #line 39 "src/ngx_http_memc_response.rl" #line 473 "src/ngx_http_memc_response.c" static const char _memc_incr_decr_key_offsets[] = { 0, 0, 6, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }; static const short _memc_incr_decr_trans_keys[] = { 67, 69, 78, 83, 48, 57, 13, 32, 48, 57, 10, 13, 32, 76, 73, 69, 78, 84, 95, 69, 82, 82, 79, 82, 32, 13, 10, 13, 82, 82, 79, 82, 13, 10, 79, 84, 95, 70, 79, 85, 78, 68, 13, 10, 69, 82, 86, 69, 82, 0 }; static const char _memc_incr_decr_single_lengths[] = { 0, 4, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }; static const char _memc_incr_decr_range_lengths[] = { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const char _memc_incr_decr_index_offsets[] = { 0, 0, 6, 10, 12, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86 }; static const char _memc_incr_decr_indicies[] = { 2, 3, 4, 5, 0, 1, 6, 7, 0, 1, 8, 1, 6, 7, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20, 1, 21, 20, 22, 21, 20, 23, 1, 24, 1, 25, 1, 26, 1, 27, 1, 22, 1, 28, 1, 29, 1, 30, 1, 31, 1, 32, 1, 33, 1, 34, 1, 35, 1, 36, 1, 37, 1, 38, 1, 39, 1, 40, 1, 41, 1, 13, 1, 1, 0 }; static const char _memc_incr_decr_trans_targs[] = { 2, 0, 5, 19, 25, 35, 3, 4, 40, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 40, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 40, 36, 37, 38, 39 }; static const char _memc_incr_decr_trans_actions[] = { 1, 0, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1 }; static const int memc_incr_decr_start = 1; static const int memc_incr_decr_first_final = 40; static const int memc_incr_decr_error = 0; #line 40 "src/ngx_http_memc_response.rl" u_char ngx_http_memc_end[] = CRLF "END" CRLF; static u_char *parse_memc_storage(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_flush_all(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_version(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_stats(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_delete(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_incr_decr(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static ngx_int_t ngx_http_memc_write_simple_response(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_http_memc_ctx_t *ctx, ngx_uint_t status, ngx_str_t *resp); ngx_int_t ngx_http_memc_process_simple_header(ngx_http_request_t *r) { ngx_int_t rc; int cs; s_char *p; s_char *pe; s_char *orig; ngx_str_t resp; ngx_http_upstream_t *u; ngx_http_memc_ctx_t *ctx; ngx_uint_t status; unsigned done = 0; int error_state; int final_state; status = NGX_HTTP_OK; dd("process simple cmd header"); ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); if (ctx->parser_state == NGX_ERROR) { dd("reinit state"); if (ctx->is_storage_cmd) { dd("init memc_storage machine..."); #line 91 "src/ngx_http_memc_response.rl" #line 613 "src/ngx_http_memc_response.c" { cs = memc_storage_start; } #line 92 "src/ngx_http_memc_response.rl" } else if (ctx->cmd == ngx_http_memc_cmd_flush_all) { dd("init memc_flush_all machine..."); #line 97 "src/ngx_http_memc_response.rl" #line 626 "src/ngx_http_memc_response.c" { cs = memc_flush_all_start; } #line 98 "src/ngx_http_memc_response.rl" } else if (ctx->cmd == ngx_http_memc_cmd_version) { dd("init memc_version machine..."); #line 103 "src/ngx_http_memc_response.rl" #line 639 "src/ngx_http_memc_response.c" { cs = memc_version_start; } #line 104 "src/ngx_http_memc_response.rl" } else if (ctx->cmd == ngx_http_memc_cmd_stats) { dd("init memc_stats machine..."); #line 109 "src/ngx_http_memc_response.rl" #line 652 "src/ngx_http_memc_response.c" { cs = memc_stats_start; } #line 110 "src/ngx_http_memc_response.rl" } else if (ctx->cmd == ngx_http_memc_cmd_delete) { dd("init memc_delete machine..."); #line 115 "src/ngx_http_memc_response.rl" #line 665 "src/ngx_http_memc_response.c" { cs = memc_delete_start; } #line 116 "src/ngx_http_memc_response.rl" } else if (ctx->cmd == ngx_http_memc_cmd_incr || ctx->cmd == ngx_http_memc_cmd_decr) { dd("init memc_incr_decr machine..."); #line 123 "src/ngx_http_memc_response.rl" #line 680 "src/ngx_http_memc_response.c" { cs = memc_incr_decr_start; } #line 124 "src/ngx_http_memc_response.rl" } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unrecognized memcached command in " "ngx_http_memc_process_simple_header: \"%V\"", &ctx->cmd_str); return NGX_ERROR; /* this results in 500 status */ } } else { cs = ctx->parser_state; } u = r->upstream; orig = (s_char *) u->buffer.pos; p = (s_char *) u->buffer.pos; pe = (s_char *) u->buffer.last; dd("buffer len: %d", (int) (pe - p)); if (ctx->is_storage_cmd) { error_state = memc_storage_error; final_state = memc_storage_first_final; p = (s_char *) parse_memc_storage(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_flush_all) { error_state = memc_flush_all_error; final_state = memc_flush_all_first_final; p = (s_char *) parse_memc_flush_all(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_version) { error_state = memc_version_error; final_state = memc_version_first_final; p = (s_char *) parse_memc_version(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_stats) { error_state = memc_stats_error; final_state = memc_stats_first_final; p = (s_char *) parse_memc_stats(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_delete) { error_state = memc_delete_error; final_state = memc_delete_first_final; p = (s_char *) parse_memc_delete(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_incr || ctx->cmd == ngx_http_memc_cmd_decr) { error_state = memc_incr_decr_error; final_state = memc_incr_decr_first_final; p = (s_char *) parse_memc_incr_decr(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unrecognized memcached command in " "ngx_http_memc_process_simple_header: \"%V\"", &ctx->cmd_str); return NGX_ERROR; /* this results in 500 status */ } ctx->parser_state = cs; resp.data = u->buffer.start; resp.len = (u_char *) p - resp.data; u->buffer.pos = (u_char *) p; dd("machine state: %d (done: %d)", cs, done); dd("memcached response: (len: %d) %s", (int) resp.len, resp.data); if (done || cs >= final_state) { dd("memcached response parsed (resp.len: %d)", (int) resp.len); rc = ngx_http_memc_write_simple_response(r, u, ctx, status, &resp); return rc; } if (cs == error_state) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid response for command \"%V\" " "at pos %O: %V", &ctx->cmd_str, (off_t) (p - orig), &resp); status = NGX_HTTP_BAD_GATEWAY; u->headers_in.status_n = status; u->state->status = status; /* u->headers_in.status_n will be the final status */ return NGX_OK; } dd("we need more data to proceed (returned NGX_AGAIN)"); return NGX_AGAIN; } ngx_int_t ngx_http_memc_empty_filter_init(void *data) { ngx_http_memc_ctx_t *ctx = data; ngx_http_upstream_t *u; u = ctx->request->upstream; u->length = 0; /* to persuade ngx_http_upstream_keepalive (if any) to cache the connection if the status is neither 200 nor 404. */ if (u->headers_in.status_n == NGX_HTTP_CREATED) { u->headers_in.status_n = NGX_HTTP_OK; } return NGX_OK; } ngx_int_t ngx_http_memc_empty_filter(void *data, ssize_t bytes) { ngx_http_memc_ctx_t *ctx = data; ngx_http_upstream_t *u; u = ctx->request->upstream; /* recover the buffer for subrequests in memory */ u->buffer.last += ctx->body_length; return NGX_OK; } ngx_int_t ngx_http_memc_get_cmd_filter_init(void *data) { ngx_http_memc_ctx_t *ctx = data; ngx_http_upstream_t *u; u = ctx->request->upstream; dd("filter init: u->length: %d", (int) u->length); u->length = u->headers_in.content_length_n + NGX_HTTP_MEMC_END; dd("filter init (2): u->length: %d", (int) u->length); return NGX_OK; } ngx_int_t ngx_http_memc_get_cmd_filter(void *data, ssize_t bytes) { ngx_http_memc_ctx_t *ctx = data; u_char *last; ngx_buf_t *b; ngx_chain_t *cl, **ll; ngx_http_upstream_t *u; u = ctx->request->upstream; b = &u->buffer; if (u->length == ctx->rest) { if (ngx_strncmp(b->last, ngx_http_memc_end + NGX_HTTP_MEMC_END - ctx->rest, bytes) != 0) { ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, "memcached sent invalid trailer"); u->length = 0; ctx->rest = 0; return NGX_OK; } u->length -= bytes; ctx->rest -= bytes; #if defined(nginx_version) && nginx_version >= 1001004 if (u->length == 0) { u->keepalive = 1; } #endif return NGX_OK; } for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { ll = &cl->next; } cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs); if (cl == NULL) { return NGX_ERROR; } cl->buf->flush = 1; cl->buf->memory = 1; *ll = cl; last = b->last; cl->buf->pos = last; b->last += bytes; cl->buf->last = b->last; cl->buf->tag = u->output.tag; ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0, "memcached filter bytes:%z size:%z length:%z rest:%z", bytes, b->last - b->pos, u->length, ctx->rest); if (bytes <= (ssize_t) (u->length - NGX_HTTP_MEMC_END)) { u->length -= bytes; return NGX_OK; } last += u->length - NGX_HTTP_MEMC_END; if (ngx_strncmp(last, ngx_http_memc_end, b->last - last) != 0) { ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, "memcached sent invalid trailer"); #if defined(nginx_version) && nginx_version >= 1001004 b->last = last; cl->buf->last = last; u->length = 0; ctx->rest = 0; return NGX_OK; #endif } ctx->rest -= b->last - last; b->last = last; cl->buf->last = last; u->length = ctx->rest; #if defined(nginx_version) && nginx_version >= 1001004 if (u->length == 0) { u->keepalive = 1; } #endif return NGX_OK; } ngx_int_t ngx_http_memc_process_get_cmd_header(ngx_http_request_t *r) { ngx_http_memc_loc_conf_t *conf; u_char *p, *len; ngx_str_t line; ngx_http_upstream_t *u; ngx_http_memc_ctx_t *ctx; ngx_http_variable_value_t *flags_vv; u = r->upstream; dd("process header: u->length: %u", (unsigned) u->length); for (p = u->buffer.pos; p < u->buffer.last; p++) { if (*p == LF) { goto found; } } return NGX_AGAIN; found: *p = '\0'; line.len = p - u->buffer.pos - 1; line.data = u->buffer.pos; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "memcached: \"%V\"", &line); p = u->buffer.pos; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); if (ngx_strncmp(p, "VALUE ", sizeof("VALUE ") - 1) == 0) { p += sizeof("VALUE ") - 1; if (ngx_strncmp(p, ctx->key.data, ctx->key.len) != 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid key in response \"%V\" " "for key \"%V\"", &line, &ctx->key); return NGX_HTTP_UPSTREAM_INVALID_HEADER; } p += ctx->key.len; if (*p++ != ' ') { goto no_valid; } /* save flags */ flags_vv = ctx->memc_flags_vv; if (flags_vv == NULL) { return NGX_ERROR; } if (flags_vv->not_found) { flags_vv->not_found = 0; flags_vv->valid = 1; flags_vv->no_cacheable = 0; } flags_vv->data = p; while (*p) { if (*p++ == ' ') { flags_vv->len = p - 1 - flags_vv->data; conf = ngx_http_get_module_loc_conf(r, ngx_http_memc_module); if (conf->flags_to_last_modified) { r->headers_out.last_modified_time = ngx_atotm(flags_vv->data, flags_vv->len); } goto length; } } goto no_valid; length: len = p; while (*p && *p++ != CR) { /* void */ } #if defined(nginx_version) && nginx_version >= 1001004 u->headers_in.content_length_n = ngx_atoof(len, p - len - 1); if (u->headers_in.content_length_n == -1) { #else r->headers_out.content_length_n = ngx_atoof(len, p - len - 1); if (r->headers_out.content_length_n == -1) { #endif ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid length in response \"%V\" " "for key \"%V\"", &line, &ctx->key); return NGX_HTTP_UPSTREAM_INVALID_HEADER; } u->headers_in.status_n = NGX_HTTP_OK; u->state->status = NGX_HTTP_OK; u->buffer.pos = p + 1; return NGX_OK; } if (ngx_strcmp(p, "END\x0d") == 0) { ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "key: \"%V\" was not found by memcached", &ctx->key); u->headers_in.status_n = NGX_HTTP_NOT_FOUND; u->state->status = NGX_HTTP_NOT_FOUND; #if defined(nginx_version) && nginx_version >= 1001004 u->keepalive = 1; #endif return NGX_OK; } no_valid: ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid response: \"%V\"", &line); return NGX_HTTP_UPSTREAM_INVALID_HEADER; } static ngx_int_t ngx_http_memc_write_simple_response(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_http_memc_ctx_t *ctx, ngx_uint_t status, ngx_str_t *resp) { ngx_chain_t *cl, **ll; for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { ll = &cl->next; } cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); if (cl == NULL) { return NGX_ERROR; } cl->buf->flush = 1; cl->buf->memory = 1; cl->buf->pos = resp->data; cl->buf->last = cl->buf->pos + resp->len; *ll = cl; /* for subrequests in memory */ u->buffer.pos = resp->data; u->buffer.last = resp->data + resp->len; ctx->body_length = resp->len; #if defined(nginx_version) && nginx_version >= 1001004 u->headers_in.content_length_n = resp->len; u->keepalive = 1; #else r->headers_out.content_length_n = resp->len; #endif u->headers_in.status_n = status; u->state->status = status; return NGX_OK; } static u_char * parse_memc_storage(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; #line 577 "src/ngx_http_memc_response.rl" #line 578 "src/ngx_http_memc_response.rl" #line 1143 "src/ngx_http_memc_response.c" { int _klen; const char *_keys; int _trans; if ( p == pe ) goto _test_eof; if ( cs == 0 ) goto _out; _resume: _keys = _memc_storage_trans_keys + _memc_storage_key_offsets[cs]; _trans = _memc_storage_index_offsets[cs]; _klen = _memc_storage_single_lengths[cs]; if ( _klen > 0 ) { const char *_lower = _keys; const char *_mid; const char *_upper = _keys + _klen - 1; while (1) { if ( _upper < _lower ) break; _mid = _lower + ((_upper-_lower) >> 1); if ( (*p) < *_mid ) _upper = _mid - 1; else if ( (*p) > *_mid ) _lower = _mid + 1; else { _trans += (unsigned int)(_mid - _keys); goto _match; } } _keys += _klen; _trans += _klen; } _klen = _memc_storage_range_lengths[cs]; if ( _klen > 0 ) { const char *_lower = _keys; const char *_mid; const char *_upper = _keys + (_klen<<1) - 2; while (1) { if ( _upper < _lower ) break; _mid = _lower + (((_upper-_lower) >> 1) & ~1); if ( (*p) < _mid[0] ) _upper = _mid - 2; else if ( (*p) > _mid[1] ) _lower = _mid + 2; else { _trans += (unsigned int)((_mid - _keys)>>1); goto _match; } } _trans += _klen; } _match: cs = _memc_storage_trans_targs[_trans]; if ( _memc_storage_trans_actions[_trans] == 0 ) goto _again; switch ( _memc_storage_trans_actions[_trans] ) { case 2: #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 1: #line 4 "src/memc_common.rl" { dd("caught error..."); dd("machine state: %d", cs); *status_addr = NGX_HTTP_BAD_GATEWAY; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 4: #line 31 "src/memc_common.rl" { dd("status set to 201"); *status_addr = NGX_HTTP_CREATED; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 3: #line 37 "src/memc_common.rl" { dd("status set to 404"); *status_addr = NGX_HTTP_NOT_FOUND; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; #line 1256 "src/ngx_http_memc_response.c" } _again: if ( cs == 0 ) goto _out; if ( ++p != pe ) goto _resume; _test_eof: {} _out: {} } #line 579 "src/ngx_http_memc_response.rl" *cs_addr = cs; return p; } static u_char * parse_memc_flush_all(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; #line 593 "src/ngx_http_memc_response.rl" #line 594 "src/ngx_http_memc_response.rl" #line 1287 "src/ngx_http_memc_response.c" { int _klen; const short *_keys; int _trans; if ( p == pe ) goto _test_eof; if ( cs == 0 ) goto _out; _resume: _keys = _memc_flush_all_trans_keys + _memc_flush_all_key_offsets[cs]; _trans = _memc_flush_all_index_offsets[cs]; _klen = _memc_flush_all_single_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + _klen - 1; while (1) { if ( _upper < _lower ) break; _mid = _lower + ((_upper-_lower) >> 1); if ( (*p) < *_mid ) _upper = _mid - 1; else if ( (*p) > *_mid ) _lower = _mid + 1; else { _trans += (unsigned int)(_mid - _keys); goto _match; } } _keys += _klen; _trans += _klen; } _klen = _memc_flush_all_range_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + (_klen<<1) - 2; while (1) { if ( _upper < _lower ) break; _mid = _lower + (((_upper-_lower) >> 1) & ~1); if ( (*p) < _mid[0] ) _upper = _mid - 2; else if ( (*p) > _mid[1] ) _lower = _mid + 2; else { _trans += (unsigned int)((_mid - _keys)>>1); goto _match; } } _trans += _klen; } _match: _trans = _memc_flush_all_indicies[_trans]; cs = _memc_flush_all_trans_targs[_trans]; if ( _memc_flush_all_trans_actions[_trans] == 0 ) goto _again; switch ( _memc_flush_all_trans_actions[_trans] ) { case 2: #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 1: #line 4 "src/memc_common.rl" { dd("caught error..."); dd("machine state: %d", cs); *status_addr = NGX_HTTP_BAD_GATEWAY; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; #line 1375 "src/ngx_http_memc_response.c" } _again: if ( cs == 0 ) goto _out; if ( ++p != pe ) goto _resume; _test_eof: {} _out: {} } #line 595 "src/ngx_http_memc_response.rl" *cs_addr = cs; return p; } static u_char * parse_memc_version(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; #line 609 "src/ngx_http_memc_response.rl" #line 610 "src/ngx_http_memc_response.rl" #line 1406 "src/ngx_http_memc_response.c" { int _klen; const short *_keys; int _trans; if ( p == pe ) goto _test_eof; if ( cs == 0 ) goto _out; _resume: _keys = _memc_version_trans_keys + _memc_version_key_offsets[cs]; _trans = _memc_version_index_offsets[cs]; _klen = _memc_version_single_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + _klen - 1; while (1) { if ( _upper < _lower ) break; _mid = _lower + ((_upper-_lower) >> 1); if ( (*p) < *_mid ) _upper = _mid - 1; else if ( (*p) > *_mid ) _lower = _mid + 1; else { _trans += (unsigned int)(_mid - _keys); goto _match; } } _keys += _klen; _trans += _klen; } _klen = _memc_version_range_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + (_klen<<1) - 2; while (1) { if ( _upper < _lower ) break; _mid = _lower + (((_upper-_lower) >> 1) & ~1); if ( (*p) < _mid[0] ) _upper = _mid - 2; else if ( (*p) > _mid[1] ) _lower = _mid + 2; else { _trans += (unsigned int)((_mid - _keys)>>1); goto _match; } } _trans += _klen; } _match: _trans = _memc_version_indicies[_trans]; cs = _memc_version_trans_targs[_trans]; if ( _memc_version_trans_actions[_trans] == 0 ) goto _again; switch ( _memc_version_trans_actions[_trans] ) { case 2: #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 1: #line 4 "src/memc_common.rl" { dd("caught error..."); dd("machine state: %d", cs); *status_addr = NGX_HTTP_BAD_GATEWAY; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; #line 1494 "src/ngx_http_memc_response.c" } _again: if ( cs == 0 ) goto _out; if ( ++p != pe ) goto _resume; _test_eof: {} _out: {} } #line 611 "src/ngx_http_memc_response.rl" *cs_addr = cs; return p; } static u_char * parse_memc_stats(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; #line 625 "src/ngx_http_memc_response.rl" #line 626 "src/ngx_http_memc_response.rl" #line 1525 "src/ngx_http_memc_response.c" { int _klen; const short *_keys; int _trans; if ( p == pe ) goto _test_eof; if ( cs == 0 ) goto _out; _resume: _keys = _memc_stats_trans_keys + _memc_stats_key_offsets[cs]; _trans = _memc_stats_index_offsets[cs]; _klen = _memc_stats_single_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + _klen - 1; while (1) { if ( _upper < _lower ) break; _mid = _lower + ((_upper-_lower) >> 1); if ( (*p) < *_mid ) _upper = _mid - 1; else if ( (*p) > *_mid ) _lower = _mid + 1; else { _trans += (unsigned int)(_mid - _keys); goto _match; } } _keys += _klen; _trans += _klen; } _klen = _memc_stats_range_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + (_klen<<1) - 2; while (1) { if ( _upper < _lower ) break; _mid = _lower + (((_upper-_lower) >> 1) & ~1); if ( (*p) < _mid[0] ) _upper = _mid - 2; else if ( (*p) > _mid[1] ) _lower = _mid + 2; else { _trans += (unsigned int)((_mid - _keys)>>1); goto _match; } } _trans += _klen; } _match: _trans = _memc_stats_indicies[_trans]; cs = _memc_stats_trans_targs[_trans]; if ( _memc_stats_trans_actions[_trans] == 0 ) goto _again; switch ( _memc_stats_trans_actions[_trans] ) { case 2: #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 1: #line 4 "src/memc_common.rl" { dd("caught error..."); dd("machine state: %d", cs); *status_addr = NGX_HTTP_BAD_GATEWAY; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; #line 1613 "src/ngx_http_memc_response.c" } _again: if ( cs == 0 ) goto _out; if ( ++p != pe ) goto _resume; _test_eof: {} _out: {} } #line 627 "src/ngx_http_memc_response.rl" *cs_addr = cs; return p; } static u_char * parse_memc_delete(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; #line 641 "src/ngx_http_memc_response.rl" #line 642 "src/ngx_http_memc_response.rl" #line 1644 "src/ngx_http_memc_response.c" { int _klen; const short *_keys; int _trans; if ( p == pe ) goto _test_eof; if ( cs == 0 ) goto _out; _resume: _keys = _memc_delete_trans_keys + _memc_delete_key_offsets[cs]; _trans = _memc_delete_index_offsets[cs]; _klen = _memc_delete_single_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + _klen - 1; while (1) { if ( _upper < _lower ) break; _mid = _lower + ((_upper-_lower) >> 1); if ( (*p) < *_mid ) _upper = _mid - 1; else if ( (*p) > *_mid ) _lower = _mid + 1; else { _trans += (unsigned int)(_mid - _keys); goto _match; } } _keys += _klen; _trans += _klen; } _klen = _memc_delete_range_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + (_klen<<1) - 2; while (1) { if ( _upper < _lower ) break; _mid = _lower + (((_upper-_lower) >> 1) & ~1); if ( (*p) < _mid[0] ) _upper = _mid - 2; else if ( (*p) > _mid[1] ) _lower = _mid + 2; else { _trans += (unsigned int)((_mid - _keys)>>1); goto _match; } } _trans += _klen; } _match: cs = _memc_delete_trans_targs[_trans]; if ( _memc_delete_trans_actions[_trans] == 0 ) goto _again; switch ( _memc_delete_trans_actions[_trans] ) { case 2: #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 1: #line 4 "src/memc_common.rl" { dd("caught error..."); dd("machine state: %d", cs); *status_addr = NGX_HTTP_BAD_GATEWAY; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; case 3: #line 37 "src/memc_common.rl" { dd("status set to 404"); *status_addr = NGX_HTTP_NOT_FOUND; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } break; #line 1744 "src/ngx_http_memc_response.c" } _again: if ( cs == 0 ) goto _out; if ( ++p != pe ) goto _resume; _test_eof: {} _out: {} } #line 643 "src/ngx_http_memc_response.rl" *cs_addr = cs; return p; } static u_char * parse_memc_incr_decr(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; #line 657 "src/ngx_http_memc_response.rl" #line 658 "src/ngx_http_memc_response.rl" #line 1775 "src/ngx_http_memc_response.c" { int _klen; const short *_keys; int _trans; if ( p == pe ) goto _test_eof; if ( cs == 0 ) goto _out; _resume: _keys = _memc_incr_decr_trans_keys + _memc_incr_decr_key_offsets[cs]; _trans = _memc_incr_decr_index_offsets[cs]; _klen = _memc_incr_decr_single_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + _klen - 1; while (1) { if ( _upper < _lower ) break; _mid = _lower + ((_upper-_lower) >> 1); if ( (*p) < *_mid ) _upper = _mid - 1; else if ( (*p) > *_mid ) _lower = _mid + 1; else { _trans += (unsigned int)(_mid - _keys); goto _match; } } _keys += _klen; _trans += _klen; } _klen = _memc_incr_decr_range_lengths[cs]; if ( _klen > 0 ) { const short *_lower = _keys; const short *_mid; const short *_upper = _keys + (_klen<<1) - 2; while (1) { if ( _upper < _lower ) break; _mid = _lower + (((_upper-_lower) >> 1) & ~1); if ( (*p) < _mid[0] ) _upper = _mid - 2; else if ( (*p) > _mid[1] ) _lower = _mid + 2; else { _trans += (unsigned int)((_mid - _keys)>>1); goto _match; } } _trans += _klen; } _match: _trans = _memc_incr_decr_indicies[_trans]; cs = _memc_incr_decr_trans_targs[_trans]; if ( _memc_incr_decr_trans_actions[_trans] == 0 ) goto _again; switch ( _memc_incr_decr_trans_actions[_trans] ) { case 1: #line 26 "src/memc_common.rl" { dd("state %d, left %d, reading char '%c'", cs, (int) (pe - p), *p); } break; case 3: #line 4 "src/memc_common.rl" { dd("caught error..."); dd("machine state: %d", cs); *status_addr = NGX_HTTP_BAD_GATEWAY; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } #line 26 "src/memc_common.rl" { dd("state %d, left %d, reading char '%c'", cs, (int) (pe - p), *p); } break; case 2: #line 31 "src/memc_common.rl" { dd("status set to 201"); *status_addr = NGX_HTTP_CREATED; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } #line 26 "src/memc_common.rl" { dd("state %d, left %d, reading char '%c'", cs, (int) (pe - p), *p); } break; case 4: #line 37 "src/memc_common.rl" { dd("status set to 404"); *status_addr = NGX_HTTP_NOT_FOUND; } #line 21 "src/memc_common.rl" { dd("done it!"); *done_addr = 1; } #line 26 "src/memc_common.rl" { dd("state %d, left %d, reading char '%c'", cs, (int) (pe - p), *p); } break; #line 1904 "src/ngx_http_memc_response.c" } _again: if ( cs == 0 ) goto _out; if ( ++p != pe ) goto _resume; _test_eof: {} _out: {} } #line 659 "src/ngx_http_memc_response.rl" *cs_addr = cs; return p; } memc-nginx-module-0.20/src/ngx_http_memc_response.h000066400000000000000000000012361454304070500224760ustar00rootroot00000000000000 /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef NGX_HTTP_MEMC_RESPONSE_H #define NGX_HTTP_MEMC_RESPONSE_H #include #include ngx_int_t ngx_http_memc_process_get_cmd_header(ngx_http_request_t *r); ngx_int_t ngx_http_memc_get_cmd_filter_init(void *data); ngx_int_t ngx_http_memc_get_cmd_filter(void *data, ssize_t bytes); ngx_int_t ngx_http_memc_process_simple_header(ngx_http_request_t *r); ngx_int_t ngx_http_memc_empty_filter_init(void *data); ngx_int_t ngx_http_memc_empty_filter(void *data, ssize_t bytes); ngx_int_t ngx_http_memc_process_flush_all_cmd_header(ngx_http_request_t *r); #endif /* NGX_HTTP_MEMC_RESPONSE_H */ memc-nginx-module-0.20/src/ngx_http_memc_response.rl000066400000000000000000000376451454304070500227010ustar00rootroot00000000000000 /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_memc_response.h" #include "ngx_http_memc_module.h" #ifdef s_char #undef s_char #endif #define s_char signed char %% machine memc_storage; %% write data; %% machine memc_flush_all; %% write data; %% machine memc_version; %% write data; %% machine memc_stats; %% write data; %% machine memc_delete; %% write data; %% machine memc_incr_decr; %% write data; u_char ngx_http_memc_end[] = CRLF "END" CRLF; static u_char *parse_memc_storage(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_flush_all(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_version(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_stats(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_delete(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static u_char *parse_memc_incr_decr(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr); static ngx_int_t ngx_http_memc_write_simple_response(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_http_memc_ctx_t *ctx, ngx_uint_t status, ngx_str_t *resp); ngx_int_t ngx_http_memc_process_simple_header(ngx_http_request_t *r) { ngx_int_t rc; int cs; s_char *p; s_char *pe; s_char *orig; ngx_str_t resp; ngx_http_upstream_t *u; ngx_http_memc_ctx_t *ctx; ngx_uint_t status; unsigned done = 0; int error_state; int final_state; status = NGX_HTTP_OK; dd("process simple cmd header"); ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); if (ctx->parser_state == NGX_ERROR) { dd("reinit state"); if (ctx->is_storage_cmd) { dd("init memc_storage machine..."); %% machine memc_storage; %% write init; } else if (ctx->cmd == ngx_http_memc_cmd_flush_all) { dd("init memc_flush_all machine..."); %% machine memc_flush_all; %% write init; } else if (ctx->cmd == ngx_http_memc_cmd_version) { dd("init memc_version machine..."); %% machine memc_version; %% write init; } else if (ctx->cmd == ngx_http_memc_cmd_stats) { dd("init memc_stats machine..."); %% machine memc_stats; %% write init; } else if (ctx->cmd == ngx_http_memc_cmd_delete) { dd("init memc_delete machine..."); %% machine memc_delete; %% write init; } else if (ctx->cmd == ngx_http_memc_cmd_incr || ctx->cmd == ngx_http_memc_cmd_decr) { dd("init memc_incr_decr machine..."); %% machine memc_incr_decr; %% write init; } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unrecognized memcached command in " "ngx_http_memc_process_simple_header: \"%V\"", &ctx->cmd_str); return NGX_ERROR; /* this results in 500 status */ } } else { cs = ctx->parser_state; } u = r->upstream; orig = (s_char *) u->buffer.pos; p = (s_char *) u->buffer.pos; pe = (s_char *) u->buffer.last; dd("buffer len: %d", (int) (pe - p)); if (ctx->is_storage_cmd) { error_state = memc_storage_error; final_state = memc_storage_first_final; p = (s_char *) parse_memc_storage(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_flush_all) { error_state = memc_flush_all_error; final_state = memc_flush_all_first_final; p = (s_char *) parse_memc_flush_all(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_version) { error_state = memc_version_error; final_state = memc_version_first_final; p = (s_char *) parse_memc_version(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_stats) { error_state = memc_stats_error; final_state = memc_stats_first_final; p = (s_char *) parse_memc_stats(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_delete) { error_state = memc_delete_error; final_state = memc_delete_first_final; p = (s_char *) parse_memc_delete(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else if (ctx->cmd == ngx_http_memc_cmd_incr || ctx->cmd == ngx_http_memc_cmd_decr) { error_state = memc_incr_decr_error; final_state = memc_incr_decr_first_final; p = (s_char *) parse_memc_incr_decr(&cs, (u_char *) p, (u_char *) pe, &status, &done); } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unrecognized memcached command in " "ngx_http_memc_process_simple_header: \"%V\"", &ctx->cmd_str); return NGX_ERROR; /* this results in 500 status */ } ctx->parser_state = cs; resp.data = u->buffer.start; resp.len = (u_char *) p - resp.data; u->buffer.pos = (u_char *) p; dd("machine state: %d (done: %d)", cs, done); dd("memcached response: (len: %d) %s", (int) resp.len, resp.data); if (done || cs >= final_state) { dd("memcached response parsed (resp.len: %d)", (int) resp.len); rc = ngx_http_memc_write_simple_response(r, u, ctx, status, &resp); return rc; } if (cs == error_state) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid response for command \"%V\" " "at pos %O: %V", &ctx->cmd_str, (off_t) (p - orig), &resp); status = NGX_HTTP_BAD_GATEWAY; u->headers_in.status_n = status; u->state->status = status; /* u->headers_in.status_n will be the final status */ return NGX_OK; } dd("we need more data to proceed (returned NGX_AGAIN)"); return NGX_AGAIN; } ngx_int_t ngx_http_memc_empty_filter_init(void *data) { ngx_http_memc_ctx_t *ctx = data; ngx_http_upstream_t *u; u = ctx->request->upstream; u->length = 0; /* to persuade ngx_http_upstream_keepalive (if any) to cache the connection if the status is neither 200 nor 404. */ if (u->headers_in.status_n == NGX_HTTP_CREATED) { u->headers_in.status_n = NGX_HTTP_OK; } return NGX_OK; } ngx_int_t ngx_http_memc_empty_filter(void *data, ssize_t bytes) { ngx_http_memc_ctx_t *ctx = data; ngx_http_upstream_t *u; u = ctx->request->upstream; /* recover the buffer for subrequests in memory */ u->buffer.last += ctx->body_length; return NGX_OK; } ngx_int_t ngx_http_memc_get_cmd_filter_init(void *data) { ngx_http_memc_ctx_t *ctx = data; ngx_http_upstream_t *u; u = ctx->request->upstream; dd("filter init: u->length: %d", (int) u->length); u->length = u->headers_in.content_length_n + NGX_HTTP_MEMC_END; dd("filter init (2): u->length: %d", (int) u->length); return NGX_OK; } ngx_int_t ngx_http_memc_get_cmd_filter(void *data, ssize_t bytes) { ngx_http_memc_ctx_t *ctx = data; u_char *last; ngx_buf_t *b; ngx_chain_t *cl, **ll; ngx_http_upstream_t *u; u = ctx->request->upstream; b = &u->buffer; if (u->length == ctx->rest) { if (ngx_strncmp(b->last, ngx_http_memc_end + NGX_HTTP_MEMC_END - ctx->rest, bytes) != 0) { ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, "memcached sent invalid trailer"); u->length = 0; ctx->rest = 0; return NGX_OK; } u->length -= bytes; ctx->rest -= bytes; #if defined(nginx_version) && nginx_version >= 1001004 if (u->length == 0) { u->keepalive = 1; } #endif return NGX_OK; } for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { ll = &cl->next; } cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs); if (cl == NULL) { return NGX_ERROR; } cl->buf->flush = 1; cl->buf->memory = 1; *ll = cl; last = b->last; cl->buf->pos = last; b->last += bytes; cl->buf->last = b->last; cl->buf->tag = u->output.tag; ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0, "memcached filter bytes:%z size:%z length:%z rest:%z", bytes, b->last - b->pos, u->length, ctx->rest); if (bytes <= (ssize_t) (u->length - NGX_HTTP_MEMC_END)) { u->length -= bytes; return NGX_OK; } last += u->length - NGX_HTTP_MEMC_END; if (ngx_strncmp(last, ngx_http_memc_end, b->last - last) != 0) { ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, "memcached sent invalid trailer"); #if defined(nginx_version) && nginx_version >= 1001004 b->last = last; cl->buf->last = last; u->length = 0; ctx->rest = 0; return NGX_OK; #endif } ctx->rest -= b->last - last; b->last = last; cl->buf->last = last; u->length = ctx->rest; #if defined(nginx_version) && nginx_version >= 1001004 if (u->length == 0) { u->keepalive = 1; } #endif return NGX_OK; } ngx_int_t ngx_http_memc_process_get_cmd_header(ngx_http_request_t *r) { ngx_http_memc_loc_conf_t *conf; u_char *p, *len; ngx_str_t line; ngx_http_upstream_t *u; ngx_http_memc_ctx_t *ctx; ngx_http_variable_value_t *flags_vv; u = r->upstream; dd("process header: u->length: %u", (unsigned) u->length); for (p = u->buffer.pos; p < u->buffer.last; p++) { if (*p == LF) { goto found; } } return NGX_AGAIN; found: *p = '\0'; line.len = p - u->buffer.pos - 1; line.data = u->buffer.pos; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "memcached: \"%V\"", &line); p = u->buffer.pos; ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); if (ngx_strncmp(p, "VALUE ", sizeof("VALUE ") - 1) == 0) { p += sizeof("VALUE ") - 1; if (ngx_strncmp(p, ctx->key.data, ctx->key.len) != 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid key in response \"%V\" " "for key \"%V\"", &line, &ctx->key); return NGX_HTTP_UPSTREAM_INVALID_HEADER; } p += ctx->key.len; if (*p++ != ' ') { goto no_valid; } /* save flags */ flags_vv = ctx->memc_flags_vv; if (flags_vv == NULL) { return NGX_ERROR; } if (flags_vv->not_found) { flags_vv->not_found = 0; flags_vv->valid = 1; flags_vv->no_cacheable = 0; } flags_vv->data = p; while (*p) { if (*p++ == ' ') { flags_vv->len = p - 1 - flags_vv->data; conf = ngx_http_get_module_loc_conf(r, ngx_http_memc_module); if (conf->flags_to_last_modified) { r->headers_out.last_modified_time = ngx_atotm(flags_vv->data, flags_vv->len); } goto length; } } goto no_valid; length: len = p; while (*p && *p++ != CR) { /* void */ } #if defined(nginx_version) && nginx_version >= 1001004 u->headers_in.content_length_n = ngx_atoof(len, p - len - 1); if (u->headers_in.content_length_n == -1) { #else r->headers_out.content_length_n = ngx_atoof(len, p - len - 1); if (r->headers_out.content_length_n == -1) { #endif ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid length in response \"%V\" " "for key \"%V\"", &line, &ctx->key); return NGX_HTTP_UPSTREAM_INVALID_HEADER; } u->headers_in.status_n = NGX_HTTP_OK; u->state->status = NGX_HTTP_OK; u->buffer.pos = p + 1; return NGX_OK; } if (ngx_strcmp(p, "END\x0d") == 0) { ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "key: \"%V\" was not found by memcached", &ctx->key); u->headers_in.status_n = NGX_HTTP_NOT_FOUND; u->state->status = NGX_HTTP_NOT_FOUND; #if defined(nginx_version) && nginx_version >= 1001004 u->keepalive = 1; #endif return NGX_OK; } no_valid: ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "memcached sent invalid response: \"%V\"", &line); return NGX_HTTP_UPSTREAM_INVALID_HEADER; } static ngx_int_t ngx_http_memc_write_simple_response(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_http_memc_ctx_t *ctx, ngx_uint_t status, ngx_str_t *resp) { ngx_chain_t *cl, **ll; for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { ll = &cl->next; } cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); if (cl == NULL) { return NGX_ERROR; } cl->buf->flush = 1; cl->buf->memory = 1; cl->buf->pos = resp->data; cl->buf->last = cl->buf->pos + resp->len; *ll = cl; /* for subrequests in memory */ u->buffer.pos = resp->data; u->buffer.last = resp->data + resp->len; ctx->body_length = resp->len; #if defined(nginx_version) && nginx_version >= 1001004 u->headers_in.content_length_n = resp->len; u->keepalive = 1; #else r->headers_out.content_length_n = resp->len; #endif u->headers_in.status_n = status; u->state->status = status; return NGX_OK; } static u_char * parse_memc_storage(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; %% machine memc_storage; %% include "memc_storage.rl"; %% write exec; *cs_addr = cs; return p; } static u_char * parse_memc_flush_all(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; %% machine memc_flush_all; %% include "memc_flush_all.rl"; %% write exec; *cs_addr = cs; return p; } static u_char * parse_memc_version(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; %% machine memc_version; %% include "memc_version.rl"; %% write exec; *cs_addr = cs; return p; } static u_char * parse_memc_stats(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; %% machine memc_stats; %% include "memc_stats.rl"; %% write exec; *cs_addr = cs; return p; } static u_char * parse_memc_delete(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; %% machine memc_delete; %% include "memc_delete.rl"; %% write exec; *cs_addr = cs; return p; } static u_char * parse_memc_incr_decr(int *cs_addr, u_char *p, u_char *pe, ngx_uint_t *status_addr, unsigned *done_addr) { int cs = *cs_addr; %% machine memc_incr_decr; %% include "memc_incr_decr.rl"; %% write exec; *cs_addr = cs; return p; } memc-nginx-module-0.20/src/ngx_http_memc_util.c000066400000000000000000000072371454304070500216170ustar00rootroot00000000000000 /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_memc_util.h" ngx_http_memc_cmd_t ngx_http_memc_parse_cmd(u_char *data, size_t len, ngx_flag_t *is_storage_cmd) { switch (len) { case 3: if (ngx_http_memc_strcmp_const(data, "set") == 0) { *is_storage_cmd = 1; return ngx_http_memc_cmd_set; } if (ngx_http_memc_strcmp_const(data, "add") == 0) { *is_storage_cmd = 1; return ngx_http_memc_cmd_add; } /* if (ngx_str3cmp(data, 'c', 'a', 's')) { *is_storage_cmd = 1; return ngx_http_memc_cmd_cas; } */ if (ngx_http_memc_strcmp_const(data, "get") == 0) { return ngx_http_memc_cmd_get; } break; case 4: /* if (ngx_str4cmp(data, 'g', 'e', 't', 's')) { return ngx_http_memc_cmd_gets; } */ if (ngx_http_memc_strcmp_const(data, "incr") == 0) { return ngx_http_memc_cmd_incr; } if (ngx_http_memc_strcmp_const(data, "decr") == 0) { return ngx_http_memc_cmd_decr; } break; case 5: if (ngx_http_memc_strcmp_const(data, "stats") == 0) { return ngx_http_memc_cmd_stats; } break; case 6: if (ngx_http_memc_strcmp_const(data, "append") == 0) { *is_storage_cmd = 1; return ngx_http_memc_cmd_append; } if (ngx_http_memc_strcmp_const(data, "delete") == 0) { return ngx_http_memc_cmd_delete; } break; case 7: if (ngx_http_memc_strcmp_const(data, "replace") == 0) { *is_storage_cmd = 1; return ngx_http_memc_cmd_replace; } if (ngx_http_memc_strcmp_const(data, "prepend") == 0) { *is_storage_cmd = 1; return ngx_http_memc_cmd_prepend; } if (ngx_http_memc_strcmp_const(data, "version") == 0) { return ngx_http_memc_cmd_version; } break; case 9: if (ngx_http_memc_strcmp_const(data, "flush_all") == 0) { return ngx_http_memc_cmd_flush_all; } /* if (ngx_str9cmp(data, 'v', 'e', 'r', 'b', 'o', 's', 'i', 't', 'y')) { return ngx_http_memc_cmd_verbosity; } */ break; default: break; } return ngx_http_memc_cmd_unknown; } ngx_http_upstream_srv_conf_t * ngx_http_memc_upstream_add(ngx_http_request_t *r, ngx_url_t *url) { ngx_http_upstream_main_conf_t *umcf; ngx_http_upstream_srv_conf_t **uscfp; ngx_uint_t i; umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); uscfp = umcf->upstreams.elts; for (i = 0; i < umcf->upstreams.nelts; i++) { if (uscfp[i]->host.len != url->host.len || ngx_strncasecmp(uscfp[i]->host.data, url->host.data, url->host.len) != 0) { dd("upstream_add: host not match"); continue; } if (uscfp[i]->port != url->port) { dd("upstream_add: port not match: %d != %d", (int) uscfp[i]->port, (int) url->port); continue; } #if (nginx_version < 1011006) if (uscfp[i]->default_port && url->default_port && uscfp[i]->default_port != url->default_port) { dd("upstream_add: default_port not match"); continue; } #endif return uscfp[i]; } dd("No upstream found: %.*s", (int) url->host.len, url->host.data); return NULL; } memc-nginx-module-0.20/src/ngx_http_memc_util.h000066400000000000000000000013001454304070500216050ustar00rootroot00000000000000 /* * Copyright (C) Yichun Zhang (agentzh) */ #ifndef NGX_HTTP_MEMC_UTIL_H #define NGX_HTTP_MEMC_UTIL_H #include #include #include "ngx_http_memc_module.h" #ifndef NGX_UINT32_LEN #define NGX_UINT32_LEN (NGX_INT32_LEN - 1) #endif #ifndef NGX_UINT64_LEN #define NGX_UINT64_LEN (NGX_INT64_LEN - 1) #endif #define ngx_http_memc_strcmp_const(a, b) \ ngx_strncmp(a, b, sizeof(b) - 1) ngx_http_memc_cmd_t ngx_http_memc_parse_cmd(u_char *data, size_t len, ngx_flag_t *is_storage_cmd); ngx_http_upstream_srv_conf_t *ngx_http_memc_upstream_add( ngx_http_request_t *r, ngx_url_t *url); #endif /* NGX_HTTP_MEMC_UTIL_H */ memc-nginx-module-0.20/t/000077500000000000000000000000001454304070500152255ustar00rootroot00000000000000memc-nginx-module-0.20/t/bugs.t000066400000000000000000000117561454304070500163640ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(3); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; no_shuffle(); no_long_string(); run_tests(); __DATA__ === TEST 1: set only --- http_config upstream mc { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location = "/set" { set $memc_cmd 'set'; set $memc_key 'jalla'; set $memc_value 'myvalue'; set $memc_exptime 24; memc_pass mc; } location = "/get" { set $memc_cmd 'get'; set $memc_key 'jalla'; memc_pass mc; } location = "/delete" { set $memc_cmd 'delete'; set $memc_key 'jalla'; memc_pass mc; } location = "/flush" { echo_location /get; #echo ""; echo_location /delete; } --- request GET /flush --- response_body eval "STORED\r\n" --- error_code: 201 --- SKIP === TEST 2: set in a subrequest issued from an output filter --- config location /memc { set $memc_cmd 'set'; set $memc_key 'foo'; set $memc_value 'blah'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } location /main { default_type 'text/html'; add_before_body '/memc'; echo '~~'; add_after_body '/memc'; } --- request GET /main --- response_body eval "STORED\r ~~ STORED\r " === TEST 3: reuse request body --- config location /main { echo_read_request_body; echo 'flush_all'; echo_location '/memc?cmd=flush_all'; echo 'set foo'; echo_subrequest POST '/memc?key=foo&cmd=set'; echo 'set bar'; echo_subrequest POST '/memc?key=bar&cmd=set'; echo 'get bar'; echo_location '/memc?key=bar&cmd=get'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; #set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request POST /main Hello --- response_body eval "flush_all status: 200 exptime: OK\r set foo status: 201 exptime: 0 STORED\r set bar status: 201 exptime: 0 STORED\r get bar status: 200 exptime: Helloget foo status: 200 exptime: Hello" === TEST 4: zero buf when $memc_value is empty http://github.com/agentzh/memc-nginx-module/issues#issue/2 --- config location /memc { set $memc_cmd 'set'; set $memc_key 'foo'; set $memc_value ''; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc --- response_body eval "STORED\r " --- error_code: 201 === TEST 5: zero buf when $memc_value is empty http://github.com/agentzh/memc-nginx-module/issues#issue/2 --- config location /memc { set $memc_cmd 'set'; set $memc_key 'foo'; set $memc_value ''; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc --- response_body eval "STORED\r " --- error_code: 201 === TEST 6: set too long keys --- config location /memc { set $memc_cmd 'set'; set $memc_key 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'; set $memc_value 'hi'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc --- response_body eval "CLIENT_ERROR bad command line format\r " --- error_code: 502 === TEST 7: get too long keys --- config location /memc { set $memc_cmd 'get'; set $memc_key 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc --- response_body_like: 502 Bad Gateway --- error_code: 502 === TEST 8: set only --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $echo_request_body; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request POST /memc?key=foo&cmd=set hello, world --- response_body eval "STORED\r\n" --- error_code: 201 --- SKIP === TEST 9: get --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc?key=foo&cmd=get --- response_body hello, world --- error_code: 200 --- SKIP memc-nginx-module-0.20/t/cmd.t000066400000000000000000000057011454304070500161600ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; plan tests => repeat_each() * (2 * blocks() + 3); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: bad cmd --- config location /foo { set $memc_cmd blah; set $memc_key foo; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /foo --- response_body_like: 400 Bad Request --- error_code: 400 --- error_log ngx_memc: unknown $memc_cmd "blah" === TEST 2: no default value for $memc_cmd for method COPY --- config location /foo { set $memc_key foo; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request COPY /foo --- response_body_like: 400 Bad Request --- error_code: 400 --- error_log ngx_memc: $memc_cmd variable not found for HTTP COPY requests === TEST 3: bad cmd (case sensitive) --- config location /foo { set $memc_cmd GET; set $memc_key foo; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /foo --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 4: explicit $memc_cmd in non-empty cmds_allowed list --- config location /allow { set $memc_cmd version; memc_cmds_allowed get version; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: ^VERSION \d+(\.\d+)+.*?\r\n$ === TEST 5: explicit $memc_cmd in non-empty cmds_allowed list (in first) --- config location /allow { set $memc_cmd version; memc_cmds_allowed version get; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: ^VERSION \d+(\.\d+)+.*?\r\n$ === TEST 6: explicit $memc_cmd NOT in non-empty cmds_allowed list --- config location /allow { set $memc_cmd version; memc_cmds_allowed set get add delete; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 403 Forbidden --- error_code: 403 --- error_log ngx_memc: memcached command "version" not allowed === TEST 7: implicit $memc_cmd NOT in non-empty cmds_allowed list --- config location /allow { memc_cmds_allowed set add delete version; set $memcached_key foo; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 403 Forbidden --- error_code: 403 === TEST 8: $memc_cmd has its default values when it's an empty string --- config location /main { echo_read_request_body; echo 'set big2'; echo_subrequest PUT '/memc?key=big2'; echo 'get big2'; echo_location '/memc?key=big2&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request POST /main nice to meet you! --- response_body eval "set big2 STORED\r get big2 nice to meet you!" memc-nginx-module-0.20/t/core-bugs.t000066400000000000000000000023721454304070500173040ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; no_shuffle(); #no_diff; #no_long_string(); run_tests(); __DATA__ === TEST 1: bug in nginx core? (1) --- http_config upstream foo { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location /set { set $memc_key foo; set $memc_cmd set; set $memc_value '[["mafiaclans.eu", 12], ["picfu.net", 5], ["www.test.com", 0], ["www.ayom.com", 0], ["www.21dezember2012.org", 0], ["the-indie.ch", 0], ["spiele-check.de", 0], ["online-right-now.net", 0], ["google.com", 0]]'; memc_pass foo; } --- request GET /set --- response_body eval "STORED\r\n" --- error_code: 201 === TEST 2: bug in nginx core? (2) --- http_config upstream foo { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location /get { set $memcached_key foo; memcached_pass foo; } --- request GET /get --- response_body eval qq{[["mafiaclans.eu", 12], ["picfu.net", 5], ["www.test.com", 0], ["www.ayom.com", 0], ["www.21dezember2012.org", 0], ["the-indie.ch", 0], ["spiele-check.de", 0], ["online-right-now.net", 0], ["google.com", 0]]} memc-nginx-module-0.20/t/delete.t000066400000000000000000000053231454304070500166570ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: delete non-existent item --- config location /main { echo 'flush_all'; echo_location '/memc?cmd=flush_all'; echo 'delete foo'; echo_location '/memc?key=foo&cmd=delete'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body_like ^flush_all status: 200 exptime: OK\r delete foo status: 404 exptime: .*?404 Not Found.*$ === TEST 2: set and delete and set --- config location /main { echo 'set foo bar'; echo_location '/memc?cmd=set&key=foo&val=bar'; echo 'delete foo'; echo_location '/memc?key=foo&cmd=delete'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body_like ^set foo bar status: 201 exptime: 0 STORED\r delete foo status: 200 exptime: DELETED\r get foo status: 404 exptime: .*?404 Not Found.*$ === TEST 3: set and delete and set (with exptime) --- config location /main { echo 'set foo bar'; echo_location '/memc?cmd=set&key=foo&val=bar'; echo 'delete foo 1'; echo_location '/memc?key=foo&cmd=delete&exptime=1'; echo 'add foo cat'; echo_location '/memc?key=foo&cmd=add&val=cat'; echo_blocking_sleep 1.6; echo 'add foo cat - 2nd'; echo_location '/memc?key=foo&cmd=add&val=cat'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo bar status: 201 exptime: 0 STORED\r delete foo 1 status: 200 exptime: 1 DELETED\r add foo cat status: 200 exptime: 0 NOT_STORED\r add foo cat - 2nd status: 201 exptime: 0 STORED\r " --- SKIP memc-nginx-module-0.20/t/errors.t000066400000000000000000000013261454304070500167300ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: empty pass little-endian systems only --- config location /memc { set $memc_key foo; set $backend "not-exist"; memc_pass $backend; } --- request GET /memc --- error_code: 500 --- response_body_like: 500 Internal Server Error === TEST 2: connection refused little-endian systems only --- config location /memc { set $memc_key foo; set $backend "not-exist"; memc_pass 127.0.0.1:1; } --- request GET /memc --- error_code: 502 --- response_body_like: 502 Bad Gateway memc-nginx-module-0.20/t/eval.t000066400000000000000000000043561454304070500163510ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; # skip_all => 'ngx_memc storage commands do not work with the ngx_eval module'; repeat_each(2); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; no_long_string(); #no_diff; run_tests(); __DATA__ === TEST 1: set in eval (NO subrequest in memory) --- http_config upstream mc { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location = /main { echo_location /eval; echo_location /eval; } location = /eval { eval_subrequest_in_memory off; eval_override_content_type text/plain; eval $res { default_type 'text/plain'; set $memc_cmd 'set'; set $memc_key 'foo'; set $memc_value 'myvalue'; set $memc_exptime 24; memc_pass mc; } echo [$res]; } --- request GET /eval --- response_body [STORED] --- timeout: 30 --- error_code: 200 === TEST 2: set in eval (subrequest in memory) --- http_config upstream mc { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location = /main { echo_location /eval; echo_location /eval; } location = /eval { eval_subrequest_in_memory on; eval_override_content_type text/plain; eval $res { default_type 'text/plain'; set $memc_cmd 'set'; set $memc_key 'foo'; set $memc_value 'myvalue'; set $memc_exptime 24; memc_pass mc; } echo [$res]; } --- request GET /eval --- response_body [STORED] --- timeout: 30 --- error_code: 200 === TEST 3: set in eval (subrequest in memory) --- http_config upstream mc { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location = /main { echo_location /get; echo_location /del?foo; } location = /get { set $memc_cmd get; set $memc_key foo; memc_pass mc; } location = /del { eval $res { set $memc_cmd delete; set $memc_key $query_string; memc_pass mc; } return 200; } --- request GET /main --- response_body [STORED] --- timeout: 30 --- error_code: 200 --- SKIP memc-nginx-module-0.20/t/exptime.t000066400000000000000000000064141454304070500170720ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * (2 * blocks() + 1); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: set exptime --- config location /exptime { echo 'flush_all'; echo_location '/memc?cmd=flush_all'; echo 'set foo BAR'; echo_subrequest PUT '/memc?key=foo&exptime=1' -b BAR; echo 'get foo - 0 sec'; echo_location '/memc?key=foo'; echo; echo_blocking_sleep 1.1; echo 'get foo - 1.1 sec'; echo_location '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /exptime --- response_body_like ^flush_all status: 200 exptime: OK\r set foo BAR status: 201 exptime: 1 STORED\r get foo - 0 sec status: 200 exptime: BAR get foo - 1\.1 sec status: 404 exptime: .*?404 Not Found.*$ === TEST 2: test empty flags (default to 0) --- config location /flags { echo 'set foo BAR (exptime: EMPTY)'; echo_subrequest PUT '/memc?key=foo' -b BAR; echo_blocking_sleep 1; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_key $arg_key; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /flags --- response_body eval "set foo BAR (exptime: EMPTY) status: 201 exptime: 0 STORED\r get foo status: 200 exptime: BAR" === TEST 3: invalid exptime in set --- config location /allow { set $memc_cmd 'set'; set $memc_key 'foo'; set $memc_value 'nice'; set $memc_exptime 'my invalid'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 400 Bad Request --- error_code: 400 --- error_log variable "$memc_exptime" takes invalid value: my invalid, === TEST 4: invalid exptime in flush_all --- config location /allow { set $memc_cmd 'flush_all'; set $memc_key 'foo'; set $memc_value 'nice'; set $memc_exptime 'invalid'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 5: invalid exptime in delete --- config location /allow { set $memc_cmd 'delete'; set $memc_key 'foo'; set $memc_value 'nice'; set $memc_exptime 'invalid'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 6: set negative exptime --- config location /allow { set $memc_cmd 'set'; set $memc_key 'sun'; set $memc_value 'tree'; set $memc_exptime '-1'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 400 Bad Request --- error_code: 400 memc-nginx-module-0.20/t/flags.t000066400000000000000000000125311454304070500165100ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * (2 * blocks() + 3); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; no_shuffle; run_tests(); __DATA__ === TEST 1: set flags and get flags --- config location /flags { echo 'set foo BAR (flag: 1234567890)'; echo_subrequest PUT '/memc?key=foo&flags=1234567890' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "flags: $memc_flags"; set $memc_key $arg_key; set $memc_flags $arg_flags; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /flags --- response_body eval "set foo BAR (flag: 1234567890) status: 201 flags: 1234567890 STORED\r get foo status: 200 flags: 1234567890 BAR" === TEST 2: test empty flags (default to 0) --- config location /flags { echo 'set foo BAR (flag: EMPTY)'; echo_subrequest PUT '/memc?key=foo' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "flags: $memc_flags"; set $memc_key $arg_key; set $memc_flags $arg_flags; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /flags --- response_body eval "set foo BAR (flag: EMPTY) status: 201 flags: 0 STORED\r get foo status: 200 flags: 0 BAR" === TEST 3: test empty flags (default to 0) (another form) --- config location /flags { echo 'set foo BAR (flag: EMPTY)'; echo_subrequest PUT '/memc?key=foo&flags=' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "flags: $memc_flags"; set $memc_key $arg_key; set $memc_flags $arg_flags; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /flags --- response_body eval "set foo BAR (flag: EMPTY) status: 201 flags: 0 STORED\r get foo status: 200 flags: 0 BAR" === TEST 4: add flags and get flags --- config location /flags { echo 'flush_all'; echo_subrequest GET '/memc?cmd=flush_all'; echo 'add foo BAR (flag: 54321)'; echo_subrequest POST '/memc?key=foo&flags=54321' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "flags: $memc_flags"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_flags $arg_flags; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /flags --- response_body eval "flush_all status: 200 flags: OK\r add foo BAR (flag: 54321) status: 201 flags: 54321 STORED\r get foo status: 200 flags: 54321 BAR" === TEST 5: set invalid flags --- config location /allow { set $memc_cmd 'set'; set $memc_key 'foo'; set $memc_value 'nice'; set $memc_flags 'invalid'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 400 Bad Request --- error_code: 400 --- error_log variable "$memc_flags" takes invalid value: invalid, === TEST 6: set negative flags --- config location /allow { set $memc_cmd 'set'; set $memc_key 'sun'; set $memc_value 'tree'; set $memc_flags '-1'; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /allow --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 7: set flags and get flags in http time --- config location /flags { echo 'set foo BAR (flag: 1264680563)'; echo_subrequest PUT '/memc?key=foo&flags=1264680563' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "flags: $memc_flags $memc_flags_as_http_time"; set $memc_key $arg_key; set $memc_flags $arg_flags; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /flags --- response_body eval "set foo BAR (flag: 1264680563) status: 201 flags: 1264680563 Thu, 28 Jan 2010 12:09:23 GMT STORED\r get foo status: 200 flags: 1264680563 Thu, 28 Jan 2010 12:09:23 GMT BAR" === TEST 8: last-modified (conditional GET) --- SKIP --- config location /memc { set $memc_key $arg_key; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; memc_flags_to_last_modified on; add_header X-Flags $memc_flags; } --- request GET /memc?key=foo --- more_headers If-Modified-Since: Thu, 28 Jan 2010 12:09:23 GMT --- response_headers Last-Modified: Thu, 28 Jan 2010 12:09:23 GMT X-Flags: 1264680563 --- error_code: 304 --- response_body: === TEST 9: last-modified (unconditional GET) --- config location /memc { set $memc_key $arg_key; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; memc_flags_to_last_modified on; add_header X-Flags $memc_flags; } --- request GET /memc?key=foo --- response_headers Last-Modified: Thu, 28 Jan 2010 12:09:23 GMT X-Flags: 1264680563 --- error_code: 200 --- response_body: BAR memc-nginx-module-0.20/t/flush-all.t000066400000000000000000000042131454304070500173010ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: flush_all --- config location /flush { set $memc_cmd flush_all; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /flush --- response_body eval "OK\r " === TEST 2: set and flush and get --- config location /main { echo 'set foo blah'; echo_location '/memc?key=foo&cmd=set&val=blah'; echo 'flush_all'; echo_location '/memc?cmd=flush_all'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body_like ^set foo blah status: 201 STORED\r flush_all status: 200 OK\r get foo status: 404.*?404 Not Found.*$ === TEST 3: set exptime --- config location /exptime { echo 'flush_all'; echo_location '/memc?cmd=flush_all'; echo 'set foo BAR'; echo_subrequest PUT '/memc?key=foo' -b BAR; echo 'flush_all exptime=2'; echo_location '/memc?cmd=flush_all&exptime=2'; echo 'get foo - 0 sec'; echo_location '/memc?key=foo'; echo; echo_blocking_sleep 2; echo 'get foo - 2 sec'; echo_location '/memc?key=foo'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /exptime --- response_body_like flush_all status: 200 exptime: OK\r set foo BAR status: 201 exptime: 0 STORED\r flush_all exptime=2 status: 200 exptime: 2 OK\r get foo - 0 sec status: 200 exptime: BAR get foo - 2 sec status: 404 exptime: .*?404 Not Found.*$ --- timeout: 3 memc-nginx-module-0.20/t/if.t000066400000000000000000000007071454304070500160140ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; plan tests => repeat_each() * (3 * blocks()); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: bad cmd --- config location /foo { if ($uri ~* 'foo') { set $memc_cmd flush_all; } memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /foo --- response_body eval "OK\r\n" --- no_error_log [error] memc-nginx-module-0.20/t/incr-decr.t000066400000000000000000000132751454304070500172700ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * (2 * blocks() + 1); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: value required for incr --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc?cmd=incr&key=foo --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 2: invalid value for incr --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc?cmd=incr&key=foo&val=nice --- response_body_like: 400 Bad Request --- error_code: 400 --- error_log variable "$memc_value" is invalid for incr/decr: nice, === TEST 3: invalid value (negative intenger) for incr --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc?cmd=incr&key=foo&val=-5 --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 4: key required for incr --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc?cmd=incr&val=2 --- response_body_like: 400 Bad Request --- error_code: 400 --- SKIP --- TODO === TEST 5: incr --- config location /main { echo 'set foo 32'; echo_location '/memc?cmd=set&key=foo&val=32'; echo 'incr 51'; echo_location '/memc?key=foo&cmd=incr&val=51'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo 32 status: 201 exptime: 0 STORED\r incr 51 status: 201 exptime: 83\r get foo status: 200 exptime: 83" === TEST 6: decr --- config location /main { echo 'set foo 32'; echo_location '/memc?cmd=set&key=foo&val=32'; echo 'decr 13'; echo_location '/memc?key=foo&cmd=decr&val=13'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo 32 status: 201 exptime: 0 STORED\r decr 13 status: 201 exptime: 19\r get foo status: 200 exptime: 19" === TEST 7: incr an non-existent key --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'incr 51'; echo_location '/memc?key=foo&cmd=incr&val=51'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body_like ^flush all status: 200 exptime: OK\r incr 51 status: 404 exptime: .*?404 Not Found.*$ === TEST 8: decr --- config location /main { echo 'set foo 32'; echo_location '/memc?cmd=set&key=foo&val=32'; echo 'decr 13'; echo_location '/memc?key=foo&cmd=decr&val=13'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo 32 status: 201 exptime: 0 STORED\r decr 13 status: 201 exptime: 19\r get foo status: 200 exptime: 19" === TEST 9: incr an non-existent key (with fallback) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'incr 51'; echo_location '/memc?key=foo&cmd=incr&val=51'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; error_page 404 = /set_and_incr?$query_string; } location /set_and_incr { internal; echo_location /memc?cmd=add&key=$arg_key&val=0; echo_location /memc?$query_string; } --- request GET /main --- response_body eval "flush all status: 200 exptime: OK\r incr 51 status: 201 exptime: 0 STORED\r status: 201 exptime: 51\r " memc-nginx-module-0.20/t/keepalive.t000066400000000000000000000012331454304070500173560ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(3); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: keepalive --- http_config upstream backend { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; #server 127.0.0.1:11985; #server 127.0.0.1:11986; keepalive 2; #hash $arg_key; } --- config location /memc { set $memc_cmd set; set $memc_key $arg_key; set $memc_value 'value'; memc_pass backend; } --- request GET /memc?key=dog --- response_body eval "STORED\r " --- error_code: 201 memc-nginx-module-0.20/t/keepalive/000077500000000000000000000000001454304070500171725ustar00rootroot00000000000000memc-nginx-module-0.20/t/keepalive/incr-decr.t000066400000000000000000000134361454304070500212340ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; our $http_config = <<'_EOC_'; upstream foo { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; keepalive 1; } _EOC_ #no_diff; run_tests(); __DATA__ === TEST 1: value required for incr --- http_config eval: $::http_config --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /memc?cmd=incr&key=foo --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 2: invalid value for incr --- http_config eval: $::http_config --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /memc?cmd=incr&key=foo&val=nice --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 3: invalid value (negative intenger) for incr --- http_config eval: $::http_config --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /memc?cmd=incr&key=foo&val=-5 --- response_body_like: 400 Bad Request --- error_code: 400 === TEST 4: key required for incr --- http_config eval: $::http_config --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /memc?cmd=incr&val=2 --- response_body_like: 400 Bad Request --- error_code: 400 --- SKIP --- TODO === TEST 5: incr --- http_config eval: $::http_config --- config location /main { echo 'set foo 32'; echo_location '/memc?cmd=set&key=foo&val=32'; echo 'incr 51'; echo_location '/memc?key=foo&cmd=incr&val=51'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /main --- response_body eval "set foo 32 status: 201 exptime: 0 STORED\r incr 51 status: 201 exptime: 83\r get foo status: 200 exptime: 83" === TEST 6: decr --- http_config eval: $::http_config --- config location /main { echo 'set foo 32'; echo_location '/memc?cmd=set&key=foo&val=32'; echo 'decr 13'; echo_location '/memc?key=foo&cmd=decr&val=13'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /main --- response_body eval "set foo 32 status: 201 exptime: 0 STORED\r decr 13 status: 201 exptime: 19\r get foo status: 200 exptime: 19" === TEST 7: incr an non-existent key --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'incr 51'; echo_location '/memc?key=foo&cmd=incr&val=51'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /main --- response_body_like ^flush all status: 200 exptime: OK\r incr 51 status: 404 exptime: .*?404 Not Found.*$ === TEST 8: decr --- http_config eval: $::http_config --- config location /main { echo 'set foo 32'; echo_location '/memc?cmd=set&key=foo&val=32'; echo 'decr 13'; echo_location '/memc?key=foo&cmd=decr&val=13'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /main --- response_body eval "set foo 32 status: 201 exptime: 0 STORED\r decr 13 status: 201 exptime: 19\r get foo status: 200 exptime: 19" === TEST 9: incr an non-existent key (with fallback) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'incr 51'; echo_location '/memc?key=foo&cmd=incr&val=51'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; error_page 404 = /set_and_incr?$query_string; } location /set_and_incr { internal; echo_location /memc?cmd=add&key=$arg_key&val=0; echo_location /memc?$query_string; } --- request GET /main --- response_body eval "flush all status: 200 exptime: OK\r incr 51 status: 201 exptime: 0 STORED\r status: 201 exptime: 51\r " memc-nginx-module-0.20/t/keepalive/storage.t000066400000000000000000000337171454304070500210360ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * (2 * blocks() - 1); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; our $http_config = <<'_EOC_'; upstream foo { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; keepalive 1; } _EOC_ #no_shuffle(); #no_diff; log_level('warn'); run_tests(); __DATA__ === TEST 1: set only --- http_config eval: $::http_config --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /memc?key=foo&cmd=set&val=blah --- response_body eval "STORED\r\n" --- error_code: 201 === TEST 2: set and get --- http_config eval: $::http_config --- config location /main { echo 'set foo blah'; echo_location '/memc?key=foo&cmd=set&val=blah'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "set foo blah STORED\r get foo blah" === TEST 3: set UTF-8 and get UTF-8 --- http_config eval: $::http_config --- config location /main { echo 'set foo 你好'; echo_location '/memc?key=foo&cmd=set&val=你好'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "set foo 你好 STORED\r get foo 你好" === TEST 4: set and get empty values --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'set foo blah'; echo_location '/memc?key=foo&cmd=set&val='; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "flush all OK\r set foo blah STORED\r get foo " === TEST 5: add --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'add foo blah'; echo_location '/memc?key=foo&cmd=add&val=added'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo blah status: 201 STORED\r get foo status: 200 added" === TEST 6: set using POST --- http_config eval: $::http_config --- config location /main { echo_read_request_body; echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'set foo'; echo_subrequest POST '/memc?key=foo&cmd=set'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass foo; } --- request POST /main hello, world --- response_body eval "flush all status: 200 OK\r set foo status: 201 STORED\r get foo status: 200 hello, world" === TEST 7: default REST interface when no $memc_cmd is set --- http_config eval: $::http_config --- config location /main { echo_read_request_body; echo 'set foo FOO'; echo_subrequest PUT '/memc?key=foo' -b FOO; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; echo 'set foo BAR'; echo_subrequest PUT '/memc?key=foo' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "set foo FOO status: 201 STORED\r get foo status: 200 FOO set foo BAR status: 201 STORED\r get foo status: 200 BAR " === TEST 8: default REST interface when no $memc_cmd is set (read client req body) --- http_config eval: $::http_config --- config location /main { echo_read_request_body; echo 'set foo '; echo_subrequest PUT '/memc?key=foo'; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; echo 'set foo BAR'; echo_subrequest PUT '/memc?key=foo' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass foo; } --- request POST /main rock --- response_body eval "set foo status: 201 STORED\r get foo status: 200 rock set foo BAR status: 201 STORED\r get foo status: 200 BAR " === TEST 9: default REST interface when no $memc_cmd is set (read client req body) --- http_config eval: $::http_config --- config location /main { echo_read_request_body; echo 'set foo '; echo_subrequest PUT '/memc?key=foo'; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; echo 'add foo BAR'; echo_subrequest POST '/memc?key=foo' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass foo; } --- request POST /main howdy --- response_body eval "set foo status: 201 STORED\r get foo status: 200 howdy add foo BAR status: 200 NOT_STORED\r get foo status: 200 howdy " === TEST 10: test replace (stored) (without sleep) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; #echo_sleep 0.001; echo 'add foo blah'; echo_location '/memc?key=foo&cmd=add&val=added'; echo 'replace foo bah'; echo_location '/memc?key=foo&cmd=replace&val=bah'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo blah status: 201 STORED\r replace foo bah status: 201 STORED\r get foo status: 200 bah" === TEST 11: test replace (stored) (with sleep) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo_blocking_sleep 0.001; echo 'add foo blah'; echo_location '/memc?key=foo&cmd=add&val=added'; #echo_sleep 0.001; echo 'replace foo bah'; echo_location '/memc?key=foo&cmd=replace&val=bah'; #echo_sleep 0.001; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo blah status: 201 STORED\r replace foo bah status: 201 STORED\r get foo status: 200 bah" === TEST 12: test replace (not stored) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'replace foo bah'; echo_location '/memc?key=foo&cmd=replace&val=bah'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body_like flush all status: 200 OK\r replace foo bah status: 200 NOT_STORED\r get foo status: 404.*?404 Not Found.*$ === TEST 13: test append (stored) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'add foo hello'; echo_location '/memc?key=foo&cmd=add&val=hello'; echo 'append foo ,world'; echo_location '/memc?key=foo&cmd=append&val=,world'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo hello status: 201 STORED\r append foo ,world status: 201 STORED\r get foo status: 200 hello,world" === TEST 14: test append (not stored) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'append foo ,world'; echo_location '/memc?key=foo&cmd=append&val=,world'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body_like flush all status: 200 OK\r append foo ,world status: 200 NOT_STORED\r get foo status: 404.*?404 Not Found.*$ === TEST 15: test prepend (stored) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'add foo hello'; echo_location '/memc?key=foo&cmd=add&val=hello'; echo 'prepend foo world,'; echo_location '/memc?key=foo&cmd=prepend&val=world,'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo hello status: 201 STORED\r prepend foo world, status: 201 STORED\r get foo status: 200 world,hello" === TEST 16: test prepend (not stored) --- http_config eval: $::http_config --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'prepend foo world,'; echo_location '/memc?key=foo&cmd=prepend&val=world,'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass foo; } --- request GET /main --- response_body_like flush all status: 200 OK\r prepend foo world, status: 200 NOT_STORED\r get foo status: 404.*?404 Not Found.*$ === TEST 17: set and get big value --- http_config eval: $::http_config --- config location /big { echo_read_request_body; client_body_buffer_size 1k; client_max_body_size 100k; echo 'set big'; echo_subrequest POST '/memc?key=big&cmd=set'; echo 'get big'; echo_location '/memc?key=big&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; memc_pass foo; } --- request eval "POST /big\n" . 'a' x (1024 * 1) . 'efg' --- response_body eval "set big STORED\r get big " . 'a' x (1024 * 1) . 'efg' --- timeout: 5 === TEST 18: set and get too big values --- http_config eval: $::http_config --- config location /big { client_body_buffer_size 1k; client_max_body_size 100k; echo 'set big'; echo_subrequest POST '/memc?key=big&cmd=set'; echo 'get big'; echo_location '/memc?key=big&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; memc_pass foo; } --- request eval "POST /big\n" . 'a' x (1024 * 100) . 'efg' --- ignore_response --- error_log client intended to send too large body: 102403 bytes --- timeout: 5 === TEST 19: replace non-existent item --- http_config eval: $::http_config --- config location /main { echo 'flush_all'; echo_location '/memc?cmd=flush_all'; echo 'replace foo bar'; echo_location '/memc?key=foo&cmd=replace&val=bar'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass foo; } --- request GET /main --- response_body eval "flush_all status: 200 exptime: OK\r replace foo bar status: 200 exptime: 0 NOT_STORED\r " === TEST 20: eval + memc --- http_config eval: $::http_config --- config location /main { eval $data { proxy_pass $scheme://127.0.0.1:$server_port/foo; } set $memc_cmd set; set $memc_key /foo; set $memc_value $data; memc_pass foo; } location /foo { echo hello; } --- request GET /main --- response_body --- SKIP memc-nginx-module-0.20/t/stats.t000066400000000000000000000013561454304070500165550ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(1); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; log_level('error'); run_tests(); __DATA__ === TEST 1: sanity --- timeout: 5 --- config location /stats { set $memc_cmd stats; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /stats --- response_body_like: ^(?:STAT [^\r]*\r\n)*END\r\n$ === TEST 2: timeout --- config memc_connect_timeout 10ms; memc_send_timeout 10ms; location /stats { set $memc_cmd stats; memc_pass 127.0.0.2:12345; } --- request GET /stats --- response_body_like: 504 Gateway Time-out --- timeout: 1 --- error_code: 504 memc-nginx-module-0.20/t/storage.t000066400000000000000000000343321454304070500170630ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_shuffle(); #no_diff; run_tests(); __DATA__ === TEST 1: set only --- config location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /memc?key=foo&cmd=set&val=blah --- response_body eval "STORED\r\n" --- error_code: 201 === TEST 2: set and get --- config location /main { echo 'set foo blah'; echo_location '/memc?key=foo&cmd=set&val=blah'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo blah STORED\r get foo blah" === TEST 3: set UTF-8 and get UTF-8 --- config location /main { echo 'set foo 你好'; echo_location '/memc?key=foo&cmd=set&val=你好'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo 你好 STORED\r get foo 你好" === TEST 4: set and get empty values --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'set foo blah'; echo_location '/memc?key=foo&cmd=set&val='; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "flush all OK\r set foo blah STORED\r get foo " === TEST 5: add --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'add foo blah'; echo_location '/memc?key=foo&cmd=add&val=added'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo blah status: 201 STORED\r get foo status: 200 added" === TEST 6: set using POST --- config location /main { echo_read_request_body; echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'set foo'; echo_subrequest POST '/memc?key=foo&cmd=set'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request POST /main hello, world --- response_body eval "flush all status: 200 OK\r set foo status: 201 STORED\r get foo status: 200 hello, world" === TEST 7: default REST interface when no $memc_cmd is set --- config location /main { echo 'set foo FOO'; echo_subrequest PUT '/memc?key=foo' -b FOO; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; echo 'set foo BAR'; echo_subrequest PUT '/memc?key=foo' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo FOO status: 201 STORED\r get foo status: 200 FOO set foo BAR status: 201 STORED\r get foo status: 200 BAR " === TEST 8: default REST interface when no $memc_cmd is set (read client req body) --- config location /main { echo_read_request_body; echo 'set foo '; echo_subrequest PUT '/memc?key=foo'; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; echo 'set foo BAR'; echo_subrequest PUT '/memc?key=foo' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request POST /main rock --- response_body eval "set foo status: 201 STORED\r get foo status: 200 rock set foo BAR status: 201 STORED\r get foo status: 200 BAR " === TEST 9: default REST interface when no $memc_cmd is set (read client req body) --- config location /main { echo_read_request_body; echo 'set foo '; echo_subrequest PUT '/memc?key=foo'; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; echo 'add foo BAR'; echo_subrequest POST '/memc?key=foo' -b BAR; echo 'get foo'; echo_subrequest GET '/memc?key=foo'; echo; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_key $arg_key; #set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request POST /main howdy --- response_body eval "set foo status: 201 STORED\r get foo status: 200 howdy add foo BAR status: 200 NOT_STORED\r get foo status: 200 howdy " === TEST 10: test replace (stored) (without sleep) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; #echo_sleep 0.001; echo 'add foo blah'; echo_location '/memc?key=foo&cmd=add&val=added'; echo 'replace foo bah'; echo_location '/memc?key=foo&cmd=replace&val=bah'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo blah status: 201 STORED\r replace foo bah status: 201 STORED\r get foo status: 200 bah" === TEST 11: test replace (stored) (with sleep) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo_blocking_sleep 0.001; echo 'add foo blah'; echo_location '/memc?key=foo&cmd=add&val=added'; #echo_sleep 0.001; echo 'replace foo bah'; echo_location '/memc?key=foo&cmd=replace&val=bah'; #echo_sleep 0.001; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo blah status: 201 STORED\r replace foo bah status: 201 STORED\r get foo status: 200 bah" === TEST 12: test replace (not stored) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'replace foo bah'; echo_location '/memc?key=foo&cmd=replace&val=bah'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body_like flush all status: 200 OK\r replace foo bah status: 200 NOT_STORED\r get foo status: 404.*?404 Not Found.*$ === TEST 13: test append (stored) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'add foo hello'; echo_location '/memc?key=foo&cmd=add&val=hello'; echo 'append foo ,world'; echo_location '/memc?key=foo&cmd=append&val=,world'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo hello status: 201 STORED\r append foo ,world status: 201 STORED\r get foo status: 200 hello,world" === TEST 14: test append (not stored) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'append foo ,world'; echo_location '/memc?key=foo&cmd=append&val=,world'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body_like flush all status: 200 OK\r append foo ,world status: 200 NOT_STORED\r get foo status: 404.*?404 Not Found.*$ === TEST 15: test prepend (stored) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'add foo hello'; echo_location '/memc?key=foo&cmd=add&val=hello'; echo 'prepend foo world,'; echo_location '/memc?key=foo&cmd=prepend&val=world,'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "flush all status: 200 OK\r add foo hello status: 201 STORED\r prepend foo world, status: 201 STORED\r get foo status: 200 world,hello" === TEST 16: test prepend (not stored) --- config location /main { echo 'flush all'; echo_location '/memc?cmd=flush_all'; echo 'prepend foo world,'; echo_location '/memc?key=foo&cmd=prepend&val=world,'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { echo_before_body "status: $echo_response_status"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body_like flush all status: 200 OK\r prepend foo world, status: 200 NOT_STORED\r get foo status: 404.*?404 Not Found.*$ === TEST 17: set and get big value --- config location /big { client_body_buffer_size 1k; client_max_body_size 100k; echo_read_request_body; echo 'set big'; echo_subrequest POST '/memc?key=big&cmd=set'; echo 'get big'; echo_location '/memc?key=big&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request eval "POST /big\n" . 'a' x (1024 * 1) . 'efg' --- response_body eval "set big STORED\r get big " . 'a' x (1024 * 1) . 'efg' --- timeout: 2 === TEST 18: set and get too big values --- config location /big { client_body_buffer_size 1k; client_max_body_size 100k; echo 'set big'; echo_subrequest POST '/memc?key=big&cmd=set'; echo 'get big'; echo_location '/memc?key=big&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set $memc_key $arg_key; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request eval "POST /big\n" . 'a' x (1024 * 100) . 'efg' --- response_body_like: 413 Request Entity Too Large --- error_code: 413 --- timeout: 10 === TEST 19: replace non-existent item --- config location /main { echo 'flush_all'; echo_location '/memc?cmd=flush_all'; echo 'replace foo bar'; echo_location '/memc?key=foo&cmd=replace&val=bar'; } location /memc { echo_before_body "status: $echo_response_status"; echo_before_body "exptime: $memc_exptime"; set $memc_cmd $arg_cmd; set $memc_key $arg_key; set $memc_value $arg_val; set $memc_exptime $arg_exptime; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "flush_all status: 200 exptime: OK\r replace foo bar status: 200 exptime: 0 NOT_STORED\r " === TEST 20: eval + memc --- config location /main { eval $data { proxy_pass $scheme://127.0.0.1:$server_port/foo; } set $memc_cmd set; set $memc_key /foo; set $memc_value $data; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } location /foo { echo hello; } --- request GET /main --- response_body --- SKIP === TEST 21: set and get (binary data containing \0) --- config location /main { echo 'set foo blah'; echo_location '/memc?key=foo&cmd=set&val=blah%00blah'; echo 'get foo'; echo_location '/memc?key=foo&cmd=get'; } location /memc { set $memc_cmd $arg_cmd; set_unescape_uri $memc_key $arg_key; set_unescape_uri $memc_value $arg_val; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /main --- response_body eval "set foo blah STORED\r get foo blah\0blah" memc-nginx-module-0.20/t/upstream.t000066400000000000000000000025451454304070500172600ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: sanity --- http_config upstream backend { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location /stats { set $memc_cmd stats; memc_pass backend; } --- request GET /stats --- response_body_like: ^(?:STAT [^\r]*\r\n)*END\r\n$ --- timeout: 3 === TEST 2: sanity --- http_config upstream backend { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; server 127.0.0.1:11985; server 127.0.0.1:11986; #keepalive 2; #hash $arg_key; } --- config location /memc { set $memc_cmd set; set $memc_key $arg_key; set $memc_value 'value'; memc_pass backend; } --- request GET /memc?key=dog --- response_body --- error_code: 201 --- timeout: 3 --- SKIP === TEST 3: sanity with dynamic backend --- http_config upstream backend { server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- config location /stats { set $memc_cmd stats; memc_pass $arg_target; } --- request GET /stats?target=backend --- response_body_like: ^(?:STAT [^\r]*\r\n)*END\r\n$ --- timeout: 3 memc-nginx-module-0.20/t/used.t000066400000000000000000000024211454304070500163510ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; plan tests => repeat_each() * (4 * blocks()); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: module not used --- config location /t { content_by_lua ' local v = ngx.var ngx.say(v.memc_cmd, v.memc_exptime, v.memc_value, v.memc_key, v.memc_flags) '; } --- request GET /t --- stap F(ngx_http_memc_add_variable) { printf("memc add variable \"%s\"\n", user_string_n($name->data, $name->len)) } --- stap_out --- response_body nilnilnilnilnil --- no_error_log [error] === TEST 2: module used --- config location /t { content_by_lua ' local v = ngx.var ngx.say(v.memc_cmd, v.memc_exptime, v.memc_value, v.memc_key, v.memc_flags) '; } location /bah { set $memc_key foo; memc_pass 127.0.0.1:11211; } --- request GET /t --- stap F(ngx_http_memc_add_variable) { printf("memc add variable \"%s\"\n", user_string_n($name->data, $name->len)) } --- stap_out memc add variable "memc_key" memc add variable "memc_cmd" memc add variable "memc_flags" memc add variable "memc_exptime" memc add variable "memc_value" --- response_body nilnilnilnilnil --- no_error_log [error] memc-nginx-module-0.20/t/version.t000066400000000000000000000006421454304070500171010ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; #no_diff; run_tests(); __DATA__ === TEST 1: the "version" command --- config location /ver { set $memc_cmd version; memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT; } --- request GET /ver --- response_body_like: ^VERSION \d+(\.\d+)+.*?\r\n$ memc-nginx-module-0.20/util/000077500000000000000000000000001454304070500157375ustar00rootroot00000000000000memc-nginx-module-0.20/util/build.sh000077500000000000000000000024521454304070500174000ustar00rootroot00000000000000#!/bin/bash # this file is mostly meant to be used by the author himself. ragel -I src -T1 src/ngx_http_memc_response.rl || exit 1 util/fix-clang-warnings if [ $? != 0 ]; then echo 'Failed to generate the memcached response parser.' 1>&2 exit 1; fi root=`pwd` version=$1 home=~ force=$2 ngx-build $force $version \ --with-ld-opt="-L$PCRE_LIB -Wl,-rpath,$PCRE_LIB:$LIBDRIZZLE_LIB:/usr/local/lib" \ --with-cc-opt="-O2" \ --with-http_addition_module \ --without-mail_pop3_module \ --without-mail_imap_module \ --without-mail_smtp_module \ --without-http_upstream_ip_hash_module \ --without-http_empty_gif_module \ --without-http_referer_module \ --without-http_autoindex_module \ --without-http_auth_basic_module \ --without-http_userid_module \ --add-module=$root $opts \ --add-module=$root/../ndk-nginx-module \ --add-module=$root/../eval-nginx-module \ --add-module=$root/../echo-nginx-module \ --add-module=$root/../set-misc-nginx-module \ --add-module=$root/../lua-nginx-module \ --with-select_module \ --with-poll_module \ --with-debug #--add-module=$home/work/nginx/nginx_upstream_hash-0.3 \ #--without-http_ssi_module # we cannot disable ssi because echo_location_async depends on it (i dunno why?!) memc-nginx-module-0.20/util/fix-clang-warnings000077500000000000000000000034001454304070500213600ustar00rootroot00000000000000#!/usr/bin/env perl use strict; use warnings; use File::Temp 'tempfile'; my $infile = "src/ngx_http_memc_response.c"; my ($out, $outfile) = tempfile(); open my $in, $infile or die "Cannot open $infile for reading: $!\n"; my $hits = 0; while (<$in>) { if (/ \b memc_ (?: storage | flush_all | version | stats | delete | incr_decr ) _en_main \b /x) { #warn "HIT!"; $hits++; next; } print $out $_; } close $in; close $out; if ($hits) { my $cmd = "cp $outfile $infile"; system($cmd) == 0 or die "Cannot run command \"$cmd\": $!"; } #die; __END__ This script is to fix the following clang warnings when using Ragel 6.8/6.9/etc: src/ngx_http_memc_response.c:33:18: error: unused variable 'memc_storage_en_main' [-Werror,-Wunused-const-variable] static const int memc_storage_en_main = 1; ^ src/ngx_http_memc_response.c:46:18: error: unused variable 'memc_flush_all_en_main' [-Werror,-Wunused-const-variable] static const int memc_flush_all_en_main = 1; ^ src/ngx_http_memc_response.c:59:18: error: unused variable 'memc_version_en_main' [-Werror,-Wunused-const-variable] static const int memc_version_en_main = 1; ^ src/ngx_http_memc_response.c:72:18: error: unused variable 'memc_stats_en_main' [-Werror,-Wunused-const-variable] static const int memc_stats_en_main = 1; ^ src/ngx_http_memc_response.c:85:18: error: unused variable 'memc_delete_en_main' [-Werror,-Wunused-const-variable] static const int memc_delete_en_main = 1; ^ src/ngx_http_memc_response.c:98:18: error: unused variable 'memc_incr_decr_en_main' [-Werror,-Wunused-const-variable] static const int memc_incr_decr_en_main = 1; ^ 6 errors generated. memc-nginx-module-0.20/util/update-readme.sh000077500000000000000000000002371454304070500210150ustar00rootroot00000000000000#!/bin/bash perl util/wiki2pod.pl doc/readme.wiki > /tmp/a.pod \ && pod2text /tmp/a.pod > README \ && perl -i -pe 's{(https?://.*?)>}{$1 >}g' README memc-nginx-module-0.20/util/wiki2pod.pl000066400000000000000000000060641454304070500200320ustar00rootroot00000000000000#!/usr/bin/env perl use strict; use warnings; use bytes; my @nl_counts; my $last_nl_count_level; my @bl_counts; my $last_bl_count_level; sub fmt_pos ($) { (my $s = $_[0]) =~ s{\#(.*)}{/"$1"}; $s; } sub fmt_mark ($$) { my ($tag, $s) = @_; my $max_level = 0; while ($s =~ /([<>])\1*/g) { my $level = length $&; if ($level > $max_level) { $max_level = $level; } } my $times = $max_level + 1; if ($times > 1) { $s = " $s "; } return $tag . ('<' x $times) . $s . ('>' x $times); } print "=encoding utf-8\n\n"; while (<>) { if ($. == 1) { # strip the leading U+FEFF byte in MS-DOS text files my $first = ord(substr($_, 0, 1)); #printf STDERR "0x%x", $first; #my $second = ord(substr($_, 2, 1)); #printf STDERR "0x%x", $second; if ($first == 0xEF) { substr($_, 0, 1, ''); #warn "Hit!"; } } s{\[(http[^ \]]+) ([^\]]*)\]}{$2 (L<$1>)}gi; s{ \[\[ ( [^\]\|]+ ) \| ([^\]]*) \]\] }{"L<$2|" . fmt_pos($1) . ">"}gixe; s{(.*?)}{fmt_mark('C', $1)}gie; s{'''(.*?)'''}{fmt_mark('B', $1)}ge; s{''(.*?)''}{fmt_mark('I', $1)}ge; if (s{^\s*<[^>]+>\s*$}{}) { next; } if (/^\s*$/) { print "\n"; next; } =begin cmt if ($. == 1) { warn $_; for my $i (0..length($_) - 1) { my $chr = substr($_, $i, 1); warn "chr ord($i): ".ord($chr)." \"$chr\"\n"; } } =end cmt =cut if (/(=+) (.*) \1$/) { #warn "HERE! $_" if $. == 1; my ($level, $title) = (length $1, $2); collapse_lists(); print "\n=head$level $title\n\n"; } elsif (/^(\#+) (.*)/) { my ($level, $txt) = (length($1) - 1, $2); if (defined $last_nl_count_level && $level != $last_nl_count_level) { print "\n=back\n\n"; } $last_nl_count_level = $level; $nl_counts[$level] ||= 0; if ($nl_counts[$level] == 0) { print "\n=over\n\n"; } $nl_counts[$level]++; print "\n=item $nl_counts[$level].\n\n"; print "$txt\n"; } elsif (/^(\*+) (.*)/) { my ($level, $txt) = (length($1) - 1, $2); if (defined $last_bl_count_level && $level != $last_bl_count_level) { print "\n=back\n\n"; } $last_bl_count_level = $level; $bl_counts[$level] ||= 0; if ($bl_counts[$level] == 0) { print "\n=over\n\n"; } $bl_counts[$level]++; print "\n=item *\n\n"; print "$txt\n"; } else { collapse_lists(); print; } } collapse_lists(); sub collapse_lists { while (defined $last_nl_count_level && $last_nl_count_level >= 0) { print "\n=back\n\n"; $last_nl_count_level--; } undef $last_nl_count_level; undef @nl_counts; while (defined $last_bl_count_level && $last_bl_count_level >= 0) { print "\n=back\n\n"; $last_bl_count_level--; } undef $last_bl_count_level; undef @bl_counts; } memc-nginx-module-0.20/valgrind.suppress000066400000000000000000000033321454304070500203770ustar00rootroot00000000000000{ Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init } { Memcheck:Param epoll_ctl(event) fun:epoll_ctl fun:ngx_epoll_add_event } { Memcheck:Param epoll_ctl(event) fun:epoll_ctl fun:ngx_epoll_add_connection } { Memcheck:Cond fun:index fun:expand_dynamic_string_token fun:_dl_map_object fun:map_doit fun:_dl_catch_error fun:do_preload fun:dl_main } { Memcheck:Param epoll_ctl(event) fun:epoll_ctl fun:ngx_epoll_init fun:ngx_event_process_init } { Memcheck:Param epoll_ctl(event) fun:epoll_ctl fun:ngx_epoll_notify_init fun:ngx_epoll_init } { Memcheck:Param epoll_ctl(event) fun:epoll_ctl fun:ngx_epoll_test_rdhup } { Memcheck:Leak match-leak-kinds: definite fun:malloc fun:ngx_alloc fun:ngx_set_environment fun:ngx_single_process_cycle } { Memcheck:Leak match-leak-kinds: definite fun:malloc fun:ngx_alloc fun:ngx_set_environment fun:ngx_worker_process_init fun:ngx_worker_process_cycle } { Memcheck:Param epoll_pwait(sigmask) fun:epoll_pwait fun:epoll_wait fun:ngx_epoll_process_events fun:ngx_process_events_and_timers } { Memcheck:Param epoll_pwait(sigmask) fun:epoll_pwait fun:epoll_wait fun:ngx_epoll_test_rdhup fun:ngx_epoll_init fun:ngx_event_process_init }