When I test the redis cmd by echo and nc, there is a redundant error in the end. I checked by strace, currently if client read nothing from stdin, it will shutdown the socket, redis server will read nothing (0 byte) from socket. But it tries to process the empty command and returns an error. $ echo -n -e '*1\r\n$4\r\nping\r\n' |strace nc localhost 6379 | ... | read(0, "*1\r\n$4\r\nping\r\n", 8192) = 14 | select(5, [4], [4], [], NULL) = 1 (out [4]) |>>> sendto(4, "*1\r\n$4\r\nping\r\n", 14, 0, NULL, 0) = 14 | select(5, [0 4], [], [], NULL) = 1 (in [0]) | recvfrom(0, 0x7ffe4d5b6c70, 8192, 0, 0x7ffe4d5b6bf0, 0x7ffe4d5b6bec) = -1 ENOTSOCK (Socket operation on non-socket) | read(0, "", 8192) = 0 |>>> shutdown(4, SHUT_WR) = 0 | select(5, [4], [], [], NULL) = 1 (in [4]) | recvfrom(4, "+PONG\r\n-ERR unknown command ''\r\n", 8192, 0, 0x7ffe4d5b6bf0, [0]) = 32 | write(1, "+PONG\r\n-ERR unknown command ''\r\n", 32+PONG | -ERR unknown command '' | ) = 32 | select(5, [4], [], [], NULL) = 1 (in [4]) | recvfrom(4, "", 8192, 0, 0x7ffe4d5b6bf0, [0]) = 0 | close(1) = 0 | close(4) = 0 Current result: $ echo -n -e '' |nc localhost 6379 -ERR unknown command '' $ echo -n -e '*1\r\n$4\r\nping\r\n' |nc localhost 6379 +PONG -ERR unknown command '' Expected: $ echo -n -e '' |nc localhost 6379 $ echo -n -e '*1\r\n$4\r\nping\r\n' |nc localhost 6379 +PONG Signed-off-by: Amos Kong <amos@scylladb.com>
142 lines
3.2 KiB
Ragel
142 lines
3.2 KiB
Ragel
/*
|
|
* Copyright (C) 2019 pengjian.uestc @ gmail.com
|
|
*/
|
|
|
|
/*
|
|
* This file is part of Scylla.
|
|
*
|
|
* Scylla is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Scylla is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <memory>
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include "bytes.hh"
|
|
#include "redis/request.hh"
|
|
#include <seastar/core/ragel.hh>
|
|
|
|
using namespace seastar;
|
|
using namespace redis;
|
|
%%{
|
|
|
|
machine redis_resp_protocol;
|
|
|
|
access _fsm_;
|
|
|
|
action mark {
|
|
g.mark_start(p);
|
|
}
|
|
|
|
action start_blob {
|
|
g.mark_start(p);
|
|
_size_left = _arg_size;
|
|
}
|
|
|
|
action start_command {
|
|
g.mark_start(p);
|
|
_size_left = _arg_size;
|
|
}
|
|
|
|
action advance_blob {
|
|
auto len = std::min(static_cast<uint32_t>(pe - p), _size_left);
|
|
_size_left -= len;
|
|
p += len;
|
|
if (_size_left == 0) {
|
|
_req._args.push_back(str());
|
|
p--;
|
|
fret;
|
|
}
|
|
p--;
|
|
}
|
|
|
|
action advance_command {
|
|
auto len = std::min(static_cast<uint32_t>(pe - p), _size_left);
|
|
_size_left -= len;
|
|
p += len;
|
|
if (_size_left == 0) {
|
|
_req._command = str();
|
|
p--;
|
|
fret;
|
|
}
|
|
p--;
|
|
}
|
|
|
|
|
|
crlf = '\r\n';
|
|
u32 = digit+ >{ _u32 = 0;} ${ _u32 *= 10; _u32 += fc - '0';};
|
|
args_count = '*' u32 crlf ${_req._args_count = _u32 - 1;};
|
|
blob := any+ >start_blob $advance_blob;
|
|
command := any+ >start_command $advance_command;
|
|
arg = '$' u32 crlf ${ _arg_size = _u32;};
|
|
|
|
main := (args_count (arg @{fcall command; } crlf) (arg @{fcall blob; } crlf)+) ${_req._state = request_state::ok;} >eof{_req._state = request_state::eof;};
|
|
|
|
prepush {
|
|
prepush();
|
|
}
|
|
|
|
postpop {
|
|
postpop();
|
|
}
|
|
|
|
}%%
|
|
|
|
class redis_protocol_parser : public ragel_parser_base<redis_protocol_parser> {
|
|
%% write data nofinal noprefix;
|
|
public:
|
|
redis::request _req;
|
|
uint32_t _u32;
|
|
uint32_t _arg_size;
|
|
uint32_t _size_left;
|
|
public:
|
|
virtual void init() {
|
|
init_base();
|
|
_req._state = request_state::error;
|
|
_req._args.clear();
|
|
_req._args_count = 0;
|
|
_size_left = 0;
|
|
_arg_size = 0;
|
|
%% write init;
|
|
}
|
|
|
|
char* parse(char* p, char* pe, char* eof) {
|
|
sstring_builder::guard g(_builder, p, pe);
|
|
auto str = [this, &g, &p] {
|
|
g.mark_end(p);
|
|
auto s = get_str();
|
|
return to_bytes(s);
|
|
};
|
|
|
|
%% write exec;
|
|
if (_req._state != request_state::error) {
|
|
return p;
|
|
}
|
|
if (p != pe) {
|
|
p = pe;
|
|
return p;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bool eof() const {
|
|
return _req._state == request_state::eof;
|
|
}
|
|
|
|
redis::request& get_request() {
|
|
std::transform(_req._command.begin(), _req._command.end(), _req._command.begin(), ::tolower);
|
|
return _req;
|
|
}
|
|
};
|