bool Endpoint::Set(const char* ip_port, bool resolv) { const char* p; int port; bool ret; DynArray<32> ipbuf; p = ip_port; while (*p != '\0' && *p != ':') p++; if (*p == '\0') { Log_Trace("No ':' in host specification"); return false; } ipbuf.Append(ip_port, p - ip_port); ipbuf.Append("", 1); p++; port = -1; port = atoi(p); if (port < 1 || port > 65535) { Log_Trace("atoi() failed to produce a sensible value"); return false; } ret = Set(ipbuf.buffer, port, resolv); return ret; }
int Client::ListKeyValues(const ByteString &prefix, const ByteString &startKey, uint64_t count, bool next, bool forward, bool dirty, bool values) { Command* cmd; ByteString args[5]; DynArray<32> countString; DynArray<10> nextString; DynArray<10> backString; ByteString sk; VALIDATE_CLIENT(); VALIDATE_NOT_BATCHED(); VALIDATE_KEY_LEN(prefix); countString.Writef("%U", count); if (next) nextString.Append("1", 1); else nextString.Append("0", 1); if (forward) backString.Append("f", 1); else backString.Append("b", 1); if (prefix.length > 0 && startKey.length >= prefix.length) { if (memcmp(prefix.buffer, startKey.buffer, MIN(prefix.length, startKey.length)) == 0) { sk.buffer = startKey.buffer + prefix.length; sk.length = startKey.length - prefix.length; sk.size = startKey.size - prefix.length; } } else sk = startKey; // TODO: this seems inconsistent args[0] = prefix; args[1] = sk; args[2] = countString; args[3] = nextString; args[4] = backString; if (dirty) { VALIDATE_DIRTY(); if (values) cmd = CreateCommand(KEYSPACECLIENT_DIRTY_LISTP, SIZE(args), args); else cmd = CreateCommand(KEYSPACECLIENT_DIRTY_LIST, SIZE(args), args); dirtyCommands.Append(cmd); } else { VALIDATE_SAFE(); if (values) cmd = CreateCommand(KEYSPACECLIENT_LISTP, SIZE(args), args); else cmd = CreateCommand(KEYSPACECLIENT_LIST, SIZE(args), args); safeCommands.Append(cmd); } result->Close(); result->AppendCommand(cmd); EventLoop(); return result->CommandStatus(); }
bool HttpFileHandler::HandleRequest(HttpConn* conn, const HttpRequest& request) { DynArray<128> path; DynArray<128> tmp; DynArray<128> ha; char buf[128 * 1024]; FILE* fp; size_t nread; size_t fsize; const char* mimeType; if (strncmp(request.line.uri, prefix, strlen(prefix))) return false; path.Writef("%s%s", documentRoot, request.line.uri); path.Append("", 1); mimeType = MimeTypeFromExtension(strrchr(path.buffer, '.')); fp = fopen(path.buffer, "rb"); if (!fp) return false; fseek(fp, 0L, SEEK_END); fsize = ftell(fp); fseek(fp, 0L, SEEK_SET); HttpHeaderAppend(ha, HTTP_HEADER_CONTENT_TYPE, sizeof(HTTP_HEADER_CONTENT_TYPE) - 1, mimeType, strlen(mimeType)); tmp.Writef("%u", fsize); HttpHeaderAppend(ha, HTTP_HEADER_CONTENT_LENGTH, sizeof(HTTP_HEADER_CONTENT_LENGTH) - 1, tmp.buffer, tmp.length); // zero-terminate ha.Append("", 1); conn->ResponseHeader(HTTP_STATUS_CODE_OK, ha.buffer); while (true) { nread = fread(buf, 1, sizeof(buf), fp); if (feof(fp)) { fclose(fp); conn->Write(buf, nread, true); break; } if (ferror(fp)) { fclose(fp); break; } conn->Write(buf, nread); } conn->Flush(); return true; }
int Client::Count(uint64_t &res, const ByteString &prefix, const ByteString &startKey, uint64_t count = 0, bool next = false, bool forward = false, bool dirty = false) { Command* cmd; ByteString args[5]; DynArray<32> countString; DynArray<10> nextString; DynArray<10> backString; ByteString sk; int status; unsigned nread; ByteString value; VALIDATE_CLIENT(); VALIDATE_NOT_BATCHED(); VALIDATE_KEY_LEN(prefix); countString.Writef("%U", count); if (next) nextString.Append("1", 1); else nextString.Append("0", 1); if (forward) backString.Append("f", 1); else backString.Append("b", 1); if (prefix.length > 0 && startKey.length >= prefix.length) { if (memcmp(prefix.buffer, startKey.buffer, MIN(prefix.length, startKey.length)) == 0) { sk.buffer = startKey.buffer + prefix.length; sk.length = startKey.length - prefix.length; sk.size = startKey.size - prefix.length; } } else sk = startKey; // TODO: this seems inconsistent args[0] = prefix; args[1] = sk; args[2] = countString; args[3] = nextString; args[4] = backString; if (dirty) { VALIDATE_DIRTY(); cmd = CreateCommand(KEYSPACECLIENT_DIRTY_COUNT, SIZE(args), args); dirtyCommands.Append(cmd); } else { VALIDATE_SAFE(); cmd = CreateCommand(KEYSPACECLIENT_COUNT, SIZE(args), args); safeCommands.Append(cmd); } result->Close(); result->AppendCommand(cmd); EventLoop(); status = result->CommandStatus(); if (status != KEYSPACE_SUCCESS) return status; // TODO check conversion status = result->Value(value); if (status == KEYSPACE_SUCCESS) res = strntoint64(value.buffer, value.length, &nread); return status; }