X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fresponse.cc;h=35a9f74de40f1c32015fd41cb5f87893b9b58b54;hb=06d0eaed5e274bc0ba93a3a61788a4bab480adf2;hp=669c633a95f1ed4ee9dc92526bd7aa81e39c32d7;hpb=e097b6cf87ee49c74404642a0862171ecc605d67;p=awesomized%2Flibmemcached diff --git a/libmemcached/response.cc b/libmemcached/response.cc index 669c633a..35a9f74d 100644 --- a/libmemcached/response.cc +++ b/libmemcached/response.cc @@ -1,5 +1,5 @@ /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * + * * Libmemcached library * * Copyright (C) 2011 Data Differential, http://datadifferential.com/ @@ -38,100 +38,25 @@ #include #include -static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr, - char *buffer, size_t buffer_length, - memcached_result_st *result); -static memcached_return_t binary_read_one_response(memcached_server_write_instance_st ptr, - char *buffer, size_t buffer_length, - memcached_result_st *result); - -memcached_return_t memcached_read_one_response(memcached_server_write_instance_st ptr, - char *buffer, size_t buffer_length, - memcached_result_st *result) -{ - memcached_server_response_decrement(ptr); - - if (result == NULL) - { - memcached_st *root= (memcached_st *)ptr->root; - result = &root->result; - } - - memcached_return_t rc; - if (ptr->root->flags.binary_protocol) - { - rc= binary_read_one_response(ptr, buffer, buffer_length, result); - } - else - { - rc= textual_read_one_response(ptr, buffer, buffer_length, result); - } - - unlikely(rc == MEMCACHED_UNKNOWN_READ_FAILURE or - rc == MEMCACHED_PROTOCOL_ERROR or - rc == MEMCACHED_CLIENT_ERROR or - rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE) - memcached_io_reset(ptr); - - return rc; -} - -memcached_return_t memcached_response(memcached_server_write_instance_st ptr, - char *buffer, size_t buffer_length, - memcached_result_st *result) -{ - /* We may have old commands in the buffer not set, first purge */ - if ((ptr->root->flags.no_block) && (memcached_is_processing_input(ptr->root) == false)) - { - (void)memcached_io_write(ptr, NULL, 0, true); - } - - /* - * The previous implementation purged all pending requests and just - * returned the last one. Purge all pending messages to ensure backwards - * compatibility. - */ - if (ptr->root->flags.binary_protocol == false) - { - while (memcached_server_response_count(ptr) > 1) - { - memcached_return_t rc= memcached_read_one_response(ptr, buffer, buffer_length, result); - - unlikely (rc != MEMCACHED_END && - rc != MEMCACHED_STORED && - rc != MEMCACHED_SUCCESS && - rc != MEMCACHED_STAT && - rc != MEMCACHED_DELETED && - rc != MEMCACHED_NOTFOUND && - rc != MEMCACHED_NOTSTORED && - rc != MEMCACHED_DATA_EXISTS) - return rc; - } - } - - return memcached_read_one_response(ptr, buffer, buffer_length, result); -} - static memcached_return_t textual_value_fetch(memcached_server_write_instance_st ptr, char *buffer, memcached_result_st *result) { - char *string_ptr; - char *end_ptr; char *next_ptr; - size_t value_length; - size_t to_read; ssize_t read_length= 0; + size_t value_length; if (ptr->root->flags.use_udp) + { return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); + } WATCHPOINT_ASSERT(ptr->root); - end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE; + char *end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE; memcached_result_reset(result); - string_ptr= buffer; + char *string_ptr= buffer; string_ptr+= 6; /* "VALUE " */ @@ -158,29 +83,39 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st } if (end_ptr == string_ptr) + { goto read_error; + } /* Flags fetch move past space */ string_ptr++; if (end_ptr == string_ptr) + { goto read_error; + } for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {}; result->item_flags= (uint32_t) strtoul(next_ptr, &string_ptr, 10); if (end_ptr == string_ptr) + { goto read_error; + } /* Length fetch move past space*/ string_ptr++; if (end_ptr == string_ptr) + { goto read_error; + } for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++) {}; value_length= (size_t)strtoull(next_ptr, &string_ptr, 10); if (end_ptr == string_ptr) + { goto read_error; + } /* Skip spaces */ if (*string_ptr == '\r') @@ -196,12 +131,13 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st } if (end_ptr < string_ptr) + { goto read_error; + } /* We add two bytes so that we can walk the \r\n */ if (memcached_failed(memcached_string_check(&result->value, value_length +2))) { - value_length= 0; return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } @@ -214,7 +150,7 @@ static memcached_return_t textual_value_fetch(memcached_server_write_instance_st We are null terminating through, which will most likely make some people lazy about using the return length. */ - to_read= (value_length) + 2; + size_t to_read= (value_length) + 2; memcached_return_t rrc= memcached_io_read(ptr, value_ptr, to_read, &read_length); if (memcached_failed(rrc) and rrc == MEMCACHED_IN_PROGRESS) { @@ -251,8 +187,10 @@ read_error: static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, - memcached_result_st *result) + memcached_result_st *result, + uint64_t& numeric_value) { + numeric_value= UINT64_MAX; size_t total_read; memcached_return_t rc= memcached_io_readline(ptr, buffer, buffer_length, total_read); @@ -264,31 +202,35 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta switch(buffer[0]) { case 'V': /* VALUE || VERSION */ - if (buffer[1] == 'A') /* VALUE */ + if (buffer[1] == 'A' and buffer[2] == 'L' and buffer[3] == 'U' and buffer[4] == 'E') /* VALUE */ { /* We add back in one because we will need to search for END */ memcached_server_response_increment(ptr); return textual_value_fetch(ptr, buffer, result); } - else if (buffer[1] == 'E') /* VERSION */ + else if (buffer[1] == 'E' and buffer[2] == 'R' and buffer[3] == 'S' and buffer[4] == 'I' and buffer[5] == 'O' and buffer[6] == 'N') /* VERSION */ { return MEMCACHED_SUCCESS; } - else + break; + + case 'O': /* OK */ + if (buffer[1] == 'K') { - WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; + return MEMCACHED_SUCCESS; } - case 'O': /* OK */ - return MEMCACHED_SUCCESS; + break; + case 'S': /* STORED STATS SERVER_ERROR */ { - if (buffer[2] == 'A') /* STORED STATS */ + if (buffer[1] == 'T' and buffer[2] == 'A' and buffer[3] == 'T') /* STORED STATS */ { memcached_server_response_increment(ptr); return MEMCACHED_STAT; } - else if (buffer[1] == 'E') /* SERVER_ERROR */ + else if (buffer[1] == 'E' and buffer[2] == 'R' and buffer[3] == 'V' and buffer[4] == 'E' and buffer[5] == 'R' + and buffer[6] == '_' + and buffer[7] == 'E' and buffer[8] == 'R' and buffer[9] == 'R' and buffer[10] == 'O' and buffer[11] == 'R' ) /* SERVER_ERROR */ { if (total_read == memcached_literal_param_size("SERVER_ERROR")) { @@ -313,65 +255,114 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta return memcached_set_error(*ptr, MEMCACHED_SERVER_ERROR, MEMCACHED_AT, startptr, size_t(endptr - startptr)); } - else if (buffer[1] == 'T') + else if (buffer[1] == 'T' and buffer[2] == 'O' and buffer[3] == 'R' and buffer[4] == 'E' and buffer[5] == 'D') { return MEMCACHED_STORED; } - else - { - WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } } + break; + case 'D': /* DELETED */ - return MEMCACHED_DELETED; + if (buffer[1] == 'E' and buffer[2] == 'L' and buffer[3] == 'E' and buffer[4] == 'T' and buffer[5] == 'E' and buffer[6] == 'D') + { + return MEMCACHED_DELETED; + } + break; case 'N': /* NOT_FOUND */ { - if (buffer[4] == 'F') + if (buffer[1] == 'O' and buffer[2] == 'T' + and buffer[3] == '_' + and buffer[4] == 'F' and buffer[5] == 'O' and buffer[6] == 'U' and buffer[7] == 'N' and buffer[8] == 'D') + { return MEMCACHED_NOTFOUND; - else if (buffer[4] == 'S') - return MEMCACHED_NOTSTORED; - else + } + else if (buffer[1] == 'O' and buffer[2] == 'T' + and buffer[3] == '_' + and buffer[4] == 'S' and buffer[5] == 'T' and buffer[6] == 'O' and buffer[7] == 'R' and buffer[8] == 'E' and buffer[9] == 'D') { - WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; + return MEMCACHED_NOTSTORED; } } + break; + case 'E': /* PROTOCOL ERROR or END */ { - if (buffer[1] == 'N') + if (buffer[1] == 'N' and buffer[2] == 'D') + { return MEMCACHED_END; - else if (buffer[1] == 'R') + } + else if (buffer[1] == 'R' and buffer[2] == 'O' and buffer[3] == 'T' and buffer[4] == 'O' and buffer[5] == 'C' and buffer[6] == 'O' and buffer[7] == 'L' + and buffer[8] == '_' + and buffer[9] == 'E' and buffer[10] == 'R' and buffer[11] == 'R' and buffer[12] == 'O' and buffer[13] == 'R') + { return MEMCACHED_PROTOCOL_ERROR; - else if (buffer[1] == 'X') + } + else if (buffer[1] == 'X' and buffer[2] == 'I' and buffer[3] == 'S' and buffer[4] == 'T' and buffer[5] == 'S') + { return MEMCACHED_DATA_EXISTS; - else + } + } + break; + + case 'T': /* TOUCHED */ + { + if (buffer[1] == 'O' and buffer[2] == 'U' and buffer[3] == 'C' and buffer[4] == 'H' and buffer[5] == 'E' and buffer[6] == 'D') { - WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; + return MEMCACHED_SUCCESS; } + } + break; + case 'I': /* ITEM */ + if (buffer[1] == 'T' and buffer[2] == 'E' and buffer[3] == 'M') + { + /* We add back in one because we will need to search for END */ + memcached_server_response_increment(ptr); + return MEMCACHED_ITEM; } - case 'I': /* CLIENT ERROR */ - /* We add back in one because we will need to search for END */ - memcached_server_response_increment(ptr); - return MEMCACHED_ITEM; + break; + case 'C': /* CLIENT ERROR */ - return MEMCACHED_CLIENT_ERROR; - default: + if (buffer[1] == 'L' and buffer[2] == 'I' and buffer[3] == 'E' and buffer[4] == 'N' and buffer[5] == 'T') + { + return MEMCACHED_CLIENT_ERROR; + } + break; + + case '0': /* INCR/DECR response */ + case '1': /* INCR/DECR response */ + case '2': /* INCR/DECR response */ + case '3': /* INCR/DECR response */ + case '4': /* INCR/DECR response */ + case '5': /* INCR/DECR response */ + case '6': /* INCR/DECR response */ + case '7': /* INCR/DECR response */ + case '8': /* INCR/DECR response */ + case '9': /* INCR/DECR response */ { - unsigned long long auto_return_value; + unsigned long long int auto_return_value= strtoull(buffer, (char **)NULL, 10); - if (sscanf(buffer, "%llu", &auto_return_value) == 1) - return MEMCACHED_SUCCESS; + if (auto_return_value == ULLONG_MAX and errno == ERANGE) + { + return MEMCACHED_UNKNOWN_READ_FAILURE; + } + else if (errno == EINVAL) + { + return MEMCACHED_UNKNOWN_READ_FAILURE; + } + + numeric_value= uint64_t(auto_return_value); WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; + return MEMCACHED_SUCCESS; } + + default: + break; } - /* NOTREACHED */ + return MEMCACHED_UNKNOWN_READ_FAILURE; } static memcached_return_t binary_read_one_response(memcached_server_write_instance_st ptr, @@ -470,7 +461,7 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan case PROTOCOL_BINARY_CMD_INCREMENT: case PROTOCOL_BINARY_CMD_DECREMENT: { - if (bodylen != sizeof(uint64_t) || buffer_length != sizeof(uint64_t)) + if (bodylen != sizeof(uint64_t) or buffer_length != sizeof(uint64_t)) { return MEMCACHED_PROTOCOL_ERROR; } @@ -512,15 +503,18 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan case PROTOCOL_BINARY_CMD_APPEND: case PROTOCOL_BINARY_CMD_PREPEND: case PROTOCOL_BINARY_CMD_DELETE: + case PROTOCOL_BINARY_CMD_TOUCH: { WATCHPOINT_ASSERT(bodylen == 0); return MEMCACHED_SUCCESS; } + case PROTOCOL_BINARY_CMD_NOOP: { WATCHPOINT_ASSERT(bodylen == 0); return MEMCACHED_END; } + case PROTOCOL_BINARY_CMD_STAT: { if (bodylen == 0) @@ -651,3 +645,95 @@ static memcached_return_t binary_read_one_response(memcached_server_write_instan return rc; } + +memcached_return_t memcached_read_one_response(memcached_server_write_instance_st ptr, + char *buffer, size_t buffer_length, + memcached_result_st *result) +{ + uint64_t numeric_value; + + return memcached_read_one_response(ptr, buffer, buffer_length, result, numeric_value); +} + +memcached_return_t memcached_read_one_response(memcached_server_write_instance_st ptr, + char *buffer, size_t buffer_length, + memcached_result_st *result, + uint64_t& numeric_value) +{ + memcached_server_response_decrement(ptr); + + if (result == NULL) + { + memcached_st *root= (memcached_st *)ptr->root; + result = &root->result; + } + + memcached_return_t rc; + if (ptr->root->flags.binary_protocol) + { + rc= binary_read_one_response(ptr, buffer, buffer_length, result); + } + else + { + rc= textual_read_one_response(ptr, buffer, buffer_length, result, numeric_value); + } + + if (rc == MEMCACHED_UNKNOWN_READ_FAILURE or + rc == MEMCACHED_READ_FAILURE or + rc == MEMCACHED_PROTOCOL_ERROR or + rc == MEMCACHED_CLIENT_ERROR or + rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE) + { + memcached_io_reset(ptr); + } + + return rc; +} + +memcached_return_t memcached_response(memcached_server_write_instance_st ptr, + char *buffer, size_t buffer_length, + memcached_result_st *result) +{ + uint64_t numeric_value; + + return memcached_response(ptr, buffer, buffer_length, result, numeric_value); +} + +memcached_return_t memcached_response(memcached_server_write_instance_st ptr, + char *buffer, size_t buffer_length, + memcached_result_st *result, + uint64_t& numeric_value) +{ + /* We may have old commands in the buffer not set, first purge */ + if ((ptr->root->flags.no_block) && (memcached_is_processing_input(ptr->root) == false)) + { + (void)memcached_io_write(ptr, NULL, 0, true); + } + + /* + * The previous implementation purged all pending requests and just + * returned the last one. Purge all pending messages to ensure backwards + * compatibility. + */ + if (ptr->root->flags.binary_protocol == false) + { + while (memcached_server_response_count(ptr) > 1) + { + memcached_return_t rc= memcached_read_one_response(ptr, buffer, buffer_length, result, numeric_value); + + if (rc != MEMCACHED_END && + rc != MEMCACHED_STORED && + rc != MEMCACHED_SUCCESS && + rc != MEMCACHED_STAT && + rc != MEMCACHED_DELETED && + rc != MEMCACHED_NOTFOUND && + rc != MEMCACHED_NOTSTORED && + rc != MEMCACHED_DATA_EXISTS) + { + return rc; + } + } + } + + return memcached_read_one_response(ptr, buffer, buffer_length, result, numeric_value); +}