1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <libmemcached/protocol/common.h>
40 #include <libmemcached/byteorder.h>
48 * Try to parse a key from the string.
49 * @pointer start pointer to a pointer to the string (IN and OUT)
50 * @return length of the string of -1 if this was an illegal key (invalid
51 * characters or invalid length)
54 static uint16_t parse_ascii_key(char **start
)
58 /* Strip leading whitespaces */
66 while (*c
!= '\0' && !isspace(*c
) && !iscntrl(*c
))
73 if (len
== 0 || len
> 240 || (*c
!= '\0' && *c
!= '\r' && iscntrl(*c
)))
82 * Spool a zero-terminated string
83 * @param client destination
84 * @param text the text to spool
85 * @return status of the spool operation
87 static protocol_binary_response_status
88 spool_string(memcached_protocol_client_st
*client
, const char *text
)
90 return client
->root
->spool(client
, text
, strlen(text
));
94 * Send a "CLIENT_ERROR" message back to the client with the correct
95 * format of the command being sent
96 * @param client the client to send the message to
98 static void send_command_usage(memcached_protocol_client_st
*client
)
100 const char *errmsg
[]= {
101 [GET_CMD
]= "CLIENT_ERROR: Syntax error: get <key>*\r\n",
102 [GETS_CMD
]= "CLIENT_ERROR: Syntax error: gets <key>*\r\n",
103 [SET_CMD
]= "CLIENT_ERROR: Syntax error: set <key> <flags> <exptime> <bytes> [noreply]\r\n",
104 [ADD_CMD
]= "CLIENT_ERROR: Syntax error: add <key> <flags> <exptime> <bytes> [noreply]\r\n",
105 [REPLACE_CMD
]= "CLIENT_ERROR: Syntax error: replace <key> <flags> <exptime> <bytes> [noreply]\r\n",
106 [CAS_CMD
]= "CLIENT_ERROR: Syntax error: cas <key> <flags> <exptime> <bytes> <casid> [noreply]\r\n",
107 [APPEND_CMD
]= "CLIENT_ERROR: Syntax error: append <key> <flags> <exptime> <bytes> [noreply]\r\n",
108 [PREPEND_CMD
]= "CLIENT_ERROR: Syntax error: prepend <key> <flags> <exptime> <bytes> [noreply]\r\n",
109 [DELETE_CMD
]= "CLIENT_ERROR: Syntax error: delete <key> [noreply]\r\n",
110 [INCR_CMD
]= "CLIENT_ERROR: Syntax error: incr <key> <value> [noreply]\r\n",
111 [DECR_CMD
]= "CLIENT_ERROR: Syntax error: decr <key> <value> [noreply]\r\n",
112 [STATS_CMD
]= "CLIENT_ERROR: Syntax error: stats [key]\r\n",
113 [FLUSH_ALL_CMD
]= "CLIENT_ERROR: Syntax error: flush_all [timeout] [noreply]\r\n",
114 [VERSION_CMD
]= "CLIENT_ERROR: Syntax error: version\r\n",
115 [QUIT_CMD
]="CLIENT_ERROR: Syntax error: quit\r\n",
117 [VERBOSITY_CMD
]= "CLIENT_ERROR: Syntax error: verbosity <num>\r\n",
118 [UNKNOWN_CMD
]= "CLIENT_ERROR: Unknown command\r\n",
121 client
->mute
= false;
122 spool_string(client
, errmsg
[client
->ascii_command
]);
126 * Callback for the VERSION responses
127 * @param cookie client identifier
128 * @param text the length of the body
129 * @param textlen the length of the body
131 static protocol_binary_response_status
132 ascii_version_response_handler(const void *cookie
,
136 memcached_protocol_client_st
*client
= (memcached_protocol_client_st
*)cookie
;
137 spool_string(client
, "VERSION ");
138 client
->root
->spool(client
, text
, textlen
);
139 spool_string(client
, "\r\n");
140 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
144 * Callback for the GET/GETQ/GETK and GETKQ responses
145 * @param cookie client identifier
146 * @param key the key for the item
147 * @param keylen the length of the key
148 * @param body the length of the body
149 * @param bodylen the length of the body
150 * @param flags the flags for the item
151 * @param cas the CAS id for the item
153 static protocol_binary_response_status
154 ascii_get_response_handler(const void *cookie
,
162 memcached_protocol_client_st
*client
= (void*)cookie
;
164 strcpy(buffer
, "VALUE ");
165 const char *source
= key
;
166 char *dest
= buffer
+ 6;
168 for (int x
= 0; x
< keylen
; ++x
)
170 if (*source
!= '\0' && !isspace(*source
) && !iscntrl(*source
))
176 return PROTOCOL_BINARY_RESPONSE_EINVAL
; /* key constraints in ascii */
183 size_t used
= (size_t)(dest
- buffer
);
185 if (client
->ascii_command
== GETS_CMD
)
187 snprintf(dest
, sizeof(buffer
) - used
, " %u %u %" PRIu64
"\r\n", flags
,
192 snprintf(dest
, sizeof(buffer
) - used
, " %u %u\r\n", flags
, bodylen
);
195 client
->root
->spool(client
, buffer
, strlen(buffer
));
196 client
->root
->spool(client
, body
, bodylen
);
197 client
->root
->spool(client
, "\r\n", 2);
199 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
203 * Callback for the STAT responses
204 * @param cookie client identifier
205 * @param key the key for the item
206 * @param keylen the length of the key
207 * @param body the length of the body
208 * @param bodylen the length of the body
210 static protocol_binary_response_status
211 ascii_stat_response_handler(const void *cookie
,
218 memcached_protocol_client_st
*client
= (void*)cookie
;
222 spool_string(client
, "STAT ");
223 client
->root
->spool(client
, key
, keylen
);
224 spool_string(client
, " ");
225 client
->root
->spool(client
, body
, bodylen
);
226 spool_string(client
, "\r\n");
230 spool_string(client
, "END\r\n");
233 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
237 * Process a get or a gets request.
238 * @param client the client handle
239 * @param buffer the complete get(s) command
240 * @param end the last character in the command
242 static void ascii_process_gets(memcached_protocol_client_st
*client
,
243 char *buffer
, char *end
)
248 key
+= (client
->ascii_command
== GETS_CMD
) ? 5 : 4;
253 uint16_t nkey
= parse_ascii_key(&key
);
254 if (nkey
== 0) /* Invalid key... stop processing this line */
259 (void)client
->root
->callback
->interface
.v1
.get(client
, key
, nkey
,
260 ascii_get_response_handler
);
267 send_command_usage(client
);
270 client
->root
->spool(client
, "END\r\n", 5);
274 * Try to split up the command line "asdf asdf asdf asdf\n" into an
275 * argument vector for easier parsing.
276 * @param start the first character in the command line
277 * @param end the last character in the command line ("\n")
278 * @param vec the vector to insert the pointers into
279 * @size the number of elements in the vector
280 * @return the number of tokens in the vector
282 static int ascii_tokenize_command(char *str
, char *end
, char **vec
, int size
)
288 /* Skip leading blanks */
289 while (str
< end
&& isspace(*str
))
300 /* find the next non-blank field */
301 while (str
< end
&& !isspace(*str
))
306 /* zero-terminate it for easier parsing later on */
310 /* Is the vector full? */
321 * If we for some reasons needs to push the line back to read more
322 * data we have to reverse the tokenization. Just do the brain-dead replace
323 * of all '\0' to ' ' and set the last character to '\n'. We could have used
324 * the vector we created, but then we would have to search for all of the
325 * spaces we ignored...
326 * @param start pointer to the first character in the buffer to recover
327 * @param end pointer to the last character in the buffer to recover
329 static void recover_tokenize_command(char *start
, char *end
)
342 * Convert the textual command into a comcode
344 static enum ascii_cmd
ascii_to_cmd(char *start
, size_t length
)
351 { .cmd
= "get", .len
= 3, .cc
= GET_CMD
},
352 { .cmd
= "gets", .len
= 4, .cc
= GETS_CMD
},
353 { .cmd
= "set", .len
= 3, .cc
= SET_CMD
},
354 { .cmd
= "add", .len
= 3, .cc
= ADD_CMD
},
355 { .cmd
= "replace", .len
= 7, .cc
= REPLACE_CMD
},
356 { .cmd
= "cas", .len
= 3, .cc
= CAS_CMD
},
357 { .cmd
= "append", .len
= 6, .cc
= APPEND_CMD
},
358 { .cmd
= "prepend", .len
= 7, .cc
= PREPEND_CMD
},
359 { .cmd
= "delete", .len
= 6, .cc
= DELETE_CMD
},
360 { .cmd
= "incr", .len
= 4, .cc
= INCR_CMD
},
361 { .cmd
= "decr", .len
= 4, .cc
= DECR_CMD
},
362 { .cmd
= "stats", .len
= 5, .cc
= STATS_CMD
},
363 { .cmd
= "flush_all", .len
= 9, .cc
= FLUSH_ALL_CMD
},
364 { .cmd
= "version", .len
= 7, .cc
= VERSION_CMD
},
365 { .cmd
= "quit", .len
= 4, .cc
= QUIT_CMD
},
366 { .cmd
= "verbosity", .len
= 9, .cc
= VERBOSITY_CMD
},
367 { .cmd
= NULL
, .len
= 0, .cc
= UNKNOWN_CMD
}};
370 while (commands
[x
].len
> 0) {
371 if (length
>= commands
[x
].len
)
373 if (strncmp(start
, commands
[x
].cmd
, commands
[x
].len
) == 0)
376 if (length
== commands
[x
].len
|| isspace(*(start
+ commands
[x
].len
)))
378 return commands
[x
].cc
;
389 * Perform a delete operation.
391 * @param client client requesting the deletion
392 * @param tokens the command as a vector
393 * @param ntokens the number of items in the vector
395 static void process_delete(memcached_protocol_client_st
*client
,
396 char **tokens
, int ntokens
)
398 char *key
= tokens
[1];
401 if (ntokens
!= 2 || (nkey
= parse_ascii_key(&key
)) == 0)
403 send_command_usage(client
);
407 if (client
->root
->callback
->interface
.v1
.delete == NULL
)
409 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
413 protocol_binary_response_status rval
;
414 rval
= client
->root
->callback
->interface
.v1
.delete(client
, key
, nkey
, 0);
416 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
418 spool_string(client
, "DELETED\r\n");
420 else if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
)
422 spool_string(client
, "NOT_FOUND\r\n");
427 snprintf(msg
, sizeof(msg
), "SERVER_ERROR: delete failed %u\r\n",(uint32_t)rval
);
428 spool_string(client
, msg
);
432 static void process_arithmetic(memcached_protocol_client_st
*client
,
433 char **tokens
, int ntokens
)
435 char *key
= tokens
[1];
438 if (ntokens
!= 3 || (nkey
= parse_ascii_key(&key
)) == 0)
440 send_command_usage(client
);
446 uint64_t delta
= strtoull(tokens
[2], NULL
, 10);
448 protocol_binary_response_status rval
;
449 if (client
->ascii_command
== INCR_CMD
)
451 if (client
->root
->callback
->interface
.v1
.increment
== NULL
)
453 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
456 rval
= client
->root
->callback
->interface
.v1
.increment(client
,
465 if (client
->root
->callback
->interface
.v1
.decrement
== NULL
)
467 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
470 rval
= client
->root
->callback
->interface
.v1
.decrement(client
,
478 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
481 snprintf(buffer
, sizeof(buffer
), "%"PRIu64
"\r\n", result
);
482 spool_string(client
, buffer
);
486 spool_string(client
, "NOT_FOUND\r\n");
491 * Process the stats command (with or without a key specified)
492 * @param key pointer to the first character after "stats"
493 * @param end pointer to the "\n"
495 static void process_stats(memcached_protocol_client_st
*client
,
496 char *key
, char *end
)
498 if (client
->root
->callback
->interface
.v1
.stat
== NULL
)
500 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
504 while (isspace(*key
))
507 uint16_t nkey
= (uint16_t)(end
- key
);
508 (void)client
->root
->callback
->interface
.v1
.stat(client
, key
, nkey
,
509 ascii_stat_response_handler
);
512 static void process_version(memcached_protocol_client_st
*client
,
513 char **tokens
, int ntokens
)
518 send_command_usage(client
);
522 if (client
->root
->callback
->interface
.v1
.version
== NULL
)
524 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
528 client
->root
->callback
->interface
.v1
.version(client
,
529 ascii_version_response_handler
);
532 static void process_flush(memcached_protocol_client_st
*client
,
533 char **tokens
, int ntokens
)
537 send_command_usage(client
);
541 if (client
->root
->callback
->interface
.v1
.flush
== NULL
)
543 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
550 timeout
= (uint32_t)strtoul(tokens
[1], NULL
, 10);
553 protocol_binary_response_status rval
;
554 rval
= client
->root
->callback
->interface
.v1
.flush(client
, timeout
);
555 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
556 spool_string(client
, "OK\r\n");
558 spool_string(client
, "SERVER_ERROR: internal error\r\n");
562 * Process one of the storage commands
563 * @param client the client performing the operation
564 * @param tokens the command tokens
565 * @param ntokens the number of tokens
566 * @param start pointer to the first character in the line
567 * @param end pointer to the pointer where the last character of this
568 * command is (IN and OUT)
569 * @param length the number of bytes available
570 * @return -1 if an error occurs (and we should just terminate the connection
571 * because we are out of sync)
572 * 0 storage command completed, continue processing
573 * 1 We need more data, so just go ahead and wait for more!
575 static inline int process_storage_command(memcached_protocol_client_st
*client
,
576 char **tokens
, int ntokens
, char *start
,
577 char **end
, ssize_t length
)
579 (void)ntokens
; /* already checked */
580 char *key
= tokens
[1];
581 uint16_t nkey
= parse_ascii_key(&key
);
585 spool_string(client
, "CLIENT_ERROR: bad key\r\n");
589 uint32_t flags
= (uint32_t)strtoul(tokens
[2], NULL
, 10);
590 uint32_t timeout
= (uint32_t)strtoul(tokens
[3], NULL
, 10);
591 unsigned long nbytes
= strtoul(tokens
[4], NULL
, 10);
593 /* Do we have all data? */
594 unsigned long need
= nbytes
+ (unsigned long)((*end
- start
) + 1) + 2; /* \n\r\n */
595 if ((ssize_t
)need
> length
)
597 /* Keep on reading */
598 recover_tokenize_command(start
, *end
);
602 void *data
= (*end
) + 1;
605 protocol_binary_response_status rval
;
606 switch (client
->ascii_command
)
609 rval
= client
->root
->callback
->interface
.v1
.set(client
, key
,
618 rval
= client
->root
->callback
->interface
.v1
.add(client
, key
,
623 timeout
, &result_cas
);
626 cas
= strtoull(tokens
[5], NULL
, 10);
629 rval
= client
->root
->callback
->interface
.v1
.replace(client
, key
,
638 rval
= client
->root
->callback
->interface
.v1
.append(client
, key
,
646 rval
= client
->root
->callback
->interface
.v1
.prepend(client
, key
,
654 /* gcc complains if I don't put all of the enums in here.. */
667 abort(); /* impossible */
670 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
672 spool_string(client
, "STORED\r\n");
676 if (client
->ascii_command
== CAS_CMD
)
678 if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
)
680 spool_string(client
, "EXISTS\r\n");
682 else if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
)
684 spool_string(client
, "NOT_FOUND\r\n");
688 spool_string(client
, "NOT_STORED\r\n");
693 spool_string(client
, "NOT_STORED\r\n");
702 static int process_cas_command(memcached_protocol_client_st
*client
,
703 char **tokens
, int ntokens
, char *start
,
704 char **end
, ssize_t length
)
708 send_command_usage(client
);
712 if (client
->root
->callback
->interface
.v1
.replace
== NULL
)
714 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
718 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
721 static int process_set_command(memcached_protocol_client_st
*client
,
722 char **tokens
, int ntokens
, char *start
,
723 char **end
, ssize_t length
)
727 send_command_usage(client
);
731 if (client
->root
->callback
->interface
.v1
.set
== NULL
)
733 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
737 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
740 static int process_add_command(memcached_protocol_client_st
*client
,
741 char **tokens
, int ntokens
, char *start
,
742 char **end
, ssize_t length
)
746 send_command_usage(client
);
750 if (client
->root
->callback
->interface
.v1
.add
== NULL
)
752 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
756 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
759 static int process_replace_command(memcached_protocol_client_st
*client
,
760 char **tokens
, int ntokens
, char *start
,
761 char **end
, ssize_t length
)
765 send_command_usage(client
);
769 if (client
->root
->callback
->interface
.v1
.replace
== NULL
)
771 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
775 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
778 static int process_append_command(memcached_protocol_client_st
*client
,
779 char **tokens
, int ntokens
, char *start
,
780 char **end
, ssize_t length
)
784 send_command_usage(client
);
788 if (client
->root
->callback
->interface
.v1
.append
== NULL
)
790 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
794 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
797 static int process_prepend_command(memcached_protocol_client_st
*client
,
798 char **tokens
, int ntokens
, char *start
,
799 char **end
, ssize_t length
)
803 send_command_usage(client
);
807 if (client
->root
->callback
->interface
.v1
.prepend
== NULL
)
809 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
813 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
817 * The ASCII protocol support is just one giant big hack. Instead of adding
818 * a optimal ascii support, I just convert the ASCII commands to the binary
819 * protocol and calls back into the command handlers for the binary protocol ;)
821 memcached_protocol_event_t
memcached_ascii_protocol_process_data(memcached_protocol_client_st
*client
, ssize_t
*length
, void **endptr
)
823 char *ptr
= (char*)client
->root
->input_buffer
;
827 /* Do we have \n (indicating the command preamble)*/
828 char *end
= memchr(ptr
, '\n', (size_t)*length
);
832 return MEMCACHED_PROTOCOL_READ_EVENT
;
835 client
->ascii_command
= ascii_to_cmd(ptr
, (size_t)(*length
));
837 /* A multiget lists all of the keys, and I don't want to have an
838 * avector of let's say 512 pointers to tokenize all of them, so let's
839 * just handle them immediately
841 if (client
->ascii_command
== GET_CMD
||
842 client
->ascii_command
== GETS_CMD
) {
843 if (client
->root
->callback
->interface
.v1
.get
!= NULL
)
844 ascii_process_gets(client
, ptr
, end
);
846 spool_string(client
, "SERVER_ERROR: Command not implemented\n");
848 /* None of the defined commands takes 10 parameters, so lets just use
849 * that as a maximum limit.
852 int ntokens
= ascii_tokenize_command(ptr
, end
, tokens
, 10);
856 client
->mute
= strcmp(tokens
[ntokens
- 1], "noreply") == 0;
858 --ntokens
; /* processed noreply token*/
863 switch (client
->ascii_command
) {
865 error
= process_set_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
868 error
= process_add_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
871 error
= process_replace_command(client
, tokens
, ntokens
,
875 error
= process_cas_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
878 error
= process_append_command(client
, tokens
, ntokens
,
882 error
= process_prepend_command(client
, tokens
, ntokens
,
886 process_delete(client
, tokens
, ntokens
);
889 case INCR_CMD
: /* FALLTHROUGH */
891 process_arithmetic(client
, tokens
, ntokens
);
896 send_command_usage(client
);
900 recover_tokenize_command(ptr
, end
);
901 process_stats(client
, ptr
+ 6, end
);
905 process_flush(client
, tokens
, ntokens
);
910 send_command_usage(client
);
914 process_version(client
, tokens
, ntokens
);
918 if (ntokens
!= 1 || client
->mute
)
920 send_command_usage(client
);
924 if (client
->root
->callback
->interface
.v1
.quit
!= NULL
)
925 client
->root
->callback
->interface
.v1
.quit(client
);
927 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
933 send_command_usage(client
);
935 spool_string(client
, "OK\r\n");
939 send_command_usage(client
);
945 /* Should already be handled */
950 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
952 return MEMCACHED_PROTOCOL_READ_EVENT
;
957 *length
-= end
- ptr
;
959 } while (*length
> 0);
962 return MEMCACHED_PROTOCOL_READ_EVENT
;