-
Notifications
You must be signed in to change notification settings - Fork 323
/
php_memcached.c
4528 lines (3772 loc) · 130 KB
/
php_memcached.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) 2009-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 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_01.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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrei Zmievski <[email protected]> |
+----------------------------------------------------------------------+
*/
/* TODO
* - set LIBKETAMA_COMPATIBLE as the default?
* - fix unserialize(serialize($memc))
*/
#include "php_memcached.h"
#include "php_memcached_private.h"
#include "php_memcached_server.h"
#include "g_fmt.h"
#include <ctype.h>
#include <limits.h>
#ifdef HAVE_MEMCACHED_SESSION
# include "php_memcached_session.h"
#endif
#ifdef HAVE_FASTLZ_H
#include <fastlz.h>
#else
#include "fastlz/fastlz.h"
#endif
#include <zlib.h>
#ifdef HAVE_ZSTD_H
#include <zstd.h>
#endif
#ifdef HAVE_JSON_API
# include "ext/json/php_json.h"
#endif
#ifdef HAVE_MEMCACHED_IGBINARY
#ifdef PHP_WIN32
//Windows extensions are generally built together,
//so it wont be in the installed location
#include "igbinary.h"
#else
# include "ext/igbinary/igbinary.h"
#endif
#endif
#ifdef HAVE_MEMCACHED_MSGPACK
# include "ext/msgpack/php_msgpack.h"
#endif
# include "ext/spl/spl_exceptions.h"
static int le_memc;
static int php_memc_list_entry(void) {
return le_memc;
}
/****************************************
Protocol parameters
****************************************/
#define MEMC_OBJECT_KEY_MAX_LENGTH 250
/****************************************
Custom options
****************************************/
#define MEMC_OPT_COMPRESSION -1001
#define MEMC_OPT_PREFIX_KEY -1002
#define MEMC_OPT_SERIALIZER -1003
#define MEMC_OPT_COMPRESSION_TYPE -1004
#define MEMC_OPT_STORE_RETRY_COUNT -1005
#define MEMC_OPT_USER_FLAGS -1006
#define MEMC_OPT_COMPRESSION_LEVEL -1007
#define MEMC_OPT_ITEM_SIZE_LIMIT -1008
/****************************************
Custom result codes
****************************************/
#define MEMC_RES_PAYLOAD_FAILURE -1001
/****************************************
Payload value flags
****************************************/
#define MEMC_CREATE_MASK(start, n_bits) (((1U << n_bits) - 1) << start)
#define MEMC_MASK_TYPE MEMC_CREATE_MASK(0, 4)
#define MEMC_MASK_INTERNAL MEMC_CREATE_MASK(4, 12)
#define MEMC_MASK_USER MEMC_CREATE_MASK(16, 16)
#define MEMC_VAL_GET_TYPE(flags) ((flags) & MEMC_MASK_TYPE)
#define MEMC_VAL_SET_TYPE(flags, type) ((flags) |= ((type) & MEMC_MASK_TYPE))
#define MEMC_VAL_IS_STRING 0
#define MEMC_VAL_IS_LONG 1
#define MEMC_VAL_IS_DOUBLE 2
#define MEMC_VAL_IS_BOOL 3
#define MEMC_VAL_IS_SERIALIZED 4
#define MEMC_VAL_IS_IGBINARY 5
#define MEMC_VAL_IS_JSON 6
#define MEMC_VAL_IS_MSGPACK 7
#define MEMC_VAL_COMPRESSED (1<<0)
#define MEMC_VAL_COMPRESSION_ZLIB (1<<1)
#define MEMC_VAL_COMPRESSION_FASTLZ (1<<2)
#define MEMC_VAL_COMPRESSION_ZSTD (1<<3)
#define MEMC_VAL_GET_FLAGS(internal_flags) (((internal_flags) & MEMC_MASK_INTERNAL) >> 4)
#define MEMC_VAL_SET_FLAG(internal_flags, internal_flag) ((internal_flags) |= (((internal_flag) << 4) & MEMC_MASK_INTERNAL))
#define MEMC_VAL_HAS_FLAG(internal_flags, internal_flag) ((MEMC_VAL_GET_FLAGS(internal_flags) & (internal_flag)) == (internal_flag))
#define MEMC_VAL_DEL_FLAG(internal_flags, internal_flag) (internal_flags &= (~(((internal_flag) << 4) & MEMC_MASK_INTERNAL)))
/****************************************
User-defined flags
****************************************/
#define MEMC_VAL_GET_USER_FLAGS(flags) ((flags & MEMC_MASK_USER) >> 16)
#define MEMC_VAL_SET_USER_FLAGS(flags, udf_flags) ((flags) |= ((udf_flags << 16) & MEMC_MASK_USER))
#define MEMC_VAL_USER_FLAGS_MAX ((1 << 16) - 1)
/****************************************
"get" operation flags
****************************************/
#define MEMC_GET_PRESERVE_ORDER 1
#define MEMC_GET_EXTENDED 2
/****************************************
Helper macros
****************************************/
#define RETURN_FROM_GET RETURN_FALSE
/****************************************
Structures and definitions
****************************************/
typedef enum {
MEMC_OP_SET,
MEMC_OP_TOUCH,
MEMC_OP_ADD,
MEMC_OP_REPLACE,
MEMC_OP_APPEND,
MEMC_OP_PREPEND
} php_memc_write_op;
typedef struct {
zend_bool is_persistent;
zend_bool compression_enabled;
zend_bool encoding_enabled;
zend_long serializer;
zend_long compression_type;
zend_long compression_level;
zend_long store_retry_count;
zend_long set_udf_flags;
zend_long item_size_limit;
#ifdef HAVE_MEMCACHED_SASL
zend_bool has_sasl_data;
#endif
} php_memc_user_data_t;
typedef struct {
memcached_st *memc;
zend_bool is_pristine;
int rescode;
int memc_errno;
zend_object zo;
} php_memc_object_t;
typedef struct {
size_t num_valid_keys;
const char **mkeys;
size_t *mkeys_len;
zend_string **strings;
} php_memc_keys_t;
typedef struct {
zval *object;
zend_fcall_info fci;
zend_fcall_info_cache fcc;
} php_memc_result_callback_ctx_t;
static inline php_memc_object_t *php_memc_fetch_object(zend_object *obj) {
return (php_memc_object_t *)((char *)obj - XtOffsetOf(php_memc_object_t, zo));
}
#define Z_MEMC_OBJ_P(zv) php_memc_fetch_object(Z_OBJ_P(zv));
#define MEMC_METHOD_INIT_VARS \
zval* object = getThis(); \
php_memc_object_t* intern = NULL; \
php_memc_user_data_t* memc_user_data = NULL;
#if PHP_VERSION_ID < 80000
#define MEMC_METHOD_FETCH_OBJECT \
intern = Z_MEMC_OBJ_P(object); \
if (!intern->memc) { \
php_error_docref(NULL, E_WARNING, "Memcached constructor was not called"); \
return; \
} \
memc_user_data = (php_memc_user_data_t *) memcached_get_user_data(intern->memc); \
(void)memc_user_data; /* avoid unused variable warning */
#else
#define MEMC_METHOD_FETCH_OBJECT \
intern = Z_MEMC_OBJ_P(object); \
if (!intern->memc) { \
zend_throw_error(NULL, "Memcached constructor was not called"); \
RETURN_THROWS(); \
} \
memc_user_data = (php_memc_user_data_t *) memcached_get_user_data(intern->memc); \
(void)memc_user_data; /* avoid unused variable warning */
#endif
static
zend_bool s_memc_valid_key_binary(zend_string *key)
{
return memchr(ZSTR_VAL(key), '\n', ZSTR_LEN(key)) == NULL;
}
static
uint32_t s_memc_object_key_max_length(php_memc_object_t *intern) {
memcached_return retval;
char *result;
result = memcached_callback_get(intern->memc, MEMCACHED_CALLBACK_PREFIX_KEY, &retval);
if (retval == MEMCACHED_SUCCESS && result) {
return MEMC_OBJECT_KEY_MAX_LENGTH - strlen(result);
} else {
return MEMC_OBJECT_KEY_MAX_LENGTH;
}
}
zend_bool s_memc_valid_key_ascii(zend_string *key, uint64_t verify_key)
{
const char *str = ZSTR_VAL(key);
size_t i, len = ZSTR_LEN(key);
if (verify_key) {
for (i = 0; i < len; i++) {
if (!isgraph(str[i]) || isspace(str[i]))
return 0;
}
} else { /* if key verification is disabled, only check for spaces to avoid injection issues */
for (i = 0; i < len; i++) {
if (isspace(str[i]))
return 0;
}
}
return 1;
}
#define MEMC_CHECK_KEY(intern, key) \
if (UNEXPECTED(ZSTR_LEN(key) == 0 || \
ZSTR_LEN(key) > s_memc_object_key_max_length(intern) || \
(memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) \
? !s_memc_valid_key_binary(key) \
: !s_memc_valid_key_ascii(key, memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_VERIFY_KEY)) \
))) { \
intern->rescode = MEMCACHED_BAD_KEY_PROVIDED; \
RETURN_FALSE; \
}
#ifdef HAVE_MEMCACHED_PROTOCOL
typedef struct {
php_memc_proto_handler_t *handler;
zend_object zo;
} php_memc_server_t;
static inline php_memc_server_t *php_memc_server_fetch_object(zend_object *obj) {
return (php_memc_server_t *)((char *)obj - XtOffsetOf(php_memc_server_t, zo));
}
#define Z_MEMC_SERVER_P(zv) php_memc_server_fetch_object(Z_OBJ_P(zv))
static zend_object_handlers memcached_server_object_handlers;
static zend_class_entry *memcached_server_ce = NULL;
#endif
static zend_class_entry *memcached_ce = NULL;
static zend_class_entry *memcached_exception_ce = NULL;
static zend_object_handlers memcached_object_handlers;
ZEND_DECLARE_MODULE_GLOBALS(php_memcached)
#ifdef COMPILE_DL_MEMCACHED
ZEND_GET_MODULE(memcached)
#endif
static PHP_INI_MH(OnUpdateCompressionType)
{
if (!new_value) {
MEMC_G(compression_type) = COMPRESSION_TYPE_FASTLZ;
} else if (!strcmp(ZSTR_VAL(new_value), "fastlz")) {
MEMC_G(compression_type) = COMPRESSION_TYPE_FASTLZ;
} else if (!strcmp(ZSTR_VAL(new_value), "zlib")) {
MEMC_G(compression_type) = COMPRESSION_TYPE_ZLIB;
#ifdef HAVE_ZSTD_H
} else if (!strcmp(ZSTR_VAL(new_value), "zstd")) {
MEMC_G(compression_type) = COMPRESSION_TYPE_ZSTD;
#endif
} else {
return FAILURE;
}
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
static PHP_INI_MH(OnUpdateSerializer)
{
if (!new_value) {
MEMC_G(serializer_type) = SERIALIZER_DEFAULT;
} else if (!strcmp(ZSTR_VAL(new_value), "php")) {
MEMC_G(serializer_type) = SERIALIZER_PHP;
#ifdef HAVE_MEMCACHED_IGBINARY
} else if (!strcmp(ZSTR_VAL(new_value), "igbinary")) {
MEMC_G(serializer_type) = SERIALIZER_IGBINARY;
#endif // IGBINARY
#ifdef HAVE_JSON_API
} else if (!strcmp(ZSTR_VAL(new_value), "json")) {
MEMC_G(serializer_type) = SERIALIZER_JSON;
} else if (!strcmp(ZSTR_VAL(new_value), "json_array")) {
MEMC_G(serializer_type) = SERIALIZER_JSON_ARRAY;
#endif // JSON
#ifdef HAVE_MEMCACHED_MSGPACK
} else if (!strcmp(ZSTR_VAL(new_value), "msgpack")) {
MEMC_G(serializer_type) = SERIALIZER_MSGPACK;
#endif // msgpack
} else {
return FAILURE;
}
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
#ifdef HAVE_MEMCACHED_SESSION
static
PHP_INI_MH(OnUpdateDeprecatedLockValue)
{
if (ZSTR_LEN(new_value) > 0 && strcmp(ZSTR_VAL(new_value), "not set")) {
php_error_docref(NULL, E_DEPRECATED, "memcached.sess_lock_wait and memcached.sess_lock_max_wait are deprecated. Please update your configuration to use memcached.sess_lock_wait_min, memcached.sess_lock_wait_max and memcached.sess_lock_retries");
}
return FAILURE;
}
static
PHP_INI_MH(OnUpdateSessionPrefixString)
{
if (new_value && ZSTR_LEN(new_value) > 0) {
if (ZSTR_LEN(new_value) > MEMCACHED_MAX_KEY) {
php_error_docref(NULL, E_WARNING, "memcached.sess_prefix too long (max: %d)", MEMCACHED_MAX_KEY - 1);
return FAILURE;
}
if (!s_memc_valid_key_ascii(new_value, 1)) {
php_error_docref(NULL, E_WARNING, "memcached.sess_prefix cannot contain whitespace or control characters");
return FAILURE;
}
}
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
static
PHP_INI_MH(OnUpdateConsistentHash)
{
if (!new_value) {
MEMC_SESS_INI(consistent_hash_type) = MEMCACHED_BEHAVIOR_KETAMA;
} else if (!strcmp(ZSTR_VAL(new_value), "ketama")) {
MEMC_SESS_INI(consistent_hash_type) = MEMCACHED_BEHAVIOR_KETAMA;
} else if (!strcmp(ZSTR_VAL(new_value), "ketama_weighted")) {
MEMC_SESS_INI(consistent_hash_type) = MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED;
} else {
php_error_docref(NULL, E_WARNING, "memcached.sess_consistent_hash_type must be ketama or ketama_weighted");
return FAILURE;
}
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
#endif // HAVE_MEMCACHED_SESSION
#define MEMC_INI_ENTRY(key, default_value, update_fn, gkey) \
STD_PHP_INI_ENTRY("memcached."key, default_value, PHP_INI_ALL, update_fn, memc.gkey, zend_php_memcached_globals, php_memcached_globals)
#define MEMC_INI_BOOL(key, default_value, update_fn, gkey) \
STD_PHP_INI_BOOLEAN("memcached."key, default_value, PHP_INI_ALL, update_fn, memc.gkey, zend_php_memcached_globals, php_memcached_globals)
#define MEMC_INI_LINK(key, default_value, update_fn, gkey) \
STD_PHP_INI_ENTRY_EX("memcached."key, default_value, PHP_INI_ALL, update_fn, memc.gkey, zend_php_memcached_globals, php_memcached_globals, display_link_numbers)
#define MEMC_SESSION_INI_ENTRY(key, default_value, update_fn, gkey) \
STD_PHP_INI_ENTRY("memcached.sess_"key, default_value, PHP_INI_ALL, update_fn, session.gkey, zend_php_memcached_globals, php_memcached_globals)
#define MEMC_SESSION_INI_BOOL(key, default_value, update_fn, gkey) \
STD_PHP_INI_BOOLEAN("memcached.sess_"key, default_value, PHP_INI_ALL, update_fn, session.gkey, zend_php_memcached_globals, php_memcached_globals)
#define MEMC_SESSION_INI_LINK(key, default_value, update_fn, gkey) \
STD_PHP_INI_ENTRY_EX("memcached.sess_"key, default_value, PHP_INI_ALL, update_fn, session.gkey, zend_php_memcached_globals, php_memcached_globals, display_link_numbers)
/* {{{ INI entries */
PHP_INI_BEGIN()
#ifdef HAVE_MEMCACHED_SESSION
MEMC_SESSION_INI_BOOL ("locking", "1", OnUpdateBool, lock_enabled)
MEMC_SESSION_INI_ENTRY("lock_wait_min", "150", OnUpdateLongGEZero, lock_wait_min)
MEMC_SESSION_INI_ENTRY("lock_wait_max", "150", OnUpdateLongGEZero, lock_wait_max)
MEMC_SESSION_INI_ENTRY("lock_retries", "5", OnUpdateLong, lock_retries)
MEMC_SESSION_INI_ENTRY("lock_expire", "0", OnUpdateLongGEZero, lock_expiration)
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000018
MEMC_SESSION_INI_BOOL ("binary_protocol", "0", OnUpdateBool, binary_protocol_enabled)
#else
MEMC_SESSION_INI_BOOL ("binary_protocol", "1", OnUpdateBool, binary_protocol_enabled)
#endif
MEMC_SESSION_INI_BOOL ("consistent_hash", "1", OnUpdateBool, consistent_hash_enabled)
MEMC_SESSION_INI_ENTRY("consistent_hash_type", "ketama", OnUpdateConsistentHash, consistent_hash_name)
MEMC_SESSION_INI_ENTRY("number_of_replicas", "0", OnUpdateLongGEZero, number_of_replicas)
MEMC_SESSION_INI_BOOL ("randomize_replica_read", "0", OnUpdateBool, randomize_replica_read_enabled)
MEMC_SESSION_INI_BOOL ("remove_failed_servers", "0", OnUpdateBool, remove_failed_servers_enabled)
MEMC_SESSION_INI_ENTRY("server_failure_limit", "0", OnUpdateLongGEZero, server_failure_limit)
MEMC_SESSION_INI_LINK ("connect_timeout", "0", OnUpdateLong, connect_timeout)
MEMC_SESSION_INI_ENTRY("sasl_username", "", OnUpdateString, sasl_username)
MEMC_SESSION_INI_ENTRY("sasl_password", "", OnUpdateString, sasl_password)
MEMC_SESSION_INI_BOOL ("persistent", "0", OnUpdateBool, persistent_enabled)
MEMC_SESSION_INI_ENTRY("prefix", "memc.sess.key.", OnUpdateSessionPrefixString, prefix)
/* Deprecated */
STD_PHP_INI_ENTRY("memcached.sess_lock_wait", "not set", PHP_INI_ALL, OnUpdateDeprecatedLockValue, no_effect, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_lock_max_wait", "not set", PHP_INI_ALL, OnUpdateDeprecatedLockValue, no_effect, zend_php_memcached_globals, php_memcached_globals)
#endif
MEMC_INI_ENTRY("compression_type", "fastlz", OnUpdateCompressionType, compression_name)
MEMC_INI_ENTRY("compression_factor", "1.3", OnUpdateReal, compression_factor)
MEMC_INI_ENTRY("compression_level", "3", OnUpdateLong, compression_level)
MEMC_INI_ENTRY("compression_threshold", "2000", OnUpdateLong, compression_threshold)
MEMC_INI_ENTRY("serializer", SERIALIZER_DEFAULT_NAME, OnUpdateSerializer, serializer_name)
MEMC_INI_ENTRY("store_retry_count", "0", OnUpdateLong, store_retry_count)
MEMC_INI_ENTRY("item_size_limit", "0", OnUpdateLongGEZero, item_size_limit)
MEMC_INI_BOOL ("default_consistent_hash", "0", OnUpdateBool, default_behavior.consistent_hash_enabled)
MEMC_INI_BOOL ("default_binary_protocol", "0", OnUpdateBool, default_behavior.binary_protocol_enabled)
MEMC_INI_LINK ("default_connect_timeout", "0", OnUpdateLong, default_behavior.connect_timeout)
PHP_INI_END()
/* }}} */
#undef MEMC_INI_BOOL
#undef MEMC_INI_LINK
#undef MEMC_INI_ENTRY
#undef MEMC_SESSION_INI_BOOL
#undef MEMC_SESSION_INI_LINK
#undef MEMC_SESSION_INI_ENTRY
/****************************************
Forward declarations
****************************************/
static void php_memc_get_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool by_key);
static void php_memc_setMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_delete_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_deleteMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_getDelayed_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
/* Invoke PHP functions */
static zend_bool s_invoke_cache_callback(zval *zobject, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_bool with_cas, zend_string *key, zval *value);
/* Iterate result sets */
typedef zend_bool (*php_memc_result_apply_fn)(php_memc_object_t *intern, zend_string *key, zval *value, zval *cas, uint32_t flags, void *context);
static
memcached_return php_memc_result_apply(php_memc_object_t *intern, php_memc_result_apply_fn result_apply_fn, zend_bool fetch_delay, void *context);
static
zend_bool php_memc_mget_apply(php_memc_object_t *intern, zend_string *server_key,
php_memc_keys_t *keys, php_memc_result_apply_fn result_apply_fn,
zend_bool with_cas, void *context);
/* Callback functions for different server list iterations */
static
memcached_return s_server_cursor_list_servers_cb(const memcached_st *ptr, php_memcached_instance_st instance, void *in_context);
static
memcached_return s_server_cursor_version_cb(const memcached_st *ptr, php_memcached_instance_st instance, void *in_context);
static
zend_bool s_memc_write_zval (php_memc_object_t *intern, php_memc_write_op op, zend_string *server_key, zend_string *key, zval *value, time_t expiration);
static
void php_memc_destroy(memcached_st *memc, php_memc_user_data_t *memc_user_data);
static
zend_bool s_memcached_result_to_zval(memcached_st *memc, memcached_result_st *result, zval *return_value);
static
zend_string *s_zval_to_payload(php_memc_object_t *intern, zval *value, uint32_t *flags);
static
void s_hash_to_keys(php_memc_keys_t *keys_out, HashTable *hash_in, zend_bool preserve_order, zval *return_value);
static
void s_clear_keys(php_memc_keys_t *keys);
/****************************************
Exported helper functions
****************************************/
zend_bool php_memc_init_sasl_if_needed()
{
#ifdef HAVE_MEMCACHED_SASL
if (MEMC_G(sasl_initialised)) {
return 1;
}
if (sasl_client_init(NULL) != SASL_OK) {
php_error_docref(NULL, E_ERROR, "Failed to initialize SASL library");
return 0;
}
return 1;
#else
php_error_docref(NULL, E_ERROR, "Memcached not built with sasl support");
return 0;
#endif
}
char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache)
{
char *buffer = NULL;
if (fci->object) {
spprintf (&buffer, 0, "%s::%s", ZSTR_VAL(fci->object->ce->name), ZSTR_VAL(fci_cache->function_handler->common.function_name));
} else {
if (Z_TYPE (fci->function_name) == IS_OBJECT) {
spprintf (&buffer, 0, "%s", ZSTR_VAL(Z_OBJCE(fci->function_name)->name));
}
else {
spprintf (&buffer, 0, "%s", Z_STRVAL(fci->function_name));
}
}
return buffer;
}
/****************************************
Handling error codes
****************************************/
static
zend_bool s_memcached_return_is_error(memcached_return status, zend_bool strict)
{
zend_bool result = 0;
switch (status) {
case MEMCACHED_SUCCESS:
case MEMCACHED_STORED:
case MEMCACHED_DELETED:
case MEMCACHED_STAT:
case MEMCACHED_END:
case MEMCACHED_BUFFERED:
result = 0;
break;
case MEMCACHED_SOME_ERRORS:
result = strict;
break;
default:
result = 1;
break;
}
return result;
}
static
int s_memc_status_handle_result_code(php_memc_object_t *intern, memcached_return status)
{
intern->rescode = status;
intern->memc_errno = 0;
if (s_memcached_return_is_error(status, 1)) {
intern->memc_errno = memcached_last_error_errno(intern->memc);
return FAILURE;
}
return SUCCESS;
}
static
void s_memc_set_status(php_memc_object_t *intern, memcached_return status, int memc_errno)
{
intern->rescode = status;
intern->memc_errno = memc_errno;
}
static
zend_bool s_memc_status_has_error(php_memc_object_t *intern)
{
return s_memcached_return_is_error(intern->rescode, 1);
}
static
zend_bool s_memc_status_has_result_code(php_memc_object_t *intern, memcached_return status)
{
return intern->rescode == status;
}
/****************************************
Marshall cas tokens
****************************************/
static
void s_uint64_to_zval (zval *target, uint64_t value)
{
if (value >= ((uint64_t) LONG_MAX)) {
zend_string *buffer;
#ifdef PRIu64
buffer = strpprintf (0, "%" PRIu64, value);
#else
/* Best effort */
buffer = strpprintf (0, "%llu", value);
#endif
ZVAL_STR(target, buffer);
}
else {
ZVAL_LONG (target, (zend_long) value);
}
}
static
uint64_t s_zval_to_uint64 (zval *cas)
{
switch (Z_TYPE_P (cas)) {
case IS_LONG:
return (uint64_t) zval_get_long (cas);
break;
case IS_DOUBLE:
if (Z_DVAL_P (cas) < 0.0)
return 0;
return (uint64_t) zval_get_double (cas);
break;
case IS_STRING:
{
uint64_t val;
char *end;
errno = 0;
val = (uint64_t) strtoull (Z_STRVAL_P (cas), &end, 0);
if (*end || (errno == ERANGE && val == UINT64_MAX) || (errno != 0 && val == 0)) {
php_error_docref(NULL, E_ERROR, "Failed to unmarshall cas token");
return 0;
}
return val;
}
break;
}
return 0;
}
/****************************************
Iterate over memcached results and mget
****************************************/
static
memcached_return php_memc_result_apply(php_memc_object_t *intern, php_memc_result_apply_fn result_apply_fn, zend_bool fetch_delay, void *context)
{
memcached_result_st result, *result_ptr;
memcached_return rc, status = MEMCACHED_SUCCESS;
memcached_result_create(intern->memc, &result);
do {
result_ptr = memcached_fetch_result(intern->memc, &result, &rc);
if (s_memcached_return_is_error(rc, 0)) {
status = rc;
}
/* nothing returned */
if (!result_ptr) {
break;
}
else {
zend_string *key;
zval val, zcas;
zend_bool retval;
uint64_t cas;
uint32_t flags;
const char *res_key;
size_t res_key_len;
if (!s_memcached_result_to_zval(intern->memc, &result, &val)) {
if (EG(exception)) {
status = MEMC_RES_PAYLOAD_FAILURE;
memcached_quit(intern->memc);
break;
}
status = MEMCACHED_SOME_ERRORS;
continue;
}
res_key = memcached_result_key_value(&result);
res_key_len = memcached_result_key_length(&result);
cas = memcached_result_cas(&result);
flags = memcached_result_flags(&result);
s_uint64_to_zval(&zcas, cas);
key = zend_string_init (res_key, res_key_len, 0);
retval = result_apply_fn(intern, key, &val, &zcas, flags, context);
zend_string_release(key);
zval_ptr_dtor(&val);
zval_ptr_dtor(&zcas);
/* Stop iterating on false */
if (!retval) {
if (!fetch_delay) {
/* Make sure we clear our results */
while (memcached_fetch_result(intern->memc, &result, &rc)) {}
}
break;
}
}
} while (result_ptr != NULL);
memcached_result_free(&result);
return status;
}
static
zend_bool php_memc_mget_apply(php_memc_object_t *intern, zend_string *server_key, php_memc_keys_t *keys,
php_memc_result_apply_fn result_apply_fn, zend_bool with_cas, void *context)
{
memcached_return status;
int mget_status;
uint64_t orig_cas_flag = 0;
// Reset status code
s_memc_set_status(intern, MEMCACHED_SUCCESS, 0);
if (!keys->num_valid_keys) {
intern->rescode = MEMCACHED_BAD_KEY_PROVIDED;
return 0;
}
if (with_cas) {
orig_cas_flag = memcached_behavior_get (intern->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS);
if (!orig_cas_flag) {
memcached_behavior_set (intern->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
}
}
if (server_key) {
status = memcached_mget_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), keys->mkeys, keys->mkeys_len, keys->num_valid_keys);
} else {
status = memcached_mget(intern->memc, keys->mkeys, keys->mkeys_len, keys->num_valid_keys);
}
/* Need to handle result code before restoring cas flags, would mess up errno */
mget_status = s_memc_status_handle_result_code(intern, status);
if (with_cas && !orig_cas_flag) {
memcached_behavior_set (intern->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, orig_cas_flag);
}
/* Return on failure codes */
if (mget_status == FAILURE) {
return 0;
}
if (!result_apply_fn) {
/* no callback, for example getDelayed */
return 1;
}
status = php_memc_result_apply(intern, result_apply_fn, 0, context);
if (s_memc_status_handle_result_code(intern, status) == FAILURE) {
return 0;
}
return 1;
}
/****************************************
Invoke callbacks
****************************************/
static
zend_bool s_invoke_new_instance_cb(zval *object, zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zend_string *persistent_id)
{
zend_bool ret = 1;
zval retval;
zval params[2];
ZVAL_COPY(¶ms[0], object);
if (persistent_id) {
ZVAL_STR(¶ms[1], zend_string_copy(persistent_id));
} else {
ZVAL_NULL(¶ms[1]);
}
fci->retval = &retval;
fci->params = params;
fci->param_count = 2;
if (zend_call_function(fci, fci_cache) == FAILURE) {
char *buf = php_memc_printable_func (fci, fci_cache);
php_error_docref(NULL, E_WARNING, "Failed to invoke 'on_new' callback %s()", buf);
efree (buf);
ret = 0;
}
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(¶ms[1]);
zval_ptr_dtor(&retval);
return ret;
}
static
zend_bool s_invoke_cache_callback(zval *zobject, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_bool with_cas, zend_string *key, zval *value)
{
zend_bool status = 0;
zval params[4];
zval retval;
php_memc_object_t *intern = Z_MEMC_OBJ_P(zobject);
/* Prepare params */
ZVAL_COPY(¶ms[0], zobject);
ZVAL_STR_COPY(¶ms[1], key); /* key */
ZVAL_NEW_REF(¶ms[2], value); /* value */
if (with_cas) {
fci->param_count = 3;
} else {
ZVAL_NEW_EMPTY_REF(¶ms[3]); /* expiration */
ZVAL_NULL(Z_REFVAL(params[3]));
fci->param_count = 4;
}
fci->retval = &retval;
fci->params = params;
if (zend_call_function(fci, fcc) == SUCCESS) {
if (zend_is_true(&retval)) {
time_t expiration;
zval *val = Z_REFVAL(params[2]);
if (with_cas) {
if (Z_TYPE_P(val) == IS_ARRAY) {
zval *rv = zend_hash_str_find(Z_ARRVAL_P(val), "value", sizeof("value") - 1);
if (rv) {
zval *cas = zend_hash_str_find(Z_ARRVAL_P(val), "cas", sizeof("cas") -1);
expiration = cas? Z_LVAL_P(cas) : 0;
status = s_memc_write_zval (intern, MEMC_OP_SET, NULL, key, rv, expiration);
}
/* memleak? zval_ptr_dtor(value); */
ZVAL_COPY(value, val);
}
} else {
expiration = zval_get_long(Z_REFVAL(params[3]));
status = s_memc_write_zval (intern, MEMC_OP_SET, NULL, key, val, expiration);
/* memleak? zval_ptr_dtor(value); */
ZVAL_COPY(value, val);
}
}
}
else {
s_memc_set_status(intern, MEMCACHED_NOTFOUND, 0);
}
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(¶ms[1]);
zval_ptr_dtor(¶ms[2]);
if (!with_cas) {
zval_ptr_dtor(¶ms[3]);
}
zval_ptr_dtor(&retval);
return status;
}
/****************************************
Wrapper for setting from zval
****************************************/
static
zend_bool s_compress_value (php_memc_compression_type compression_type, zend_long compression_level, zend_string **payload_in, uint32_t *flags)
{
/* status */
zend_bool compress_status = 0;
zend_string *payload = *payload_in;
uint32_t compression_type_flag = 0;
/* Additional 5% for the data */
size_t buffer_size = (size_t) (((double) ZSTR_LEN(payload) * 1.05) + 1.0);
char *buffer = emalloc(buffer_size);
/* Store compressed size here */
size_t compressed_size = 0;
uint32_t original_size = ZSTR_LEN(payload);
switch (compression_type) {
case COMPRESSION_TYPE_FASTLZ:
{
compressed_size = fastlz_compress(ZSTR_VAL(payload), ZSTR_LEN(payload), buffer);
if (compressed_size > 0) {
compress_status = 1;
compression_type_flag = MEMC_VAL_COMPRESSION_FASTLZ;
}
}
break;
#ifdef HAVE_ZSTD_H
case COMPRESSION_TYPE_ZSTD:
{
compressed_size = ZSTD_compress((void *)buffer, buffer_size, ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level);
if (compression_level < -22) {
compression_level = -22;
} else if (compression_level > 22) {
compression_level = 22;
}
if (!ZSTD_isError(compressed_size)) {
compress_status = 1;
compression_type_flag = MEMC_VAL_COMPRESSION_ZSTD;
}
}
break;
#endif
case COMPRESSION_TYPE_ZLIB:
{
unsigned long cs = compressed_size = buffer_size;
if (compression_level < 0) {
compression_level = 0;
} else if (compression_level > 9) {
compression_level = 9;
}
int status = compress2((Bytef *) buffer, &cs, (Bytef *) ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level);
if (status == Z_OK) {
compressed_size = cs;
compress_status = 1;
compression_type_flag = MEMC_VAL_COMPRESSION_ZLIB;
}
}
break;
default:
compress_status = 0;
break;
}
/* This means the value was too small to be compressed and ended up larger */
if (ZSTR_LEN(payload) <= (compressed_size * MEMC_G(compression_factor))) {
compress_status = 0;
}
/* Replace the payload with the compressed copy */
if (compress_status) {
MEMC_VAL_SET_FLAG(*flags, MEMC_VAL_COMPRESSED | compression_type_flag);
payload = zend_string_realloc(payload, compressed_size + sizeof(uint32_t), 0);
/* Copy the uin32_t at the beginning */
memcpy(ZSTR_VAL(payload), &original_size, sizeof(uint32_t));
memcpy(ZSTR_VAL(payload) + sizeof (uint32_t), buffer, compressed_size);
efree(buffer);
zend_string_forget_hash_val(payload);
*payload_in = payload;
return 1;
}
/* Original payload was not modified */
efree(buffer);