X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Flibmemcachedprotocol%2Fbinary_handler.c;h=cdc03b93e3ae1b261b2b15f902312ff93d652038;hb=43fa1d415799c1b15de6381fc3b53281e4559f36;hp=c7a76b3f292823684820f0d4718dafe7b6faaf58;hpb=0f27cca46a4a529459ca988268972f480a9730c6;p=awesomized%2Flibmemcached diff --git a/src/libmemcachedprotocol/binary_handler.c b/src/libmemcachedprotocol/binary_handler.c index c7a76b3f..cdc03b93 100644 --- a/src/libmemcachedprotocol/binary_handler.c +++ b/src/libmemcachedprotocol/binary_handler.c @@ -1,6 +1,6 @@ /* +--------------------------------------------------------------------+ - | libmemcached - C/C++ Client Library for memcached | + | libmemcached-awesome - C/C++ Client Library for memcached | +--------------------------------------------------------------------+ | Redistribution and use in source and binary forms, with or without | | modification, are permitted under the terms of the BSD license. | @@ -9,11 +9,12 @@ | the terms online at: https://opensource.org/licenses/BSD-3-Clause | +--------------------------------------------------------------------+ | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | - | Copyright (c) 2020 Michael Wallner | + | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ | +--------------------------------------------------------------------+ */ #include "libmemcachedprotocol/common.h" +#include "p9y/socket.hpp" #include #include @@ -43,7 +44,7 @@ binary_raw_response_handler(const void *cookie, protocol_binary_request_header * protocol_binary_response_header *response) { memcached_protocol_client_st *client = (void *) cookie; - if (client->root->pedantic + if (response && client->root->pedantic && !memcached_binary_protocol_pedantic_check_response(request, response)) { return PROTOCOL_BINARY_RESPONSE_EINVAL; } @@ -52,6 +53,10 @@ binary_raw_response_handler(const void *cookie, protocol_binary_request_header * return PROTOCOL_BINARY_RESPONSE_EINTERNAL; } + if (!response) { + return PROTOCOL_BINARY_RESPONSE_SUCCESS; + } + size_t len = sizeof(protocol_binary_response_header) + htonl(response->response.bodylen); size_t offset = 0; char *ptr = (void *) response; @@ -405,7 +410,7 @@ add_command_handler(const void *cookie, protocol_binary_request_header *header, protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.add != NULL) { + if (client->root->callback->interface.v1.add) { uint16_t keylen = ntohs(header->request.keylen); uint32_t datalen = ntohl(header->request.bodylen) - keylen - 8; protocol_binary_request_add *request = (void *) header; @@ -450,7 +455,7 @@ decrement_command_handler(const void *cookie, protocol_binary_request_header *he protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.decrement != NULL) { + if (client->root->callback->interface.v1.decrement) { uint16_t keylen = ntohs(header->request.keylen); protocol_binary_request_decr *request = (void *) header; uint64_t init = memcached_ntohll(request->message.body.initial); @@ -497,7 +502,7 @@ delete_command_handler(const void *cookie, protocol_binary_request_header *heade protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.delete_object != NULL) { + if (client->root->callback->interface.v1.delete_object) { uint16_t keylen = ntohs(header->request.keylen); void *key = (header + 1); uint64_t cas = memcached_ntohll(header->request.cas); @@ -536,7 +541,7 @@ flush_command_handler(const void *cookie, protocol_binary_request_header *header protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.flush_object != NULL) { + if (client->root->callback->interface.v1.flush_object) { protocol_binary_request_flush *flush_object = (void *) header; uint32_t timeout = 0; if (htonl(header->request.bodylen) == 4) { @@ -577,7 +582,7 @@ get_command_handler(const void *cookie, protocol_binary_request_header *header, protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.get != NULL) { + if (client->root->callback->interface.v1.get) { uint16_t keylen = ntohs(header->request.keylen); void *key = (header + 1); rval = client->root->callback->interface.v1.get(cookie, key, keylen, get_response_handler); @@ -610,7 +615,7 @@ increment_command_handler(const void *cookie, protocol_binary_request_header *he protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.increment != NULL) { + if (client->root->callback->interface.v1.increment) { uint16_t keylen = ntohs(header->request.keylen); protocol_binary_request_incr *request = (void *) header; uint64_t init = memcached_ntohll(request->message.body.initial); @@ -657,7 +662,7 @@ static protocol_binary_response_status noop_command_handler(const void *cookie, protocol_binary_request_header *header, memcached_binary_protocol_raw_response_handler response_handler) { memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.noop != NULL) { + if (client->root->callback->interface.v1.noop) { client->root->callback->interface.v1.noop(cookie); } @@ -686,7 +691,7 @@ append_command_handler(const void *cookie, protocol_binary_request_header *heade protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.append != NULL) { + if (client->root->callback->interface.v1.append) { uint16_t keylen = ntohs(header->request.keylen); uint32_t datalen = ntohl(header->request.bodylen) - keylen; char *key = (void *) (header + 1); @@ -734,7 +739,7 @@ prepend_command_handler(const void *cookie, protocol_binary_request_header *head protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.prepend != NULL) { + if (client->root->callback->interface.v1.prepend) { uint16_t keylen = ntohs(header->request.keylen); uint32_t datalen = ntohl(header->request.bodylen) - keylen; char *key = (char *) (header + 1); @@ -778,7 +783,7 @@ static protocol_binary_response_status quit_command_handler(const void *cookie, protocol_binary_request_header *header, memcached_binary_protocol_raw_response_handler response_handler) { memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.quit != NULL) { + if (client->root->callback->interface.v1.quit) { client->root->callback->interface.v1.quit(cookie); } @@ -810,7 +815,7 @@ replace_command_handler(const void *cookie, protocol_binary_request_header *head protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.replace != NULL) { + if (client->root->callback->interface.v1.replace) { uint16_t keylen = ntohs(header->request.keylen); uint32_t datalen = ntohl(header->request.bodylen) - keylen - 8; protocol_binary_request_replace *request = (void *) header; @@ -861,7 +866,7 @@ set_command_handler(const void *cookie, protocol_binary_request_header *header, protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.set != NULL) { + if (client->root->callback->interface.v1.set) { uint16_t keylen = ntohs(header->request.keylen); uint32_t datalen = ntohl(header->request.bodylen) - keylen - 8; protocol_binary_request_replace *request = (void *) header; @@ -911,11 +916,25 @@ stat_command_handler(const void *cookie, protocol_binary_request_header *header, protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.stat != NULL) { + if (client->root->callback->interface.v1.stat) { uint16_t keylen = ntohs(header->request.keylen); rval = client->root->callback->interface.v1.stat(cookie, (void *) (header + 1), keylen, stat_response_handler); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) { + /* END message */ + protocol_binary_response_no_extras response = { + .message = { + .header.response = + { + .magic = PROTOCOL_BINARY_RES, + .opcode = PROTOCOL_BINARY_CMD_STAT, + .status = htons(PROTOCOL_BINARY_RESPONSE_SUCCESS), + .opaque = header->request.opaque, + }, + }}; + rval = response_handler(cookie, header, (void *) &response); + } } else { rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } @@ -938,8 +957,11 @@ version_command_handler(const void *cookie, protocol_binary_request_header *head protocol_binary_response_status rval; memcached_protocol_client_st *client = (void *) cookie; - if (client->root->callback->interface.v1.version != NULL) { + if (client->root->callback->interface.v1.version) { rval = client->root->callback->interface.v1.version(cookie, version_response_handler); + if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS) { + rval = response_handler(cookie, header, NULL); + } } else { rval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND; } @@ -996,7 +1018,7 @@ static protocol_binary_response_status execute_command(memcached_protocol_client } /* we got all data available, execute the callback! */ - if (client->root->callback->pre_execute != NULL) { + if (client->root->callback->pre_execute) { client->root->callback->pre_execute(client, header); } @@ -1009,14 +1031,14 @@ static protocol_binary_response_status execute_command(memcached_protocol_client switch (client->root->callback->interface_version) { case 0: - if (client->root->callback->interface.v0.comcode[cc] != NULL) { + if (client->root->callback->interface.v0.comcode[cc]) { rval = client->root->callback->interface.v0.comcode[cc](client, header, binary_raw_response_handler); } break; case 1: - if (comcode_v0_v1_remap[cc] != NULL) { + if (comcode_v0_v1_remap[cc]) { rval = comcode_v0_v1_remap[cc](client, header, binary_raw_response_handler); } break; @@ -1029,7 +1051,7 @@ static protocol_binary_response_status execute_command(memcached_protocol_client abort(); } - if (rval == PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND && client->root->callback->unknown != NULL) { + if (rval == PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND && client->root->callback->unknown) { rval = client->root->callback->unknown(client, header, binary_raw_response_handler); } @@ -1048,7 +1070,7 @@ static protocol_binary_response_status execute_command(memcached_protocol_client rval = binary_raw_response_handler(client, header, (void *) &response); } - if (client->root->callback->post_execute != NULL) { + if (client->root->callback->post_execute) { client->root->callback->post_execute(client, header); }