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.
37 #include <libmemcachedprotocol/common.h>
45 * Try to parse a key from the string.
46 * @pointer start pointer to a pointer to the string (IN and OUT)
47 * @return length of the string of -1 if this was an illegal key (invalid
48 * characters or invalid length)
51 static uint16_t parse_ascii_key(char **start
)
55 /* Strip leading whitespaces */
63 while (*c
!= '\0' && !isspace(*c
) && !iscntrl(*c
))
70 if (len
== 0 || len
> 240 || (*c
!= '\0' && *c
!= '\r' && iscntrl(*c
)))
79 * Spool a zero-terminated string
80 * @param client destination
81 * @param text the text to spool
82 * @return status of the spool operation
84 static protocol_binary_response_status
85 spool_string(memcached_protocol_client_st
*client
, const char *text
)
87 return client
->root
->spool(client
, text
, strlen(text
));
91 * Send a "CLIENT_ERROR" message back to the client with the correct
92 * format of the command being sent
93 * @param client the client to send the message to
95 static void send_command_usage(memcached_protocol_client_st
*client
)
97 const char *errmsg
[]= {
98 [GET_CMD
]= "CLIENT_ERROR: Syntax error: get <key>*\r\n",
99 [GETS_CMD
]= "CLIENT_ERROR: Syntax error: gets <key>*\r\n",
100 [SET_CMD
]= "CLIENT_ERROR: Syntax error: set <key> <flags> <exptime> <bytes> [noreply]\r\n",
101 [ADD_CMD
]= "CLIENT_ERROR: Syntax error: add <key> <flags> <exptime> <bytes> [noreply]\r\n",
102 [REPLACE_CMD
]= "CLIENT_ERROR: Syntax error: replace <key> <flags> <exptime> <bytes> [noreply]\r\n",
103 [CAS_CMD
]= "CLIENT_ERROR: Syntax error: cas <key> <flags> <exptime> <bytes> <casid> [noreply]\r\n",
104 [APPEND_CMD
]= "CLIENT_ERROR: Syntax error: append <key> <flags> <exptime> <bytes> [noreply]\r\n",
105 [PREPEND_CMD
]= "CLIENT_ERROR: Syntax error: prepend <key> <flags> <exptime> <bytes> [noreply]\r\n",
106 [DELETE_CMD
]= "CLIENT_ERROR: Syntax error: delete <key> [noreply]\r\n",
107 [INCR_CMD
]= "CLIENT_ERROR: Syntax error: incr <key> <value> [noreply]\r\n",
108 [DECR_CMD
]= "CLIENT_ERROR: Syntax error: decr <key> <value> [noreply]\r\n",
109 [STATS_CMD
]= "CLIENT_ERROR: Syntax error: stats [key]\r\n",
110 [FLUSH_ALL_CMD
]= "CLIENT_ERROR: Syntax error: flush_all [timeout] [noreply]\r\n",
111 [VERSION_CMD
]= "CLIENT_ERROR: Syntax error: version\r\n",
112 [QUIT_CMD
]="CLIENT_ERROR: Syntax error: quit\r\n",
114 [VERBOSITY_CMD
]= "CLIENT_ERROR: Syntax error: verbosity <num>\r\n",
115 [UNKNOWN_CMD
]= "CLIENT_ERROR: Unknown command\r\n",
118 client
->mute
= false;
119 spool_string(client
, errmsg
[client
->ascii_command
]);
123 * Callback for the VERSION responses
124 * @param cookie client identifier
125 * @param text the length of the body
126 * @param textlen the length of the body
128 static protocol_binary_response_status
129 ascii_version_response_handler(const void *cookie
,
133 memcached_protocol_client_st
*client
= (memcached_protocol_client_st
*)cookie
;
134 spool_string(client
, "VERSION ");
135 client
->root
->spool(client
, text
, textlen
);
136 spool_string(client
, "\r\n");
137 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
141 * Callback for the GET/GETQ/GETK and GETKQ responses
142 * @param cookie client identifier
143 * @param key the key for the item
144 * @param keylen the length of the key
145 * @param body the length of the body
146 * @param bodylen the length of the body
147 * @param flags the flags for the item
148 * @param cas the CAS id for the item
150 static protocol_binary_response_status
151 ascii_get_response_handler(const void *cookie
,
159 memcached_protocol_client_st
*client
= (void*)cookie
;
161 strcpy(buffer
, "VALUE ");
162 const char *source
= key
;
163 char *dest
= buffer
+ 6;
165 for (int x
= 0; x
< keylen
; ++x
)
167 if (*source
!= '\0' && !isspace(*source
) && !iscntrl(*source
))
173 return PROTOCOL_BINARY_RESPONSE_EINVAL
; /* key constraints in ascii */
180 size_t used
= (size_t)(dest
- buffer
);
182 if (client
->ascii_command
== GETS_CMD
)
184 snprintf(dest
, sizeof(buffer
) - used
, " %u %u %" PRIu64
"\r\n", flags
,
189 snprintf(dest
, sizeof(buffer
) - used
, " %u %u\r\n", flags
, bodylen
);
192 client
->root
->spool(client
, buffer
, strlen(buffer
));
193 client
->root
->spool(client
, body
, bodylen
);
194 client
->root
->spool(client
, "\r\n", 2);
196 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
200 * Callback for the STAT responses
201 * @param cookie client identifier
202 * @param key the key for the item
203 * @param keylen the length of the key
204 * @param body the length of the body
205 * @param bodylen the length of the body
207 static protocol_binary_response_status
208 ascii_stat_response_handler(const void *cookie
,
215 memcached_protocol_client_st
*client
= (void*)cookie
;
219 spool_string(client
, "STAT ");
220 client
->root
->spool(client
, key
, keylen
);
221 spool_string(client
, " ");
222 client
->root
->spool(client
, body
, bodylen
);
223 spool_string(client
, "\r\n");
227 spool_string(client
, "END\r\n");
230 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
234 * Process a get or a gets request.
235 * @param client the client handle
236 * @param buffer the complete get(s) command
237 * @param end the last character in the command
239 static void ascii_process_gets(memcached_protocol_client_st
*client
,
240 char *buffer
, char *end
)
245 key
+= (client
->ascii_command
== GETS_CMD
) ? 5 : 4;
250 uint16_t nkey
= parse_ascii_key(&key
);
251 if (nkey
== 0) /* Invalid key... stop processing this line */
256 (void)client
->root
->callback
->interface
.v1
.get(client
, key
, nkey
,
257 ascii_get_response_handler
);
264 send_command_usage(client
);
267 client
->root
->spool(client
, "END\r\n", 5);
271 * Try to split up the command line "asdf asdf asdf asdf\n" into an
272 * argument vector for easier parsing.
273 * @param start the first character in the command line
274 * @param end the last character in the command line ("\n")
275 * @param vec the vector to insert the pointers into
276 * @size the number of elements in the vector
277 * @return the number of tokens in the vector
279 static int ascii_tokenize_command(char *str
, char *end
, char **vec
, int size
)
285 /* Skip leading blanks */
286 while (str
< end
&& isspace(*str
))
297 /* find the next non-blank field */
298 while (str
< end
&& !isspace(*str
))
303 /* zero-terminate it for easier parsing later on */
307 /* Is the vector full? */
318 * If we for some reasons needs to push the line back to read more
319 * data we have to reverse the tokenization. Just do the brain-dead replace
320 * of all '\0' to ' ' and set the last character to '\n'. We could have used
321 * the vector we created, but then we would have to search for all of the
322 * spaces we ignored...
323 * @param start pointer to the first character in the buffer to recover
324 * @param end pointer to the last character in the buffer to recover
326 static void recover_tokenize_command(char *start
, char *end
)
339 * Convert the textual command into a comcode
341 static enum ascii_cmd
ascii_to_cmd(char *start
, size_t length
)
348 { .cmd
= "get", .len
= 3, .cc
= GET_CMD
},
349 { .cmd
= "gets", .len
= 4, .cc
= GETS_CMD
},
350 { .cmd
= "set", .len
= 3, .cc
= SET_CMD
},
351 { .cmd
= "add", .len
= 3, .cc
= ADD_CMD
},
352 { .cmd
= "replace", .len
= 7, .cc
= REPLACE_CMD
},
353 { .cmd
= "cas", .len
= 3, .cc
= CAS_CMD
},
354 { .cmd
= "append", .len
= 6, .cc
= APPEND_CMD
},
355 { .cmd
= "prepend", .len
= 7, .cc
= PREPEND_CMD
},
356 { .cmd
= "delete", .len
= 6, .cc
= DELETE_CMD
},
357 { .cmd
= "incr", .len
= 4, .cc
= INCR_CMD
},
358 { .cmd
= "decr", .len
= 4, .cc
= DECR_CMD
},
359 { .cmd
= "stats", .len
= 5, .cc
= STATS_CMD
},
360 { .cmd
= "flush_all", .len
= 9, .cc
= FLUSH_ALL_CMD
},
361 { .cmd
= "version", .len
= 7, .cc
= VERSION_CMD
},
362 { .cmd
= "quit", .len
= 4, .cc
= QUIT_CMD
},
363 { .cmd
= "verbosity", .len
= 9, .cc
= VERBOSITY_CMD
},
364 { .cmd
= NULL
, .len
= 0, .cc
= UNKNOWN_CMD
}};
367 while (commands
[x
].len
> 0) {
368 if (length
>= commands
[x
].len
)
370 if (strncmp(start
, commands
[x
].cmd
, commands
[x
].len
) == 0)
373 if (length
== commands
[x
].len
|| isspace(*(start
+ commands
[x
].len
)))
375 return commands
[x
].cc
;
386 * Perform a delete operation.
388 * @param client client requesting the deletion
389 * @param tokens the command as a vector
390 * @param ntokens the number of items in the vector
392 static void process_delete(memcached_protocol_client_st
*client
,
393 char **tokens
, int ntokens
)
395 char *key
= tokens
[1];
398 if (ntokens
!= 2 || (nkey
= parse_ascii_key(&key
)) == 0)
400 send_command_usage(client
);
404 if (client
->root
->callback
->interface
.v1
.delete == NULL
)
406 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
410 protocol_binary_response_status rval
;
411 rval
= client
->root
->callback
->interface
.v1
.delete(client
, key
, nkey
, 0);
413 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
415 spool_string(client
, "DELETED\r\n");
417 else if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
)
419 spool_string(client
, "NOT_FOUND\r\n");
424 snprintf(msg
, sizeof(msg
), "SERVER_ERROR: delete failed %u\r\n",(uint32_t)rval
);
425 spool_string(client
, msg
);
429 static void process_arithmetic(memcached_protocol_client_st
*client
,
430 char **tokens
, int ntokens
)
432 char *key
= tokens
[1];
435 if (ntokens
!= 3 || (nkey
= parse_ascii_key(&key
)) == 0)
437 send_command_usage(client
);
443 uint64_t delta
= strtoull(tokens
[2], NULL
, 10);
445 protocol_binary_response_status rval
;
446 if (client
->ascii_command
== INCR_CMD
)
448 if (client
->root
->callback
->interface
.v1
.increment
== NULL
)
450 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
453 rval
= client
->root
->callback
->interface
.v1
.increment(client
,
462 if (client
->root
->callback
->interface
.v1
.decrement
== NULL
)
464 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
467 rval
= client
->root
->callback
->interface
.v1
.decrement(client
,
475 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
478 snprintf(buffer
, sizeof(buffer
), "%"PRIu64
"\r\n", result
);
479 spool_string(client
, buffer
);
483 spool_string(client
, "NOT_FOUND\r\n");
488 * Process the stats command (with or without a key specified)
489 * @param key pointer to the first character after "stats"
490 * @param end pointer to the "\n"
492 static void process_stats(memcached_protocol_client_st
*client
,
493 char *key
, char *end
)
495 if (client
->root
->callback
->interface
.v1
.stat
== NULL
)
497 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
501 while (isspace(*key
))
504 uint16_t nkey
= (uint16_t)(end
- key
);
505 (void)client
->root
->callback
->interface
.v1
.stat(client
, key
, nkey
,
506 ascii_stat_response_handler
);
509 static void process_version(memcached_protocol_client_st
*client
,
510 char **tokens
, int ntokens
)
515 send_command_usage(client
);
519 if (client
->root
->callback
->interface
.v1
.version
== NULL
)
521 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
525 client
->root
->callback
->interface
.v1
.version(client
,
526 ascii_version_response_handler
);
529 static void process_flush(memcached_protocol_client_st
*client
,
530 char **tokens
, int ntokens
)
534 send_command_usage(client
);
538 if (client
->root
->callback
->interface
.v1
.flush
== NULL
)
540 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
547 timeout
= (uint32_t)strtoul(tokens
[1], NULL
, 10);
550 protocol_binary_response_status rval
;
551 rval
= client
->root
->callback
->interface
.v1
.flush(client
, timeout
);
552 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
553 spool_string(client
, "OK\r\n");
555 spool_string(client
, "SERVER_ERROR: internal error\r\n");
559 * Process one of the storage commands
560 * @param client the client performing the operation
561 * @param tokens the command tokens
562 * @param ntokens the number of tokens
563 * @param start pointer to the first character in the line
564 * @param end pointer to the pointer where the last character of this
565 * command is (IN and OUT)
566 * @param length the number of bytes available
567 * @return -1 if an error occurs (and we should just terminate the connection
568 * because we are out of sync)
569 * 0 storage command completed, continue processing
570 * 1 We need more data, so just go ahead and wait for more!
572 static inline int process_storage_command(memcached_protocol_client_st
*client
,
573 char **tokens
, int ntokens
, char *start
,
574 char **end
, ssize_t length
)
576 (void)ntokens
; /* already checked */
577 char *key
= tokens
[1];
578 uint16_t nkey
= parse_ascii_key(&key
);
582 spool_string(client
, "CLIENT_ERROR: bad key\r\n");
586 uint32_t flags
= (uint32_t)strtoul(tokens
[2], NULL
, 10);
587 uint32_t timeout
= (uint32_t)strtoul(tokens
[3], NULL
, 10);
588 unsigned long nbytes
= strtoul(tokens
[4], NULL
, 10);
590 /* Do we have all data? */
591 unsigned long need
= nbytes
+ (unsigned long)((*end
- start
) + 1) + 2; /* \n\r\n */
592 if ((ssize_t
)need
> length
)
594 /* Keep on reading */
595 recover_tokenize_command(start
, *end
);
599 void *data
= (*end
) + 1;
602 protocol_binary_response_status rval
;
603 switch (client
->ascii_command
)
606 rval
= client
->root
->callback
->interface
.v1
.set(client
, key
,
615 rval
= client
->root
->callback
->interface
.v1
.add(client
, key
,
620 timeout
, &result_cas
);
623 cas
= strtoull(tokens
[5], NULL
, 10);
626 rval
= client
->root
->callback
->interface
.v1
.replace(client
, key
,
635 rval
= client
->root
->callback
->interface
.v1
.append(client
, key
,
643 rval
= client
->root
->callback
->interface
.v1
.prepend(client
, key
,
651 /* gcc complains if I don't put all of the enums in here.. */
664 abort(); /* impossible */
667 if (rval
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
669 spool_string(client
, "STORED\r\n");
673 if (client
->ascii_command
== CAS_CMD
)
675 if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
)
677 spool_string(client
, "EXISTS\r\n");
679 else if (rval
== PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
)
681 spool_string(client
, "NOT_FOUND\r\n");
685 spool_string(client
, "NOT_STORED\r\n");
690 spool_string(client
, "NOT_STORED\r\n");
699 static int process_cas_command(memcached_protocol_client_st
*client
,
700 char **tokens
, int ntokens
, char *start
,
701 char **end
, ssize_t length
)
705 send_command_usage(client
);
709 if (client
->root
->callback
->interface
.v1
.replace
== NULL
)
711 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
715 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
718 static int process_set_command(memcached_protocol_client_st
*client
,
719 char **tokens
, int ntokens
, char *start
,
720 char **end
, ssize_t length
)
724 send_command_usage(client
);
728 if (client
->root
->callback
->interface
.v1
.set
== NULL
)
730 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
734 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
737 static int process_add_command(memcached_protocol_client_st
*client
,
738 char **tokens
, int ntokens
, char *start
,
739 char **end
, ssize_t length
)
743 send_command_usage(client
);
747 if (client
->root
->callback
->interface
.v1
.add
== NULL
)
749 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
753 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
756 static int process_replace_command(memcached_protocol_client_st
*client
,
757 char **tokens
, int ntokens
, char *start
,
758 char **end
, ssize_t length
)
762 send_command_usage(client
);
766 if (client
->root
->callback
->interface
.v1
.replace
== NULL
)
768 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
772 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
775 static int process_append_command(memcached_protocol_client_st
*client
,
776 char **tokens
, int ntokens
, char *start
,
777 char **end
, ssize_t length
)
781 send_command_usage(client
);
785 if (client
->root
->callback
->interface
.v1
.append
== NULL
)
787 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
791 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
794 static int process_prepend_command(memcached_protocol_client_st
*client
,
795 char **tokens
, int ntokens
, char *start
,
796 char **end
, ssize_t length
)
800 send_command_usage(client
);
804 if (client
->root
->callback
->interface
.v1
.prepend
== NULL
)
806 spool_string(client
, "SERVER_ERROR: callback not implemented\r\n");
810 return process_storage_command(client
, tokens
, ntokens
, start
, end
, length
);
814 * The ASCII protocol support is just one giant big hack. Instead of adding
815 * a optimal ascii support, I just convert the ASCII commands to the binary
816 * protocol and calls back into the command handlers for the binary protocol ;)
818 memcached_protocol_event_t
memcached_ascii_protocol_process_data(memcached_protocol_client_st
*client
, ssize_t
*length
, void **endptr
)
820 char *ptr
= (char*)client
->root
->input_buffer
;
824 /* Do we have \n (indicating the command preamble)*/
825 char *end
= memchr(ptr
, '\n', (size_t)*length
);
829 return MEMCACHED_PROTOCOL_READ_EVENT
;
832 client
->ascii_command
= ascii_to_cmd(ptr
, (size_t)(*length
));
834 /* A multiget lists all of the keys, and I don't want to have an
835 * avector of let's say 512 pointers to tokenize all of them, so let's
836 * just handle them immediately
838 if (client
->ascii_command
== GET_CMD
||
839 client
->ascii_command
== GETS_CMD
) {
840 if (client
->root
->callback
->interface
.v1
.get
!= NULL
)
841 ascii_process_gets(client
, ptr
, end
);
843 spool_string(client
, "SERVER_ERROR: Command not implemented\n");
845 /* None of the defined commands takes 10 parameters, so lets just use
846 * that as a maximum limit.
849 int ntokens
= ascii_tokenize_command(ptr
, end
, tokens
, 10);
853 client
->mute
= strcmp(tokens
[ntokens
- 1], "noreply") == 0;
855 --ntokens
; /* processed noreply token*/
860 switch (client
->ascii_command
) {
862 error
= process_set_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
865 error
= process_add_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
868 error
= process_replace_command(client
, tokens
, ntokens
,
872 error
= process_cas_command(client
, tokens
, ntokens
, ptr
, &end
, *length
);
875 error
= process_append_command(client
, tokens
, ntokens
,
879 error
= process_prepend_command(client
, tokens
, ntokens
,
883 process_delete(client
, tokens
, ntokens
);
886 case INCR_CMD
: /* FALLTHROUGH */
888 process_arithmetic(client
, tokens
, ntokens
);
893 send_command_usage(client
);
897 recover_tokenize_command(ptr
, end
);
898 process_stats(client
, ptr
+ 6, end
);
902 process_flush(client
, tokens
, ntokens
);
907 send_command_usage(client
);
911 process_version(client
, tokens
, ntokens
);
915 if (ntokens
!= 1 || client
->mute
)
917 send_command_usage(client
);
921 if (client
->root
->callback
->interface
.v1
.quit
!= NULL
)
922 client
->root
->callback
->interface
.v1
.quit(client
);
924 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
930 send_command_usage(client
);
932 spool_string(client
, "OK\r\n");
936 send_command_usage(client
);
942 /* Should already be handled */
947 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
949 return MEMCACHED_PROTOCOL_READ_EVENT
;
954 *length
-= end
- ptr
;
956 } while (*length
> 0);
959 return MEMCACHED_PROTOCOL_READ_EVENT
;