X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fresponse.cc;h=445b92338b29ca5d45d4c6988298dc1600860b99;hb=fd8a1d785722f36d111915b0c96331e907df2746;hp=061f7401fee84a19dbd462a0564601ebce5a7276;hpb=afddcf83538afc8300886702fcddf2333eb94c1f;p=m6w6%2Flibmemcached diff --git a/libmemcached/response.cc b/libmemcached/response.cc index 061f7401..445b9233 100644 --- a/libmemcached/response.cc +++ b/libmemcached/response.cc @@ -38,104 +38,20 @@ #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; - - if (ptr->root->flags.use_udp) - { - return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); - } + size_t value_length; 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 " */ @@ -162,29 +78,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') @@ -200,7 +126,9 @@ 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))) @@ -217,7 +145,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) { @@ -254,8 +182,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); @@ -267,32 +197,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")) { @@ -317,88 +250,117 @@ 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') + 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') { return MEMCACHED_NOTSTORED; } - else - { - WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } } + 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 - { - WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - } + 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') + if (buffer[1] == 'O' and buffer[2] == 'U' and buffer[3] == 'C' and buffer[4] == 'H' and buffer[5] == 'E' and buffer[6] == 'D') { return MEMCACHED_SUCCESS; } } - return MEMCACHED_UNKNOWN_READ_FAILURE; + break; - 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; + 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; + } + 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_set_error(*ptr, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("Numeric response was out of range")); + } + else if (errno == EINVAL) + { + return memcached_set_error(*ptr, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("Numeric response was out of range")); + } + + numeric_value= uint64_t(auto_return_value); WATCHPOINT_STRING(buffer); - return MEMCACHED_UNKNOWN_READ_FAILURE; + return MEMCACHED_SUCCESS; } + + default: + break; } - /* NOTREACHED */ + return memcached_set_error(*ptr, MEMCACHED_UNKNOWN_READ_FAILURE, MEMCACHED_AT, + memcached_literal_param("Could not determine response")); } static memcached_return_t binary_read_one_response(memcached_server_write_instance_st ptr, @@ -497,7 +459,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; } @@ -681,3 +643,105 @@ 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) +{ + if (memcached_is_udp(ptr->root)) + { + return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); + } + + 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) +{ + if (memcached_is_udp(ptr->root)) + { + return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); + } + + /* We may have old commands in the buffer not set, first purge */ + if ((ptr->root->flags.no_block) and (memcached_is_processing_input(ptr->root) == false)) + { + (void)memcached_io_write(ptr); + } + + /* + * The previous implementation purged all pending requests and just + * returned the last one. Purge all pending messages to ensure backwards + * compatibility. + */ + if (memcached_is_binary(ptr->root) == 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); +}