2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: memcached_response() is used to determine the return result from an issued command.
14 static memcached_return_t
textual_read_one_response(memcached_server_instance_st
*ptr
,
15 char *buffer
, size_t buffer_length
,
16 memcached_result_st
*result
);
17 static memcached_return_t
binary_read_one_response(memcached_server_instance_st
*ptr
,
18 char *buffer
, size_t buffer_length
,
19 memcached_result_st
*result
);
21 memcached_return_t
memcached_read_one_response(memcached_server_instance_st
*ptr
,
22 char *buffer
, size_t buffer_length
,
23 memcached_result_st
*result
)
25 memcached_server_response_decrement(ptr
);
28 result
= &ptr
->root
->result
;
30 memcached_return_t rc
;
31 if (ptr
->root
->flags
.binary_protocol
)
32 rc
= binary_read_one_response(ptr
, buffer
, buffer_length
, result
);
34 rc
= textual_read_one_response(ptr
, buffer
, buffer_length
, result
);
36 unlikely(rc
== MEMCACHED_UNKNOWN_READ_FAILURE
||
37 rc
== MEMCACHED_PROTOCOL_ERROR
||
38 rc
== MEMCACHED_CLIENT_ERROR
||
39 rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
40 memcached_io_reset(ptr
);
45 memcached_return_t
memcached_response(memcached_server_instance_st
*ptr
,
46 char *buffer
, size_t buffer_length
,
47 memcached_result_st
*result
)
49 /* We may have old commands in the buffer not set, first purge */
50 if (ptr
->root
->flags
.no_block
)
51 (void)memcached_io_write(ptr
, NULL
, 0, 1);
54 * The previous implementation purged all pending requests and just
55 * returned the last one. Purge all pending messages to ensure backwards
58 if (ptr
->root
->flags
.binary_protocol
== false)
59 while (memcached_server_response_count(ptr
) > 1)
61 memcached_return_t rc
= memcached_read_one_response(ptr
, buffer
, buffer_length
, result
);
63 unlikely (rc
!= MEMCACHED_END
&&
64 rc
!= MEMCACHED_STORED
&&
65 rc
!= MEMCACHED_SUCCESS
&&
66 rc
!= MEMCACHED_STAT
&&
67 rc
!= MEMCACHED_DELETED
&&
68 rc
!= MEMCACHED_NOTFOUND
&&
69 rc
!= MEMCACHED_NOTSTORED
&&
70 rc
!= MEMCACHED_DATA_EXISTS
)
74 return memcached_read_one_response(ptr
, buffer
, buffer_length
, result
);
77 static memcached_return_t
textual_value_fetch(memcached_server_instance_st
*ptr
,
79 memcached_result_st
*result
)
81 memcached_return_t rc
= MEMCACHED_SUCCESS
;
88 ssize_t read_length
= 0;
89 memcached_return_t rrc
;
91 if (ptr
->root
->flags
.use_udp
)
92 return MEMCACHED_NOT_SUPPORTED
;
94 WATCHPOINT_ASSERT(ptr
->root
);
95 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
97 memcached_result_reset(result
);
100 string_ptr
+= 6; /* "VALUE " */
103 /* We load the key */
106 size_t prefix_length
;
109 result
->key_length
= 0;
111 for (prefix_length
= ptr
->root
->prefix_key_length
; !(iscntrl(*string_ptr
) || isspace(*string_ptr
)) ; string_ptr
++)
113 if (prefix_length
== 0)
117 result
->key_length
++;
122 result
->key
[result
->key_length
]= 0;
125 if (end_ptr
== string_ptr
)
128 /* Flags fetch move past space */
130 if (end_ptr
== string_ptr
)
132 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
133 result
->flags
= (uint32_t) strtoul(next_ptr
, &string_ptr
, 10);
135 if (end_ptr
== string_ptr
)
138 /* Length fetch move past space*/
140 if (end_ptr
== string_ptr
)
143 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
144 value_length
= (size_t)strtoull(next_ptr
, &string_ptr
, 10);
146 if (end_ptr
== string_ptr
)
150 if (*string_ptr
== '\r')
152 /* Skip past the \r\n */
158 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
159 result
->cas
= strtoull(next_ptr
, &string_ptr
, 10);
162 if (end_ptr
< string_ptr
)
165 /* We add two bytes so that we can walk the \r\n */
166 rc
= memcached_string_check(&result
->value
, value_length
+2);
167 if (rc
!= MEMCACHED_SUCCESS
)
170 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
173 value_ptr
= memcached_string_value(&result
->value
);
175 We read the \r\n into the string since not doing so is more
176 cycles then the waster of memory to do so.
178 We are null terminating through, which will most likely make
179 some people lazy about using the return length.
181 to_read
= (value_length
) + 2;
182 rrc
= memcached_io_read(ptr
, value_ptr
, to_read
, &read_length
);
183 if (rrc
!= MEMCACHED_SUCCESS
)
186 if (read_length
!= (ssize_t
)(value_length
+ 2))
191 /* This next bit blows the API, but this is internal....*/
194 char_ptr
= memcached_string_value(&result
->value
);;
195 char_ptr
[value_length
]= 0;
196 char_ptr
[value_length
+ 1]= 0;
197 memcached_string_set_length(&result
->value
, value_length
);
200 return MEMCACHED_SUCCESS
;
203 memcached_io_reset(ptr
);
205 return MEMCACHED_PARTIAL_READ
;
208 static memcached_return_t
textual_read_one_response(memcached_server_instance_st
*ptr
,
209 char *buffer
, size_t buffer_length
,
210 memcached_result_st
*result
)
212 memcached_return_t rc
= memcached_io_readline(ptr
, buffer
, buffer_length
);
213 if (rc
!= MEMCACHED_SUCCESS
)
218 case 'V': /* VALUE || VERSION */
219 if (buffer
[1] == 'A') /* VALUE */
221 /* We add back in one because we will need to search for END */
222 memcached_server_response_increment(ptr
);
223 return textual_value_fetch(ptr
, buffer
, result
);
225 else if (buffer
[1] == 'E') /* VERSION */
227 return MEMCACHED_SUCCESS
;
231 WATCHPOINT_STRING(buffer
);
232 WATCHPOINT_ASSERT(0);
233 return MEMCACHED_UNKNOWN_READ_FAILURE
;
236 return MEMCACHED_SUCCESS
;
237 case 'S': /* STORED STATS SERVER_ERROR */
239 if (buffer
[2] == 'A') /* STORED STATS */
241 memcached_server_response_increment(ptr
);
242 return MEMCACHED_STAT
;
244 else if (buffer
[1] == 'E') /* SERVER_ERROR */
247 char *startptr
= buffer
+ 13, *endptr
= startptr
;
249 while (*endptr
!= '\r' && *endptr
!= '\n') endptr
++;
252 Yes, we could make this "efficent" but to do that we would need
253 to maintain more state for the size of the buffer. Why waste
254 memory in the struct, which is important, for something that
255 rarely should happen?
257 rel_ptr
= (char *)ptr
->root
->call_realloc(ptr
->root
,
258 ptr
->cached_server_error
,
259 (size_t) (endptr
- startptr
+ 1));
263 /* If we happened to have some memory, we just null it since we don't know the size */
264 if (ptr
->cached_server_error
)
265 ptr
->cached_server_error
[0]= 0;
266 return MEMCACHED_SERVER_ERROR
;
268 ptr
->cached_server_error
= rel_ptr
;
270 memcpy(ptr
->cached_server_error
, startptr
, (size_t) (endptr
- startptr
));
271 ptr
->cached_server_error
[endptr
- startptr
]= 0;
272 return MEMCACHED_SERVER_ERROR
;
274 else if (buffer
[1] == 'T')
275 return MEMCACHED_STORED
;
278 WATCHPOINT_STRING(buffer
);
279 WATCHPOINT_ASSERT(0);
280 return MEMCACHED_UNKNOWN_READ_FAILURE
;
283 case 'D': /* DELETED */
284 return MEMCACHED_DELETED
;
285 case 'N': /* NOT_FOUND */
287 if (buffer
[4] == 'F')
288 return MEMCACHED_NOTFOUND
;
289 else if (buffer
[4] == 'S')
290 return MEMCACHED_NOTSTORED
;
292 return MEMCACHED_UNKNOWN_READ_FAILURE
;
294 case 'E': /* PROTOCOL ERROR or END */
296 if (buffer
[1] == 'N')
297 return MEMCACHED_END
;
298 else if (buffer
[1] == 'R')
299 return MEMCACHED_PROTOCOL_ERROR
;
300 else if (buffer
[1] == 'X')
301 return MEMCACHED_DATA_EXISTS
;
303 return MEMCACHED_UNKNOWN_READ_FAILURE
;
305 case 'I': /* CLIENT ERROR */
306 /* We add back in one because we will need to search for END */
307 memcached_server_response_increment(ptr
);
308 return MEMCACHED_ITEM
;
309 case 'C': /* CLIENT ERROR */
310 return MEMCACHED_CLIENT_ERROR
;
313 unsigned long long auto_return_value
;
315 if (sscanf(buffer
, "%llu", &auto_return_value
) == 1)
316 return MEMCACHED_SUCCESS
;
318 return MEMCACHED_UNKNOWN_READ_FAILURE
;
325 static memcached_return_t
binary_read_one_response(memcached_server_instance_st
*ptr
,
326 char *buffer
, size_t buffer_length
,
327 memcached_result_st
*result
)
329 protocol_binary_response_header header
;
331 unlikely (memcached_safe_read(ptr
, &header
.bytes
,
332 sizeof(header
.bytes
)) != MEMCACHED_SUCCESS
)
333 return MEMCACHED_UNKNOWN_READ_FAILURE
;
335 unlikely (header
.response
.magic
!= PROTOCOL_BINARY_RES
)
336 return MEMCACHED_PROTOCOL_ERROR
;
339 ** Convert the header to host local endian!
341 header
.response
.keylen
= ntohs(header
.response
.keylen
);
342 header
.response
.status
= ntohs(header
.response
.status
);
343 header
.response
.bodylen
= ntohl(header
.response
.bodylen
);
344 header
.response
.cas
= ntohll(header
.response
.cas
);
345 uint32_t bodylen
= header
.response
.bodylen
;
347 if (header
.response
.status
== 0)
349 switch (header
.response
.opcode
)
351 case PROTOCOL_BINARY_CMD_GETKQ
:
353 * We didn't increment the response counter for the GETKQ packet
354 * (only the final NOOP), so we need to increment the counter again.
356 memcached_server_response_increment(ptr
);
358 case PROTOCOL_BINARY_CMD_GETK
:
360 uint16_t keylen
= header
.response
.keylen
;
361 memcached_result_reset(result
);
362 result
->cas
= header
.response
.cas
;
364 if (memcached_safe_read(ptr
, &result
->flags
,
365 sizeof (result
->flags
)) != MEMCACHED_SUCCESS
)
366 return MEMCACHED_UNKNOWN_READ_FAILURE
;
368 result
->flags
= ntohl(result
->flags
);
369 bodylen
-= header
.response
.extlen
;
371 result
->key_length
= keylen
;
372 if (memcached_safe_read(ptr
, result
->key
, keylen
) != MEMCACHED_SUCCESS
)
373 return MEMCACHED_UNKNOWN_READ_FAILURE
;
376 if (memcached_string_check(&result
->value
,
377 bodylen
) != MEMCACHED_SUCCESS
)
378 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
380 char *vptr
= memcached_string_value(&result
->value
);
381 if (memcached_safe_read(ptr
, vptr
, bodylen
) != MEMCACHED_SUCCESS
)
382 return MEMCACHED_UNKNOWN_READ_FAILURE
;
384 memcached_string_set_length(&result
->value
, bodylen
);
387 case PROTOCOL_BINARY_CMD_INCREMENT
:
388 case PROTOCOL_BINARY_CMD_DECREMENT
:
390 if (bodylen
!= sizeof(uint64_t) || buffer_length
!= sizeof(uint64_t))
391 return MEMCACHED_PROTOCOL_ERROR
;
393 WATCHPOINT_ASSERT(bodylen
== buffer_length
);
395 if (memcached_safe_read(ptr
, &val
, sizeof(val
)) != MEMCACHED_SUCCESS
)
396 return MEMCACHED_UNKNOWN_READ_FAILURE
;
399 memcpy(buffer
, &val
, sizeof(val
));
402 case PROTOCOL_BINARY_CMD_VERSION
:
404 memset(buffer
, 0, buffer_length
);
405 if (bodylen
>= buffer_length
)
406 /* not enough space in buffer.. should not happen... */
407 return MEMCACHED_UNKNOWN_READ_FAILURE
;
408 else if (memcached_safe_read(ptr
, buffer
, bodylen
) != MEMCACHED_SUCCESS
)
409 return MEMCACHED_UNKNOWN_READ_FAILURE
;
412 case PROTOCOL_BINARY_CMD_FLUSH
:
413 case PROTOCOL_BINARY_CMD_QUIT
:
414 case PROTOCOL_BINARY_CMD_SET
:
415 case PROTOCOL_BINARY_CMD_ADD
:
416 case PROTOCOL_BINARY_CMD_REPLACE
:
417 case PROTOCOL_BINARY_CMD_APPEND
:
418 case PROTOCOL_BINARY_CMD_PREPEND
:
419 case PROTOCOL_BINARY_CMD_DELETE
:
421 WATCHPOINT_ASSERT(bodylen
== 0);
422 return MEMCACHED_SUCCESS
;
424 case PROTOCOL_BINARY_CMD_NOOP
:
426 WATCHPOINT_ASSERT(bodylen
== 0);
427 return MEMCACHED_END
;
429 case PROTOCOL_BINARY_CMD_STAT
:
432 return MEMCACHED_END
;
433 else if (bodylen
+ 1 > buffer_length
)
434 /* not enough space in buffer.. should not happen... */
435 return MEMCACHED_UNKNOWN_READ_FAILURE
;
438 size_t keylen
= header
.response
.keylen
;
439 memset(buffer
, 0, buffer_length
);
440 if (memcached_safe_read(ptr
, buffer
, keylen
) != MEMCACHED_SUCCESS
||
441 memcached_safe_read(ptr
, buffer
+ keylen
+ 1,
442 bodylen
- keylen
) != MEMCACHED_SUCCESS
)
443 return MEMCACHED_UNKNOWN_READ_FAILURE
;
449 /* Command not implemented yet! */
450 WATCHPOINT_ASSERT(0);
451 return MEMCACHED_PROTOCOL_ERROR
;
455 else if (header
.response
.bodylen
)
457 /* What should I do with the error message??? just discard it for now */
458 char hole
[SMALL_STRING_LEN
];
461 size_t nr
= (bodylen
> SMALL_STRING_LEN
) ? SMALL_STRING_LEN
: bodylen
;
462 if (memcached_safe_read(ptr
, hole
, nr
) != MEMCACHED_SUCCESS
)
463 return MEMCACHED_UNKNOWN_READ_FAILURE
;
464 bodylen
-= (uint32_t) nr
;
467 /* This might be an error from one of the quiet commands.. if
468 * so, just throw it away and get the next one. What about creating
469 * a callback to the user with the error information?
471 switch (header
.response
.opcode
)
473 case PROTOCOL_BINARY_CMD_SETQ
:
474 case PROTOCOL_BINARY_CMD_ADDQ
:
475 case PROTOCOL_BINARY_CMD_REPLACEQ
:
476 case PROTOCOL_BINARY_CMD_APPENDQ
:
477 case PROTOCOL_BINARY_CMD_PREPENDQ
:
478 return binary_read_one_response(ptr
, buffer
, buffer_length
, result
);
484 memcached_return_t rc
= MEMCACHED_SUCCESS
;
485 unlikely(header
.response
.status
!= 0)
486 switch (header
.response
.status
)
488 case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
:
489 rc
= MEMCACHED_NOTFOUND
;
491 case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
:
492 rc
= MEMCACHED_DATA_EXISTS
;
494 case PROTOCOL_BINARY_RESPONSE_NOT_STORED
:
495 rc
= MEMCACHED_NOTSTORED
;
497 case PROTOCOL_BINARY_RESPONSE_E2BIG
:
500 case PROTOCOL_BINARY_RESPONSE_ENOMEM
:
501 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
503 case PROTOCOL_BINARY_RESPONSE_EINVAL
:
504 case PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND
:
506 /* @todo fix the error mappings */
507 rc
= MEMCACHED_PROTOCOL_ERROR
;