204 lines
4.9 KiB
C++
204 lines
4.9 KiB
C++
|
|
#include "envoy.h"
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
namespace lua {
|
|
|
|
/**
|
|
* A wrapper for a buffer.
|
|
*/
|
|
int BufferWrapper::luaLength(lua_State* state) {
|
|
lua_pushnumber(state, 0);
|
|
return 1;
|
|
}
|
|
|
|
int BufferWrapper::luaGetBytes(lua_State* state) {
|
|
lua_pushlstring(state, "buffer", 6);
|
|
return 1;
|
|
}
|
|
|
|
int BufferWrapper::luaSetBytes(lua_State* state) {
|
|
lua_pushnumber(state, 0);
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Lua wrapper for a header map. Methods that will modify the map will call a check function
|
|
* to see if modification is allowed.
|
|
*/
|
|
int HeaderMapWrapper::luaAdd(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
int HeaderMapWrapper::luaGet(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
int HeaderMapWrapper::luaPairs(lua_State* state) {
|
|
if (iterator_.get() != nullptr) {
|
|
luaL_error(state, "cannot create a second iterator before completing the first");
|
|
}
|
|
iterator_.reset(HeaderMapIterator::create(state), true);
|
|
lua_pushcclosure(state, HeaderMapIterator::static_luaPairsIterator, 1);
|
|
return 1;
|
|
}
|
|
|
|
int HeaderMapIterator::luaPairsIterator(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/**
|
|
* Lua wrapper for a metadata map.
|
|
*/
|
|
int MetadataMapWrapper::luaGet(lua_State* state) {
|
|
const char* key = luaL_checkstring(state, 2);
|
|
UNUSED(key);
|
|
return 0;
|
|
}
|
|
|
|
int MetadataMapWrapper::luaPairs(lua_State* state) {
|
|
if (iterator_.get() != nullptr) {
|
|
luaL_error(state, "cannot create a second iterator before completing the first");
|
|
}
|
|
iterator_.reset(MetadataMapIterator::create(state), true);
|
|
lua_pushcclosure(state, MetadataMapIterator::static_luaPairsIterator, 1);
|
|
return 1;
|
|
}
|
|
|
|
int MetadataMapIterator::luaPairsIterator(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Lua wrapper for a stream info.
|
|
*/
|
|
int StreamInfoWrapper::luaProtocol(lua_State* state) {
|
|
const std::string protocol = "HTTP/1.1";
|
|
lua_pushlstring(state, protocol.data(), protocol.size());
|
|
return 1;
|
|
}
|
|
|
|
int StreamInfoWrapper::luaDynamicMetadata(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
int StreamInfoWrapper::luaDownstreamSslConnection(lua_State* state) {
|
|
lua_pushnil(state);
|
|
return 1;
|
|
}
|
|
|
|
int StreamInfoWrapper::luaDownstreamLocalAddress(lua_State* state) {
|
|
const std::string local_address = "127.0.0.1:1234";
|
|
lua_pushlstring(state, local_address.data(), local_address.size());
|
|
return 1;
|
|
}
|
|
|
|
int StreamInfoWrapper::luaDownstreamDirectRemoteAddress(lua_State* state) {
|
|
const std::string direct_remote_address = "172.17.0.3:80";
|
|
lua_pushlstring(state, direct_remote_address.data(), direct_remote_address.size());
|
|
return 1;
|
|
}
|
|
|
|
int StreamInfoWrapper::luaRequestedServerName(lua_State* state) {
|
|
const std::string server_name = "envoy";
|
|
lua_pushlstring(state, server_name.data(), server_name.size());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* A wrapper for a currently running request/response. This is the primary handle passed to Lua.
|
|
* The script interacts with Envoy entirely through this handle.
|
|
*/
|
|
int StreamHandleWrapper::luaHeaders(lua_State* state) {
|
|
if (headers_wrapper_.get() != nullptr) {
|
|
headers_wrapper_.pushStack();
|
|
} else {
|
|
headers_wrapper_.reset(HeaderMapWrapper::create(state), true);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int StreamHandleWrapper::luaBody(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
int StreamHandleWrapper::luaBodyChunks(lua_State* state) {
|
|
lua_pushcclosure(state, static_luaBodyIterator, 1);
|
|
return 1;
|
|
}
|
|
|
|
int StreamHandleWrapper::luaBodyIterator(lua_State* state) {
|
|
LuaDeathRef<BufferWrapper> wrapper;
|
|
wrapper.reset(BufferWrapper::create(state), true);
|
|
return 1;
|
|
}
|
|
|
|
int StreamHandleWrapper::luaMetadata(lua_State* state) {
|
|
if (metadata_wrapper_.get() != nullptr) {
|
|
metadata_wrapper_.pushStack();
|
|
} else {
|
|
metadata_wrapper_.reset(MetadataMapWrapper::create(state), true);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int StreamHandleWrapper::luaStreamInfo(lua_State* state) {
|
|
if (stream_info_wrapper_.get() != nullptr) {
|
|
stream_info_wrapper_.pushStack();
|
|
} else {
|
|
stream_info_wrapper_.reset(StreamInfoWrapper::create(state), true);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int luaLog(lua_State* state, const char* prefix) {
|
|
size_t input_size = 0;
|
|
const char* input = luaL_checklstring(state, 2, &input_size);
|
|
std::string message(input, input_size);
|
|
std::cout << prefix << ": " << message << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
int StreamHandleWrapper::luaLogTrace(lua_State* state) {
|
|
return luaLog(state, "trace");
|
|
}
|
|
|
|
int StreamHandleWrapper::luaLogDebug(lua_State* state) {
|
|
return luaLog(state, "debug");
|
|
}
|
|
|
|
int StreamHandleWrapper::luaLogInfo(lua_State* state) {
|
|
return luaLog(state, "info");
|
|
}
|
|
|
|
int StreamHandleWrapper::luaLogWarn(lua_State* state) {
|
|
return luaLog(state, "warn");
|
|
}
|
|
|
|
int StreamHandleWrapper::luaLogErr(lua_State* state) {
|
|
return luaLog(state, "error");
|
|
}
|
|
|
|
int StreamHandleWrapper::luaLogCritical(lua_State* state) {
|
|
return luaLog(state, "crit");
|
|
}
|
|
|
|
int StreamHandleWrapper::luaHttpCall(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
int StreamHandleWrapper::luaRespond(lua_State* state) {
|
|
UNUSED(state);
|
|
return 0;
|
|
}
|
|
|
|
} |