LCOV - code coverage report
Current view: top level - home/build/openresty-1.13.6.1/build/memc-nginx-module-0.18/src - ngx_http_memc_request.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 0 238 0.0 %
Date: 2020-03-03 04:25:50 Functions: 0 6 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : 
       2             : /*
       3             :  * Copyright (c) Yichun Zhang (agentzh)
       4             :  */
       5             : 
       6             : 
       7             : #ifndef DDEBUG
       8             : #define DDEBUG 0
       9             : #endif
      10             : #include "ddebug.h"
      11             : 
      12             : 
      13             : #include "ngx_http_memc_request.h"
      14             : #include "ngx_http_memc_module.h"
      15             : #include "ngx_http_memc_util.h"
      16             : 
      17             : 
      18             : ngx_int_t
      19           0 : ngx_http_memc_create_storage_cmd_request(ngx_http_request_t *r)
      20             : {
      21             :     size_t                          len;
      22             :     off_t                           bytes;
      23             :     size_t                          bytes_len;
      24             :     uintptr_t                       escape;
      25             :     ngx_buf_t                      *b;
      26             :     ngx_chain_t                    *cl, *in;
      27             :     ngx_chain_t                   **ll;
      28             :     ngx_http_memc_ctx_t            *ctx;
      29             : 
      30             :     ngx_http_variable_value_t      *key_vv;
      31             :     ngx_http_variable_value_t      *flags_vv;
      32             :     ngx_http_variable_value_t      *exptime_vv;
      33             :     ngx_http_variable_value_t      *memc_value_vv;
      34             : 
      35             :     u_char                          bytes_buf[NGX_UINT32_LEN];
      36             : 
      37             :     /* TODO add support for the "cas" command */
      38             : 
      39           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module);
      40             : 
      41             :     /* prepare the "key" argument */
      42             : 
      43           0 :     key_vv = ctx->memc_key_vv;
      44             : 
      45           0 :     if (key_vv == NULL || key_vv->not_found || key_vv->len == 0) {
      46           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
      47             :                       "the \"$memc_key\" variable is not set");
      48           0 :         return NGX_ERROR;
      49             :     }
      50             : 
      51           0 :     escape = 2 * ngx_escape_uri(NULL, key_vv->data, key_vv->len,
      52             :                                 NGX_ESCAPE_MEMCACHED);
      53             : 
      54             :     /* prepare the "bytes" argument */
      55             : 
      56           0 :     if (ctx->memc_value_vv && !ctx->memc_value_vv->not_found) {
      57             :         dd("found variable $memc_value");
      58             : 
      59           0 :         memc_value_vv = ctx->memc_value_vv;
      60             : 
      61           0 :         bytes = memc_value_vv->len;
      62             : 
      63           0 :     } else if (r->request_body == NULL || r->request_body->bufs == NULL) {
      64           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
      65             :                       "neither the \"$memc_value\" variable "
      66             :                       "nor the request body is available");
      67           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
      68             : 
      69             :     } else {
      70           0 :         memc_value_vv = NULL;
      71             : 
      72           0 :         bytes = 0;
      73           0 :         for (cl = r->request_body->bufs; cl; cl = cl->next) {
      74           0 :             bytes += ngx_buf_size(cl->buf);
      75             :         }
      76             :     }
      77             : 
      78           0 :     bytes_len = ngx_snprintf(bytes_buf, sizeof(bytes_buf), "%O", bytes)
      79           0 :         - bytes_buf;
      80             : 
      81             :     /* prepare the "flags" argument */
      82             : 
      83           0 :     flags_vv = ctx->memc_flags_vv;
      84             : 
      85           0 :     if (flags_vv == NULL) {
      86           0 :         return NGX_ERROR;
      87             :     }
      88             : 
      89           0 :     if (flags_vv->not_found) {
      90           0 :         flags_vv->not_found = 0;
      91           0 :         flags_vv->valid = 1;
      92           0 :         flags_vv->no_cacheable = 0;
      93           0 :         flags_vv->len = sizeof("0") - 1;
      94           0 :         flags_vv->data = (u_char *) "0";
      95           0 :     } else if (flags_vv->len == 0) {
      96           0 :         flags_vv->len = sizeof("0") - 1;
      97           0 :         flags_vv->data = (u_char *) "0";
      98             :     }
      99             : 
     100             :     /* prepare the "exptime" argument */
     101             : 
     102           0 :     exptime_vv = ctx->memc_exptime_vv;
     103             : 
     104           0 :     if (exptime_vv == NULL) {
     105           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     106             :     }
     107             : 
     108           0 :     if (exptime_vv->not_found || exptime_vv->len == 0) {
     109             :         dd("setting exptime to its default value 0...");
     110             : 
     111           0 :         exptime_vv->not_found = 0;
     112           0 :         exptime_vv->valid = 1;
     113           0 :         exptime_vv->no_cacheable = 0;
     114           0 :         exptime_vv->len = sizeof("0") - 1;
     115           0 :         exptime_vv->data = (u_char *) "0";
     116             :     }
     117             : 
     118             :     /* calculate the total length of the command */
     119             : 
     120           0 :     len = ctx->cmd_str.len + sizeof(" ") - 1
     121           0 :         + key_vv->len + escape + sizeof(" ") - 1
     122           0 :         + flags_vv->len + sizeof(" ") - 1
     123           0 :         + exptime_vv->len + sizeof(" ") - 1
     124           0 :         + bytes_len
     125             :         + sizeof(CRLF) - 1;
     126             : 
     127           0 :     b = ngx_create_temp_buf(r->pool, len);
     128           0 :     if (b == NULL) {
     129           0 :         return NGX_ERROR;
     130             :     }
     131             : 
     132           0 :     cl = ngx_alloc_chain_link(r->pool);
     133           0 :     if (cl == NULL) {
     134           0 :         return NGX_ERROR;
     135             :     }
     136             : 
     137           0 :     cl->buf = b;
     138           0 :     cl->next = NULL;
     139             : 
     140           0 :     ll = &cl->next;
     141             : 
     142           0 :     r->upstream->request_bufs = cl;
     143             : 
     144             :     /* copy the memcached command over */
     145             : 
     146           0 :     b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len);
     147             : 
     148           0 :     *b->last++ = ' ';
     149             : 
     150             :     /* copy the memcached key over */
     151             : 
     152           0 :     ctx->key.data = b->last;
     153             : 
     154           0 :     if (escape == 0) {
     155           0 :         b->last = ngx_copy(b->last, key_vv->data, key_vv->len);
     156             : 
     157             :     } else {
     158           0 :         b->last = (u_char *) ngx_escape_uri(b->last, key_vv->data, key_vv->len,
     159             :                                             NGX_ESCAPE_MEMCACHED);
     160             :     }
     161             : 
     162           0 :     ctx->key.len = b->last - ctx->key.data;
     163             : 
     164           0 :     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
     165             :                    "http memcached request: \"%V\"", &ctx->key);
     166             : 
     167           0 :     *b->last++ = ' ';
     168             : 
     169             :     /* copy the memcached flags over */
     170             : 
     171           0 :     b->last = ngx_copy(b->last, flags_vv->data, flags_vv->len);
     172             : 
     173           0 :     *b->last++ = ' ';
     174             : 
     175             :     /* copy the memcached exptime over */
     176             : 
     177           0 :     b->last = ngx_copy(b->last, exptime_vv->data, exptime_vv->len);
     178             : 
     179           0 :     *b->last++ = ' ';
     180             : 
     181             :     /* copy the memcached bytes over */
     182             : 
     183           0 :     b->last = ngx_copy(b->last, bytes_buf, bytes_len);
     184             : 
     185           0 :     *b->last++ = CR; *b->last++ = LF;
     186             : 
     187           0 :     if (memc_value_vv) {
     188           0 :         if (memc_value_vv->len) {
     189             :             dd("copy $memc_value to the request");
     190           0 :             b = ngx_calloc_buf(r->pool);
     191             : 
     192           0 :             if (b == NULL) {
     193           0 :                 return NGX_ERROR;
     194             :             }
     195             : 
     196           0 :             b->memory = 1;
     197             : 
     198           0 :             b->start = b->pos = memc_value_vv->data;
     199           0 :             b->last  = b->end = b->start + memc_value_vv->len;
     200             : 
     201           0 :             cl = ngx_alloc_chain_link(r->pool);
     202           0 :             if (cl == NULL) {
     203           0 :                 return NGX_ERROR;
     204             :             }
     205             : 
     206           0 :             cl->buf = b;
     207           0 :             cl->next = NULL;
     208             : 
     209           0 :             *ll = cl;
     210           0 :             ll = &cl->next;
     211             :         }
     212             : 
     213             :     } else {
     214             :         /* to preserve the r->request_body->bufs untouched */
     215             : 
     216           0 :         in = r->request_body->bufs;
     217             : 
     218           0 :         while (in) {
     219           0 :             cl = ngx_alloc_chain_link(r->pool);
     220           0 :             if (cl == NULL) {
     221           0 :                 return NGX_ERROR;
     222             :             }
     223             : 
     224           0 :             cl->buf = ngx_calloc_buf(r->pool);
     225           0 :             if (cl->buf == NULL) {
     226           0 :                 return NGX_ERROR;
     227             :             }
     228             : 
     229           0 :             cl->buf->memory = 1;
     230           0 :             *cl->buf = *in->buf;
     231             : 
     232           0 :             *ll = cl;
     233           0 :             ll = &cl->next;
     234           0 :             in = in->next;
     235             :         }
     236             :     }
     237             : 
     238             :     /* append the trailing CRLF */
     239             : 
     240           0 :     b = ngx_calloc_buf(r->pool);
     241             : 
     242           0 :     if (b == NULL) {
     243           0 :         return NGX_ERROR;
     244             :     }
     245             : 
     246           0 :     b->start = b->pos = (u_char *) CRLF;
     247           0 :     b->last  = b->end = b->start + sizeof(CRLF) - 1;
     248             : 
     249           0 :     b->memory = 1;
     250             : 
     251           0 :     cl = ngx_alloc_chain_link(r->pool);
     252           0 :     if (cl == NULL) {
     253           0 :         return NGX_ERROR;
     254             :     }
     255             : 
     256           0 :     cl->buf = b;
     257           0 :     cl->next = NULL;
     258             : 
     259           0 :     *ll = cl;
     260             : 
     261           0 :     return NGX_OK;
     262             : }
     263             : 
     264             : 
     265             : ngx_int_t
     266           0 : ngx_http_memc_create_get_cmd_request(ngx_http_request_t *r)
     267             : {
     268             :     size_t                          len;
     269             :     uintptr_t                       escape;
     270             :     ngx_buf_t                      *b;
     271             :     ngx_chain_t                    *cl;
     272             :     ngx_http_memc_ctx_t            *ctx;
     273             :     ngx_http_variable_value_t      *vv;
     274             : 
     275           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module);
     276             : 
     277           0 :     vv = ctx->memc_key_vv;
     278             : 
     279           0 :     if (vv == NULL || vv->not_found || vv->len == 0) {
     280           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     281             :                       "the \"$memc_key\" variable is not set");
     282           0 :         return NGX_ERROR;
     283             :     }
     284             : 
     285           0 :     escape = 2 * ngx_escape_uri(NULL, vv->data, vv->len, NGX_ESCAPE_MEMCACHED);
     286             : 
     287           0 :     len = sizeof("get ") - 1 + vv->len + escape + sizeof(CRLF) - 1;
     288             : 
     289           0 :     b = ngx_create_temp_buf(r->pool, len);
     290           0 :     if (b == NULL) {
     291           0 :         return NGX_ERROR;
     292             :     }
     293             : 
     294           0 :     cl = ngx_alloc_chain_link(r->pool);
     295           0 :     if (cl == NULL) {
     296           0 :         return NGX_ERROR;
     297             :     }
     298             : 
     299           0 :     cl->buf = b;
     300           0 :     cl->next = NULL;
     301             : 
     302           0 :     r->upstream->request_bufs = cl;
     303             : 
     304           0 :     *b->last++ = 'g'; *b->last++ = 'e'; *b->last++ = 't'; *b->last++ = ' ';
     305             : 
     306           0 :     ctx->key.data = b->last;
     307             : 
     308           0 :     if (escape == 0) {
     309           0 :         b->last = ngx_copy(b->last, vv->data, vv->len);
     310             : 
     311             :     } else {
     312           0 :         b->last = (u_char *) ngx_escape_uri(b->last, vv->data, vv->len,
     313             :                                             NGX_ESCAPE_MEMCACHED);
     314             :     }
     315             : 
     316           0 :     ctx->key.len = b->last - ctx->key.data;
     317             : 
     318           0 :     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
     319             :                    "http memcached request: \"%V\"", &ctx->key);
     320             : 
     321           0 :     *b->last++ = CR; *b->last++ = LF;
     322             : 
     323           0 :     ctx->parser_state = NGX_ERROR;
     324             : 
     325           0 :     return NGX_OK;
     326             : }
     327             : 
     328             : ngx_int_t
     329           0 : ngx_http_memc_create_noarg_cmd_request(ngx_http_request_t *r)
     330             : {
     331             :     size_t                          len;
     332             :     ngx_buf_t                      *b;
     333             :     ngx_http_memc_ctx_t            *ctx;
     334             :     ngx_chain_t                    *cl;
     335             : 
     336           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module);
     337             : 
     338           0 :     len = ctx->cmd_str.len + sizeof(CRLF) - 1;
     339             : 
     340           0 :     b = ngx_create_temp_buf(r->pool, len);
     341           0 :     if (b == NULL) {
     342           0 :         return NGX_ERROR;
     343             :     }
     344             : 
     345           0 :     cl = ngx_alloc_chain_link(r->pool);
     346           0 :     if (cl == NULL) {
     347           0 :         return NGX_ERROR;
     348             :     }
     349             : 
     350           0 :     cl->buf = b;
     351           0 :     cl->next = NULL;
     352             : 
     353           0 :     r->upstream->request_bufs = cl;
     354             : 
     355           0 :     b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len);
     356             : 
     357           0 :     *b->last++ = CR; *b->last++ = LF;
     358             : 
     359           0 :     return NGX_OK;
     360             : }
     361             : 
     362             : 
     363             : ngx_int_t
     364           0 : ngx_http_memc_create_flush_all_cmd_request(ngx_http_request_t *r)
     365             : {
     366             :     size_t                          len;
     367             :     ngx_buf_t                      *b;
     368             :     ngx_http_memc_ctx_t            *ctx;
     369             :     ngx_chain_t                    *cl;
     370             :     ngx_http_variable_value_t      *exptime_vv;
     371             : 
     372           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module);
     373             : 
     374             :     /* prepare the (optional) "exptime" argument */
     375             : 
     376           0 :     exptime_vv = ctx->memc_exptime_vv;
     377             : 
     378           0 :     if (exptime_vv == NULL) {
     379           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     380             :     }
     381             : 
     382           0 :     len = ctx->cmd_str.len;
     383             : 
     384           0 :     if (!exptime_vv->not_found && exptime_vv->len) {
     385             :         dd("found exptime: %s", exptime_vv->data);
     386             : 
     387           0 :         len += sizeof(' ') + exptime_vv->len;
     388             :     }
     389             : 
     390           0 :     len += sizeof(CRLF) - 1;
     391             : 
     392           0 :     b = ngx_create_temp_buf(r->pool, len);
     393           0 :     if (b == NULL) {
     394           0 :         return NGX_ERROR;
     395             :     }
     396             : 
     397           0 :     cl = ngx_alloc_chain_link(r->pool);
     398           0 :     if (cl == NULL) {
     399           0 :         return NGX_ERROR;
     400             :     }
     401             : 
     402           0 :     cl->buf = b;
     403           0 :     cl->next = NULL;
     404             : 
     405           0 :     r->upstream->request_bufs = cl;
     406             : 
     407           0 :     b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len);
     408             : 
     409           0 :     if (!exptime_vv->not_found && exptime_vv->len) {
     410           0 :         *b->last++ = ' ';
     411           0 :         b->last = ngx_copy(b->last, exptime_vv->data, exptime_vv->len);
     412             :     }
     413             : 
     414           0 :     *b->last++ = CR; *b->last++ = LF;
     415             : 
     416           0 :     return NGX_OK;
     417             : }
     418             : 
     419             : 
     420             : ngx_int_t
     421           0 : ngx_http_memc_create_delete_cmd_request(ngx_http_request_t *r)
     422             : {
     423             :     size_t                          len;
     424             :     ngx_buf_t                      *b;
     425             :     ngx_http_memc_ctx_t            *ctx;
     426             :     ngx_chain_t                    *cl;
     427             :     uintptr_t                       escape;
     428             :     ngx_http_variable_value_t      *key_vv;
     429             :     ngx_http_variable_value_t      *exptime_vv;
     430             : 
     431           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module);
     432             : 
     433             :     /* prepare the "key" argument */
     434             : 
     435           0 :     key_vv = ctx->memc_key_vv;
     436             : 
     437           0 :     if (key_vv == NULL || key_vv->not_found || key_vv->len == 0) {
     438           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     439             :                       "the \"$memc_key\" variable is not set");
     440           0 :         return NGX_ERROR;
     441             :     }
     442             : 
     443           0 :     escape = 2 * ngx_escape_uri(NULL, key_vv->data, key_vv->len,
     444             :                                 NGX_ESCAPE_MEMCACHED);
     445             : 
     446             :     /* prepare the (optional) "exptime" argument */
     447             : 
     448           0 :     exptime_vv = ctx->memc_exptime_vv;
     449             : 
     450           0 :     if (exptime_vv == NULL) {
     451           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     452             :     }
     453             : 
     454           0 :     len = ctx->cmd_str.len + sizeof(' ') + key_vv->len + escape;
     455             : 
     456           0 :     if (!exptime_vv->not_found && exptime_vv->len) {
     457             :         dd("found exptime: %s", exptime_vv->data);
     458             : 
     459           0 :         len += sizeof(' ') + exptime_vv->len;
     460             :     }
     461             : 
     462           0 :     len += sizeof(CRLF) - 1;
     463             : 
     464           0 :     b = ngx_create_temp_buf(r->pool, len);
     465           0 :     if (b == NULL) {
     466           0 :         return NGX_ERROR;
     467             :     }
     468             : 
     469           0 :     cl = ngx_alloc_chain_link(r->pool);
     470           0 :     if (cl == NULL) {
     471           0 :         return NGX_ERROR;
     472             :     }
     473             : 
     474           0 :     cl->buf = b;
     475           0 :     cl->next = NULL;
     476             : 
     477           0 :     r->upstream->request_bufs = cl;
     478             : 
     479           0 :     b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len);
     480             : 
     481           0 :     *b->last++ = ' ';
     482             : 
     483           0 :     if (escape == 0) {
     484           0 :         b->last = ngx_copy(b->last, key_vv->data, key_vv->len);
     485             : 
     486             :     } else {
     487           0 :         b->last = (u_char *) ngx_escape_uri(b->last, key_vv->data, key_vv->len,
     488             :                                             NGX_ESCAPE_MEMCACHED);
     489             :     }
     490             : 
     491           0 :     if (!exptime_vv->not_found && exptime_vv->len) {
     492           0 :         *b->last++ = ' ';
     493           0 :         b->last = ngx_copy(b->last, exptime_vv->data, exptime_vv->len);
     494             :     }
     495             : 
     496           0 :     *b->last++ = CR; *b->last++ = LF;
     497             : 
     498           0 :     return NGX_OK;
     499             : }
     500             : 
     501             : 
     502             : ngx_int_t
     503           0 : ngx_http_memc_create_incr_decr_cmd_request(ngx_http_request_t *r)
     504             : {
     505             :     size_t                          len;
     506             :     ngx_buf_t                      *b;
     507             :     ngx_http_memc_ctx_t            *ctx;
     508             :     ngx_chain_t                    *cl;
     509             :     uintptr_t                       escape;
     510             :     ngx_http_variable_value_t      *key_vv;
     511             :     ngx_http_variable_value_t      *value_vv;
     512             : 
     513           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module);
     514             : 
     515             :     /* prepare the "key" argument */
     516             : 
     517           0 :     key_vv = ctx->memc_key_vv;
     518             : 
     519           0 :     if (key_vv == NULL || key_vv->not_found || key_vv->len == 0) {
     520           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     521             :                       "the \"$memc_key\" variable is not set");
     522           0 :         return NGX_ERROR;
     523             :     }
     524             : 
     525           0 :     escape = 2 * ngx_escape_uri(NULL, key_vv->data, key_vv->len,
     526             :                                 NGX_ESCAPE_MEMCACHED);
     527             : 
     528             :     /* prepare the "value" argument */
     529             : 
     530           0 :     value_vv = ctx->memc_value_vv;
     531             : 
     532             :     /* XXX validate if $memc_value_vv is a valid uint64 string */
     533             : 
     534           0 :     len = ctx->cmd_str.len + sizeof(' ') + key_vv->len + escape
     535           0 :         + sizeof(' ') + value_vv->len + sizeof(CRLF) - 1;
     536             : 
     537           0 :     b = ngx_create_temp_buf(r->pool, len);
     538           0 :     if (b == NULL) {
     539           0 :         return NGX_ERROR;
     540             :     }
     541             : 
     542           0 :     cl = ngx_alloc_chain_link(r->pool);
     543           0 :     if (cl == NULL) {
     544           0 :         return NGX_ERROR;
     545             :     }
     546             : 
     547           0 :     cl->buf = b;
     548           0 :     cl->next = NULL;
     549             : 
     550           0 :     r->upstream->request_bufs = cl;
     551             : 
     552           0 :     b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len);
     553             : 
     554           0 :     *b->last++ = ' ';
     555             : 
     556           0 :     if (escape == 0) {
     557           0 :         b->last = ngx_copy(b->last, key_vv->data, key_vv->len);
     558             : 
     559             :     } else {
     560           0 :         b->last = (u_char *) ngx_escape_uri(b->last, key_vv->data, key_vv->len,
     561             :                                             NGX_ESCAPE_MEMCACHED);
     562             :     }
     563             : 
     564           0 :     *b->last++ = ' ';
     565             : 
     566           0 :     b->last = ngx_copy(b->last, value_vv->data, value_vv->len);
     567             : 
     568           0 :     *b->last++ = CR; *b->last++ = LF;
     569             : 
     570           0 :     return NGX_OK;
     571             : }

Generated by: LCOV version 1.13