2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libmemcachedprotocol/common.h"
24 static void print_ascii_command(memcached_protocol_client_st
*client
) {
25 if (client
->is_verbose
) {
26 switch (client
->ascii_command
) {
28 fprintf(stderr
, "%s:%d SET_CMD\n", __FILE__
, __LINE__
);
32 fprintf(stderr
, "%s:%d ADD_CMD\n", __FILE__
, __LINE__
);
36 fprintf(stderr
, "%s:%d REPLACE_CMD\n", __FILE__
, __LINE__
);
40 fprintf(stderr
, "%s:%d CAS_CMD\n", __FILE__
, __LINE__
);
44 fprintf(stderr
, "%s:%d APPEND_CMD\n", __FILE__
, __LINE__
);
48 fprintf(stderr
, "%s:%d PREPEND_CMD\n", __FILE__
, __LINE__
);
52 fprintf(stderr
, "%s:%d DELETE_CMD\n", __FILE__
, __LINE__
);
55 case INCR_CMD
: /* FALLTHROUGH */
56 fprintf(stderr
, "%s:%d INCR_CMD\n", __FILE__
, __LINE__
);
60 fprintf(stderr
, "%s:%d DECR_CMD\n", __FILE__
, __LINE__
);
64 fprintf(stderr
, "%s:%d STATS_CMD\n", __FILE__
, __LINE__
);
68 fprintf(stderr
, "%s:%d FLUSH_ALL_CMD\n", __FILE__
, __LINE__
);
72 fprintf(stderr
, "%s:%d VERSION_CMD\n", __FILE__
, __LINE__
);
76 fprintf(stderr
, "%s:%d QUIT_CMD\n", __FILE__
, __LINE__
);
80 fprintf(stderr
, "%s:%d VERBOSITY_CMD\n", __FILE__
, __LINE__
);
84 fprintf(stderr
, "%s:%d GET_CMD\n", __FILE__
, __LINE__
);
88 fprintf(stderr
, "%s:%d GETS_CMD\n", __FILE__
, __LINE__
);
93 fprintf(stderr
, "%s:%d UNKNOWN_CMD\n", __FILE__
, __LINE__
);
100 * Try to parse a key from the string.
101 * @pointer start pointer to a pointer to the string (IN and OUT)
102 * @return length of the string of -1 if this was an illegal key (invalid
103 * characters or invalid length)
106 static uint16_t parse_ascii_key(char **start
) {
109 /* Strip leading whitespaces */
110 while (isspace(*c
)) {
116 while (*c
!= '\0' && !isspace(*c
) && !iscntrl(*c
)) {
121 if (len
== 0 || len
> 240 || (*c
!= '\0' && *c
!= '\r' && iscntrl(*c
))) {
129 * Spool a zero-terminated string
130 * @param client destination
131 * @param text the text to spool
132 * @return status of the spool operation
134 static protocol_binary_response_status
135 ascii_raw_response_handler(memcached_protocol_client_st
*client
, const char *text
) {
136 if (client
->is_verbose
) {
137 fprintf(stderr
, "%s:%d %s\n", __FILE__
, __LINE__
, text
);
140 if (client
->root
->drain(client
) == false) {
141 return PROTOCOL_BINARY_RESPONSE_EINTERNAL
;
144 assert(client
->output
);
146 if (client
->output
== NULL
)
148 /* I can write directly to the socket.... */
151 size_t num_bytes
= len
-offset
;
152 ssize_t nw
= client
->root
->send(client
,
158 if (get_socket_errno() == EWOULDBLOCK
)
162 else if (get_socket_errno() != EINTR
)
164 client
->error
= errno
;
165 return PROTOCOL_BINARY_RESPONSE_EINTERNAL
;
170 offset
+= (size_t)nw
;
172 } while (offset
< len
);
176 return client
->root
->spool(client
, text
, strlen(text
));
180 * Send a "CLIENT_ERROR" message back to the client with the correct
181 * format of the command being sent
182 * @param client the client to send the message to
184 static void send_command_usage(memcached_protocol_client_st
*client
) {
185 const char *errmsg
[] = {
186 [GET_CMD
] = "CLIENT_ERROR: Syntax error: get <key>*\r\n",
187 [GETS_CMD
] = "CLIENT_ERROR: Syntax error: gets <key>*\r\n",
188 [SET_CMD
] = "CLIENT_ERROR: Syntax error: set <key> <flags> <exptime> <bytes> [noreply]\r\n",
189 [ADD_CMD
] = "CLIENT_ERROR: Syntax error: add <key> <flags> <exptime> <bytes> [noreply]\r\n",
191 "CLIENT_ERROR: Syntax error: replace <key> <flags> <exptime> <bytes> [noreply]\r\n",
193 "CLIENT_ERROR: Syntax error: cas <key> <flags> <exptime> <bytes> <casid> [noreply]\r\n",
195 "CLIENT_ERROR: Syntax error: append <key> <flags> <exptime> <bytes> [noreply]\r\n",
197 "CLIENT_ERROR: Syntax error: prepend <key> <flags> <exptime> <bytes> [noreply]\r\n",
198 [DELETE_CMD
] = "CLIENT_ERROR: Syntax error: delete_object <key> [noreply]\r\n",
199 [INCR_CMD
] = "CLIENT_ERROR: Syntax error: incr <key> <value> [noreply]\r\n",
200 [DECR_CMD
] = "CLIENT_ERROR: Syntax error: decr <key> <value> [noreply]\r\n",
201 [STATS_CMD
] = "CLIENT_ERROR: Syntax error: stats [key]\r\n",
202 [FLUSH_ALL_CMD
] = "CLIENT_ERROR: Syntax error: flush_all [timeout] [noreply]\r\n",
203 [VERSION_CMD
] = "CLIENT_ERROR: Syntax error: version\r\n",
204 [QUIT_CMD
] = "CLIENT_ERROR: Syntax error: quit\r\n",
206 [VERBOSITY_CMD
] = "CLIENT_ERROR: Syntax error: verbosity <num>\r\n",
207 [UNKNOWN_CMD
] = "CLIENT_ERROR: Unknown command\r\n",
210 client
->mute
= false;
211 ascii_raw_response_handler(client
, errmsg
[client
->ascii_command
]);
215 * Callback for the VERSION responses
216 * @param cookie client identifier
217 * @param text the length of the body
218 * @param textlen the length of the body
220 static protocol_binary_response_status
221 ascii_version_response_handler(const void *cookie
, const void *text
, uint32_t textlen
) {
222 memcached_protocol_client_st
*client
= (memcached_protocol_client_st
*) cookie
;
223 ascii_raw_response_handler(client
, "VERSION ");
224 client
->root
->spool(client
, text
, textlen
);
225 ascii_raw_response_handler(client
, "\r\n");
226 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
230 * Callback for the GET/GETQ/GETK and GETKQ responses
231 * @param cookie client identifier
232 * @param key the key for the item
233 * @param keylen the length of the key
234 * @param body the length of the body
235 * @param bodylen the length of the body
236 * @param flags the flags for the item
237 * @param cas the CAS id for the item
239 static protocol_binary_response_status
240 ascii_get_response_handler(const void *cookie
, const void *key
, uint16_t keylen
, const void *body
,
241 uint32_t bodylen
, uint32_t flags
, uint64_t cas
) {
242 memcached_protocol_client_st
*client
= (void *) cookie
;
244 strcpy(buffer
, "VALUE ");
245 const char *source
= key
;
246 char *dest
= buffer
+ 6;
248 for (int x
= 0; x
< keylen
; ++x
) {
249 if (*source
!= '\0' && !isspace(*source
) && !iscntrl(*source
)) {
252 return PROTOCOL_BINARY_RESPONSE_EINVAL
; /* key constraints in ascii */
259 size_t used
= (size_t)(dest
- buffer
);
261 if (client
->ascii_command
== GETS_CMD
) {
262 snprintf(dest
, sizeof(buffer
) - used
, " %u %u %" PRIu64
"\r\n", flags
, bodylen
, cas
);
264 snprintf(dest
, sizeof(buffer
) - used
, " %u %u\r\n", flags
, bodylen
);
267 client
->root
->spool(client
, buffer
, strlen(buffer
));
268 client
->root
->spool(client
, body
, bodylen
);
269 client
->root
->spool(client
, "\r\n", 2);
271 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
275 * Callback for the STAT responses
276 * @param cookie client identifier
277 * @param key the key for the item
278 * @param keylen the length of the key
279 * @param body the length of the body
280 * @param bodylen the length of the body
282 static protocol_binary_response_status
ascii_stat_response_handler(const void *cookie
,
283 const void *key
, uint16_t keylen
,
286 memcached_protocol_client_st
*client
= (void *) cookie
;
289 ascii_raw_response_handler(client
, "STAT ");
290 client
->root
->spool(client
, key
, keylen
);
291 ascii_raw_response_handler(client
, " ");
292 client
->root
->spool(client
, body
, bodylen
);
293 ascii_raw_response_handler(client
, "\r\n");
295 ascii_raw_response_handler(client
, "END\r\n");
298 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
302 * Process a get or a gets request.
303 * @param client the client handle
304 * @param buffer the complete get(s) command
305 * @param end the last character in the command
307 static void ascii_process_gets(memcached_protocol_client_st
*client
, char *buffer
, char *end
) {
311 key
+= (client
->ascii_command
== GETS_CMD
) ? 5 : 4;
315 uint16_t nkey
= parse_ascii_key(&key
);
316 if (nkey
== 0) /* Invalid key... stop processing this line */ {
320 (void) client
->root
->callback
->interface
.v1
.get(client
, key
, nkey
, ascii_get_response_handler
);
326 send_command_usage(client
);
328 client
->root
->spool(client
, "END\r\n", 5);
333 * Try to split up the command line "asdf asdf asdf asdf\n" into an
334 * argument vector for easier parsing.
335 * @param start the first character in the command line
336 * @param end the last character in the command line ("\n")
337 * @param vec the vector to insert the pointers into
338 * @size the number of elements in the vector
339 * @return the number of tokens in the vector
341 static int ascii_tokenize_command(char *str
, char *end
, char **vec
, int size
) {
345 /* Skip leading blanks */
346 while (str
< end
&& isspace(*str
)) {
355 /* find the next non-blank field */
356 while (str
< end
&& !isspace(*str
)) {
360 /* zero-terminate it for easier parsing later on */
364 /* Is the vector full? */
374 * If we for some reasons needs to push the line back to read more
375 * data we have to reverse the tokenization. Just do the brain-dead replace
376 * of all '\0' to ' ' and set the last character to '\n'. We could have used
377 * the vector we created, but then we would have to search for all of the
378 * spaces we ignored...
379 * @param start pointer to the first character in the buffer to recover
380 * @param end pointer to the last character in the buffer to recover
382 static void recover_tokenize_command(char *start
, char *end
) {
383 while (start
< end
) {
393 * Convert the textual command into a comcode
395 static enum ascii_cmd
ascii_to_cmd(char *start
, size_t length
) {
400 } commands
[] = {{.cmd
= "get", .len
= 3, .cc
= GET_CMD
},
401 {.cmd
= "gets", .len
= 4, .cc
= GETS_CMD
},
402 {.cmd
= "set", .len
= 3, .cc
= SET_CMD
},
403 {.cmd
= "add", .len
= 3, .cc
= ADD_CMD
},
404 {.cmd
= "replace", .len
= 7, .cc
= REPLACE_CMD
},
405 {.cmd
= "cas", .len
= 3, .cc
= CAS_CMD
},
406 {.cmd
= "append", .len
= 6, .cc
= APPEND_CMD
},
407 {.cmd
= "prepend", .len
= 7, .cc
= PREPEND_CMD
},
408 {.cmd
= "delete_object", .len
= 6, .cc
= DELETE_CMD
},
409 {.cmd
= "incr", .len
= 4, .cc
= INCR_CMD
},
410 {.cmd
= "decr", .len
= 4, .cc
= DECR_CMD
},
411 {.cmd
= "stats", .len
= 5, .cc
= STATS_CMD
},
412 {.cmd
= "flush_all", .len
= 9, .cc
= FLUSH_ALL_CMD
},
413 {.cmd
= "version", .len
= 7, .cc
= VERSION_CMD
},
414 {.cmd
= "quit", .len
= 4, .cc
= QUIT_CMD
},
415 {.cmd
= "verbosity", .len
= 9, .cc
= VERBOSITY_CMD
},
416 {.cmd
= NULL
, .len
= 0, .cc
= UNKNOWN_CMD
}};
419 while (commands
[x
].len
> 0) {
420 if (length
>= commands
[x
].len
) {
421 if (strncmp(start
, commands
[x
].cmd
, commands
[x
].len
) == 0) {
423 if (length
== commands
[x
].len
|| isspace(*(start
+ commands
[x
].len
))) {
424 return commands
[x
].cc
;
435 * Perform a delete_object operation.
437 * @param client client requesting the deletion
438 * @param tokens the command as a vector
439 * @param ntokens the number of items in the vector
441 static void process_delete(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
) {
442 char *key
= tokens
[1];
445 if (ntokens
!= 2 || (nkey
= parse_ascii_key(&key
)) == 0) {
446 send_command_usage(client
);
450 if (client
->root
->callback
->interface
.v1
.delete_object
== NULL
) {
451 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
455 protocol_binary_response_status rval
=
456 client
->root
->callback
->interface
.v1
.delete_object(client
, key
, nkey
, 0);
458 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
) {
459 ascii_raw_response_handler(client
, "DELETED\r\n");
460 } else if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
) {
461 ascii_raw_response_handler(client
, "NOT_FOUND\r\n");
464 snprintf(msg
, sizeof(msg
), "SERVER_ERROR: delete_object failed %u\r\n", (uint32_t) rval
);
465 ascii_raw_response_handler(client
, msg
);
469 static void process_arithmetic(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
) {
470 char *key
= tokens
[1];
473 if (ntokens
!= 3 || (nkey
= parse_ascii_key(&key
)) == 0) {
474 send_command_usage(client
);
481 uint64_t delta
= strtoull(tokens
[2], NULL
, 10);
486 protocol_binary_response_status rval
;
487 if (client
->ascii_command
== INCR_CMD
) {
488 if (client
->root
->callback
->interface
.v1
.increment
== NULL
) {
489 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
492 rval
= client
->root
->callback
->interface
.v1
.increment(client
, key
, nkey
, delta
, 0, 0, &result
,
495 if (client
->root
->callback
->interface
.v1
.decrement
== NULL
) {
496 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
499 rval
= client
->root
->callback
->interface
.v1
.decrement(client
, key
, nkey
, delta
, 0, 0, &result
,
503 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
) {
505 snprintf(buffer
, sizeof(buffer
), "%" PRIu64
"\r\n", result
);
506 ascii_raw_response_handler(client
, buffer
);
508 ascii_raw_response_handler(client
, "NOT_FOUND\r\n");
513 * Process the stats command (with or without a key specified)
514 * @param key pointer to the first character after "stats"
515 * @param end pointer to the "\n"
517 static void process_stats(memcached_protocol_client_st
*client
, char *key
, char *end
) {
518 if (client
->root
->callback
->interface
.v1
.stat
== NULL
) {
519 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
523 while (isspace(*key
)) {
527 uint16_t nkey
= (uint16_t)(end
- key
);
528 (void) client
->root
->callback
->interface
.v1
.stat(client
, key
, nkey
, ascii_stat_response_handler
);
531 static void process_version(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
) {
534 send_command_usage(client
);
538 if (client
->root
->callback
->interface
.v1
.version
== NULL
) {
539 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
543 client
->root
->callback
->interface
.v1
.version(client
, ascii_version_response_handler
);
546 static void process_flush(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
) {
548 send_command_usage(client
);
552 if (client
->root
->callback
->interface
.v1
.flush_object
== NULL
) {
553 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
557 uint32_t timeout
= 0;
560 timeout
= (uint32_t) strtoul(tokens
[1], NULL
, 10);
566 protocol_binary_response_status rval
;
567 rval
= client
->root
->callback
->interface
.v1
.flush_object(client
, timeout
);
568 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
569 ascii_raw_response_handler(client
, "OK\r\n");
571 ascii_raw_response_handler(client
, "SERVER_ERROR: internal error\r\n");
575 * Process one of the storage commands
576 * @param client the client performing the operation
577 * @param tokens the command tokens
578 * @param ntokens the number of tokens
579 * @param start pointer to the first character in the line
580 * @param end pointer to the pointer where the last character of this
581 * command is (IN and OUT)
582 * @param length the number of bytes available
583 * @return -1 if an error occurs (and we should just terminate the connection
584 * because we are out of sync)
585 * 0 storage command completed, continue processing
586 * 1 We need more data, so just go ahead and wait for more!
588 static inline int process_storage_command(memcached_protocol_client_st
*client
, char **tokens
,
589 int ntokens
, char *start
, char **end
, ssize_t length
) {
590 (void) ntokens
; /* already checked */
591 char *key
= tokens
[1];
592 uint16_t nkey
= parse_ascii_key(&key
);
595 ascii_raw_response_handler(client
, "CLIENT_ERROR: bad key\r\n");
600 uint32_t flags
= (uint32_t) strtoul(tokens
[2], NULL
, 10);
603 ascii_raw_response_handler(client
, "CLIENT_ERROR: bad key\r\n");
607 uint32_t timeout
= (uint32_t) strtoul(tokens
[3], NULL
, 10);
610 ascii_raw_response_handler(client
, "CLIENT_ERROR: bad key\r\n");
614 unsigned long nbytes
= strtoul(tokens
[4], NULL
, 10);
617 ascii_raw_response_handler(client
, "CLIENT_ERROR: bad key\r\n");
621 /* Do we have all data? */
622 unsigned long need
= nbytes
+ (unsigned long) ((*end
- start
) + 1) + 2; /* \n\r\n */
623 if ((ssize_t
) need
> length
) {
624 /* Keep on reading */
625 recover_tokenize_command(start
, *end
);
629 void *data
= (*end
) + 1;
632 protocol_binary_response_status rval
;
633 switch (client
->ascii_command
) {
635 rval
= client
->root
->callback
->interface
.v1
.set(
636 client
, key
, (uint16_t) nkey
, data
, (uint32_t) nbytes
, flags
, timeout
, cas
, &result_cas
);
639 rval
= client
->root
->callback
->interface
.v1
.add(client
, key
, (uint16_t) nkey
, data
,
640 (uint32_t) nbytes
, flags
, timeout
, &result_cas
);
644 cas
= strtoull(tokens
[5], NULL
, 10);
647 ascii_raw_response_handler(client
, "CLIENT_ERROR: bad key\r\n");
652 rval
= client
->root
->callback
->interface
.v1
.replace(
653 client
, key
, (uint16_t) nkey
, data
, (uint32_t) nbytes
, flags
, timeout
, cas
, &result_cas
);
656 rval
= client
->root
->callback
->interface
.v1
.append(client
, key
, (uint16_t) nkey
, data
,
657 (uint32_t) nbytes
, cas
, &result_cas
);
660 rval
= client
->root
->callback
->interface
.v1
.prepend(client
, key
, (uint16_t) nkey
, data
,
661 (uint32_t) nbytes
, cas
, &result_cas
);
664 /* gcc complains if I don't put all of the enums in here.. */
677 abort(); /* impossible */
680 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
) {
681 ascii_raw_response_handler(client
, "STORED\r\n");
683 if (client
->ascii_command
== CAS_CMD
) {
684 if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
) {
685 ascii_raw_response_handler(client
, "EXISTS\r\n");
686 } else if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
) {
687 ascii_raw_response_handler(client
, "NOT_FOUND\r\n");
689 ascii_raw_response_handler(client
, "NOT_STORED\r\n");
692 ascii_raw_response_handler(client
, "NOT_STORED\r\n");
701 static int process_cas_command(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
,
702 char *start
, char **end
, ssize_t length
) {
704 send_command_usage(client
);
708 if (client
->root
->callback
->interface
.v1
.replace
== NULL
) {
709 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
713 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
716 static int process_set_command(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
,
717 char *start
, char **end
, ssize_t length
) {
719 send_command_usage(client
);
723 if (client
->root
->callback
->interface
.v1
.set
== NULL
) {
724 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
728 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
731 static int process_add_command(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
,
732 char *start
, char **end
, ssize_t length
) {
734 send_command_usage(client
);
738 if (client
->root
->callback
->interface
.v1
.add
== NULL
) {
739 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
743 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
746 static int process_replace_command(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
,
747 char *start
, char **end
, ssize_t length
) {
749 send_command_usage(client
);
753 if (client
->root
->callback
->interface
.v1
.replace
== NULL
) {
754 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
758 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
761 static int process_append_command(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
,
762 char *start
, char **end
, ssize_t length
) {
764 send_command_usage(client
);
768 if (client
->root
->callback
->interface
.v1
.append
== NULL
) {
769 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
773 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
776 static int process_prepend_command(memcached_protocol_client_st
*client
, char **tokens
, int ntokens
,
777 char *start
, char **end
, ssize_t length
) {
779 send_command_usage(client
);
783 if (client
->root
->callback
->interface
.v1
.prepend
== NULL
) {
784 ascii_raw_response_handler(client
, "SERVER_ERROR: callback not implemented\r\n");
788 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
792 * The ASCII protocol support is just one giant big hack. Instead of adding
793 * a optimal ascii support, I just convert the ASCII commands to the binary
794 * protocol and calls back into the command handlers for the binary protocol ;)
796 memcached_protocol_event_t
797 memcached_ascii_protocol_process_data(memcached_protocol_client_st
*client
, ssize_t
*length
,
799 char *ptr
= (char *) client
->root
->input_buffer
;
803 /* Do we have \n (indicating the command preamble)*/
804 char *end
= memchr(ptr
, '\n', (size_t) *length
);
807 return MEMCACHED_PROTOCOL_READ_EVENT
;
810 client
->ascii_command
= ascii_to_cmd(ptr
, (size_t)(*length
));
812 /* we got all data available, execute the callback! */
813 if (client
->root
->callback
->pre_execute
) {
814 client
->root
->callback
->pre_execute(client
, NULL
);
817 /* A multiget lists all of the keys, and I don't want to have an
818 * avector of let's say 512 pointers to tokenize all of them, so let's
819 * just handle them immediately
821 if (client
->ascii_command
== GET_CMD
|| client
->ascii_command
== GETS_CMD
) {
822 if (client
->root
->callback
->interface
.v1
.get
) {
823 ascii_process_gets(client
, ptr
, end
);
825 ascii_raw_response_handler(client
, "SERVER_ERROR: Command not implemented\n");
828 /* None of the defined commands takes 10 parameters, so lets just use
829 * that as a maximum limit.
832 int ntokens
= ascii_tokenize_command(ptr
, end
, tokens
, 10);
835 client
->mute
= strcmp(tokens
[ntokens
- 1], "noreply") == 0;
837 --ntokens
; /* processed noreply token*/
843 print_ascii_command(client
);
844 switch (client
->ascii_command
) {
846 error
= process_set_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
850 error
= process_add_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
854 error
= process_replace_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
858 error
= process_cas_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
862 error
= process_append_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
866 error
= process_prepend_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
870 process_delete(client
, tokens
, ntokens
);
873 case INCR_CMD
: /* FALLTHROUGH */
875 process_arithmetic(client
, tokens
, ntokens
);
880 send_command_usage(client
);
882 recover_tokenize_command(ptr
, end
);
883 process_stats(client
, ptr
+ 6, end
);
888 process_flush(client
, tokens
, ntokens
);
893 send_command_usage(client
);
895 process_version(client
, tokens
, ntokens
);
900 if (ntokens
!= 1 || client
->mute
) {
901 send_command_usage(client
);
903 if (client
->root
->callback
->interface
.v1
.quit
) {
904 client
->root
->callback
->interface
.v1
.quit(client
);
907 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
913 send_command_usage(client
);
915 ascii_raw_response_handler(client
, "OK\r\n");
920 send_command_usage(client
);
926 /* Should already be handled */
931 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
932 } else if (error
== 1) {
933 return MEMCACHED_PROTOCOL_READ_EVENT
;
937 if (client
->root
->callback
->post_execute
) {
938 client
->root
->callback
->post_execute(client
, NULL
);
943 *length
-= end
- ptr
;
945 } while (*length
> 0);
948 return MEMCACHED_PROTOCOL_READ_EVENT
;