int handle_request(char *req_buffer, char *resp_buffer) { char req_type[10]; char req_arg[100]; sscanf(req_buffer, "%10s %100s", req_type, req_arg); syslog(LOG_INFO, "got request, type=%s arg=%s", &req_type, &req_arg); memset(resp_buffer, '\0', RESP_BUFFER_SIZE); if (strcmp("GET", req_type) == 0) { handle_get_request(req_buffer, resp_buffer, req_arg); } else { handle_invalid_request(req_buffer, resp_buffer, req_arg); } }
static void client_input(struct socket_info *si) { struct client_connection *c = si->udata; char buf[1500]; int n; int got = 0; int ok; if (!c) croak(1, "client_input: no client_connection information"); if ( (n = read(si->fd, buf, 1500)) == -1) { switch (errno) { case EPIPE: client_gone(si); return; case ECONNRESET: client_gone(si); return; } croak(1, "client_input: read error"); } if (n == 0) { client_gone(si); return; } msgpack_unpacker_reserve_buffer(&c->unpacker, n); memcpy(msgpack_unpacker_buffer(&c->unpacker), buf, n); msgpack_unpacker_buffer_consumed(&c->unpacker, n); while (msgpack_unpacker_next(&c->unpacker, &c->input)) { msgpack_object *o; uint32_t cid; uint32_t type; got = 1; ok = -1; PS.client_requests++; si->PS.client_requests++; //if (!opt_quiet) { // printf("got client input: "); // msgpack_object_print(stdout, c->input.data); // printf("\n"); //} o = &c->input.data; if (o->type != MSGPACK_OBJECT_ARRAY) { error_reply(si, RT_ERROR, 0, "Request is not an array"); goto end; } if (o->via.array.size < 1) { error_reply(si, RT_ERROR, 0, "Request is an empty array"); goto end; } if (o->via.array.size < 2) { error_reply(si, RT_ERROR, 0, "Request without an id"); goto end; } if (o->via.array.ptr[RI_CID].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { error_reply(si, RT_ERROR, 0, "Request id is not a positive integer"); goto end; } cid = o->via.array.ptr[RI_CID].via.u64; if (o->via.array.ptr[RI_TYPE].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { error_reply(si, RT_ERROR, cid, "Request type is not a positive integer"); goto end; } type = o->via.array.ptr[RI_TYPE].via.u64; switch (type) { case RT_SETOPT: ok = handle_setopt_request(si, cid, o); break; case RT_GETOPT: ok = handle_getopt_request(si, cid, o); break; case RT_INFO: ok = handle_info_request(si, cid, o); break; case RT_DEST_INFO: ok = handle_dest_info_request(si, cid, o); break; case RT_GET: ok = handle_get_request(si, cid, o); break; case RT_GETTABLE: ok = handle_gettable_request(si, cid, o); break; default: error_reply(si, type|RT_ERROR, cid, "Unknown request type"); } end: if (ok < 0) { PS.invalid_requests++; si->PS.invalid_requests++; } } if (got) { msgpack_unpacker_expand_buffer(&c->unpacker, 0); } }