File: memcache_ascii_protocol.c

package info (click to toggle)
php-memcache 3.0.9~20170802.e702b5f-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,172 kB
  • sloc: ansic: 9,126; php: 1,015; xml: 760; sh: 3; makefile: 1
file content (430 lines) | stat: -rw-r--r-- 13,670 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2007 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.0 of the PHP license,       |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_0.txt.                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Authors: Antony Dovgal <tony2001@phpclub.net>                        |
  |          Mikael Johansson <mikael AT synd DOT info>                  |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "memcache_pool.h"
#include "ext/standard/php_smart_string.h"

typedef struct mmc_ascii_request {
	mmc_request_t	base;							/* enable cast to mmc_request_t */
	struct {										/* stores value info while the body is being read */
		char			key[MMC_MAX_KEY_LEN + 1];
		unsigned int	flags;
		unsigned long	length;
		unsigned long	cas;						/* CAS counter */
	} value;
} mmc_ascii_request_t;

static int mmc_server_read_value(mmc_t *, mmc_request_t *);

static int mmc_stream_get_line(mmc_stream_t *io, char **line) /* 
	attempts to read a line from server, returns the line size or 0 if no complete line was available {{{ */
{
	size_t returned_len = 0;
	io->readline(io, io->input.value + io->input.idx, MMC_BUFFER_SIZE - io->input.idx, &returned_len);
	io->input.idx += returned_len;
	
	if (io->input.idx && io->input.value[io->input.idx - 1] == '\n') {
		int result = io->input.idx;
		*line = io->input.value;
		io->input.idx = 0;
		return result;
	}
	
	return 0;
}	
/* }}} */

static int mmc_request_check_response(const char *line, int line_len) /* 
	checks for response status and error codes {{{ */
{
	int response;

	if (mmc_str_left(line, "OK", line_len, sizeof("OK")-1) ||
		mmc_str_left(line, "STORED", line_len, sizeof("STORED")-1) ||
		mmc_str_left(line, "DELETED", line_len, sizeof("DELETED")-1)) 
	{
		response = MMC_OK;
	}
	else if (mmc_str_left(line, "NOT_FOUND", line_len, sizeof("NOT_FOUND")-1)) {
		response = MMC_RESPONSE_NOT_FOUND;
	}
	else if (
		mmc_str_left(line, "NOT_STORED", line_len, sizeof("NOT_STORED")-1) ||
		mmc_str_left(line, "EXISTS", line_len, sizeof("EXISTS")-1)) 
	{
		response = MMC_RESPONSE_EXISTS;
	}
	else if (mmc_str_left(line, "SERVER_ERROR out of memory", line_len, sizeof("SERVER_ERROR out of memory")-1)) {
		response = MMC_RESPONSE_OUT_OF_MEMORY;
	}
	else if (mmc_str_left(line, "SERVER_ERROR object too large", line_len, sizeof("SERVER_ERROR object too large")-1)) {
		response = MMC_RESPONSE_TOO_LARGE;
	}
	else if (
		mmc_str_left(line, "ERROR", line_len, sizeof("ERROR")-1) ||
		mmc_str_left(line, "SERVER_ERROR", line_len, sizeof("SERVER_ERROR")-1)) 
	{
		response = MMC_RESPONSE_ERROR;
	}
	else if (mmc_str_left(line, "CLIENT_ERROR", line_len, sizeof("CLIENT_ERROR")-1)) {
		response = MMC_RESPONSE_CLIENT_ERROR;
	}
	else {
		response = MMC_RESPONSE_UNKNOWN;
	}
	
	return response;
}

static int mmc_request_parse_response(mmc_t *mmc, mmc_request_t *request) /* 
	reads a generic response header and delegates it to response_handler {{{ */
{
	char *line;
	int line_len = mmc_stream_get_line(request->io, &line);
	
	if (line_len > 0) {
		int response = mmc_request_check_response(line, line_len);
		return request->response_handler(mmc, request, response, line, line_len - (sizeof("\r\n")-1), request->response_handler_param);
	}
	
	return MMC_REQUEST_MORE;
}
/* }}}*/

static int mmc_request_parse_mutate(mmc_t *mmc, mmc_request_t *request) /* 
	reads and parses the <long-value> response header {{{ */
{
	char *line;
	int line_len;
	
	line_len = mmc_stream_get_line(request->io, &line);
	if (line_len > 0) {
		long lval;
		zval value;

		int response = mmc_request_check_response(line, line_len);
		if (response != MMC_RESPONSE_UNKNOWN) {
			return request->response_handler(mmc, request, response, line, line_len - (sizeof("\r\n")-1), request->response_handler_param);
		}

		if (sscanf(line, "%lu", &lval) < 1) {
			return mmc_server_failure(mmc, request->io, "Malformed VALUE header", 0);
		}
	
		ZVAL_LONG(&value, lval);
		return request->value_handler(request->key, request->key_len, &value, 0, 0, request->value_handler_param);
	}
	
	return MMC_REQUEST_MORE;
}
/* }}}*/

static int mmc_request_parse_value(mmc_t *mmc, mmc_request_t *request) /* 
	reads and parses the VALUE <key> <flags> <size> <cas> response header and then reads the value body {{{ */
{
	char *line;
	int line_len;
	mmc_ascii_request_t *req = (mmc_ascii_request_t *)request;
	
	line_len = mmc_stream_get_line(request->io, &line);
	if (line_len > 0) {
		if (mmc_str_left(line, "END", line_len, sizeof("END")-1)) {
			return MMC_REQUEST_DONE;
		}
		
		if (sscanf(line, MMC_VALUE_HEADER, req->value.key, &(req->value.flags), &(req->value.length), &(req->value.cas)) < 3) {
			return mmc_server_failure(mmc, request->io, "Malformed VALUE header", 0);
		}
		
		/* memory for data + \r\n */
		mmc_buffer_alloc(&(request->readbuf), req->value.length + 2);

		/* allow read_value handler to read the value body */
		request->parse = mmc_server_read_value;

		/* read more, php streams buffer input which must be read if available */
		return MMC_REQUEST_AGAIN;
	}
	
	return MMC_REQUEST_MORE;
}
/* }}}*/

static int mmc_server_read_value(mmc_t *mmc, mmc_request_t *request) /* 
	read the value body into the buffer {{{ */
{
	mmc_ascii_request_t *req = (mmc_ascii_request_t *)request;
	request->readbuf.idx += 
		request->io->read(request->io, request->readbuf.value.c + request->readbuf.idx, req->value.length + 2 - request->readbuf.idx);

	/* done reading? */
	if (request->readbuf.idx >= req->value.length + 2) {
		int result;

		/* allow parse_value to read next VALUE or END line */
		request->parse = mmc_request_parse_value;
		mmc_buffer_reset(&(request->readbuf));

		result = mmc_unpack_value(
			mmc, request, &(request->readbuf), req->value.key, strlen(req->value.key), 
			req->value.flags, req->value.cas, req->value.length);
		
		/* request more data (END line) */
		if (result == MMC_REQUEST_DONE) {
			return MMC_REQUEST_AGAIN;
		}
		
		return result;
	}
	
	return MMC_REQUEST_MORE;
}
/* }}}*/

static mmc_request_t *mmc_ascii_create_request() /* {{{ */ 
{
	mmc_ascii_request_t *request = emalloc(sizeof(mmc_ascii_request_t));
	ZEND_SECURE_ZERO(request, sizeof(*request));
	return (mmc_request_t *)request;
}
/* }}} */

static void mmc_ascii_clone_request(mmc_request_t *clone, mmc_request_t *request) /* {{{ */ 
{}
/* }}} */

static void mmc_ascii_reset_request(mmc_request_t *request) /* {{{ */ 
{
	mmc_ascii_request_t *req = (mmc_ascii_request_t *)request;
	req->value.cas = 0;
	mmc_request_reset(request);
}
/* }}} */

static void mmc_ascii_begin_get(mmc_request_t *request, int op) /* {{{ */
{
	request->parse = mmc_request_parse_value;

	if (op == MMC_OP_GETS) {
		smart_string_appendl(&(request->sendbuf.value), "gets", sizeof("gets")-1);
	}
	else {
		smart_string_appendl(&(request->sendbuf.value), "get", sizeof("get")-1);
	}
}
/* }}} */

static void mmc_ascii_append_get(mmc_request_t *request, zval *zkey, const char *key, unsigned int key_len) /* {{{ */
{
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_appendl(&(request->sendbuf.value), key, key_len);
}
/* }}} */

static void mmc_ascii_end_get(mmc_request_t *request) /* {{{ */ 
{
	smart_string_appendl(&(request->sendbuf.value), "\r\n", sizeof("\r\n")-1);
}
/* }}} */

static void mmc_ascii_get(mmc_request_t *request, int op, zval *zkey, const char *key, unsigned int key_len) /* {{{ */ 
{
	mmc_ascii_begin_get(request, op);
	mmc_ascii_append_get(request, zkey, key, key_len);
	mmc_ascii_end_get(request);
}
/* }}} */

static int mmc_ascii_store(
	mmc_pool_t *pool, mmc_request_t *request, int op, const char *key, unsigned int key_len, 
	unsigned int flags, unsigned int exptime, unsigned long cas, zval *value) /* {{{ */ 
{
	int status;
	mmc_buffer_t buffer;
	request->parse = mmc_request_parse_response;

	ZEND_SECURE_ZERO(&buffer, sizeof(buffer));
	status = mmc_pack_value(pool, &buffer, value, &flags);
	
	if (status != MMC_OK) {
		return status;
	}
	
	switch (op) {
		case MMC_OP_SET:
			smart_string_appendl(&(request->sendbuf.value), "set", sizeof("set")-1);
			break;
		case MMC_OP_ADD:
			smart_string_appendl(&(request->sendbuf.value), "add", sizeof("add")-1);
			break;
		case MMC_OP_REPLACE:
			smart_string_appendl(&(request->sendbuf.value), "replace", sizeof("replace")-1);
			break;
		case MMC_OP_CAS:
			smart_string_appendl(&(request->sendbuf.value), "cas", sizeof("cas")-1);
			break;
		case MMC_OP_APPEND:
			smart_string_appendl(&(request->sendbuf.value), "append", sizeof("append")-1);
			break;
		case MMC_OP_PREPEND:
			smart_string_appendl(&(request->sendbuf.value), "prepend", sizeof("prepend")-1);
			break;
		default:
			return MMC_REQUEST_FAILURE;
	}
	
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_appendl(&(request->sendbuf.value), key, key_len);
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_append_unsigned(&(request->sendbuf.value), flags);
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_append_unsigned(&(request->sendbuf.value), exptime);
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_append_unsigned(&(request->sendbuf.value), buffer.value.len);

	if (op == MMC_OP_CAS) {
		smart_string_appendl(&(request->sendbuf.value), " ", 1);
		smart_string_append_unsigned(&(request->sendbuf.value), cas);
	}
	
	smart_string_appendl(&(request->sendbuf.value), "\r\n", sizeof("\r\n")-1);
	smart_string_appendl(&(request->sendbuf.value), buffer.value.c, buffer.value.len);
	smart_string_appendl(&(request->sendbuf.value), "\r\n", sizeof("\r\n")-1);
	
	mmc_buffer_free(&buffer);	
	return MMC_OK;
}
/* }}} */

static void mmc_ascii_delete(mmc_request_t *request, const char *key, unsigned int key_len, unsigned int exptime) /* {{{ */ 
{
	request->parse = mmc_request_parse_response;
	
	smart_string_appendl(&(request->sendbuf.value), "delete", sizeof("delete")-1);
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_appendl(&(request->sendbuf.value), key, key_len);
	
	if (exptime > 0) {
		smart_string_appendl(&(request->sendbuf.value), " ", 1);
		smart_string_append_unsigned(&(request->sendbuf.value), exptime);
	}

	smart_string_appendl(&(request->sendbuf.value), "\r\n", sizeof("\r\n")-1);
}
/* }}} */

static void mmc_ascii_mutate(mmc_request_t *request, zval *zkey, const char *key, unsigned int key_len, long value, long defval, int defval_used, unsigned int exptime) /* {{{ */
{
	request->parse = mmc_request_parse_mutate;
	
	if (value >= 0) {
		smart_string_appendl(&(request->sendbuf.value), "incr", sizeof("incr")-1);
	}
	else {
		smart_string_appendl(&(request->sendbuf.value), "decr", sizeof("decr")-1);
	}
	
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_appendl(&(request->sendbuf.value), key, key_len);
	smart_string_appendl(&(request->sendbuf.value), " ", 1);
	smart_string_append_unsigned(&(request->sendbuf.value), value >= 0 ? value : -value);
	smart_string_appendl(&(request->sendbuf.value), "\r\n", sizeof("\r\n")-1);
}
/* }}} */

static void mmc_ascii_flush(mmc_request_t *request, unsigned int exptime) /* {{{ */
{
	request->parse = mmc_request_parse_response;
	smart_string_appendl(&(request->sendbuf.value), "flush_all", sizeof("flush_all")-1);

	if (exptime > 0) {
		smart_string_appendl(&(request->sendbuf.value), " ", 1);
		smart_string_append_unsigned(&(request->sendbuf.value), exptime);
	}

	smart_string_appendl(&(request->sendbuf.value), "\r\n", sizeof("\r\n")-1);
}
/* }}} */

static void mmc_ascii_version(mmc_request_t *request) /* {{{ */ 
{
	request->parse = mmc_request_parse_response;
	smart_string_appendl(&(request->sendbuf.value), "version\r\n", sizeof("version\r\n")-1);
}
/* }}} */

static void mmc_ascii_stats(mmc_request_t *request, const char *type, long slabid, long limit) /* {{{ */
{
	char *cmd;
	unsigned int cmd_len;
	request->parse = mmc_request_parse_response;
	
	if (slabid) {
		cmd_len = spprintf(&cmd, 0, "stats %s %ld %ld\r\n", type, slabid, limit);
	}
	else if (type) {
		cmd_len = spprintf(&cmd, 0, "stats %s\r\n", type);
	}
	else {
		cmd_len = spprintf(&cmd, 0, "stats\r\n");
	}
	
	smart_string_appendl(&(request->sendbuf.value), cmd, cmd_len);
	efree(cmd);
}
/* }}} */

static void mmc_set_sasl_auth_data(mmc_pool_t *pool, mmc_request_t *request, const char *user,  const char *password) /* {{{ */
{
	/* stats not supported */
}
/* }}} */

mmc_protocol_t mmc_ascii_protocol = {
	mmc_ascii_create_request,
	mmc_ascii_clone_request,
	mmc_ascii_reset_request,
	mmc_request_free,
	mmc_ascii_get,
	mmc_ascii_begin_get,
	mmc_ascii_append_get,
	mmc_ascii_end_get,
	mmc_ascii_store,
	mmc_ascii_delete,
	mmc_ascii_mutate,
	mmc_ascii_flush,
	mmc_ascii_version,
	mmc_ascii_stats,
	mmc_set_sasl_auth_data
};

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */